-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
318 lines (274 loc) · 7.29 KB
/
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
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
package proxylite
import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"sort"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
// RegisterEntry entry to discribe a single service registration
type RegisterEntry struct {
// Basic Info
Info RegisterInfo
// Cancel function
Cancel func()
// Done channel
Done <-chan struct{}
}
// ProxyLiteClient inner client that forwards traffic between inner service and proxy server server.
type ProxyLiteClient struct {
ready bool
serverAddr string
lock sync.RWMutex
avaliablePorts map[int]struct{}
registered map[int]*RegisterEntry
logger *log.Logger
}
// NewProxyLiteClient Create a inner client binding with a proxy server.
func NewProxyLiteClient(serverAddr string) *ProxyLiteClient {
client := &ProxyLiteClient{
ready: false,
serverAddr: serverAddr,
avaliablePorts: map[int]struct{}{},
registered: map[int]*RegisterEntry{},
logger: log.New(),
}
client.logger.SetReportCaller(true)
ports, err := AskFreePort(serverAddr)
if err != nil {
return client
}
client.ready = true
for _, port := range ports {
client.avaliablePorts[port] = struct{}{}
}
return client
}
// AvaliablePorts Get avaliable ports from proxy server.
func (c *ProxyLiteClient) AvaliablePorts() ([]int, bool) {
ports, err := AskFreePort(c.serverAddr)
if err != nil {
c.ready = false
return nil, false
}
c.ready = true
for _, port := range ports {
c.avaliablePorts[port] = struct{}{}
}
return ports, true
}
// AnyPort Get a random avaliable port from proxy server.
func (c *ProxyLiteClient) AnyPort() (int, bool) {
if c.ready {
for port := range c.avaliablePorts {
return port, true
}
return 0, false
}
_, ok := c.AvaliablePorts()
if !ok {
return 0, false
}
for port := range c.avaliablePorts {
return port, true
}
return 0, false
}
// ActiveServices Discover all active services from proxy server.
func (c *ProxyLiteClient) ActiveServices() ([]ServiceInfo, error) {
return DiscoverServices(c.serverAddr)
}
func register(conn net.Conn, info RegisterInfo) error {
req := RegisterServiceReq{
Info: RegisterInfo{
OuterPort: info.OuterPort,
InnerAddr: info.InnerAddr,
},
}
raw, _ := json.Marshal(req)
err := sendMessage(conn, TypeRegisterServiceReq, raw)
if err != nil {
return err
}
mtype, data, err := recvMessage(conn)
if err != nil {
return err
}
if mtype != TypeRegisterServiceRsp {
return errors.New("protocol mismatch")
}
rsp := RegisterServiceRsp{}
err = json.Unmarshal(data, &rsp)
if err != nil {
return err
}
if rsp.Code != 0 {
return fmt.Errorf("register failed: Code=%d", rsp.Code)
}
return nil
}
// SetLogger Set customized logrus logger for the inner client.
func (c *ProxyLiteClient) SetLogger(logger *log.Logger) {
c.logger = logger
}
// RegisterInnerService Register inner server to proxy server's outer port.
func (c *ProxyLiteClient) RegisterInnerService(info RegisterInfo) error {
if !c.ready {
return errors.New("client not ready")
}
c.lock.Lock()
defer c.lock.Unlock()
if _, exists := c.registered[info.OuterPort]; exists {
return errors.New("ports is occupied")
}
// If we cannot connect to inner target service. We should not register this service to proxy server.
innerConn, err := net.Dial("tcp", info.InnerAddr)
if err != nil {
return err
}
// If we cannot connect to proxy server. We cannot register any service. And set client not ready.
serverConn, err := net.Dial("tcp", c.serverAddr)
if err != nil {
c.ready = false
innerConn.Close()
return err
}
// Register the service must return nil.
err = register(serverConn, info)
c.logTunnelMessage(info.Name, "REGISTER", fmt.Sprint("err=", err))
if err != nil {
innerConn.Close()
serverConn.Close()
return err
}
// cancelFunc can be used to cancel the service mapping
cancel := make(chan struct{})
cancelFunc := func() {
cancel <- struct{}{}
close(cancel)
}
// done channel can be used to wait the service mapping finish
done := make(chan struct{})
// Now we can add the new entry to registered table.
c.registered[info.OuterPort] = &RegisterEntry{
Info: info,
Cancel: cancelFunc,
Done: done,
}
go func() {
// anyClose is a signal channel to wait any of 2 below goroutine exit.
anyClose := make(chan struct{})
go func() {
total, err := io.Copy(innerConn, serverConn)
c.logTunnelMessage(info.Name, "FINISH", fmt.Sprintf("server->inner Finish [%d], [%v]", total, err))
<-anyClose
}()
go func() {
total, err := io.Copy(serverConn, innerConn)
c.logTunnelMessage(info.Name, "FINISH", fmt.Sprintf("inner->server Finish [%d], [%v]", total, err))
<-anyClose
}()
select {
case anyClose <- struct{}{}:
// Any of 2 above gouroutine done.
case <-cancel:
// Here we receive cancel signal by caller.
c.logTunnelMessage(info.Name, "CANCEL", "service cancelled")
}
// Good principle: let writer goroutine to close the channel.
close(anyClose)
// Drain out the sending buffer. The Tunnel can be destroy now.
time.Sleep(time.Millisecond * 10)
innerConn.Close()
serverConn.Close()
// Tell caller that the service mapping is already finish.
done <- struct{}{}
close(done)
c.logTunnelMessage(info.Name, "UNREGISTER", "service unregister")
// Delete the service mapping from table.
c.lock.Lock()
delete(c.registered, info.OuterPort)
c.lock.Unlock()
}()
return nil
}
// GetRegisterEntryByName Get RegisterEntry
func (c *ProxyLiteClient) GetRegisterEntryByName(name string) (*RegisterEntry, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
for _, entry := range c.registered {
if entry.Info.Name == name {
return entry, true
}
}
return nil, false
}
// GetRegisterEntryByPort Get RegisterEntry
func (c *ProxyLiteClient) GetRegisterEntryByPort(port int) (*RegisterEntry, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
entry, exists := c.registered[port]
return entry, exists
}
func (c *ProxyLiteClient) logTunnelMessage(service, header, msg string) {
c.logger.Infof("[%s] [%s] %s", service, header, msg)
}
// AskFreePort Ask avaliable free port from proxy server with given address.
func AskFreePort(addr string) ([]int, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
defer conn.Close()
req := AskFreePortReq{}
raw, _ := json.Marshal(&req)
err = sendMessage(conn, TypeAskFreePortReq, raw)
if err != nil {
return nil, err
}
mtype, data, err := recvMessage(conn)
if err != nil {
return nil, err
}
if mtype != TypeAskFreePortRsp {
return nil, errors.New("protocol mismatch")
}
rsp := AskFreePortRsp{}
err = json.Unmarshal(data, &rsp)
if err != nil {
return nil, err
}
sort.Ints(rsp.Ports)
return rsp.Ports, nil
}
// DiscoverServices Discover all active services from proxy server with given address.
func DiscoverServices(addr string) ([]ServiceInfo, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
defer conn.Close()
req := AskServiceReq{}
raw, _ := json.Marshal(&req)
err = sendMessage(conn, TypeAskServiceReq, raw)
if err != nil {
return nil, err
}
mtype, data, err := recvMessage(conn)
if err != nil {
return nil, err
}
if mtype != TypeAskServiceRsp {
return nil, errors.New("protocol mismatch")
}
rsp := AskServiceRsp{}
err = json.Unmarshal(data, &rsp)
if err != nil {
return nil, err
}
return rsp.Services, nil
}