-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
69 lines (61 loc) · 1.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"os"
"flag"
"fmt"
"strings"
"github.com/Snawoot/extip"
"context"
"time"
)
const SRV_LIST_SEP = ";"
type CLIArgs struct {
servers string
quorum uint
timeout time.Duration
ipv6 bool
}
func parseArgs() CLIArgs {
var args CLIArgs
flag.StringVar(&args.servers, "s", strings.Join(extip.PublicServerList, SRV_LIST_SEP),
"STUN server list")
flag.UintVar(&args.quorum, "q", 2, "required number of matches for success")
flag.DurationVar(&args.timeout, "t", 0, "hard timeout. Examples values: 1m, 3s, 1s500ms")
flag.BoolVar(&args.ipv6, "6", false, "use IPv6")
flag.Parse()
return args
}
func run() int {
args := parseArgs()
var ctx context.Context
if args.timeout == 0 {
ctx = context.Background()
} else {
ctx, _ = context.WithTimeout(context.Background(), args.timeout)
}
ip, err := extip.QueryMultipleServers(ctx,
strings.Split(args.servers, SRV_LIST_SEP),
args.quorum,
args.ipv6)
if err != nil {
switch res := err.(type) {
case extip.InconclusiveResult:
fmt.Fprintf(os.Stderr, "Inconclusive result:\n")
fmt.Fprintf(os.Stderr, "Required quorum = %v\n", res.Quorum)
for k, v := range res.Results {
fmt.Fprintf(os.Stderr, "Server %s responded: %s\n", k, v)
}
for k, v := range res.Errors {
fmt.Fprintf(os.Stderr, "Server %s failed: %v\n", k, v)
}
default:
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return 3
}
fmt.Println(ip)
return 0
}
func main() {
os.Exit(run())
}