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
import "time"
const (
dialerTimeout = 5 * time.Second
defaultPort = 443
protocol = "tcp"
defaultDialerTimeout = 5
defaultPort = 443
protocol = "tcp"
)

View File

@ -18,9 +18,16 @@ func main() {
Usage: "config file",
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 {
prettyPrintCertsInfo(c.String("config"))
prettyPrintCertsInfo(c.String("config"), c.Int("timeout"))
return nil
},
}

View File

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