-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
83 lines (74 loc) · 1.72 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/ccding/go-stun/stun"
"os"
"strings"
)
type NatResult struct {
Code int `json:"code"`
Msg string `json:"msg"`
Nat string `json:"nat"`
IP string `json:"ip"`
Port uint16 `json:"port"`
IPFamily uint16 `json:"ip_family"`
}
var returnJson *bool
func main() {
var serverAddr = flag.String("s", "stun.qwq.pink:3478", "STUN server address")
var ip = flag.String("i", "", "bind ip")
var debugMode = flag.Bool("debug", false, "debug mode")
returnJson = flag.Bool("json", false, "return json")
flag.Parse()
client := stun.NewClient()
client.SetServerAddr(*serverAddr)
client.SetLocalIP(*ip)
if *debugMode {
client.SetVerbose(true)
client.SetVVerbose(true)
}
nat, host, err := client.Discover()
if err != nil {
printResult(NatResult{
Code: -1,
Msg: err.Error(),
})
}
if host != nil {
printResult(NatResult{
Code: 1,
Msg: "ok",
Nat: nat.String(),
IP: host.IP(),
Port: host.Port(),
IPFamily: host.Family(),
})
} else {
printResult(NatResult{
Code: 0,
Msg: "NO_HOST",
Nat: nat.String(),
})
}
}
func printResult(result NatResult) {
if *returnJson {
var rt, err = json.Marshal(result)
if err != nil {
fmt.Printf("%d|%s\n", -1, err.Error())
} else {
fmt.Println(string(rt))
}
} else {
if result.Code == 1 {
fmt.Printf("%d|%s|%s|%s|%d|%d\n", result.Code, result.Msg, strings.Replace(result.Nat, " ", "-", -1), result.IP, result.Port, result.IPFamily)
} else if result.Code == 0 {
fmt.Printf("%d|%s|%s\n", result.Code, result.Msg, strings.Replace(result.Nat, " ", "-", -1))
} else {
fmt.Printf("%d|%s\n", result.Code, result.Msg)
}
}
os.Exit(0)
}