|
1 | 1 | package tcprouter
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
5 | 6 | "io"
|
6 | 7 | "net"
|
7 | 8 |
|
| 9 | + "github.com/libp2p/go-yamux" |
8 | 10 | "github.com/rs/zerolog/log"
|
9 | 11 | )
|
10 | 12 |
|
11 | 13 | type Client struct {
|
12 |
| - // connection to the tcp router server |
13 |
| - RemoteConn net.Conn |
14 |
| - // connection to the local application |
15 |
| - LocalConn net.Conn |
16 |
| - |
| 14 | + localAddr string |
| 15 | + remoteAddr string |
17 | 16 | // secret used to identify the connection in the tcp router server
|
18 | 17 | secret []byte
|
| 18 | + |
| 19 | + // connection to the tcp router server |
| 20 | + remoteSession *yamux.Session |
19 | 21 | }
|
20 | 22 |
|
21 | 23 | // NewClient creates a new TCP router client
|
22 |
| -func NewClient(secret string) *Client { |
| 24 | +func NewClient(secret, local, remote string) *Client { |
23 | 25 | return &Client{
|
24 |
| - secret: []byte(secret), |
| 26 | + localAddr: local, |
| 27 | + remoteAddr: remote, |
| 28 | + secret: []byte(secret), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Start starts the client by opening a connection to the router server, doing the handshake |
| 33 | +// then start listening for incoming steam from the router server |
| 34 | +func (c Client) Start(ctx context.Context) error { |
| 35 | + if err := c.connectRemote(c.remoteAddr); err != nil { |
| 36 | + return fmt.Errorf("failed to connect to TCP router server: %w", err) |
25 | 37 | }
|
| 38 | + |
| 39 | + log.Info().Msg("start handshake") |
| 40 | + if err := c.handshake(); err != nil { |
| 41 | + return fmt.Errorf("failed to handshake with TCP router server: %w", err) |
| 42 | + } |
| 43 | + log.Info().Msg("handshake done") |
| 44 | + |
| 45 | + return c.listen(ctx) |
26 | 46 | }
|
27 | 47 |
|
28 |
| -func (c *Client) ConnectRemote(addr string) error { |
| 48 | +func (c *Client) connectRemote(addr string) error { |
29 | 49 | if len(c.secret) == 0 {
|
30 | 50 | return fmt.Errorf("no secret configured")
|
31 | 51 | }
|
32 | 52 |
|
33 |
| - conn, err := net.Dial("tcp", addr) |
| 53 | + tcpAddr, err := net.ResolveTCPAddr("tcp", addr) |
34 | 54 | if err != nil {
|
35 | 55 | return err
|
36 | 56 | }
|
37 | 57 |
|
38 |
| - c.RemoteConn = conn |
| 58 | + conn, err := net.DialTCP("tcp", nil, tcpAddr) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // Setup client side of yamux |
| 64 | + session, err := yamux.Client(conn, nil) |
| 65 | + if err != nil { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | + |
| 69 | + c.remoteSession = session |
39 | 70 |
|
40 | 71 | return nil
|
41 | 72 | }
|
42 | 73 |
|
43 |
| -func (c *Client) ConnectLocal(addr string) error { |
44 |
| - conn, err := net.Dial("tcp", addr) |
| 74 | +func (c *Client) connectLocal(addr string) (WriteCloser, error) { |
| 75 | + tcpAddr, err := net.ResolveTCPAddr("tcp", addr) |
45 | 76 | if err != nil {
|
46 |
| - return err |
| 77 | + return nil, err |
47 | 78 | }
|
48 | 79 |
|
49 |
| - c.LocalConn = conn |
| 80 | + conn, err := net.DialTCP("tcp", nil, tcpAddr) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
50 | 84 |
|
51 |
| - return nil |
| 85 | + return conn, nil |
52 | 86 | }
|
53 | 87 |
|
54 |
| -func (c *Client) Handshake() error { |
55 |
| - if c.RemoteConn == nil { |
| 88 | +func (c *Client) handshake() error { |
| 89 | + if c.remoteSession == nil { |
56 | 90 | return fmt.Errorf("not connected")
|
57 | 91 | }
|
58 | 92 |
|
59 | 93 | h := Handshake{
|
60 | 94 | MagicNr: MagicNr,
|
61 | 95 | Secret: []byte(c.secret),
|
62 | 96 | }
|
63 |
| - // at this point if the server refuse the hanshake it will |
| 97 | + // at this point if the server refuse the handshake it will |
64 | 98 | // just close the connection which should return an error
|
65 |
| - return h.Write(c.RemoteConn) |
| 99 | + stream, err := c.remoteSession.OpenStream() |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + defer stream.Close() |
| 104 | + |
| 105 | + return h.Write(stream) |
66 | 106 | }
|
67 | 107 |
|
68 |
| -func (c *Client) Forward() { |
| 108 | +func (c *Client) listen(ctx context.Context) error { |
| 109 | + ctx, cancel := context.WithCancel(ctx) |
| 110 | + defer cancel() |
69 | 111 |
|
| 112 | + cCon := make(chan WriteCloser) |
70 | 113 | cErr := make(chan error)
|
71 |
| - defer func() { |
72 |
| - c.RemoteConn.Close() |
73 |
| - c.LocalConn.Close() |
74 |
| - }() |
75 |
| - |
76 |
| - go forward(c.LocalConn, c.RemoteConn, cErr) |
77 |
| - go forward(c.RemoteConn, c.LocalConn, cErr) |
78 |
| - |
79 |
| - err := <-cErr |
80 |
| - if err != nil { |
81 |
| - log.Error().Err(err).Msg("Error during connection") |
| 114 | + go func(ctx context.Context, cCon chan<- WriteCloser, cErr chan<- error) { |
| 115 | + for { |
| 116 | + select { |
| 117 | + case <-ctx.Done(): |
| 118 | + return |
| 119 | + default: |
| 120 | + conn, err := c.remoteSession.AcceptStream() |
| 121 | + if err != nil { |
| 122 | + cErr <- err |
| 123 | + return |
| 124 | + } |
| 125 | + cCon <- WrapConn(conn) |
| 126 | + } |
| 127 | + } |
| 128 | + }(ctx, cCon, cErr) |
| 129 | + |
| 130 | + for { |
| 131 | + select { |
| 132 | + case <-ctx.Done(): |
| 133 | + return nil |
| 134 | + case err := <-cErr: |
| 135 | + return fmt.Errorf("accept connection failed: %w", err) |
| 136 | + case remote := <-cCon: |
| 137 | + log.Info(). |
| 138 | + Str("remote add", remote.RemoteAddr().String()). |
| 139 | + Msg("incoming stream, connect to local application") |
| 140 | + |
| 141 | + local, err := c.connectLocal(c.localAddr) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("failed to connect to local application: %w", err) |
| 144 | + } |
| 145 | + |
| 146 | + go func(remote, local WriteCloser) { |
| 147 | + log.Info().Msg("start forwarding") |
| 148 | + |
| 149 | + cErr := make(chan error) |
| 150 | + go forward(local, remote, cErr) |
| 151 | + go forward(remote, local, cErr) |
| 152 | + |
| 153 | + err = <-cErr |
| 154 | + if err != nil { |
| 155 | + log.Error().Err(err).Msg("Error during forwarding: %w") |
| 156 | + } |
| 157 | + |
| 158 | + <-cErr |
| 159 | + |
| 160 | + if err := remote.Close(); err != nil { |
| 161 | + log.Error().Err(err).Msg("Error while terminating connection") |
| 162 | + } |
| 163 | + if err := local.Close(); err != nil { |
| 164 | + log.Error().Err(err).Msg("Error while terminating connection") |
| 165 | + } |
| 166 | + }(remote, local) |
| 167 | + } |
82 | 168 | }
|
83 |
| - |
84 |
| - <-cErr |
85 | 169 | }
|
86 | 170 |
|
87 |
| -func forward(dst, src net.Conn, cErr chan<- error) { |
| 171 | +func forward(dst, src WriteCloser, cErr chan<- error) { |
88 | 172 | _, err := io.Copy(dst, src)
|
89 | 173 | cErr <- err
|
90 |
| - |
91 |
| - tcpConn, ok := dst.(*net.TCPConn) |
92 |
| - if ok { |
93 |
| - if err := tcpConn.CloseWrite(); err != nil { |
94 |
| - log.Error().Err(err).Msg("Error while terminating connection") |
95 |
| - } |
| 174 | + if err := dst.CloseWrite(); err != nil { |
| 175 | + log.Error().Err(err).Msgf("error closing %s", dst.RemoteAddr().String()) |
96 | 176 | }
|
97 | 177 | }
|
| 178 | + |
| 179 | +type wrappedCon struct { |
| 180 | + *yamux.Stream |
| 181 | +} |
| 182 | + |
| 183 | +// WrapConn wraps a stream into a wrappedCon so it implements the WriteCloser interface |
| 184 | +func WrapConn(conn *yamux.Stream) WriteCloser { |
| 185 | + return wrappedCon{conn} |
| 186 | +} |
| 187 | + |
| 188 | +func (c wrappedCon) CloseWrite() error { |
| 189 | + return c.Stream.Close() |
| 190 | +} |
0 commit comments