-
Notifications
You must be signed in to change notification settings - Fork 5
/
natgo-client.go
174 lines (153 loc) · 5.11 KB
/
natgo-client.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
package main
import (
"net"
"./natgo"
"sync"
"os"
"time"
"strings"
"errors"
"log"
)
var serviceMap = make(map[string]string)
var mgrChannelLockForClient = sync.Mutex{}
var isServerAlive = true
func main() {
log.Println("Start NAT client")
if len(os.Args) < 3 {
log.Println("Usage: natgo-client <remoteHost:port> <servicePort:targetHost:port> [servicePort:targetHost:port] ")
return
}
remoteAddr := os.Args[1]
targetAddresses := os.Args[2:]
var services = ""
for _, targetAddr := range targetAddresses {
i := strings.Index(targetAddr, ":")
service := targetAddr[0:i]
targetAddr = targetAddr[i + 1:]
serviceMap[service] = targetAddr
services += service
services += ","
}
services = strings.Trim(services, ",")
log.Println(services)
log.Println("Connect to control address ", remoteAddr)
wg := sync.WaitGroup{}
wg.Add(1)
for {
connRemote, err := net.DialTimeout("tcp", remoteAddr, 10 * time.Second)
if err != nil {
log.Println("Can't connect to remote: ", err)
time.Sleep(10 * time.Second)
continue
}
connRemote.SetReadDeadline(time.Now().Add(5 * time.Second))
err = natgo.ClientRegisterRequest(connRemote, services)
if err != nil {
log.Println("Can not register to server.")
continue
}
connRemote.SetReadDeadline(time.Time{})
go heartBeat(connRemote)
for {
sessionId, service, err := serverStartSessionResponse(connRemote)
if err == nil {
targetAddr := serviceMap[service]
work(remoteAddr, targetAddr, sessionId)
} else {
log.Println("Failed to start session with server.")
connRemote.Close()
break
}
}
}
wg.Wait()
}
func heartBeat(conn net.Conn) {
for {
time.Sleep(30 * time.Second)
log.Println("Begin heart beat")
log.Println("Get mgr conn lock")
mgrChannelLockForClient.Lock()
isServerAlive = false
_, err := conn.Write([]byte{natgo.CMD_HEART_BEAT_REQUEST})
if err != nil {
log.Println("Failed to send heartbeat request to server, close the connection.", err)
conn.Close()
mgrChannelLockForClient.Unlock()
return
}
time.Sleep(2 * time.Second) //waiting for response
if isServerAlive {
log.Println("Get heartbeat response from server")
} else {
log.Println("Failed to get heartbeat response, close the connection.", err)
conn.Close()
mgrChannelLockForClient.Unlock()
return
}
log.Println("Release mgr lock")
mgrChannelLockForClient.Unlock()
log.Println("Done heartbeat")
}
}
func work(remoteAddr, targetAddr string, sessionId int32) {
log.Println("Connecting target host ", targetAddr)
targetConn := connectPort(targetAddr)
if targetConn == nil {
log.Println("Failed to connect to target addr")
return
}
log.Println("Connecting to session remoteAddr ", remoteAddr)
sessionConn := connectPort(remoteAddr)
if sessionConn == nil {
log.Println("Failed to connect to remote addr")
return
}
natgo.ClientReplySessionRequest(sessionConn, sessionId)
log.Println("Begin transfer data ...")
natgo.ConnectionExchange(targetConn, sessionConn)
}
func connectPort(remoteAddr string) net.Conn {
conn, err := net.DialTimeout("tcp", remoteAddr, 5 * time.Second)
if err != nil {
log.Println("Can't connect to addr: ", err)
return nil
}
log.Println("Connectted:", conn.RemoteAddr())
return conn
}
func serverStartSessionResponse(conn net.Conn) (int32, string, error) {
log.Println("ServerStartSessionResponse")
for {
request := make([]byte, 20)
log.Println("Begin read cmd...")
_, err := conn.Read(request)
if err != nil {
return 0, "", err
}
log.Println("Read cmd:", request)
if request[0] == natgo.CMD_HEART_BEAT_RESPONSE {
log.Println("Get heart beat cmd.")
isServerAlive = true
continue
} else if request[0] != natgo.CMD_SERVER_START_SESSION_REQUEST {
log.Println("Invalid cmd")
return 0, "", errors.New("invalid cmd from server")
}
requestId := natgo.BytesToInt32(request[1:5])
log.Println("request id:", requestId)
serviceBuffer := request[5:]
service := string(serviceBuffer)
service = strings.Trim(service, "\x00")
log.Println("Client got service:", service)
response := make([]byte, 1)
response[0] = natgo.CMD_SERVER_START_SESSION_RESPONSE
log.Println("Sending response ", response)
mgrChannelLockForClient.Lock()
conn.Write(response)
mgrChannelLockForClient.Unlock()
log.Println("Sent CMD_SERVER_START_SESSION_RESPONSE response")
return requestId, service, nil
}
}