From b7d5135eac73d9db583efeb292484722cb6bf0fe Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Date: Fri, 29 Dec 2023 10:59:22 +0300 Subject: [PATCH] feat: parse timeout and deadline from string --- cmd/main.go | 20 ++++++++++++++------ cmd/models.go | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index cdeb8b2..4ca185a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,7 +13,7 @@ import ( func main() { flag.IntVarP(&poolSize, "num_relays", "n", 100, `The number of concurrent relays tested.`) flag.IntVarP(&goal, "working_relay_num_goal", "g", 5, `Test until at least this number of working relays are found`) - flag.IntVarP(&timeout, "timeout", "t", 1, `Socket connection timeout`) + flag.StringVarP(&timeoutStr, "timeout", "t", "200ms", `Socket connection timeout`) flag.StringVarP(&outfile, "outfile", "o", "", `Output reachable relays to file`) flag.BoolVar(&torrc, "torrc", false, `Output reachable relays in torrc format (with "Bridge" prefix)`) flag.StringArrayVarP(&urls, "url", "u", []string{}, `Preferred alternative URL for onionoo relay list. Could be used multiple times.`) @@ -22,25 +22,33 @@ func main() { flag.BoolVarP(&ipv6, "ipv6", "6", false, `Use ipv6 only nodes`) flag.BoolVarP(&jsonRelays, "json", "j", false, `Get available relays in json format`) flag.BoolVarP(&silent, "silent", "s", false, `Silent mode`) - flag.IntVarP(&deadline, "deadline", "d", 1, `The deadline of program execution (in minutes)`) + flag.StringVarP(&deadlineStr, "deadline", "d", "1m", `The deadline of program execution`) flag.Usage = usage flag.Parse() - if timeout < 1 { - timeout = 1 + timeout, err := time.ParseDuration(timeoutStr) + if err != nil { + color.Errorf("cannot parse timeout duration: %s\n", err) + os.Exit(1) + } + + deadline, err := time.ParseDuration(deadlineStr) + if err != nil { + color.Errorf("cannot parse deadline duration: %s\n", err) + os.Exit(1) } sc := scanner.New( poolSize, goal, - time.Duration(timeout)*time.Second, + timeout, outfile, urls, port, ipv4, ipv6, silent, - time.Duration(deadline)*time.Minute, + deadline, ) var prefix string diff --git a/cmd/models.go b/cmd/models.go index 485bb94..1f5ffba 100644 --- a/cmd/models.go +++ b/cmd/models.go @@ -5,7 +5,7 @@ var ( // Test until at least this number of working relays are found. default=5 goal int // Socket connection timeout. default=1s - timeout int + timeoutStr string // Output reachable relays to file. default=sys.stdout outfile string // Output reachable relays in torrc format (with "Bridge" prefix) @@ -23,5 +23,5 @@ var ( // Silent mode silent bool // Deadline - deadline int + deadlineStr string )