-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathmain.go
230 lines (182 loc) · 4.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"crypto/rand"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/matishsiao/go_reuseport"
"github.com/songgao/water"
"golang.org/x/net/ipv4"
)
const (
// AppVersion contains current application version for -version command flag
AppVersion = "0.2.0b"
)
const (
// I use TUN interface, so only plain IP packet,
// no ethernet header + mtu is set to 1300
// BUFFERSIZE is size of buffer to receive packets
// (little bit bigger than maximum)
BUFFERSIZE = 1518
)
func rcvrThread(proto string, port int, iface *water.Interface) {
conn, err := reuseport.NewReusableUDPPortConn(proto, fmt.Sprintf(":%v", port))
if nil != err {
log.Fatalln("Unable to get UDP socket:", err)
}
encrypted := make([]byte, BUFFERSIZE)
var decrypted IPPacket = make([]byte, BUFFERSIZE)
for {
n, _, err := conn.ReadFrom(encrypted)
if err != nil {
log.Println("Error: ", err)
continue
}
// ReadFromUDP can return 0 bytes on timeout
if 0 == n {
continue
}
conf := config.Load().(VPNState)
if !conf.Main.main.CheckSize(n) {
log.Println("invalid packet size ", n)
continue
}
size, mainErr := DecryptV4Chk(conf.Main.main, encrypted[:n], decrypted)
if nil != mainErr {
if nil != conf.Main.alt {
size, err = DecryptV4Chk(conf.Main.alt, encrypted[:n], decrypted)
if nil != err {
log.Println("Corrupted package: ", mainErr, " / ", err)
continue
}
} else {
log.Println("Corrupted package: ", mainErr)
continue
}
}
n, err = iface.Write(decrypted[:size])
if nil != err {
log.Println("Error writing to local interface: ", err)
} else if n != size {
log.Println("Partial package written to local interface")
}
}
}
func sndrThread(conn *net.UDPConn, iface *water.Interface) {
// first time fill with random numbers
ivbuf := make([]byte, config.Load().(VPNState).Main.main.IVLen())
if _, err := io.ReadFull(rand.Reader, ivbuf); err != nil {
log.Fatalln("Unable to get rand data:", err)
}
var packet IPPacket = make([]byte, BUFFERSIZE)
var encrypted = make([]byte, BUFFERSIZE)
for {
plen, err := iface.Read(packet[:MTU])
if err != nil {
break
}
if 4 != packet.IPver() {
header, _ := ipv4.ParseHeader(packet)
log.Printf("Non IPv4 packet [%+v]\n", header)
continue
}
// each time get pointer to (probably) new config
c := config.Load().(VPNState)
dst := packet.Dst()
wanted := false
addr, ok := c.remotes[dst]
if ok {
wanted = true
}
if dst == c.Main.bcastIP || packet.IsMulticast() {
wanted = true
}
// very ugly and useful only for a limited numbers of routes!
if !wanted {
ip := packet.DstV4()
for n, s := range c.routes {
if n.Contains(ip) {
addr = s
ok = true
wanted = true
break
}
}
}
if wanted {
// new len contatins also 2byte original size
clen := c.Main.main.AdjustInputSize(plen)
if clen+c.Main.main.OutputAdd() > len(packet) {
log.Println("clen + data > len(package)", clen, len(packet))
continue
}
tsize := c.Main.main.Encrypt(packet[:clen], encrypted, ivbuf)
if ok {
n, err := conn.WriteToUDP(encrypted[:tsize], addr)
if nil != err {
log.Println("Error sending package:", err)
}
if n != tsize {
log.Println("Only ", n, " bytes of ", tsize, " sent")
}
} else {
// multicast or broadcast
for _, addr := range c.remotes {
n, err := conn.WriteToUDP(encrypted[:tsize], addr)
if nil != err {
log.Println("Error sending package:", err)
}
if n != tsize {
log.Println("Only ", n, " bytes of ", tsize, " sent")
}
}
}
} else {
log.Println("Unknown dst: ", dst)
}
}
}
func main() {
version := flag.Bool("version", false, "print lcvpn version")
flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
routeReload := make(chan bool, 1)
initConfig(routeReload)
conf := config.Load().(VPNState)
iface := ifaceSetup(conf.Main.local)
// start routes changes in config monitoring
go routesThread(iface.Name(), routeReload)
log.Println("Interface parameters configured")
// Start listen threads
for i := 0; i < conf.Main.RecvThreads; i++ {
go rcvrThread("udp4", conf.Main.Port, iface)
}
// init udp socket for write
writeAddr, err := net.ResolveUDPAddr("udp", ":")
if nil != err {
log.Fatalln("Unable to get UDP socket:", err)
}
writeConn, err := net.ListenUDP("udp", writeAddr)
if nil != err {
log.Fatalln("Unable to create UDP socket:", err)
}
// Start sender threads
for i := 0; i < conf.Main.SendThreads; i++ {
go sndrThread(writeConn, iface)
}
exitChan := make(chan os.Signal, 1)
signal.Notify(exitChan, syscall.SIGTERM)
<-exitChan
err = writeConn.Close()
if nil != err {
log.Println("Error closing UDP connection: ", err)
}
}