nat-type 是 RFC 3489 的go实现,从 NatTypeDetector 移植而来。 代码写得烂,思想跟不上,就这样了
package main
import (
"fmt"
"github.com/ppma/nat-type"
)
const (
STUN_SERVER = "stun.miwifi.com"
STUN_PORT = 3478
)
func main() {
localAddr := fmt.Sprintf("%s:%d", "192.168.101.3", STUN_PORT)
stunAddr := fmt.Sprintf("%s:%d", STUN_SERVER, STUN_PORT)
result, err := stun.Query(stunAddr, localAddr)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Nat type: ", result.GetNatType())
fmt.Println("Public IP: ", result.GetIpAddr())
}