新增客户端内网地址上报功能

This commit is contained in:
yisier
2026-07-12 18:38:47 -06:00
parent 3c848e5216
commit b45dfe048e
9 changed files with 155 additions and 4 deletions
+33
View File
@@ -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:
+8
View File
@@ -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 {
+2
View File
@@ -1,5 +1,7 @@
# 更新日志
- 2026-07-12 v0.26.36
- 新增:
- 新增客户端内网地址上报功能,客户端列表展示 npc 内网地址 [#333](https://github.com/yisier/nps/issues/333)
- 优化:
- 不再依赖外部 `web` 目录的静态文件
- 修复:
+3
View File
@@ -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"
+93
View File
@@ -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
+1 -1
View File
@@ -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++
+3 -1
View File
@@ -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,
+6 -2
View File
@@ -122,8 +122,12 @@
</lang>
<lang id="word-address">
<zh-CN>客户端地址</zh-CN>
<en-US>Client address</en-US>
<zh-CN>客户端出口地址</zh-CN>
<en-US>Client egress address</en-US>
</lang>
<lang id="word-localaddr">
<zh-CN>内网地址</zh-CN>
<en-US>LAN address</en-US>
</lang>
<lang id="word-add">
<zh-CN>新增</zh-CN>
+6
View File
@@ -126,6 +126,12 @@
halign: 'center',
visible: true//false表示不显示
},
{
field: 'LocalAddr',//域值
title: '<span langtag="word-localaddr"></span>',//标题
halign: 'center',
visible: true//false表示不显示
},
{
field: 'InletFlow',//域值
title: '<span langtag="word-inletflow"></span>',//标题