Skip to content

Commit

Permalink
feat: allow specifiying ports for https and tls, fix timeout for tls
Browse files Browse the repository at this point in the history
Signed-off-by: Yagiz Degirmenci <yagizcanilbey1903@gmail.com>
  • Loading branch information
ycd committed Nov 24, 2021
1 parent 323f0fb commit d9a319a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Options:
-o, --out <string> The type of the output, either json or plaintext [Default: plaintext]
-p <int> Number of ping packets [Default: 3]
-t <int> Give up on ping after this many seconds [Default: 2s per ping packet]
-p <string> Port for testing TLS and HTTPS connectivity [Default: 443]
-h, --help Show this message and exit.
```

Expand Down Expand Up @@ -129,6 +130,7 @@ for 64-bit Windows, macOS, and Linux targets. They contain the compiled executab
-o, --out <string> The type of the output, either json or plaintext [Default: plaintext]
-p <int> Number of ping packets [Default: 3]
-t <int> Give up on ping after this many seconds [Default: 2s per ping packet]
-p <string> Port for testing TLS and HTTPS connectivity [Default: 443]
-h, --help Show this message and exit.
```

Expand Down
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string> The URL or the IP address to run tests against [REQUIRED]
-o, --out <string> The type of the output, either json or plaintext [Default: plaintext]
-p <int> Number of ping packets [Default: 3]
-t <int> Give up on ping after this many seconds [Default: 2s per ping packet]
-p <string> Port for testing TLS and HTTPS connectivity [Default: 443]
-h, --help Show this message and exit.
`

Expand All @@ -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")
Expand Down
36 changes: 30 additions & 6 deletions pkg/dstp/dstp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"fmt"
"math"
"net"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit d9a319a

Please sign in to comment.