func main() { stun.SetServerHost("v.mozilla.com", 3478) nat, host, err := stun.Discover() if err != nil { panic(err) } fmt.Println(host.Ip(), nat) }
func findNATviaStun(orig_ctx Context) (ctx Context, err error) { ctx = orig_ctx attempt := 0 for stunSrv, stunPort := range stunHosts { stun.SetServerHost(stunSrv, stunPort) nat, host, err := stun.Discover() if err != nil { ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("STUN discovery failed against %s:%d with error '%v'", stunSrv, stunPort, err)}.Debug() } switch nat { case stun.NAT_ERROR: ctx.Agent.Env.NAT.Result = "Test failed" case stun.NAT_UNKNOWN: ctx.Agent.Env.NAT.Result = "Unexpected response from the STUN server" case stun.NAT_BLOCKED: ctx.Agent.Env.NAT.Result = "UDP is blocked" case stun.NAT_FULL: ctx.Agent.Env.NAT.Result = "Full cone NAT" case stun.NAT_SYMETRIC: ctx.Agent.Env.NAT.Result = "Symmetric NAT" case stun.NAT_RESTRICTED: ctx.Agent.Env.NAT.Result = "Restricted NAT" case stun.NAT_PORT_RESTRICTED: ctx.Agent.Env.NAT.Result = "Port restricted NAT" case stun.NAT_NONE: ctx.Agent.Env.NAT.Result = "Not behind a NAT" case stun.NAT_SYMETRIC_UDP_FIREWALL: ctx.Agent.Env.NAT.Result = "Symmetric UDP firewall" default: ctx.Agent.Env.NAT.Result = "Unknown" } if host != nil { ctx.Agent.Env.NAT.IP = host.Ip() ctx.Agent.Env.NAT.StunServer = fmt.Sprintf("%s:%d", stunSrv, stunPort) break } attempt++ if attempt == 3 { ctx.Agent.Env.NAT.Result = "Attempted 3 lookups without results" break } } return }