From b45dfe048ee55567cef515cabe6533f6f3b08630 Mon Sep 17 00:00:00 2001
From: yisier <1340976576@qq.com>
Date: Sun, 12 Jul 2026 18:38:47 -0600
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AE=A2=E6=88=B7=E7=AB=AF?=
=?UTF-8?q?=E5=86=85=E7=BD=91=E5=9C=B0=E5=9D=80=E4=B8=8A=E6=8A=A5=E5=8A=9F?=
=?UTF-8?q?=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
bridge/bridge.go | 33 +++++++++++++
client/client.go | 8 +++
docs/changelog/index.md | 2 +
lib/common/const.go | 3 ++
lib/common/util.go | 93 +++++++++++++++++++++++++++++++++++
lib/file/db.go | 2 +-
lib/file/obj.go | 4 +-
web/static/page/languages.xml | 8 ++-
web/views/client/list.html | 6 +++
9 files changed, 155 insertions(+), 4 deletions(-)
diff --git a/bridge/bridge.go b/bridge/bridge.go
index fbb8ea2..30a8c50 100755
--- a/bridge/bridge.go
+++ b/bridge/bridge.go
@@ -117,6 +117,37 @@ func (s *Bridge) StartTunnel() error {
return nil
}
+// requestClientLocalAddr asks the client for private/LAN IPs on the main signal conn.
+// New clients reply with WriteLenContent; old clients ignore the flag and we time out.
+func (s *Bridge) requestClientLocalAddr(id int, c *conn.Conn) {
+ if c == nil || c.Conn == nil {
+ return
+ }
+ if _, err := c.Write([]byte(common.REPORT_LOCAL_IP)); err != nil {
+ return
+ }
+ _ = c.Conn.SetReadDeadline(time.Now().Add(2 * time.Second))
+ defer func() {
+ // clear deadline so health / later reads are not affected
+ _ = c.Conn.SetReadDeadline(time.Time{})
+ }()
+ b, err := c.GetShortLenContent()
+ if err != nil {
+ logs.Trace("clientId %d did not report local addr (old client or timeout): %v", id, err)
+ return
+ }
+ localAddr := strings.TrimSpace(string(b))
+ if localAddr == "" {
+ return
+ }
+ if client, err := file.GetDb().GetClient(id); err == nil {
+ client.Lock()
+ client.LocalAddr = localAddr
+ client.Unlock()
+ logs.Info("clientId %d local addr: %s", id, localAddr)
+ }
+}
+
// get health information form client
func (s *Bridge) GetHealthFromClient(id int, c *conn.Conn) {
for {
@@ -285,6 +316,8 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int, vs string) {
oldSignal.WriteClose()
}
}
+ // Request private/LAN IPs from client. Old clients ignore the flag; short timeout keeps compatibility.
+ s.requestClientLocalAddr(id, c)
go s.GetHealthFromClient(id, c)
logs.Info("clientId %d connection succeeded, address:%s ", id, c.Conn.RemoteAddr())
case common.WORK_CHAN:
diff --git a/client/client.go b/client/client.go
index 4f14d4f..13ac8ec 100755
--- a/client/client.go
+++ b/client/client.go
@@ -141,6 +141,14 @@ func (s *TRPClient) handleMain() {
break
}
switch flags {
+ case common.REPORT_LOCAL_IP:
+ // server requests private/LAN IPs (new nps); ignore failures so main loop continues
+ localIPs := common.GetLocalIPs(s.signal.Conn)
+ if err := s.signal.WriteLenContent([]byte(localIPs)); err != nil {
+ s.logWarn("report local ip failed: %s", err.Error())
+ } else {
+ s.logInfo("reported local addr: %s", localIPs)
+ }
case common.NEW_UDP_CONN:
//read server udp addr and password
if lAddr, err := s.signal.GetShortLenContent(); err != nil {
diff --git a/docs/changelog/index.md b/docs/changelog/index.md
index 642c8f9..72de054 100644
--- a/docs/changelog/index.md
+++ b/docs/changelog/index.md
@@ -1,5 +1,7 @@
# 更新日志
- 2026-07-12 v0.26.36
+ - 新增:
+ - 新增客户端内网地址上报功能,客户端列表展示 npc 内网地址 [#333](https://github.com/yisier/nps/issues/333)
- 优化:
- 不再依赖外部 `web` 目录的静态文件
- 修复:
diff --git a/lib/common/const.go b/lib/common/const.go
index b9a7617..f545d34 100644
--- a/lib/common/const.go
+++ b/lib/common/const.go
@@ -21,6 +21,9 @@ const (
RES_MSG = "msg0"
RES_CLOSE = "clse"
NEW_UDP_CONN = "udpc" //p2p udp conn
+ // REPORT_LOCAL_IP: server requests client local/private IPs on WORK_MAIN.
+ // New clients reply with WriteLenContent; old clients ignore the flag (server times out).
+ REPORT_LOCAL_IP = "rlip"
NEW_TASK = "task"
NEW_CONF = "conf"
NEW_HOST = "host"
diff --git a/lib/common/util.go b/lib/common/util.go
index e6a75c9..27a4fb7 100755
--- a/lib/common/util.go
+++ b/lib/common/util.go
@@ -482,6 +482,99 @@ func GetIntranetIp() (error, string) {
return errors.New("get intranet ip error"), ""
}
+// isUsefulPrivateIPv4 reports whether ip is a usable LAN IPv4 for display:
+// RFC1918 private only — excludes loopback, link-local (169.254.0.0/16), public, etc.
+func isUsefulPrivateIPv4(ip net.IP) bool {
+ if ip == nil {
+ return false
+ }
+ v4 := ip.To4()
+ if v4 == nil {
+ return false
+ }
+ if v4.IsLoopback() || v4.IsLinkLocalUnicast() || v4.IsLinkLocalMulticast() || v4.IsUnspecified() || v4.IsMulticast() {
+ return false
+ }
+ // RFC1918 only (not 169.254, not public)
+ return v4.IsPrivate()
+}
+
+func ipFromAddr(addr net.Addr) net.IP {
+ if addr == nil {
+ return nil
+ }
+ switch a := addr.(type) {
+ case *net.TCPAddr:
+ return a.IP
+ case *net.UDPAddr:
+ return a.IP
+ default:
+ return net.ParseIP(GetIpByAddr(addr.String()))
+ }
+}
+
+// GetLocalIPs returns private (RFC1918) IPv4 addresses useful for identifying the npc host.
+// Prefers the local IP of conn (path toward nps). Falls back to interface scan.
+// Link-local 169.254.0.0/16, loopback and public addresses are excluded.
+func GetLocalIPs(conn net.Conn) string {
+ seen := make(map[string]struct{})
+ ips := make([]string, 0, 4)
+
+ add := func(ip net.IP) {
+ if !isUsefulPrivateIPv4(ip) {
+ return
+ }
+ s := ip.To4().String()
+ if _, ok := seen[s]; ok {
+ return
+ }
+ seen[s] = struct{}{}
+ ips = append(ips, s)
+ }
+
+ // 1) Local endpoint of the bridge connection — usually the real LAN IP used to reach nps.
+ if conn != nil {
+ add(ipFromAddr(conn.LocalAddr()))
+ // One correct path IP is enough for most hosts (avoids docker/vm junk alongside).
+ if len(ips) > 0 {
+ return ips[0]
+ }
+ }
+
+ // 2) Outbound interface toward the internet (same idea as GetLocalUdpAddr).
+ if tmp, err := net.Dial("udp", "114.114.114.114:53"); err == nil {
+ add(ipFromAddr(tmp.LocalAddr()))
+ _ = tmp.Close()
+ if len(ips) > 0 {
+ return ips[0]
+ }
+ }
+
+ // 3) Fallback: all up interfaces with private IPv4.
+ ifaces, err := net.Interfaces()
+ if err == nil {
+ for _, iface := range ifaces {
+ if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
+ continue
+ }
+ addrs, err := iface.Addrs()
+ if err != nil {
+ continue
+ }
+ for _, address := range addrs {
+ if ipnet, ok := address.(*net.IPNet); ok {
+ add(ipnet.IP)
+ }
+ }
+ }
+ }
+
+ if len(ips) > 1 {
+ sort.Strings(ips)
+ }
+ return strings.Join(ips, ",")
+}
+
func IsPublicIP(IP net.IP) bool {
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return false
diff --git a/lib/file/db.go b/lib/file/db.go
index 6ff76fa..40097bf 100644
--- a/lib/file/db.go
+++ b/lib/file/db.go
@@ -61,7 +61,7 @@ func (s *DbUtils) GetClientList(start, length int, search, sort, order string, c
if clientId != 0 && clientId != v.Id {
continue
}
- if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.VerifyKey, search) || strings.Contains(v.Remark, search)) {
+ if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.VerifyKey, search) || strings.Contains(v.Remark, search) || strings.Contains(v.Addr, search) || strings.Contains(v.LocalAddr, search)) {
continue
}
cnt++
diff --git a/lib/file/obj.go b/lib/file/obj.go
index f73d489..eb1bd65 100644
--- a/lib/file/obj.go
+++ b/lib/file/obj.go
@@ -35,7 +35,8 @@ type Client struct {
Cnf *Config
Id int //id
VerifyKey string //verify key
- Addr string //the ip of client
+ Addr string // client egress address as seen by server (RemoteAddr)
+ LocalAddr string // client private/LAN addresses reported by npc
Remark string //remark
Status bool //is allow connect
IsConnect bool //is the client connect
@@ -67,6 +68,7 @@ func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
Id: 0,
VerifyKey: vKey,
Addr: "",
+ LocalAddr: "",
Remark: "",
Status: true,
IsConnect: false,
diff --git a/web/static/page/languages.xml b/web/static/page/languages.xml
index 0e55437..ba86871 100644
--- a/web/static/page/languages.xml
+++ b/web/static/page/languages.xml
@@ -122,8 +122,12 @@
- 客户端地址
- Client address
+ 客户端出口地址
+ Client egress address
+
+
+ 内网地址
+ LAN address
新增
diff --git a/web/views/client/list.html b/web/views/client/list.html
index 7e31653..1e1801d 100644
--- a/web/views/client/list.html
+++ b/web/views/client/list.html
@@ -126,6 +126,12 @@
halign: 'center',
visible: true//false表示不显示
},
+ {
+ field: 'LocalAddr',//域值
+ title: '',//标题
+ halign: 'center',
+ visible: true//false表示不显示
+ },
{
field: 'InletFlow',//域值
title: '',//标题