v0.0.2 - 进一步解耦

This commit is contained in:
spiritlhl
2024-06-25 02:55:02 +00:00
parent 42023a68b2
commit 631eff3feb
3 changed files with 133 additions and 58 deletions

View File

@@ -17,6 +17,7 @@ func main() {
flag.IntVar(&model.Verbose, "verbose", 0, "the verbosity level")
flag.IntVar(&model.Timeout, "timeout", 3, "the number of seconds to wait for STUN server's response")
flag.StringVar(&model.AddrStr, "server", "stun.voipgate.com:3478", "STUN server address")
flag.BoolVar(&model.EnableLoger, "e", true, "Enable logging")
flag.Parse()
go func() {
http.Get("https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Foneclickvirt%2Fgostun&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false")
@@ -26,10 +27,11 @@ func main() {
fmt.Println(model.GoStunVersion)
return
}
if model.EnableLoger {
var logLevel logging.LogLevel
switch model.Verbose {
case 0:
logLevel = logging.LogLevelWarn // default // my changes
logLevel = logging.LogLevelWarn // default
case 1:
logLevel = logging.LogLevelInfo
case 2:
@@ -38,13 +40,18 @@ func main() {
logLevel = logging.LogLevelTrace
}
model.Log = logging.NewDefaultLeveledLoggerForScope("", logLevel, os.Stdout)
}
if model.AddrStr != "stun.voipgate.com:3478" {
if err := stuncheck.MappingTests(model.AddrStr); err != nil {
model.NatMappingBehavior = "inconclusive" // my changes
if model.EnableLoger {
model.NatMappingBehavior = "inconclusive"
}
model.Log.Warn("NAT mapping behavior: inconclusive")
}
if err := stuncheck.FilteringTests(model.AddrStr); err != nil {
model.NatFilteringBehavior = "inconclusive" // my changes
if model.EnableLoger {
model.NatFilteringBehavior = "inconclusive"
}
model.Log.Warn("NAT filtering behavior: inconclusive")
}
} else {
@@ -58,13 +65,17 @@ func main() {
err1 := stuncheck.MappingTests(addrStr)
if err1 != nil {
model.NatMappingBehavior = "inconclusive"
if model.EnableLoger {
model.Log.Warn("NAT mapping behavior: inconclusive")
}
checkStatus = false
}
err2 := stuncheck.FilteringTests(addrStr)
if err2 != nil {
model.NatFilteringBehavior = "inconclusive"
if model.EnableLoger {
model.Log.Warn("NAT filtering behavior: inconclusive")
}
checkStatus = false
}
if model.NatMappingBehavior == "inconclusive" || model.NatFilteringBehavior == "inconclusive" {
@@ -78,5 +89,5 @@ func main() {
}
}
res := stuncheck.CheckType()
fmt.Printf("NAT Type: %s", res)
fmt.Printf("NAT Type: %s\n", res)
}

View File

@@ -11,4 +11,5 @@ var (
Log logging.LeveledLogger
NatMappingBehavior string
NatFilteringBehavior string
EnableLoger = true
)

View File

@@ -37,12 +37,16 @@ var (
func MappingTests(addrStr string) error {
mapTestConn, err := connect(addrStr)
if err != nil {
if model.EnableLoger {
model.Log.Warnf("Error creating STUN connection: %s", err)
}
return err
}
// Test I: Regular binding request
if model.EnableLoger {
model.Log.Info("Mapping Test I: Regular binding request")
}
request := stun.MustBuild(stun.TransactionID, stun.BindingRequest)
resp, err := mapTestConn.roundTrip(request, mapTestConn.RemoteAddr)
@@ -53,26 +57,36 @@ func MappingTests(addrStr string) error {
// Parse response message for XOR-MAPPED-ADDRESS and make sure OTHER-ADDRESS valid
resps1 := parse(resp)
if resps1.xorAddr == nil || resps1.otherAddr == nil {
if model.EnableLoger {
model.Log.Info("Error: NAT discovery feature not supported by this server")
}
return errNoOtherAddress
}
addr, err := net.ResolveUDPAddr("udp4", resps1.otherAddr.String())
if err != nil {
if model.EnableLoger {
model.Log.Infof("Failed resolving OTHER-ADDRESS: %v", resps1.otherAddr)
}
return err
}
mapTestConn.OtherAddr = addr
if model.EnableLoger {
model.Log.Infof("Received XOR-MAPPED-ADDRESS: %v", resps1.xorAddr)
}
// Assert mapping behavior
if resps1.xorAddr.String() == mapTestConn.LocalAddr.String() {
model.NatMappingBehavior = "endpoint independent (no NAT)" // my changes
if model.EnableLoger {
model.Log.Warn("=> NAT mapping behavior: endpoint independent (no NAT)")
}
return nil
}
// Test II: Send binding request to the other address but primary port
if model.EnableLoger {
model.Log.Info("Mapping Test II: Send binding request to the other address but primary port")
}
oaddr := *mapTestConn.OtherAddr
oaddr.Port = mapTestConn.RemoteAddr.Port
resp, err = mapTestConn.roundTrip(request, &oaddr)
@@ -82,15 +96,21 @@ func MappingTests(addrStr string) error {
// Assert mapping behavior
resps2 := parse(resp)
if model.EnableLoger {
model.Log.Infof("Received XOR-MAPPED-ADDRESS: %v", resps2.xorAddr)
}
if resps2.xorAddr.String() == resps1.xorAddr.String() {
model.NatMappingBehavior = "endpoint independent" // my changes
if model.EnableLoger {
model.Log.Warn("=> NAT mapping behavior: endpoint independent")
}
return nil
}
// Test III: Send binding request to the other address and port
if model.EnableLoger {
model.Log.Info("Mapping Test III: Send binding request to the other address and port")
}
resp, err = mapTestConn.roundTrip(request, mapTestConn.OtherAddr)
if err != nil {
return err
@@ -98,14 +118,20 @@ func MappingTests(addrStr string) error {
// Assert mapping behavior
resps3 := parse(resp)
if model.EnableLoger {
model.Log.Infof("Received XOR-MAPPED-ADDRESS: %v", resps3.xorAddr)
}
if resps3.xorAddr.String() == resps2.xorAddr.String() {
model.NatMappingBehavior = "address dependent" // my changes
if model.EnableLoger {
model.Log.Warn("=> NAT mapping behavior: address dependent")
}
} else {
model.NatMappingBehavior = "address and port dependent" // my changes
if model.EnableLoger {
model.Log.Warn("=> NAT mapping behavior: address and port dependent")
}
}
return mapTestConn.Close()
}
@@ -113,12 +139,16 @@ func MappingTests(addrStr string) error {
func FilteringTests(addrStr string) error {
mapTestConn, err := connect(addrStr)
if err != nil {
if model.EnableLoger {
model.Log.Warnf("Error creating STUN connection: %s", err)
}
return err
}
// Test I: Regular binding request
if model.EnableLoger {
model.Log.Info("Filtering Test I: Regular binding request")
}
request := stun.MustBuild(stun.TransactionID, stun.BindingRequest)
resp, err := mapTestConn.roundTrip(request, mapTestConn.RemoteAddr)
@@ -127,18 +157,24 @@ func FilteringTests(addrStr string) error {
}
resps := parse(resp)
if resps.xorAddr == nil || resps.otherAddr == nil {
if model.EnableLoger {
model.Log.Warn("Error: NAT discovery feature not supported by this server")
}
return errNoOtherAddress
}
addr, err := net.ResolveUDPAddr("udp4", resps.otherAddr.String())
if err != nil {
if model.EnableLoger {
model.Log.Infof("Failed resolving OTHER-ADDRESS: %v", resps.otherAddr)
}
return err
}
mapTestConn.OtherAddr = addr
// Test II: Request to change both IP and port
if model.EnableLoger {
model.Log.Info("Filtering Test II: Request to change both IP and port")
}
request = stun.MustBuild(stun.TransactionID, stun.BindingRequest)
request.Add(stun.AttrChangeRequest, []byte{0x00, 0x00, 0x00, 0x06})
@@ -146,14 +182,18 @@ func FilteringTests(addrStr string) error {
if err == nil {
parse(resp) // just to print out the resp
model.NatFilteringBehavior = "endpoint independent" // my changes
if model.EnableLoger {
model.Log.Warn("=> NAT filtering behavior: endpoint independent")
}
return nil
} else if !errors.Is(err, errTimedOut) {
return err // something else went wrong
}
// Test III: Request to change port only
if model.EnableLoger {
model.Log.Info("Filtering Test III: Request to change port only")
}
request = stun.MustBuild(stun.TransactionID, stun.BindingRequest)
request.Add(stun.AttrChangeRequest, []byte{0x00, 0x00, 0x00, 0x02})
@@ -161,11 +201,15 @@ func FilteringTests(addrStr string) error {
if err == nil {
parse(resp) // just to print out the resp
model.NatFilteringBehavior = "address dependent" // my changes
if model.EnableLoger {
model.Log.Warn("=> NAT filtering behavior: address dependent")
}
} else if errors.Is(err, errTimedOut) {
model.NatFilteringBehavior = "address and port dependent" // my changes
if model.EnableLoger {
model.Log.Warn("=> NAT filtering behavior: address and port dependent")
}
}
return mapTestConn.Close()
}
@@ -199,12 +243,14 @@ func parse(msg *stun.Message) (ret struct {
if ret.software.GetFrom(msg) != nil {
ret.software = nil
}
if model.EnableLoger {
model.Log.Debugf("%v", msg)
model.Log.Debugf("\tMAPPED-ADDRESS: %v", ret.mappedAddr)
model.Log.Debugf("\tXOR-MAPPED-ADDRESS: %v", ret.xorAddr)
model.Log.Debugf("\tRESPONSE-ORIGIN: %v", ret.respOrigin)
model.Log.Debugf("\tOTHER-ADDRESS: %v", ret.otherAddr)
model.Log.Debugf("\tSOFTWARE: %v", ret.software)
}
for _, attr := range msg.Attributes {
switch attr.Type {
case
@@ -215,18 +261,24 @@ func parse(msg *stun.Message) (ret struct {
stun.AttrSoftware:
break //nolint:staticcheck
default:
if model.EnableLoger {
model.Log.Debugf("\t%v (l=%v)", attr, attr.Length)
}
}
}
return ret
}
// Given an address string, returns a StunServerConn
func connect(addrStr string) (*stunServerConn, error) {
if model.EnableLoger {
model.Log.Infof("Connecting to STUN server: %s", addrStr)
}
addr, err := net.ResolveUDPAddr("udp4", addrStr)
if err != nil {
if model.EnableLoger {
model.Log.Warnf("Error resolving address: %s", err)
}
return nil, err
}
@@ -234,8 +286,10 @@ func connect(addrStr string) (*stunServerConn, error) {
if err != nil {
return nil, err
}
if model.EnableLoger {
model.Log.Infof("Local address: %s", c.LocalAddr())
model.Log.Infof("Remote address: %s", addr.String())
}
mChan := listen(c)
@@ -250,14 +304,18 @@ func connect(addrStr string) (*stunServerConn, error) {
// Send request and wait for response or timeout
func (c *stunServerConn) roundTrip(msg *stun.Message, addr net.Addr) (*stun.Message, error) {
_ = msg.NewTransactionID()
if model.EnableLoger {
model.Log.Infof("Sending to %v: (%v bytes)", addr, msg.Length+messageHeaderSize)
model.Log.Debugf("%v", msg)
for _, attr := range msg.Attributes {
model.Log.Debugf("\t%v (l=%v)", attr, attr.Length)
}
}
_, err := c.conn.WriteTo(msg.Raw, addr)
if err != nil {
if model.EnableLoger {
model.Log.Warnf("Error sending request to %v", addr)
}
return nil, err
}
@@ -269,7 +327,9 @@ func (c *stunServerConn) roundTrip(msg *stun.Message, addr net.Addr) (*stun.Mess
}
return m, nil
case <-time.After(time.Duration(model.Timeout) * time.Second):
if model.EnableLoger {
model.Log.Infof("Timed out waiting for response from server %v", addr)
}
return nil, errTimedOut
}
}
@@ -286,18 +346,21 @@ func listen(conn *net.UDPConn) (messages chan *stun.Message) {
close(messages)
return
}
if model.EnableLoger {
model.Log.Infof("Response from %v: (%v bytes)", addr, n)
}
buf = buf[:n]
m := new(stun.Message)
m.Raw = buf
err = m.Decode()
if err != nil {
if model.EnableLoger {
model.Log.Infof("Error decoding message: %v", err)
}
close(messages)
return
}
messages <- m
}
}()