修复 SOCKS5 代理 DSN 污染问题
This commit is contained in:
@@ -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
|
||||
- 新增:
|
||||
|
||||
+47
-12
@@ -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() {
|
||||
|
||||
@@ -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, "")
|
||||
|
||||
Reference in New Issue
Block a user