From 4670c9931e53a6bd1a22f22f4c577365b48dae1a Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Sat, 13 Jan 2018 13:20:15 +0800 Subject: [PATCH] conn: add the ability to get outbound ip --- conn.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/conn.go b/conn.go index d217c1f..6ce6c6d 100644 --- a/conn.go +++ b/conn.go @@ -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 +}