From 91c1ffc6ce340e2c32be9314d0d1555d0d349d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Mon, 2 Dec 2019 02:15:13 +0800 Subject: [PATCH] adjust external ip --- lib/common/util.go | 15 +++++++++++++-- server/proxy/socks5.go | 8 ++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/common/util.go b/lib/common/util.go index 0985d00..1de4455 100755 --- a/lib/common/util.go +++ b/lib/common/util.go @@ -109,8 +109,8 @@ func ChangeHostAndHeader(r *http.Request, host string, header string, addr strin } addr = strings.Split(addr, ":")[0] if prior, ok := r.Header["X-Forwarded-For"]; ok { - addr = strings.Join(prior, ", ") + ", " + addr - } + addr = strings.Join(prior, ", ") + ", " + addr + } r.Header.Set("X-Forwarded-For", addr) r.Header.Set("X-Real-IP", addr) } @@ -396,3 +396,14 @@ func GetExtFromPath(path string) string { } return string(re.Find([]byte(s[0]))) } + + +func GetExternalIp() string { + resp, err := http.Get("http://myexternalip.com/raw") + if err != nil { + return "" + } + defer resp.Body.Close() + content, _ := ioutil.ReadAll(resp.Body) + return string(content) +} diff --git a/server/proxy/socks5.go b/server/proxy/socks5.go index 9943747..ea78fd8 100755 --- a/server/proxy/socks5.go +++ b/server/proxy/socks5.go @@ -174,7 +174,12 @@ func (s *Sock5ModeServer) sendUdpReply(writeConn net.Conn, c net.Conn, rep uint8 } +var serveExternalIp string + func (s *Sock5ModeServer) handleUDP(c net.Conn) { + if serveExternalIp == "" { + serveExternalIp = common.GetExternalIp() + } defer c.Close() addrType := make([]byte, 1) c.Read(addrType) @@ -213,9 +218,8 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) { logs.Error("listen local reply udp port error") return } - // reply the local addr - s.sendUdpReply(c, reply, succeeded, "106.12.146.199") + s.sendUdpReply(c, reply, succeeded, serveExternalIp) defer reply.Close() // new a tunnel to client