diff --git a/README.md b/README.md index ba854ce..c8e850c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Options: -o, --out The type of the output, either json or plaintext [Default: plaintext] -p Number of ping packets [Default: 3] -t Give up on ping after this many seconds [Default: 2s per ping packet] + -p Port for testing TLS and HTTPS connectivity [Default: 443] -h, --help Show this message and exit. ``` @@ -129,6 +130,7 @@ for 64-bit Windows, macOS, and Linux targets. They contain the compiled executab -o, --out The type of the output, either json or plaintext [Default: plaintext] -p Number of ping packets [Default: 3] -t Give up on ping after this many seconds [Default: 2s per ping packet] + -p Port for testing TLS and HTTPS connectivity [Default: 443] -h, --help Show this message and exit. ``` diff --git a/config/config.go b/config/config.go index d703ae2..ad33f00 100644 --- a/config/config.go +++ b/config/config.go @@ -14,15 +14,16 @@ type Config struct { PingCount int Timeout int ShowHelp bool + Port string } -var usageStr = ` -Usage: dstp [OPTIONS] [ARGS] +var usageStr = `Usage: dstp [OPTIONS] [ARGS] Options: -a, --addr The URL or the IP address to run tests against [REQUIRED] -o, --out The type of the output, either json or plaintext [Default: plaintext] -p Number of ping packets [Default: 3] -t Give up on ping after this many seconds [Default: 2s per ping packet] + -p Port for testing TLS and HTTPS connectivity [Default: 443] -h, --help Show this message and exit. ` @@ -48,6 +49,7 @@ func ConfigureOptions(fs *flag.FlagSet, args []string) (*Config, error) { fs.StringVar(&opts.Addr, "addr", "", "The URL or the IP address to run tests against") fs.StringVar(&opts.Output, "o", "plaintext", "The type of the output") fs.StringVar(&opts.Output, "out", "plaintext", "The type of the output") + fs.StringVar(&opts.Port, "port", "", "Port for testing TLS and HTTPS connectivity") fs.IntVar(&opts.PingCount, "p", 3, "Number of ping packets") fs.IntVar(&opts.Timeout, "t", -1, "Give up on ping after this many seconds") fs.BoolVar(&opts.ShowHelp, "h", false, "Show help message") diff --git a/pkg/dstp/dstp.go b/pkg/dstp/dstp.go index 4cde786..47e763d 100644 --- a/pkg/dstp/dstp.go +++ b/pkg/dstp/dstp.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "fmt" "math" + "net" "net/http" "sync" "time" @@ -33,9 +34,9 @@ func RunAllTests(ctx context.Context, config config.Config) error { go lookup.Host(ctx, &wg, common.Address(addr), &result) - go testTLS(ctx, &wg, common.Address(addr), &result) + go testTLS(ctx, &wg, common.Address(addr), config.Timeout, config.Port, &result) - go testHTTPS(ctx, &wg, common.Address(addr), config.Timeout, &result) + go testHTTPS(ctx, &wg, common.Address(addr), config.Timeout, config.Port, &result) wg.Wait() s := result.Output(config.Output) @@ -46,16 +47,28 @@ func RunAllTests(ctx context.Context, config config.Config) error { return nil } -func testTLS(ctx context.Context, wg *sync.WaitGroup, address common.Address, result *common.Result) error { +func testTLS(ctx context.Context, wg *sync.WaitGroup, address common.Address, t int, port string, result *common.Result) error { var output string defer wg.Done() - conn, err := tls.Dial("tcp", fmt.Sprintf("%s:443", string(address)), nil) + p := "443" + + if port != "" { + p = port + } + + conn, err := tls.DialWithDialer(&net.Dialer{Timeout: time.Duration(t) * time.Second}, "tcp", fmt.Sprintf("%s:%s", string(address), p), nil) if err != nil { + result.Mu.Lock() + result.TLS = err.Error() + result.Mu.Unlock() return err } err = conn.VerifyHostname(string(address)) if err != nil { + result.Mu.Lock() + result.TLS = err.Error() + result.Mu.Unlock() return err } @@ -75,11 +88,19 @@ func testTLS(ctx context.Context, wg *sync.WaitGroup, address common.Address, re return nil } -func testHTTPS(ctx context.Context, wg *sync.WaitGroup, address common.Address, t int, result *common.Result) error { +func testHTTPS(ctx context.Context, wg *sync.WaitGroup, address common.Address, t int, port string, result *common.Result) error { defer wg.Done() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s", address), nil) + url := fmt.Sprintf("https://%s", address.String()) + if port != "" { + url += fmt.Sprintf(":%s", port) + } + + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { + result.Mu.Lock() + result.HTTPS = err.Error() + result.Mu.Unlock() return err } @@ -89,6 +110,9 @@ func testHTTPS(ctx context.Context, wg *sync.WaitGroup, address common.Address, resp, err := client.Do(req) if err != nil { + result.Mu.Lock() + result.HTTPS = err.Error() + result.Mu.Unlock() return err }