func main() { nat, host, err := stun.NewClient().Discover() if err != nil { fmt.Println(err) } switch nat { case stun.NAT_ERROR: fmt.Println("Test failed") case stun.NAT_UNKNOWN: fmt.Println("Unexpected response from the STUN server") case stun.NAT_BLOCKED: fmt.Println("UDP is blocked") case stun.NAT_FULL: fmt.Println("Full cone NAT") case stun.NAT_SYMETRIC: fmt.Println("Symetric NAT") case stun.NAT_RESTRICTED: fmt.Println("Restricted NAT") case stun.NAT_PORT_RESTRICTED: fmt.Println("Port restricted NAT") case stun.NAT_NONE: fmt.Println("Not behind a NAT") case stun.NAT_SYMETRIC_UDP_FIREWALL: fmt.Println("Symetric UDP firewall") } if host != nil { fmt.Println(host.Family()) fmt.Println(host.IP()) fmt.Println(host.Port()) } }
func Stun() (int, error) { Shuffle(STUN_SERVERS) for _, server := range STUN_SERVERS { client := stunlib.NewClient() client.SetServerHost(server, STUN_PORT) nat, host, err := client.Discover() if err == nil { log.Infof("%s on UDP %s:%d", NATtoString(nat), host.IP(), host.Port()) return int(host.Port()), nil } } return 0, errStunFailed }
func stunResults() { stunOnce.Do(func() { // TODO(d4l3k): Fetch IP by talking to other nodes. stunc := stun.NewClient() natType, stunHost, err := stunc.Discover() if err != nil { log.Fatal(err) } nat = natType host = stunHost.IP() stunWG.Done() }) stunWG.Wait() }
func work() { targetDomain := config.Server.TargetDomain nameServer := config.Server.NameServer // counter status.SameCount = 0 status.CheckCount = 0 // connection slack-api api := slack.New(config.Slack.Token) channel, err := api.FindChannelByName(config.Slack.Channel) if err != nil { panic(err) } for { status.CheckCount = status.CheckCount + 1 // notify running? if status.CheckCount%60 == 0 { err = api.ChatPostMessage(channel.Id, "@takoyaki I'm running. ", nil) if err != nil { fmt.Println("slack api error", err) } } time.Sleep(60 * time.Second) // nonreq-resovle using dig(/hack/resolve.sh), get registry ipv4 address ripaddr, err := resolve(targetDomain, nameServer) if err != nil { fmt.Println("resolve error:", err) continue } if ripaddr == nil { fmt.Println("resolve error:", "resolve ip == nil") continue } fmt.Println("resovle ipaddr:", ripaddr.String()) // get global-ipv4-address using STUN _, host, err := stun.NewClient().Discover() if err != nil { fmt.Println("stun client error", err) continue } if host == nil { fmt.Println("stun client error", "host == nil") continue } gipaddr := net.ParseIP(host.IP()) fmt.Println("stun client ipaddr:", gipaddr.String()) // compare if gipaddr.String() == ripaddr.String() { fmt.Println("same") status.SameCount = status.SameCount + 1 continue } // notify update err = api.ChatPostMessage(channel.Id, "@takoyaki global-ipv4-address != resolve-ipv4-address. Now, I try to update. ", nil) if err != nil { fmt.Println("slack api error", err) } fmt.Println("not equal ipaddr") // update _, err = exec.Command("/bin/bash", "hack/update.sh", gipaddr.String()).Output() if err != nil { fmt.Println("update error", err) continue } // wait time.Sleep(5 * 10 * time.Second) } }
func (s *STUNService) Init(ln *LocalNode) error { s.client = stun.NewClient() s.client.SetServerAddr(STUNAddress) return nil }