forked from koding/tunnel
/
client.go
406 lines (334 loc) · 10 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
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
403
404
405
406
package tunnel
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
"sync"
"time"
"github.com/cenkalti/backoff"
"github.com/hashicorp/yamux"
"github.com/koding/logging"
)
// Client is responsible for creating a control connection to a tunnel server,
// creating new tunnels and proxy them to tunnel server.
type Client struct {
// underlying yamux session
session *yamux.Session
// config holds the ClientConfig
config *ClientConfig
// yamuxConfig is passed to new yamux.Session's
yamuxConfig *yamux.Config
log logging.Logger
mu sync.Mutex // guards the following
closed bool // if client calls Close() and quits
startNotify chan bool // notifies if client established a conn to server
reqWg sync.WaitGroup
// redialBackoff is used to reconnect in exponential backoff intervals
redialBackoff backoff.BackOff
}
// ClientConfig defines the configuration for the Client
type ClientConfig struct {
// Identifier is the secret token that needs to be passed to the server.
// Required if FetchIdentifier is not set
Identifier string
// FetchIdentifier can be used to fetch identifier. Required if Identifier
// is not set.
FetchIdentifier func() (string, error)
// ServerAddr defines the TCP address of the tunnel server to be connected. This is required.
ServerAddr string
// LocalAddr defines the TCP address of the local server. This is optional
// if you want to specify a single TCP address. Otherwise the client will
// always proxy to 127.0.0.1:incomingPort, where incomingPort is the
// tunnelserver's public exposed Port.
LocalAddr string
// Debug enables debug mode, enable only if you want to debug the server.
Debug bool
// Log defines the logger. If nil a default logging.Logger is used.
Log logging.Logger
// YamuxConfig defines the config which passed to every new yamux.Session. If nil
// yamux.DefaultConfig() is used.
YamuxConfig *yamux.Config
}
// verify is used to verify the ClientConfig
func (c *ClientConfig) verify() error {
if c.ServerAddr == "" {
return errors.New("config.ServerAddr must be set")
}
if c.Identifier == "" && c.FetchIdentifier == nil {
return errors.New("neither config.Identifier nor config.FetchIdentifier is set")
}
if c.YamuxConfig != nil {
if err := yamux.VerifyConfig(c.YamuxConfig); err != nil {
return err
}
}
return nil
}
// NewClient creates a new tunnel that is established between the serverAddr
// and localAddr. It exits if it can't create a new control connection to the
// server. If localAddr is empty client will always try to proxy to a local
// port.
func NewClient(cfg *ClientConfig) (*Client, error) {
yamuxConfig := yamux.DefaultConfig()
if cfg.YamuxConfig != nil {
yamuxConfig = cfg.YamuxConfig
}
log := newLogger("tunnel-client", cfg.Debug)
if cfg.Log != nil {
log = cfg.Log
}
if err := cfg.verify(); err != nil {
return nil, err
}
forever := backoff.NewExponentialBackOff()
forever.MaxElapsedTime = 365 * 24 * time.Hour // 1 year
client := &Client{
config: cfg,
log: log,
yamuxConfig: yamuxConfig,
redialBackoff: forever,
startNotify: make(chan bool, 1),
}
return client, nil
}
// Start starts the client and connects to the server with the identifier.
// client.FetchIdentifier() will be used if it's not nil. It's supports
// reconnecting with exponential backoff intervals when the connection to the
// server disconnects. Call client.Close() to shutdown the client completely. A
// successfull connection will cause StartNotify() to receive a value.
func (c *Client) Start() {
id := func() (string, error) {
if c.config.FetchIdentifier != nil {
return c.config.FetchIdentifier()
}
return c.config.Identifier, nil
}
c.redialBackoff.Reset()
for {
time.Sleep(c.redialBackoff.NextBackOff())
identifier, err := id()
if err != nil {
c.log.Critical("client fetch identifier err: %s", err.Error())
continue
}
// mark it as not closed. Also empty the value inside the chan by
// retrieving it (if any), so it doesn't block during connect, when the
// client was closed and started again, and startNotify was never
// listened to.
c.mu.Lock()
c.closed = false
select {
case <-c.startNotify:
default:
}
c.mu.Unlock()
if err := c.connect(identifier); err != nil {
c.log.Debug("client connect err: %s", err.Error())
}
// exit if closed
c.mu.Lock()
if c.closed {
c.mu.Unlock()
return
}
c.mu.Unlock()
}
}
// StartNotify returns a channel that receives a single value when the client
// established a successfull connection to the server.
func (c *Client) StartNotify() <-chan bool {
return c.startNotify
}
// Close closes the client and shutdowns the connection to the tunnel server
func (c *Client) Close() error {
if c.session == nil {
return errors.New("session is not initialized")
}
if err := c.session.GoAway(); err != nil {
return err
}
c.mu.Lock()
c.closed = true
c.mu.Unlock()
c.reqWg.Wait() // wait until all connections are finished
if err := c.session.Close(); err != nil {
return err
}
return nil
}
func (c *Client) connect(identifier string) error {
c.log.Debug("Trying to connect to '%s' with identifier '%s'", c.config.ServerAddr, identifier)
conn, err := net.Dial("tcp", c.config.ServerAddr)
if err != nil {
return err
}
remoteAddr := fmt.Sprintf("http://%s%s", conn.RemoteAddr(), controlPath)
c.log.Debug("CONNECT to '%s'", remoteAddr)
req, err := http.NewRequest("CONNECT", remoteAddr, nil)
if err != nil {
return fmt.Errorf("CONNECT %s", err)
}
req.Header.Set(xKTunnelIdentifier, identifier)
c.log.Debug("Writing request to TCP: %+v", req)
if err := req.Write(conn); err != nil {
return err
}
c.log.Debug("Reading response from TCP")
resp, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
return fmt.Errorf("read response %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 && resp.Status != connected {
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("proxy server: %s. err: %s", resp.Status, string(out))
}
c.session, err = yamux.Client(conn, c.yamuxConfig)
if err != nil {
return err
}
var stream net.Conn
openStream := func() error {
// this is blocking until client opens a session to us
stream, err = c.session.Open()
return err
}
// if we don't receive anything from the server, we'll timeout
select {
case err := <-async(openStream):
if err != nil {
return err
}
case <-time.After(time.Second * 10):
if stream != nil {
stream.Close()
}
return errors.New("timeout opening session")
}
if _, err := stream.Write([]byte(ctHandshakeRequest)); err != nil {
return err
}
buf := make([]byte, len(ctHandshakeResponse))
if _, err := stream.Read(buf); err != nil {
return err
}
if string(buf) != ctHandshakeResponse {
return fmt.Errorf("handshake aborted. got: %s", string(buf))
}
ct := newControl(stream)
c.log.Debug("client has started successfully.")
c.redialBackoff.Reset() // we successfully connected, so we can reset the backoff
c.mu.Lock()
if c.startNotify != nil && !c.closed {
c.log.Debug("sending ok to startNotify chan")
select {
case c.startNotify <- true:
default:
// reaching here means the client never read the signal via
// StartNotify(). This is OK, we shouldn't except it the consumer
// to read from this channel. It's optional, so we just drop the
// signal.
}
}
c.mu.Unlock()
return c.listenControl(ct)
}
func (c *Client) listenControl(ct *control) error {
for {
var msg controlMsg
err := ct.dec.Decode(&msg)
if err != nil {
c.reqWg.Wait() // wait until all requests are finished
c.session.GoAway()
c.session.Close()
return err
}
c.log.Debug("Received control msg %+v", msg)
switch msg.Action {
case requestClientSession:
c.log.Debug("Received request to open a session to server")
go func() {
if err := c.proxy(msg.LocalPort); err != nil {
c.log.Error("Proxy err between remote and local: '%s'", err)
}
}()
}
}
}
func (c *Client) proxy(port int) error {
c.log.Debug("Opening a new stream from server session")
remote, err := c.session.Open()
if err != nil {
return err
}
defer remote.Close()
if port == 0 {
port = 80
}
localAddr := "127.0.0.1:" + strconv.Itoa(port)
if c.config.LocalAddr != "" {
localAddr = c.config.LocalAddr
}
c.log.Debug("Dialing local server %s", localAddr)
local, err := net.Dial("tcp", localAddr)
if err != nil {
c.log.Debug("Dialing local server(%s) failed: %s", localAddr, err)
// send a response instead of canceling it on the server side. at least
// the public connection will know what's happening or not
body := bytes.NewBufferString("no local server")
resp := &http.Response{
Status: http.StatusText(http.StatusServiceUnavailable),
StatusCode: http.StatusServiceUnavailable,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(body),
ContentLength: int64(body.Len()),
}
buf := new(bytes.Buffer)
resp.Write(buf)
if _, err := io.Copy(remote, buf); err != nil {
c.log.Debug("copy in-mem response error: %s\n", err.Error())
}
return nil
}
c.log.Debug("Starting to proxy between remote and local server")
c.reqWg.Add(1)
c.join(local, remote)
c.reqWg.Done()
c.log.Debug("Proxing between remote and local server finished")
return nil
}
func (c *Client) join(local, remote io.ReadWriteCloser) {
var wg sync.WaitGroup
wg.Add(2)
transfer := func(side string, dst, src io.ReadWriteCloser) {
_, err := io.Copy(dst, src)
if err != nil {
c.log.Debug("copy error: %s\n", err.Error())
}
if err := src.Close(); err != nil {
c.log.Debug("%s: close error: %s\n", side, err.Error())
}
// not for yamux streams, but for client to local server connections
if d, ok := dst.(*net.TCPConn); ok {
if err := d.CloseWrite(); err != nil {
c.log.Debug("%s: closeWrite error: %s\n", side, err.Error())
}
}
wg.Done()
}
go transfer("remote to local", local, remote)
go transfer("local to remote", remote, local)
wg.Wait()
return
}