Add timeout to GetPublicIP func

This commit is contained in:
Alexey Khit
2022-11-02 12:47:26 +03:00
parent ceed146fb8
commit f72440712b
+10 -1
View File
@@ -63,11 +63,20 @@ func LookupIP(address string) (string, error) {
// GetPublicIP example from https://github.com/pion/stun
func GetPublicIP() (net.IP, error) {
c, err := stun.Dial("udp", "stun.l.google.com:19302")
conn, err := net.Dial("udp", "stun.l.google.com:19302")
if err != nil {
return nil, err
}
c, err := stun.NewClient(conn)
if err != nil {
return nil, err
}
if err = conn.SetDeadline(time.Now().Add(time.Second * 3)); err != nil {
return nil, err
}
var res stun.Event
message := stun.MustBuild(stun.TransactionID, stun.BindingRequest)