Skip to content

Commit 5e0b443

Browse files
committed
Add support for tls in client
When local-tls is passed we forward tls traffic to another port than none tls traffic if local-tls is ommited all traffic goes to local Fixes: #27 Signed-off-by: Jo De Boeck <deboeck.jo@gmail.com>
1 parent b1af8dc commit 5e0b443

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,9 @@ Second starts the tcp router client and make it opens a connection to the tcp ro
231231
The following command will connect the the server located at `tcprouter-1.com`, forward traffic for `mydomain.com` to the local application running at `localhost:8080` and send the response back.
232232

233233

234-
`trc -local localhost:8080 -remote tcprouter-1.com -secret TB2pbZ5FR8GQZp9W2z97jBjxSgWgQKaQTxEgrZNBa4pEFzv3PJcRVEtG2a5BU9qd`
234+
`trc -local localhost:8080 -remote tcprouter-1.com -secret TB2pbZ5FR8GQZp9W2z97jBjxSgWgQKaQTxEgrZNBa4pEFzv3PJcRVEtG2a5BU9qd`
235+
236+
237+
To forward tls traffic to a difference port than none-tls traffic add the `--local-tls` flag
238+
239+
`trc -local localhost:8080 -local-tls localhost:443 -remote tcprouter-1.com -secret TB2pbZ5FR8GQZp9W2z97jBjxSgWgQKaQTxEgrZNBa4pEFzv3PJcRVEtG2a5BU9qd`

client.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tcprouter
22

33
import (
4+
"bufio"
45
"context"
56
"fmt"
67
"io"
@@ -12,8 +13,9 @@ import (
1213

1314
// Client connect to a tpc router server and opens a reverse tunnel
1415
type Client struct {
15-
localAddr string
16-
remoteAddr string
16+
localAddr string
17+
localTLSAddr string
18+
remoteAddr string
1719
// secret used to identify the connection in the tcp router server
1820
secret []byte
1921

@@ -22,11 +24,12 @@ type Client struct {
2224
}
2325

2426
// NewClient creates a new TCP router client
25-
func NewClient(secret, local, remote string) *Client {
27+
func NewClient(secret, local, localTLS, remote string) *Client {
2628
return &Client{
27-
localAddr: local,
28-
remoteAddr: remote,
29-
secret: []byte(secret),
29+
localAddr: local,
30+
localTLSAddr: localTLS,
31+
remoteAddr: remote,
32+
secret: []byte(secret),
3033
}
3134
}
3235

@@ -139,10 +142,19 @@ func (c *Client) listen(ctx context.Context) error {
139142
Str("remote add", remote.RemoteAddr().String()).
140143
Msg("incoming stream, connect to local application")
141144

142-
local, err := c.connectLocal(c.localAddr)
145+
var err error
146+
var local WriteCloser
147+
br := bufio.NewReader(remote)
148+
_, isTLS, peeked := clientHelloServerName(br)
149+
if isTLS {
150+
local, err = c.connectLocal(c.localTLSAddr)
151+
} else {
152+
local, err = c.connectLocal(c.localAddr)
153+
}
143154
if err != nil {
144155
return fmt.Errorf("failed to connect to local application: %w", err)
145156
}
157+
incoming := GetConn(remote, peeked)
146158

147159
go func(remote, local WriteCloser) {
148160
log.Info().Msg("start forwarding")
@@ -164,7 +176,7 @@ func (c *Client) listen(ctx context.Context) error {
164176
if err := local.Close(); err != nil {
165177
log.Error().Err(err).Msg("Error while terminating connection")
166178
}
167-
}(remote, local)
179+
}(incoming, local)
168180
}
169181
}
170182
}

cmds/client/main.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func main() {
3636
Usage: "address to the local application",
3737
EnvVars: []string{"TRC_LOCAL"},
3838
},
39+
&cli.StringFlag{
40+
Name: "local-tls",
41+
Usage: "address to the local tls application",
42+
EnvVars: []string{"TRC_LOCAL"},
43+
},
3944
&cli.IntFlag{
4045
Name: "backoff",
4146
Value: 5,
@@ -47,6 +52,10 @@ func main() {
4752
app.Action = func(c *cli.Context) error {
4853
remotes := c.StringSlice("remote")
4954
local := c.String("local")
55+
localtls := c.String("local-tls")
56+
if len(localtls) == 0 {
57+
localtls = local
58+
}
5059
backoff := c.Int("backoff")
5160
secret := c.String("secret")
5261

@@ -60,10 +69,11 @@ func main() {
6069

6170
for _, remote := range remotes {
6271
c := connection{
63-
Secret: secret,
64-
Remote: remote,
65-
Local: local,
66-
Backoff: backoff,
72+
Secret: secret,
73+
Remote: remote,
74+
Local: local,
75+
LocalTLS: localtls,
76+
Backoff: backoff,
6777
}
6878
go func() {
6979
defer func() {
@@ -89,14 +99,15 @@ func main() {
8999
}
90100

91101
type connection struct {
92-
Secret string
93-
Remote string
94-
Local string
95-
Backoff int
102+
Secret string
103+
Remote string
104+
Local string
105+
LocalTLS string
106+
Backoff int
96107
}
97108

98109
func start(ctx context.Context, c connection) {
99-
client := tcprouter.NewClient(c.Secret, c.Local, c.Remote)
110+
client := tcprouter.NewClient(c.Secret, c.Local, c.LocalTLS, c.Remote)
100111

101112
op := func() error {
102113
for {

e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func testEnd2End(t *testing.T, size int) {
7474
local := u.Host
7575
remote := fmt.Sprintf("%s:%d", domain, clientPort)
7676
log.Printf("start client local:%v remote:%v\n", local, remote)
77-
client := NewClient(secret, local, remote)
77+
client := NewClient(secret, local, local, remote)
7878
client.Start(ctx)
7979
}()
8080

0 commit comments

Comments
 (0)