From 1b95dcde253571de4477f1ba66b36466d32cbb95 Mon Sep 17 00:00:00 2001 From: yisier <1340976576@qq.com> Date: Sat, 10 Oct 2026 14:44:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20SOCKS5=20=E4=BB=A3?= =?UTF-8?q?=E7=90=86=20DSN=20=E6=B1=A1=E6=9F=93=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/changelog/index.md | 1 + lib/common/util.go | 59 ++++++++++++++++++++++++++++++++--------- server/proxy/socks5.go | 7 ++++- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/docs/changelog/index.md b/docs/changelog/index.md index 236efde..c94d801 100644 --- a/docs/changelog/index.md +++ b/docs/changelog/index.md @@ -3,6 +3,7 @@ - 修复: - 同时开启 `web_base_url` 与 `open_captcha` 时,验证码图片路径未跟随 `web_base_url`,反向代理到子路径后无法登录 [#359](https://github.com/yisier/nps/pull/359) - Web 面板语言机制:浏览器语言按前缀归一化匹配(`zh*`→`zh-CN`、`en*`→`en-US`),匹配不到时回退默认 `en-US`;用户手动切换语言后长期持久化,不再因关闭浏览器而失效 [#356](https://github.com/yisier/nps/issues/356) + - SOCKS5(私密代理)UDP ASSOCIATE 回复的中继地址 `BND.ADDR` 优先使用客户端实际连接到的服务端地址,避免外网 IP 探测被运营商 HTTP 劫持返回错误 IP 导致 UDP 不通;外网探测改为 HTTPS 端点并加超时与校验 [#355](https://github.com/yisier/nps/issues/355) - 2026-08-24 v0.26.38 - 新增: diff --git a/lib/common/util.go b/lib/common/util.go index c44d271..1a4ec0a 100755 --- a/lib/common/util.go +++ b/lib/common/util.go @@ -20,6 +20,7 @@ import ( "strconv" "strings" "sync" + "time" "ehang.io/nps/lib/crypt" ) @@ -452,18 +453,41 @@ func GetExtFromPath(path string) string { var externalIp string +// externalIpServices are queried in order until one returns a valid IP. +// HTTPS plain-text echo services are preferred, because a plain HTTP endpoint +// can be hijacked or transparently cached by some ISPs (see #355). +var externalIpServices = []string{ + "https://ifconfig.me/ip", + "https://ipinfo.io/ip", + "http://v4.ipip.net", +} + +// GetExternalIp returns the public IP of this server by asking an external +// "what is my IP" service. The result is cached only after a valid answer, +// so a transient failure or a hijacked/empty response is not cached forever. func GetExternalIp() string { if externalIp != "" { return externalIp } - resp, err := http.Get("http://myexternalip.com/raw") - if err != nil { - return "" + client := &http.Client{Timeout: 3 * time.Second} + for _, url := range externalIpServices { + resp, err := client.Get(url) + if err != nil { + continue + } + content, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 64)) + resp.Body.Close() + if resp.StatusCode != http.StatusOK { + continue + } + ip := strings.TrimSpace(string(content)) + if net.ParseIP(ip) == nil { + continue + } + externalIp = ip + return externalIp } - defer resp.Body.Close() - content, _ := ioutil.ReadAll(resp.Body) - externalIp = string(content) - return externalIp + return "" } func GetIntranetIp() (error, string) { @@ -609,12 +633,23 @@ func IsPublicIP(IP net.IP) bool { return false } -func GetServerIpByClientIp(clientIp net.IP) string { - if IsPublicIP(clientIp) { - return GetExternalIp() +// GetServerIpByClientIp returns an address the SOCKS5 client can use to reach +// the server's UDP relay (the SOCKS5 BND.ADDR). +// +// - LAN client: the server's intranet IP. +// - WAN client: the local IP the client actually connected to, which is +// guaranteed to be reachable because the TCP control connection succeeded. +// Only when that address is not public (the server sits behind NAT) do we +// fall back to querying an external "what is my IP" service. +func GetServerIpByClientIp(clientIp, localIp net.IP) string { + if !IsPublicIP(clientIp) { + _, ip := GetIntranetIp() + return ip } - _, ip := GetIntranetIp() - return ip + if IsPublicIP(localIp) { + return localIp.String() + } + return GetExternalIp() } func PrintVersion() { diff --git a/server/proxy/socks5.go b/server/proxy/socks5.go index d59986e..cc2debe 100755 --- a/server/proxy/socks5.go +++ b/server/proxy/socks5.go @@ -267,7 +267,12 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) { return } // reply the local addr - s.sendUdpReply(c, reply, succeeded, common.GetServerIpByClientIp(c.RemoteAddr().(*net.TCPAddr).IP)) + // Use the address the client actually connected to as BND.ADDR whenever it + // is public; only fall back to external IP detection when the server is + // behind NAT (see #355). + clientIp := c.RemoteAddr().(*net.TCPAddr).IP + localIp := c.LocalAddr().(*net.TCPAddr).IP + s.sendUdpReply(c, reply, succeeded, common.GetServerIpByClientIp(clientIp, localIp)) defer reply.Close() // new a tunnel to client link := conn.NewLink("udp5", "", s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.RemoteAddr().String(), false, "")