diff --git a/core/printer.go b/core/printer.go index 50f8a1d..0b0def1 100644 --- a/core/printer.go +++ b/core/printer.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "runtime" + "strconv" "github.com/jedib0t/go-pretty/v6/table" ) @@ -33,9 +34,9 @@ func (p *Printer) ToTable(results *[]TestResult) { t := table.NewWriter() t.SetStyle(table.StyleColoredBright) t.SetOutputMirror(os.Stdout) - t.AppendHeader(table.Row{"#", "Address", "Status"}) - for i, r := range *results { - t.AppendRow(table.Row{i, r.url.Host, r.status}) + t.AppendHeader(table.Row{"Address", "Connection", "Status"}) + for _, r := range *results { + t.AppendRow(table.Row{r.url.Host, strconv.FormatInt(r.duration.Milliseconds(), 10) + "ms", r.status}) } t.AppendSeparator() diff --git a/core/utils.go b/core/utils.go index 28364d9..cdbbadb 100644 --- a/core/utils.go +++ b/core/utils.go @@ -2,10 +2,11 @@ package core import ( "context" + "fmt" "io" - "net" "net/http" "net/url" + "regexp" "time" ) @@ -34,18 +35,26 @@ func GetStringFromURL(url string) (string, error) { } type TestResult struct { - url url.URL - status string + url url.URL + status string + duration time.Duration } func Test(url *url.URL, timeoutSeconds int, out chan TestResult) { - timeOut := time.Duration(timeoutSeconds) * time.Second - _, err := net.DialTimeout(url.Scheme, url.Host, timeOut) + timeout := time.Duration(timeoutSeconds) * time.Second + tp := NewTransport(timeout) + + _, err := tp.Dial(url.Scheme, url.Host) if err != nil { - out <- TestResult{url: *url, status: err.Error()} + out <- TestResult{url: *url, status: formatError(err, url), duration: tp.ConnDuration()} return } - out <- TestResult{url: *url, status: "OK"} + out <- TestResult{url: *url, status: "OK", duration: tp.Duration()} +} + +func formatError(err error, url *url.URL) string { + m := regexp.MustCompile(fmt.Sprintf(`(net/http: )| \(.*\)|(dial .* %s: )`, url.Host)) + return m.ReplaceAllString(err.Error(), "") }