conn: add the ability to get outbound ip

This commit is contained in:
nadoo 2018-01-13 13:20:15 +08:00
parent 824c9b3788
commit 4670c9931e

14
conn.go
View File

@ -3,6 +3,7 @@ package main
import (
"bufio"
"io"
"log"
"net"
"time"
)
@ -79,3 +80,16 @@ func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout
}
}
}
// OutboundIP returns preferred outbound ip of this machine
func OutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}