Skip to content

Commit 170fc79

Browse files
committed
Add --connect-timeout
Allow initial dial timeout duration to be configured and not just set as 'wait forever'
1 parent 8ae9c0e commit 170fc79

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

cmd/client/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313
"strings"
1414
"syscall"
15+
"time"
1516

1617
"github.com/NHAS/reverse_ssh/internal/client"
1718
"github.com/NHAS/reverse_ssh/internal/client/keys"
@@ -66,6 +67,7 @@ func printHelp() {
6667
fmt.Println("\t\t--log-level\tChange logging output levels, [INFO,WARNING,ERROR,FATAL,DISABLED]")
6768
fmt.Println("\t\t--version-string\tSSH version string to use, i.e SSH-VERSION, defaults to internal.Version-runtime.GOOS_runtime.GOARCH")
6869
fmt.Println("\t\t--private-key-path\tOptional path to unencrypted SSH key to use for connecting")
70+
fmt.Println("\t\t--connect-timeout\tDuration to wait for initial connection seconds, default 180, set to 0 to wait indefinitely")
6971

7072
if runtime.GOOS == "windows" {
7173
fmt.Println("\t\t--host-kerberos\tUse kerberos authentication on proxy server (if proxy server specified)")
@@ -173,6 +175,18 @@ func main() {
173175
settings.SNI = userSpecifiedSNI
174176
}
175177

178+
timeoutInt := 180
179+
timeout, err := line.GetArgString("connect-timeout")
180+
if err == nil {
181+
timeoutInt, err = strconv.Atoi(timeout)
182+
if err != nil {
183+
log.Printf("could not parse --connect-timeout as number %v, setting default to 180", err)
184+
timeoutInt = 180
185+
}
186+
}
187+
188+
settings.ConnectTimeout = time.Duration(timeoutInt) * time.Second
189+
176190
userSpecifiedNTLMCreds, err := line.GetArgString("ntlm-proxy-creds")
177191
if err == nil {
178192
if line.IsSet("host-kerberos") {

internal/client/client.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ type Settings struct {
310310

311311
VersionString string
312312

313+
ConnectTimeout time.Duration
314+
313315
ntlm *ntlmssp.Client
314316
}
315317

@@ -363,7 +365,8 @@ func Run(settings *Settings) {
363365
}
364366

365367
config := &ssh.ClientConfig{
366-
User: fmt.Sprintf("%s.%s", username, hostname),
368+
Timeout: settings.ConnectTimeout,
369+
User: fmt.Sprintf("%s.%s", username, hostname),
367370
Auth: []ssh.AuthMethod{
368371
ssh.PublicKeys(sshPriv),
369372
},
@@ -396,8 +399,9 @@ func Run(settings *Settings) {
396399
var conn net.Conn
397400
if scheme != "stdio" {
398401
log.Println("Connecting to", settings.Addr)
402+
399403
// First create raw TCP connection
400-
conn, err = Connect(realAddr, settings.ProxyAddr, config.Timeout, settings.ProxyUseHostKerberos, settings.ntlm)
404+
conn, err = Connect(realAddr, settings.ProxyAddr, settings.ConnectTimeout, settings.ProxyUseHostKerberos, settings.ntlm)
401405
if err != nil {
402406

403407
if errMsg := err.Error(); strings.Contains(errMsg, "missing port in address") {
@@ -426,7 +430,7 @@ func Run(settings *Settings) {
426430
continue
427431
}
428432

429-
<-time.After(10 * time.Second)
433+
time.Sleep(10 * time.Second)
430434
continue
431435
}
432436

@@ -480,7 +484,7 @@ func Run(settings *Settings) {
480484
case "http", "https":
481485

482486
conn, err = NewHTTPConn(scheme+"://"+realAddr, func() (net.Conn, error) {
483-
return Connect(realAddr, settings.ProxyAddr, config.Timeout, settings.ProxyUseHostKerberos, settings.ntlm)
487+
return Connect(realAddr, settings.ProxyAddr, settings.ConnectTimeout, settings.ProxyUseHostKerberos, settings.ntlm)
484488
})
485489

486490
if err != nil {

0 commit comments

Comments
 (0)