Skip to content

Commit

Permalink
feat: allow specifying ports for HTTPS and TLS, add timeout for TLS
Browse files Browse the repository at this point in the history
feat: allow specifying ports for HTTPS and TLS, add timeout for TLS
  • Loading branch information
ycd authored Nov 24, 2021
2 parents 323f0fb + bc54891 commit c2fd5bd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 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
2 changes: 1 addition & 1 deletion cmd/dstp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"context"
"flag"
"fmt"
"github.com/ycd/dstp/config"
"github.com/ycd/dstp/pkg/dstp"
"os"
"path/filepath"
"fmt"
)

func main() {
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
41 changes: 35 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 All @@ -24,6 +25,10 @@ func RunAllTests(ctx context.Context, config config.Config) error {
return err
}

if config.Timeout == -1 {
config.Timeout = 2 * config.PingCount
}

var wg sync.WaitGroup
wg.Add(5)

Expand All @@ -33,9 +38,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 +51,29 @@ 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 +93,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 +115,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
7 changes: 2 additions & 5 deletions pkg/ping/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ func RunDNSTest(ctx context.Context, wg *sync.WaitGroup, addr common.Address, co
}

pinger.Count = count
if timeout == -1 {
pinger.Timeout = time.Duration(2*count) * time.Second
} else {
pinger.Timeout = time.Duration(timeout) * time.Second
}
pinger.Timeout = time.Duration(timeout) * time.Second

err = pinger.Run()
if err != nil {
return fmt.Errorf("failed to run ping: %v", err.Error())
Expand Down
10 changes: 3 additions & 7 deletions pkg/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"bytes"
"context"
"fmt"
"github.com/ycd/dstp/pkg/common"
"log"
"os/exec"
"runtime"
"strings"
"sync"
"time"

"github.com/ycd/dstp/pkg/common"
)

func RunTest(ctx context.Context, wg *sync.WaitGroup, addr common.Address, count int, timeout int, result *common.Result) error {
Expand All @@ -29,11 +28,8 @@ func runPing(ctx context.Context, wg *sync.WaitGroup, addr common.Address, count
}

pinger.Count = count
if timeout == -1 {
pinger.Timeout = time.Duration(2*count) * time.Second
} else {
pinger.Timeout = time.Duration(timeout) * time.Second
}
pinger.Timeout = time.Duration(timeout) * time.Second

err = pinger.Run()
if err != nil {
if out, err := runPingFallback(ctx, addr, count); err == nil {
Expand Down

0 comments on commit c2fd5bd

Please sign in to comment.