forked from ccding/go-stun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
169 lines (151 loc) · 5.07 KB
/
Copy pathmain.go
File metadata and controls
169 lines (151 loc) · 5.07 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2016 Cong Ding
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/exclavenetwork/go-stun/stun"
)
func main() {
var serverAddr = flag.String("s", stun.DefaultServerAddr, "STUN server address")
var localPort = flag.Int("p", 0, "The port on which to bind requests, set to 0 to pick a random port")
var localIP = flag.String("i", "", "The ip on which to bind requests, set to empty will use default")
var behaviorTestMode = flag.Bool("b", false, "Enable NAT behavior test mode")
var legacyMode = flag.Bool("legacy", false, "Enable compatibility with RFC 3489-only STUN servers")
var transport = flag.String("t", "udp", "STUN transport (udp or tcp)")
var verboseLevel = flag.Int("v", 0, "Verbose level (0: none, 1: verbose, 2: double verbose, 3: triple verbose)")
flag.Parse()
// Validate verbose level
if *verboseLevel < 0 || *verboseLevel > 3 {
fmt.Fprintln(os.Stderr, "Error: Invalid verbose level. Use -v with values 0, 1, 2, or 3.")
os.Exit(1)
}
// Create a STUN client
client := stun.NewClient()
client.SetServerAddr(*serverAddr)
client.SetLocalPort(*localPort)
client.SetLocalIP(*localIP)
client.SetRFC3489Compatibility(*legacyMode)
client.SetVerbose(*verboseLevel >= 1)
client.SetVVerbose(*verboseLevel >= 2)
network := strings.ToLower(*transport)
// Run behavior test if specified
if *behaviorTestMode {
if network != "udp" {
fmt.Fprintln(os.Stderr, "Error: NAT behavior tests require UDP transport")
os.Exit(1)
}
err := runBehaviorTest(client)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
return
}
// Discover the mapped transport address and, for UDP, the NAT type.
nat, host, hasNATType, err := runDiscovery(client, network)
if host != nil {
fmt.Println("External IP Family:", host.Family())
fmt.Println("External IP: ", host.IP())
fmt.Println("External Port: ", host.Port())
}
if err != nil {
fmt.Fprintln(os.Stderr, "Error: ", err)
os.Exit(1)
}
if hasNATType {
fmt.Println("NAT Type: ", nat)
} else {
fmt.Println("Transport: ", "TCP")
}
}
type discoveryClient interface {
Discover() (stun.NATType, *stun.Host, error)
DiscoverTCP() (*stun.Host, error)
}
func runDiscovery(client discoveryClient, transport string) (stun.NATType, *stun.Host, bool, error) {
switch transport {
case "udp":
nat, host, err := client.Discover()
return nat, host, true, err
case "tcp":
host, err := client.DiscoverTCP()
return stun.NATUnknown, host, false, err
default:
return stun.NATError, nil, false, fmt.Errorf("unsupported STUN transport %q; use udp or tcp", transport)
}
}
func runBehaviorTest(c *stun.Client) error {
natBehavior, host, err := c.BehaviorTestWithAddress()
return writeBehaviorTestResult(os.Stdout, natBehavior, host, err)
}
func writeBehaviorTestResult(w io.Writer, natBehavior *stun.NATBehavior, host *stun.Host, err error) error {
if host != nil {
fmt.Println("External IP Family:", host.Family())
fmt.Println("External IP: ", host.IP())
fmt.Println("External Port: ", host.Port())
}
if err != nil {
if errors.Is(err, stun.ErrBehaviorDiscoveryUnsupported) {
if writeErr := writePartialBehaviorTestResult(w, natBehavior); writeErr != nil {
return writeErr
}
_, writeErr := fmt.Fprintln(w, err)
return writeErr
}
if writeErr := writePartialBehaviorTestResult(w, natBehavior); writeErr != nil {
return writeErr
}
return err
}
if natBehavior != nil {
if _, err := fmt.Fprintln(w, "Mapping Behavior: ", natBehavior.MappingType); err != nil {
return err
}
if _, err := fmt.Fprintln(w, "Filtering Behavior:", natBehavior.FilteringType); err != nil {
return err
}
if _, err := fmt.Fprintln(w, "Normal NAT Type: ", natBehavior.NormalType()); err != nil {
return err
}
}
return nil
}
func writePartialBehaviorTestResult(w io.Writer, natBehavior *stun.NATBehavior) error {
if natBehavior == nil {
return nil
}
if natBehavior.MappingType != stun.BehaviorTypeUnknown {
if _, err := fmt.Fprintln(w, " Mapping Behavior:", natBehavior.MappingType); err != nil {
return err
}
}
if natBehavior.FilteringType != stun.BehaviorTypeUnknown {
if _, err := fmt.Fprintln(w, "Filtering Behavior:", natBehavior.FilteringType); err != nil {
return err
}
}
if natBehavior.NoTranslation ||
(natBehavior.MappingType != stun.BehaviorTypeUnknown &&
natBehavior.FilteringType != stun.BehaviorTypeUnknown) {
if _, err := fmt.Fprintln(w, " Normal NAT Type:", natBehavior.NormalType()); err != nil {
return err
}
}
return nil
}