diff --git a/main.go b/main.go index b06298f..18e747f 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "bytes" "context" "crypto/tls" + "encoding/json" "encoding/pem" "flag" "fmt" @@ -16,6 +18,7 @@ import ( "net/url" "os" "path" + "reflect" "runtime" "sort" "strconv" @@ -27,25 +30,47 @@ import ( "github.com/fatih/color" ) +type Report struct { + Address string + Header http.Header + Proto string + Status string + Timing Timing +} + +type Timing struct { + DNS int + TCP int + TLS int + Server int + Transfer int + + Lookup int + Connect int + PreTransfer int + StartTransfer int + Total int +} + const ( HTTPSTemplate = `` + ` DNS Lookup TCP Connection TLS Handshake Server Processing Content Transfer` + "\n" + - `[%s | %s | %s | %s | %s ]` + "\n" + + `[ %>DNS | %>TCP | %>TLS | %>Server | %>Transfer ]` + "\n" + ` | | | | |` + "\n" + - ` namelookup:%s | | | |` + "\n" + - ` connect:%s | | |` + "\n" + - ` pretransfer:%s | |` + "\n" + - ` starttransfer:%s |` + "\n" + - ` total:%s` + "\n" + ` namelookup:%DNS | %>TCP | %>Server | %>Transfer ]` + "\n" + + ` | | | |` + "\n" + + ` namelookup:%= 'a' && b[end] <= 'z') || (b[end] >= 'A' && b[end] <= 'Z')); end++ { + } + vnam := string(b[idx+2 : end]) + copy(b[idx:end], bytes.Repeat([]byte{' '}, end-idx)) + val := rvars.FieldByName(vnam) + if !val.IsValid() { + panic("invalid template variable: " + vnam) + } + v := strconv.Itoa(val.Interface().(int)) + "ms" + vlen := len(v) + v = color.CyanString(v) + switch dir { + case '>': + b = append(append(append([]byte{}, b[:end-vlen]...), []byte(v)...), b[end:]...) + case '<': + b = append(append(append([]byte{}, b[:idx]...), []byte(v)...), b[idx+vlen:]...) + default: + panic("invalid direction: " + string(dir)) + } + idx = end + } + print(string(b)) +} + func isRedirect(resp *http.Response) bool { return resp.StatusCode > 299 && resp.StatusCode < 400 }