-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectivity.go
301 lines (270 loc) · 7.07 KB
/
connectivity.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
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
// Copyright 2017-2019 ajee.cai@gmail.com. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"crypto/tls"
"fmt"
"strconv"
"strings"
"time"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
"github.com/u-root/u-root/pkg/dhclient"
"github.com/vishvananda/netlink"
)
const (
KeepAliveMsg = 0xffffffff
dhcpTimeout = 15 * time.Second
dhcpTries = 3
)
func (c *vpnSetting) startConnect() {
// create connection to server
// a bit ugly to append port number
hostport := c.host
ipEnd := strings.LastIndexByte(c.host, ']')
if ipEnd != -1 {
Debug("with IPv6:port\n")
} else if off := strings.IndexByte(c.host, ':'); off != -1 {
ipEnd := strings.LastIndexByte(c.host, ':')
if ipEnd != -1 && off != ipEnd {
// This is IPv6 IP address without port
hostport = "[" + c.host + "]" + ":443"
}
} else {
hostport += ":443"
}
conn, err := tls.Dial("tcp", hostport, &tls.Config{InsecureSkipVerify: true})
c.conn = conn
Debug("conn type %T, to %v\n", conn, hostport)
defer func() {
c.connState = nDisconnected
if conn != nil {
conn.Close()
}
}()
if err != nil {
c.err = eConn
Debug("Connection failed\n")
(*c.mw).Changed()
return
}
// Steps: upload signature
waterMarkLen, waterMarkData := getWatermarkData()
ipEnd = strings.LastIndexByte(conn.LocalAddr().String(), ':')
if ipEnd == -1 {
Debug("Can't get port from LocalAddr" + conn.LocalAddr().String())
return
}
myIP := conn.LocalAddr().String()[:ipEnd]
Debug("Connection established\n")
fmt.Fprintf(conn, "POST /vpnsvc/connect.cgi HTTP/1.1\r\n"+
"Connection: Keep-Alive\r\n"+
"Content-Length: %d\r\n"+
"Content-Type: image/jpedg\r\n"+
"Host: %s\r\n\r\n%s", waterMarkLen, myIP, waterMarkData)
Debug("TX done\n")
// Steps: download server hello
body, err := parseHttpResponse(conn)
if err != nil {
Debug("err: %v\n", err)
return
}
serverResp, err := parseData(body)
if err != nil {
Debug("err: %v\n", err)
return
}
//Debug("serverResp is: %v\n",serverResp)
/* This is test code
test_byte, err := AddData(serverResp)
if err != nil {
Debug("err: %v\n", err)
return
}
serverResp2, err := ParseData(test_byte)
if err != nil {
Debug("err: %v\n", err)
return
}
Debug("serverResp2 is: %v\n",serverResp2)
*/
// Steps: TODO: verify server certificate
// Steps: send authentication
authHdr := "POST /vpnsvc/vpn.cgi HTTP/1.1\r\n" +
"Connection: Keep-Alive\r\n" +
"Content-Length: %s\r\n" +
"Content-Type: application/octet-stream\r\n"
authHdr += "Date:" + time.Now().Format("Mon Jan 2 15:04:05 -0700 MST 2006") + "\r\n"
authHdr += "Host: " + myIP + "\r\n"
authHdr += "Keep-Alive: timeout=15; max=19" + "\r\n\r\n"
authMap := make(map[string]interface{})
authMap["method"] = "login"
authMap["hubname"] = "DEFAULT"
authMap["username"] = c.usr
authMap["authtype"] = 2
// authMap["secure_password"] = HashTxt("password","") //string(serverResp["random"].([]byte)))
// Debug("txt: % x\nSHA1 Hash: % x\n", "Passw0rd!",authMap["secure_password"] )
// Should use securepassword for preventing memory hacking
authMap["plain_password"] = c.passwd //"password"
authMap["timestamp"] = time.Now().Format("123456")
authMap["client_str"] = serverResp["hello"]
authMap["client_ver"] = serverResp["version"]
authMap["client_build"] = serverResp["build"]
// Add more control option if needed
authMap["max_connection"] = 8
authMap["use_encrypt"] = 1
authMap["use_compress"] = 0
var authByte []byte = make([]byte, 0)
authByte, err = addData(authByte, authMap)
if err != nil {
Debug("err: %v\n", err)
return
}
fmt.Fprintf(conn, "POST /vpnsvc/vpn.cgi HTTP/1.1\r\n"+
"Connection: Keep-Alive\r\n"+
"Content-Length: %s\r\n"+
"Content-Type: application/octet-stream\r\n"+
"Date: %s\r\n"+
"Host: %s\r\n"+
"Keep-Alive: timeout=15; max=19\r\n\r\n",
strconv.Itoa(len(authByte)),
time.Now().Format("Mon Jan 2 15:04:05 -0700 MST 2006"),
myIP)
//Debug("authByte is: %v\n",auth)
conn.Write(authByte)
body, err = parseHttpResponse(conn)
if err != nil {
Debug("err: %v\n", err)
return
}
serverResp, err = parseData(body)
if err != nil {
Debug("err: %v\n", err)
return
}
Debug("Server Response from auth: %v\n", serverResp)
e := serverResp["error"]
if e != nil {
r, ok := e.(uint32)
if !ok {
Debug("can't cast, err %T\n", e)
}
Debug("Server Response With error %v,%T,(%d)\n", e, e, r)
if int(r) == 9 {
c.err = ePsw
}
(*c.mw).Changed()
return
}
//Create Virtual Interface
config := water.Config{
DeviceType: water.TAP,
}
config.Name = "vpn_go"
ifce, err := water.New(config)
c.ifce = ifce
if err != nil {
Debug("err when creating tap: %v\n", err)
c.err = ePerm
(*c.mw).Changed()
return
}
c.chanQuit = make(chan struct{})
// Will there be any performance to pass slice over chan?
chanWrite := make(chan []byte)
// read tap interface and send frame to chanWrite
go func() {
for {
var frame ethernet.Frame
frame.Resize(1500)
n, err := ifce.Read([]byte(frame))
if err != nil {
Debug("iface is closed for read, quit\n")
return
}
//This parse is for debug only
//pktParse(frame[:n])
frameSent := framePack(1, n, frame[:n])
if frameSent != nil {
chanWrite <- frameSent
}
}
}()
// wait on chanWrite and write packets to SSL tunnel
go func() {
for {
frameSent := <-chanWrite
//Debug("Write to tunnel:\n% x\n", frameSent)
_, err = conn.Write(frameSent)
if err != nil {
Debug("conn is closed for write, quit\n")
return
}
}
}()
// read from SSL tunnel
go func() {
for {
var frameRead ethernet.Frame
frameRead.Resize(1500)
n, err := conn.Read([]byte(frameRead))
if err != nil {
Debug("conn is closed for read, quit\n")
return
}
frame := frameUnpack(frameRead[:n])
if frame == nil {
continue
}
//This parse is for debug only
//pktParse(frame)
// write thru TAP interface
n, err = ifce.Write(frame)
if err != nil {
Debug("iface is closed for write, quit\n")
return
}
}
}()
c.connState = nConnected
// manually call an UI update
Debug("Call UI update\n")
(*c.mw).Changed()
// get dhcp address for interface
ctx, cancel := context.WithTimeout(context.Background(), dhcpTries*dhcpTimeout)
defer cancel()
var filteredIfs []netlink.Link
ifs, err := netlink.LinkList()
for _, iface := range ifs {
if config.Name == iface.Attrs().Name {
filteredIfs = append(filteredIfs, iface)
break
}
}
r := dhclient.SendRequests(ctx, filteredIfs, dhcpTimeout, dhcpTries, true, false)
if r == nil {
fmt.Printf("r is null\n")
return
} else {
// After result back, dhclient will close chan r immediately.
result := <-r
if nil != result && result.Err == nil {
Debug("result %v\n", result)
result.Lease.Configure()
}
}
for {
select {
case <-c.chanQuit:
Debug("Quit connectivity\n")
break
case <-time.After(10 * time.Second):
kaData := []byte{0x00, 0x11, 0x22, 0x33, 0x44}
frameSent := framePack(KeepAliveMsg, len(kaData), kaData)
//Debug("keep alive timer wake up\n")
chanWrite <- frameSent
}
}
}