Skip to content

Commit

Permalink
support tunnel relay
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Sep 17, 2023
1 parent 23fc310 commit 7891a1c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ require (
github.com/chzyer/readline v1.5.1
github.com/mattn/go-isatty v0.0.19
github.com/natefinch/npipe v0.0.0-20160621034901-c1b8fa8bdcce
github.com/skeema/knownhosts v1.2.0
github.com/skeema/knownhosts v1.2.1-0.20230916192230-09454b7d5683
github.com/stretchr/testify v1.8.4
github.com/trzsz/go-arg v1.5.2
github.com/trzsz/promptui v0.10.3
github.com/trzsz/ssh_config v1.3.3
github.com/trzsz/trzsz-go v1.1.6-0.20230903153817-767c161acdc1
golang.org/x/crypto v0.12.0
golang.org/x/sys v0.11.0
golang.org/x/term v0.11.0
github.com/trzsz/trzsz-go v1.1.6-0.20230917131507-3c175df078af
golang.org/x/crypto v0.13.0
golang.org/x/sys v0.12.0
golang.org/x/term v0.12.0
)

require (
Expand All @@ -31,9 +31,9 @@ require (
github.com/ncruces/zenity v0.10.10 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
golang.org/x/image v0.11.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/image v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 h1:GranzK4hv1/pqTIhMTXt2X8MmMOuH3hMeUR0o9SP5yc=
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844/go.mod h1:T1TLSfyWVBRXVGzWd0o9BI4kfoO9InEgfQe4NV3mLz8=
github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM=
github.com/skeema/knownhosts v1.2.0/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo=
github.com/skeema/knownhosts v1.2.1-0.20230916192230-09454b7d5683 h1:MGiUIJBzWhQ32bSlwCNzhXKSyZsttl/3O2CuR71p8dw=
github.com/skeema/knownhosts v1.2.1-0.20230916192230-09454b7d5683/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand All @@ -51,24 +51,24 @@ github.com/trzsz/promptui v0.10.3 h1:uhcLQsLZqMxEtGiYoeM2lR/Hd4pSxoYsd2eFctH8MCs
github.com/trzsz/promptui v0.10.3/go.mod h1:GMZtu6ZTzU73CBFkzGtmB4wnTROIAbv4GFA74fV8V8g=
github.com/trzsz/ssh_config v1.3.3 h1:FodC5NBpnBSI2CJqU62d0Ya3yTUHWDpbWKugKX0LXZs=
github.com/trzsz/ssh_config v1.3.3/go.mod h1:Dl1okTjVVfsrtTA8nqkJ1OnjiCrZY6DUEI2DGT2/YoQ=
github.com/trzsz/trzsz-go v1.1.6-0.20230903153817-767c161acdc1 h1:DzQNVqnvl5S/U9YAqCoR/17xwjq1YOW2JHZBp+GMlSM=
github.com/trzsz/trzsz-go v1.1.6-0.20230903153817-767c161acdc1/go.mod h1:kMIPDvZb04WSStbM8/zrpnEbCuAiCe+OjEasAC/O9/A=
github.com/trzsz/trzsz-go v1.1.6-0.20230917131507-3c175df078af h1:AtKnK0fUS/08r3H0NXvimL+9tKIOoguIb9yqwqZvn3I=
github.com/trzsz/trzsz-go v1.1.6-0.20230917131507-3c175df078af/go.mod h1:kMIPDvZb04WSStbM8/zrpnEbCuAiCe+OjEasAC/O9/A=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ=
golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -83,19 +83,19 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
6 changes: 3 additions & 3 deletions tssh/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func listenOnRemote(args *sshArgs, client *ssh.Client, addr *string, port string
}

func stdioForward(client *ssh.Client, addr string) (*sync.WaitGroup, error) {
conn, err := dialWithTimeout(client, "tcp", addr)
conn, err := dialWithTimeout(client, "tcp", addr, 10*time.Second)
if err != nil {
return nil, fmt.Errorf("stdio forward failed: %v", err)
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func dynamicForward(client *ssh.Client, b *bindCfg, args *sshArgs) {
server, err := socks5.New(&socks5.Config{
Resolver: &sshResolver{},
Dial: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialWithTimeout(client, network, addr)
return dialWithTimeout(client, network, addr, 10*time.Second)
},
Logger: log.New(io.Discard, "", log.LstdFlags),
})
Expand Down Expand Up @@ -336,7 +336,7 @@ func localForward(client *ssh.Client, f *forwardCfg, args *sshArgs) {
fmt.Fprintf(os.Stderr, "local forward accept failed: %v\r\n", err)
continue
}
remote, err := dialWithTimeout(client, "tcp", remoteAddr)
remote, err := dialWithTimeout(client, "tcp", remoteAddr, 10*time.Second)
if err != nil {
fmt.Fprintf(os.Stderr, "local forward dial [%s] failed: %v\r\n", remoteAddr, err)
local.Close()
Expand Down
16 changes: 11 additions & 5 deletions tssh/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,15 @@ func execProxyCommand(param *loginParam) (net.Conn, string, error) {
return &cmdPipe{stdin: cmdIn, stdout: cmdOut, addr: param.addr}, command, nil
}

func dialWithTimeout(client *ssh.Client, network, addr string) (conn net.Conn, err error) {
func dialWithTimeout(client *ssh.Client, network, addr string, timeout time.Duration) (conn net.Conn, err error) {
done := make(chan struct{}, 1)
go func() {
defer close(done)
conn, err = client.Dial(network, addr)
done <- struct{}{}
}()
select {
case <-time.After(10 * time.Second):
case <-time.After(timeout):
err = fmt.Errorf("dial [%s] timeout", addr)
case <-done:
}
Expand Down Expand Up @@ -720,7 +720,7 @@ func sshConnect(args *sshArgs, client *ssh.Client, proxy string) (*ssh.Client, e

proxyConnect := func(client *ssh.Client, proxy string) (*ssh.Client, error) {
debug("login to [%s], addr: %s", args.Destination, param.addr)
conn, err := dialWithTimeout(client, "tcp", param.addr)
conn, err := dialWithTimeout(client, "tcp", param.addr, 10*time.Second)
if err != nil {
return nil, fmt.Errorf("proxy [%s] dial tcp [%s] failed: %v", proxy, param.addr, err)
}
Expand Down Expand Up @@ -950,10 +950,16 @@ func sshLogin(args *sshArgs, tty bool) (client *ssh.Client, session *ssh.Session
trzsz.SetAffectedByWindows(false)
if args.Relay || isNoGUI() {
// run as a relay
trzsz.NewTrzszRelay(os.Stdin, os.Stdout, serverIn, serverOut, trzsz.TrzszOptions{
trzszRelay := trzsz.NewTrzszRelay(os.Stdin, os.Stdout, serverIn, serverOut, trzsz.TrzszOptions{
DetectTraceLog: args.TraceLog,
})
// reset terminal size on resize
onTerminalResize(func(width, height int) { _ = session.WindowChange(height, width) })
// setup tunnel connect
trzszRelay.SetTunnelConnector(func(port int) net.Conn {
conn, _ := dialWithTimeout(client, "tcp", fmt.Sprintf("127.0.0.1:%d", port), time.Second)
return conn
})
} else {
// create a TrzszFilter to support trzsz ( trz / tsz )
//
Expand Down Expand Up @@ -982,7 +988,7 @@ func sshLogin(args *sshArgs, tty bool) (client *ssh.Client, session *ssh.Session

// setup tunnel connect
trzszFilter.SetTunnelConnector(func(port int) net.Conn {
conn, _ := client.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
conn, _ := dialWithTimeout(client, "tcp", fmt.Sprintf("127.0.0.1:%d", port), time.Second)
return conn
})
}
Expand Down

0 comments on commit 7891a1c

Please sign in to comment.