-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpolevpn_client_vlan.go
More file actions
402 lines (333 loc) · 9.49 KB
/
Copy pathpolevpn_client_vlan.go
File metadata and controls
402 lines (333 loc) · 9.49 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package core
import (
"errors"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/polevpn/anyvalue"
)
type PoleVpnClientVLAN struct {
tunio *TunIO
conn Conn
state int
mutex *sync.Mutex
endpoint string
user string
pwd string
sni string
skipVerifySSL bool
deviceType string
deviceId string
allocip string
remoteip string
lasttimeHeartbeat time.Time
reconnecting bool
wg *sync.WaitGroup
device *TunDevice
handler func(int, PoleVpnClient, *anyvalue.AnyValue)
host string
}
func NewPoleVpnClientVLAN() (*PoleVpnClientVLAN, error) {
client := &PoleVpnClientVLAN{
conn: nil,
state: POLE_CLIENT_INIT,
mutex: &sync.Mutex{},
wg: &sync.WaitGroup{},
}
return client, nil
}
func (pc *PoleVpnClientVLAN) AttachTunDevice(device *TunDevice) {
pc.device = device
if pc.tunio != nil {
pc.tunio.Close()
}
pc.tunio = NewTunIO(TUN_DEVICE_CH_WRITE_SIZE)
pc.tunio.SetPacketHandler(pc.handleTunPacket)
pc.tunio.AttachDevice(device)
pc.tunio.StartProcess()
}
func (pc *PoleVpnClientVLAN) SetEventHandler(handler func(int, PoleVpnClient, *anyvalue.AnyValue)) {
pc.handler = handler
}
func (pc *PoleVpnClientVLAN) GetUpDownBytes() (uint64, uint64) {
if pc.conn != nil {
return pc.conn.GetUpDownBytes()
}
return 0, 0
}
func (pc *PoleVpnClientVLAN) GetRemoteIP() string {
return pc.remoteip
}
func (pc *PoleVpnClientVLAN) SetLocalIP(ip string) {
}
func (pc *PoleVpnClientVLAN) Start(endpoint string, user string, pwd string, sni string, skipVerifySSL bool, deviceType string, deviceId string) error {
pc.mutex.Lock()
defer pc.mutex.Unlock()
if pc.state != POLE_CLIENT_INIT {
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "client stoped or not init").Set("type", ERROR_UNKNOWN))
pc.handler(CLIENT_EVENT_STOPPED, pc, nil)
}
return errors.New("client stoped or not init")
}
if sni == "" {
sni = "www.apple.com"
}
pc.user = user
pc.pwd = pwd
pc.sni = sni
pc.skipVerifySSL = skipVerifySSL
pc.deviceId = deviceId
pc.deviceType = deviceType
var err error
pc.host, err = GetHostByEndpoint(endpoint)
if err != nil {
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "get host fail,"+err.Error()).Set("type", ERROR_UNKNOWN))
pc.handler(CLIENT_EVENT_STOPPED, pc, nil)
}
return err
}
pc.remoteip, err = GetRemoteIPByEndpoint(endpoint)
if err != nil {
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "get remote ip fail,"+err.Error()).Set("type", ERROR_UNKNOWN))
pc.handler(CLIENT_EVENT_STOPPED, pc, nil)
}
return err
}
if strings.HasPrefix(endpoint, "wss://") {
pc.conn = NewWebSocketConn()
} else if strings.HasPrefix(endpoint, "h3s://") {
endpoint = strings.Replace(endpoint, "h3s://", "https://", -1)
pc.conn = NewHttp3Conn()
} else {
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "invalid protocol").Set("type", ERROR_UNKNOWN))
pc.handler(CLIENT_EVENT_STOPPED, pc, nil)
}
return errors.New("invalid protocol")
}
pc.endpoint = strings.Replace(endpoint, pc.host, pc.remoteip, -1)
header := http.Header{}
header.Add("Host", pc.host)
//clear remote ip route,avoid assign address fail
DeleteRemoteRoute(pc.remoteip + "/32")
err = pc.conn.Connect(endpoint, user, pwd, "", sni, pc.skipVerifySSL, deviceType, deviceId, header)
if err != nil {
if err == ErrLoginVerify {
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "user or password invalid").Set("type", ERROR_LOGIN))
}
} else {
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "connet fail,"+err.Error()).Set("type", ERROR_NETWORK))
}
}
if pc.handler != nil {
pc.handler(CLIENT_EVENT_STOPPED, pc, nil)
}
return err
}
pc.state = POLE_CLIENT_RUNING
if pc.handler != nil {
pc.handler(CLIENT_EVENT_STARTED, pc, nil)
}
pc.conn.SetHandler(CMD_ALLOC_IPADDR, pc.handlerAllocAdressRespose)
pc.conn.SetHandler(CMD_S2C_IPDATA, pc.handlerIPDataResponse)
pc.conn.SetHandler(CMD_CLIENT_CLOSED, pc.handlerConnCloseEvent)
pc.conn.SetHandler(CMD_HEART_BEAT, pc.handlerHeartBeatRespose)
pc.conn.StartProcess()
pc.askAllocIPAddress()
pc.lasttimeHeartbeat = time.Now()
go pc.heartBeat()
pc.wg.Add(1)
return nil
}
func (pc *PoleVpnClientVLAN) CloseConnect(flag bool) {
pc.conn.Close(flag)
}
func (pc *PoleVpnClientVLAN) WaitStop() {
pc.wg.Wait()
}
func (pc *PoleVpnClientVLAN) handleTunPacket(pkt []byte) {
if pkt == nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("type", ERROR_IO).Set("error", "tun device close exception"))
pc.Stop()
return
}
version := pkt[0]
version = version >> 4
if version != VERSION_IP_V4 {
return
}
pc.sendIPPacketToRemoteConn(pkt)
}
func (pc *PoleVpnClientVLAN) sendIPPacketToRemoteConn(pkt []byte) {
if pc.conn != nil {
buf := make([]byte, POLE_PACKET_HEADER_LEN+len(pkt))
copy(buf[POLE_PACKET_HEADER_LEN:], pkt)
polepkt := PolePacket(buf)
polepkt.SetCmd(CMD_C2S_IPDATA)
polepkt.SetLen(uint16(len(buf)))
pc.conn.Send(polepkt)
} else {
plog.Error("remote ws conn haven't set")
}
}
func (pc *PoleVpnClientVLAN) askAllocIPAddress() {
buf := make([]byte, POLE_PACKET_HEADER_LEN)
PolePacket(buf).SetCmd(CMD_ALLOC_IPADDR)
PolePacket(buf).SetLen(POLE_PACKET_HEADER_LEN)
pc.conn.Send(buf)
}
func (pc *PoleVpnClientVLAN) handlerAllocAdressRespose(pkt PolePacket, conn Conn) {
av, err := anyvalue.NewFromJson(pkt.Payload())
if err != nil {
plog.Error("alloc address av decode fail,", err)
pc.Stop()
return
}
ip1 := av.Get("ip").AsStr()
if ip1 == "" {
plog.Error("alloc ip fail,stop client")
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "alloc ip fail").Set("type", ERROR_ALLOC))
}
pc.Stop()
return
}
pc.allocip = ip1
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ADDRESS_ALLOCED, pc, av)
}
}
func (pc *PoleVpnClientVLAN) handlerHeartBeatRespose(pkt PolePacket, conn Conn) {
plog.Debug("received heartbeat")
pc.lasttimeHeartbeat = time.Now()
}
func (pc *PoleVpnClientVLAN) handlerIPDataResponse(pkt PolePacket, conn Conn) {
if pc.tunio != nil {
pc.tunio.Enqueue(pkt[POLE_PACKET_HEADER_LEN:])
}
}
func (pc *PoleVpnClientVLAN) handlerConnCloseEvent(pkt PolePacket, conn Conn) {
plog.Info("client closed,start reconnect")
pc.reconnect()
}
func (pc *PoleVpnClientVLAN) reconnect() {
if pc.reconnecting {
plog.Info("conn is reconnecting")
return
}
pc.reconnecting = true
pc.state = POLE_CLIENT_RECONNETING
success := false
for i := 0; i < RECONNECT_TIMES; i++ {
if pc.state == POLE_CLIENT_CLOSED {
break
}
plog.Info("reconnecting")
if pc.handler != nil {
pc.handler(CLIENT_EVENT_RECONNECTING, pc, nil)
}
header := http.Header{}
header.Add("Host", pc.host)
err := pc.conn.Connect(pc.endpoint, pc.user, pc.pwd, pc.allocip, pc.sni, pc.skipVerifySSL, pc.deviceType, pc.deviceId, header)
if pc.state == POLE_CLIENT_CLOSED {
break
}
if err != nil {
if err == ErrNetwork {
if i < 10 {
time.Sleep(time.Second)
plog.Error("retry 1 seconds later")
} else {
time.Sleep(time.Second * RECONNECT_INTERVAL)
plog.Error("retry " + strconv.Itoa(RECONNECT_INTERVAL) + " seconds later")
}
continue
} else if err == ErrIPNotExist {
plog.Error("ip address expired,reconnect and request new")
pc.allocip = ""
} else {
time.Sleep(time.Second * RECONNECT_INTERVAL)
plog.Error("server refuse connect")
continue
}
} else {
plog.Info("reconnect ok")
if pc.allocip == "" {
pc.askAllocIPAddress()
}
pc.conn.StartProcess()
pc.sendHeartBeat()
success = true
pc.state = POLE_CLIENT_RUNING
if pc.handler != nil {
pc.handler(CLIENT_EVENT_RECONNECTED, pc, nil)
}
break
}
}
if !success {
plog.Error("reconnet failed,stop client")
if pc.handler != nil {
pc.handler(CLIENT_EVENT_ERROR, pc, anyvalue.New().Set("error", "reconnet failed").Set("type", ERROR_NETWORK))
}
pc.Stop()
}
pc.reconnecting = false
}
func (pc *PoleVpnClientVLAN) sendHeartBeat() {
buf := make([]byte, POLE_PACKET_HEADER_LEN)
PolePacket(buf).SetCmd(CMD_HEART_BEAT)
PolePacket(buf).SetLen(POLE_PACKET_HEADER_LEN)
pc.conn.Send(buf)
}
func (pc *PoleVpnClientVLAN) heartBeat() {
timer := time.NewTicker(time.Second * time.Duration(HEART_BEAT_INTERVAL))
for range timer.C {
if pc.state == POLE_CLIENT_CLOSED {
timer.Stop()
break
}
timeNow := time.Now()
if timeNow.Sub(pc.lasttimeHeartbeat) > time.Second*HEART_BEAT_INTERVAL*WEBSOCKET_NO_HEARTBEAT_TIMES {
plog.Error("have not recevied heartbeat for ", WEBSOCKET_NO_HEARTBEAT_TIMES, " times,close connection and reconnet")
pc.conn.Close(true)
pc.lasttimeHeartbeat = timeNow
continue
}
pc.sendHeartBeat()
}
}
func (pc *PoleVpnClientVLAN) IsStoped() bool {
pc.mutex.Lock()
defer pc.mutex.Unlock()
if pc.state == POLE_CLIENT_CLOSED || pc.state == POLE_CLIENT_INIT {
return true
}
return false
}
func (pc *PoleVpnClientVLAN) Stop() {
pc.mutex.Lock()
defer pc.mutex.Unlock()
if pc.state == POLE_CLIENT_CLOSED || pc.state == POLE_CLIENT_INIT {
plog.Error("client have been closed or not start")
return
}
if pc.conn != nil {
pc.conn.Close(false)
}
if pc.tunio != nil {
pc.tunio.Close()
}
pc.state = POLE_CLIENT_CLOSED
if pc.handler != nil {
pc.handler(CLIENT_EVENT_STOPPED, pc, nil)
}
pc.wg.Done()
}