Make it possible to change timeout value

This commit is contained in:
guessi 2020-05-10 17:03:11 +08:00
parent 0a11acd998
commit 7454c88506
3 changed files with 18 additions and 12 deletions

View File

@ -1,9 +1,7 @@
package main package main
import "time"
const ( const (
dialerTimeout = 5 * time.Second defaultDialerTimeout = 5
defaultPort = 443 defaultPort = 443
protocol = "tcp" protocol = "tcp"
) )

View File

@ -18,9 +18,16 @@ func main() {
Usage: "config file", Usage: "config file",
Required: true, Required: true,
}, },
&cli.IntFlag{
Name: "timeout",
Aliases: []string{"t"},
Value: defaultDialerTimeout,
Usage: "dialer timeout in second(s)",
Required: false,
},
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
prettyPrintCertsInfo(c.String("config")) prettyPrintCertsInfo(c.String("config"), c.Int("timeout"))
return nil return nil
}, },
} }

View File

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
"github.com/jedib0t/go-pretty/table" "github.com/jedib0t/go-pretty/table"
"github.com/jedib0t/go-pretty/text" "github.com/jedib0t/go-pretty/text"
@ -37,10 +38,10 @@ func readConfig(config string) Config {
return c return c
} }
func getPeerCertificates(h string, port int) ([]*x509.Certificate, error) { func getPeerCertificates(h string, port int, timeout int) ([]*x509.Certificate, error) {
conn, err := tls.DialWithDialer( conn, err := tls.DialWithDialer(
&net.Dialer{ &net.Dialer{
Timeout: dialerTimeout, Timeout: time.Duration(timeout) * time.Second,
}, },
protocol, protocol,
h+":"+strconv.Itoa(port), h+":"+strconv.Itoa(port),
@ -58,9 +59,9 @@ func getPeerCertificates(h string, port int) ([]*x509.Certificate, error) {
return conn.ConnectionState().PeerCertificates, nil return conn.ConnectionState().PeerCertificates, nil
} }
func getCells(t table.Writer, host string, port int, wg *sync.WaitGroup) { func getCells(t table.Writer, host string, port, timeout int, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
certs, err := getPeerCertificates(host, port) certs, err := getPeerCertificates(host, port, timeout)
if err != nil { if err != nil {
fmt.Printf("err: %s\n", err) fmt.Printf("err: %s\n", err)
return // skip if target host invalid return // skip if target host invalid
@ -82,7 +83,7 @@ func getCells(t table.Writer, host string, port int, wg *sync.WaitGroup) {
} }
} }
func prettyPrintCertsInfo(config string) { func prettyPrintCertsInfo(config string, timeout int) {
rc := readConfig(config) rc := readConfig(config)
if len(rc.Hosts) <= 0 { if len(rc.Hosts) <= 0 {
fmt.Printf("key not found, or empty input\n") fmt.Printf("key not found, or empty input\n")
@ -116,7 +117,7 @@ func prettyPrintCertsInfo(config string) {
} }
wg.Add(1) wg.Add(1)
go getCells(t, ts[0], p, &wg) go getCells(t, ts[0], p, timeout, &wg)
} }
wg.Wait() wg.Wait()