From 0d104beb2159c1af6f7a5e8a90c1124d49a4ae8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Sollie?= Date: Thu, 4 Jan 2024 20:02:41 +0100 Subject: [PATCH 1/2] add configurable timeout --- main.go | 8 ++++++-- tscanner/tscanner.go | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index c3ce2ec..33e8b34 100644 --- a/main.go +++ b/main.go @@ -127,6 +127,10 @@ func main() { "help", false, "Prints this usage help to the user.") + timeoutPtr := flag.Int( + "timeout", + 5, + "Timeout in seconds for the connection to the server.") flag.Parse() color.NoColor = *noColor if (*connectPtr == "" && *listenPtr == "" && !*versionPtr) || *helpPtr { @@ -146,13 +150,13 @@ func main() { if *connectPtr != "" { address := formatAddress(*connectPtr, tscanner.ServerScan) var err error - if report, err = tscanner.Scan(address, tscanner.ServerScan, true); err != nil { + if report, err = tscanner.Scan(address, tscanner.ServerScan, true, timeoutPtr); err != nil { panic(err) } } else if *listenPtr != "" { address := formatAddress(*listenPtr, tscanner.ClientScan) var err error - if report, err = tscanner.Scan(address, tscanner.ClientScan, true); err != nil { + if report, err = tscanner.Scan(address, tscanner.ClientScan, true, timeoutPtr); err != nil { panic(err) } } diff --git a/tscanner/tscanner.go b/tscanner/tscanner.go index 50a9eb0..7ac3802 100644 --- a/tscanner/tscanner.go +++ b/tscanner/tscanner.go @@ -7,11 +7,13 @@ import ( "encoding/binary" "encoding/json" "fmt" - "golang.org/x/exp/slices" "io" "net" "os" "strings" + "time" + + "golang.org/x/exp/slices" ) // ScanMode describes a scan mode for the scanner. @@ -59,11 +61,12 @@ func (report *Report) MarshalJSON() ([]byte, error) { } // Scan performs a vulnerability scan to check whether the remote peer is likely to be vulnerable against prefix truncation. -func Scan(address string, scanMode ScanMode, verbose bool) (*Report, error) { +func Scan(address string, scanMode ScanMode, verbose bool, timeout *int) (*Report, error) { var conn net.Conn if scanMode == ServerScan { var err error - if conn, err = net.Dial("tcp", address); err != nil { + dialer := net.Dialer{Timeout: time.Duration(*timeout) * time.Second} + if conn, err = dialer.Dial("tcp", address); err != nil { return nil, err } } else if scanMode == ClientScan { From 5b92fa37aad471b5c81d202586fa8b1782222db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=A4umer?= Date: Tue, 9 Jan 2024 12:54:35 +0100 Subject: [PATCH 2/2] feat: Add ScanWithTimeout function --- main.go | 4 ++-- tscanner/tscanner.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 33e8b34..ec76073 100644 --- a/main.go +++ b/main.go @@ -150,13 +150,13 @@ func main() { if *connectPtr != "" { address := formatAddress(*connectPtr, tscanner.ServerScan) var err error - if report, err = tscanner.Scan(address, tscanner.ServerScan, true, timeoutPtr); err != nil { + if report, err = tscanner.ScanWithTimeout(address, tscanner.ServerScan, true, *timeoutPtr); err != nil { panic(err) } } else if *listenPtr != "" { address := formatAddress(*listenPtr, tscanner.ClientScan) var err error - if report, err = tscanner.Scan(address, tscanner.ClientScan, true, timeoutPtr); err != nil { + if report, err = tscanner.ScanWithTimeout(address, tscanner.ClientScan, true, *timeoutPtr); err != nil { panic(err) } } diff --git a/tscanner/tscanner.go b/tscanner/tscanner.go index 7ac3802..c0b7eec 100644 --- a/tscanner/tscanner.go +++ b/tscanner/tscanner.go @@ -61,11 +61,17 @@ func (report *Report) MarshalJSON() ([]byte, error) { } // Scan performs a vulnerability scan to check whether the remote peer is likely to be vulnerable against prefix truncation. -func Scan(address string, scanMode ScanMode, verbose bool, timeout *int) (*Report, error) { +func Scan(address string, scanMode ScanMode, verbose bool) (*Report, error) { + return ScanWithTimeout(address, scanMode, verbose, 0) +} + +// ScanWithTimeout performs a vulnerability scan with configurable timeout to check whether the remote peer +// is likely to be vulnerable against prefix truncation. +func ScanWithTimeout(address string, scanMode ScanMode, verbose bool, timeout int) (*Report, error) { var conn net.Conn if scanMode == ServerScan { var err error - dialer := net.Dialer{Timeout: time.Duration(*timeout) * time.Second} + dialer := net.Dialer{Timeout: time.Duration(timeout) * time.Second} if conn, err = dialer.Dial("tcp", address); err != nil { return nil, err }