diff --git a/README.md b/README.md index 1bb752e..3e6f43c 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ - P2P UDP 无超时永久阻塞,添加 30s ReadDeadline - P2P goroutine 退出控制改用 context,修复潜在泄漏 - 客户端 UDP ReadFrom 错误后未退出,修复无效数据继续处理 + - [#324](https://github.com/yisier/nps/issues/324) 隧道/域名解析/UDP 流量始终为 0 - 优化:ioutil.WriteFile → os.WriteFile、rand.Seed → rand.New 本地随机源 - 2026-05-23 v0.26.33 diff --git a/client/client.go b/client/client.go index 8b33738..c6bad53 100755 --- a/client/client.go +++ b/client/client.go @@ -307,7 +307,7 @@ func (s *TRPClient) handleChan(src net.Conn) { } } - conn.CopyWaitGroup(src, targetConn, lk.Crypt, lk.Compress, nil, nil, false, nil, nil) + conn.CopyWaitGroup(src, targetConn, lk.Crypt, lk.Compress, nil, nil, false, nil, nil, nil) } } diff --git a/client/local.go b/client/local.go index 291f5ae..5984eff 100644 --- a/client/local.go +++ b/client/local.go @@ -161,7 +161,7 @@ func handleSecret(localTcpConn net.Conn, config *config.CommonConfig, l *config. logs.Error("Local connection server failed ", err.Error()) return } - conn.CopyWaitGroup(remoteConn.Conn, localTcpConn, false, false, nil, nil, false, nil, nil) + conn.CopyWaitGroup(remoteConn.Conn, localTcpConn, false, false, nil, nil, false, nil, nil, nil) } func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) { @@ -178,7 +178,7 @@ func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *con udpConnStatus = false return } else { - conn.CopyWaitGroup(target, localTcpConn, false, config.Client.Cnf.Compress, nil, nil, false, nil, nil) + conn.CopyWaitGroup(target, localTcpConn, false, config.Client.Cnf.Compress, nil, nil, false, nil, nil, nil) } } diff --git a/conf/clients.json b/conf/clients.json index da29ba0..e69de29 100644 --- a/conf/clients.json +++ b/conf/clients.json @@ -1,2 +0,0 @@ -{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":4,"VerifyKey":"02cea9bedf","Addr":"127.0.0.1","Remark":"","Status":true,"IsConnect":true,"RateLimit":0,"Flow":{"ExportFlow":1405996,"InletFlow":1405996,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":6,"WebUserName":"","WebPassword":"","ConfigConnAllow":true,"MaxTunnelNum":0,"Version":"0.26.34","BlackIpList":[],"CreateTime":"2026-06-05 15:54:21","LastOnlineTime":"2026-06-05 15:55:40","IpWhite":false,"IpWhitePass":"","IpWhiteList":[]} -*#* \ No newline at end of file diff --git a/conf/tasks.json b/conf/tasks.json index c7e3ce7..e69de29 100644 --- a/conf/tasks.json +++ b/conf/tasks.json @@ -1,2 +0,0 @@ -{"Id":2,"Port":44263,"ServerIp":"0.0.0.0","Mode":"tcp","Status":true,"RunStatus":true,"Client":{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":4,"VerifyKey":"02cea9bedf","Addr":"127.0.0.1","Remark":"","Status":true,"IsConnect":true,"RateLimit":0,"Flow":{"ExportFlow":1405996,"InletFlow":1405996,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":6,"WebUserName":"","WebPassword":"","ConfigConnAllow":true,"MaxTunnelNum":0,"Version":"0.26.34","BlackIpList":[],"CreateTime":"2026-06-05 15:54:21","LastOnlineTime":"2026-06-05 15:55:40","IpWhite":false,"IpWhitePass":"","IpWhiteList":[]},"Ports":"","Flow":{"ExportFlow":0,"InletFlow":0,"FlowLimit":0},"Password":"","Remark":"","TargetAddr":"","NoStore":false,"LocalPath":"","StripPre":"","ProtoVersion":"","Target":{"TargetStr":"127.0.0.1:8081","TargetArr":["127.0.0.1:8081"],"LocalProxy":false},"MultiAccount":null,"HealthCheckTimeout":0,"HealthMaxFail":0,"HealthCheckInterval":0,"HealthNextTime":"0001-01-01T00:00:00Z","HealthMap":null,"HttpHealthUrl":"","HealthRemoveArr":null,"HealthCheckType":"","HealthCheckTarget":""} -*#* \ No newline at end of file diff --git a/lib/conn/conn.go b/lib/conn/conn.go index 8c8618f..8a35b8b 100755 --- a/lib/conn/conn.go +++ b/lib/conn/conn.go @@ -376,7 +376,7 @@ func SetUdpSession(sess *kcp.UDPSession) { //conn1 mux conn func CopyWaitGroup(conn1, conn2 net.Conn, crypt bool, snappy bool, rate *rate.Rate, - flow *file.Flow, isServer bool, rb []byte, task *file.Tunnel) { + flow *file.Flow, isServer bool, rb []byte, task *file.Tunnel, host *file.Host) { //var in, out int64 //var wg sync.WaitGroup connHandle := GetConn(conn1, crypt, snappy, rate, isServer) @@ -399,7 +399,7 @@ func CopyWaitGroup(conn1, conn2 net.Conn, crypt bool, snappy bool, rate *rate.Ra //} wg := new(sync.WaitGroup) wg.Add(1) - err := goroutine.CopyConnsPool.Invoke(goroutine.NewConns(connHandle, conn2, flow, wg, task)) + err := goroutine.CopyConnsPool.Invoke(goroutine.NewConns(connHandle, conn2, flow, wg, task, host)) wg.Wait() if err != nil { logs.Error(err) diff --git a/lib/goroutine/pool.go b/lib/goroutine/pool.go index 8c4c338..b6ff9ea 100644 --- a/lib/goroutine/pool.go +++ b/lib/goroutine/pool.go @@ -22,6 +22,7 @@ type connGroup struct { n *int64 flow *file.Flow task *file.Tunnel + host *file.Host remote string } @@ -34,7 +35,7 @@ type connGroup struct { // } //} -func newConnGroup(dst, src io.ReadWriteCloser, wg *sync.WaitGroup, n *int64, flow *file.Flow, task *file.Tunnel, remote string) connGroup { +func newConnGroup(dst, src io.ReadWriteCloser, wg *sync.WaitGroup, n *int64, flow *file.Flow, task *file.Tunnel, host *file.Host, remote string) connGroup { return connGroup{ src: src, dst: dst, @@ -42,11 +43,12 @@ func newConnGroup(dst, src io.ReadWriteCloser, wg *sync.WaitGroup, n *int64, flo n: n, flow: flow, task: task, + host: host, remote: remote, } } -func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel, remote string) (err error) { +func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel, host *file.Host, remote string) (err error) { buf := common.CopyBuff.Get() defer common.CopyBuff.Put(buf) for { @@ -129,7 +131,12 @@ func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel break } } - + if task != nil && task.Flow != nil && task.Flow != flow { + task.Flow.Add(int64(nw), int64(nw)) + } + if host != nil && host.Flow != nil && host.Flow != flow { + host.Flow.Add(int64(nw), int64(nw)) + } } if ew != nil { err = ew @@ -156,7 +163,7 @@ func copyConnGroup(group interface{}) { } var err error - err = CopyBuffer(cg.dst, cg.src, cg.flow, cg.task, cg.remote) + err = CopyBuffer(cg.dst, cg.src, cg.flow, cg.task, cg.host, cg.remote) if err != nil { cg.src.Close() cg.dst.Close() @@ -175,15 +182,17 @@ type Conns struct { flow *file.Flow wg *sync.WaitGroup task *file.Tunnel + host *file.Host } -func NewConns(c1 io.ReadWriteCloser, c2 net.Conn, flow *file.Flow, wg *sync.WaitGroup, task *file.Tunnel) Conns { +func NewConns(c1 io.ReadWriteCloser, c2 net.Conn, flow *file.Flow, wg *sync.WaitGroup, task *file.Tunnel, host *file.Host) Conns { return Conns{ conn1: c1, conn2: c2, flow: flow, wg: wg, task: task, + host: host, } } @@ -194,9 +203,9 @@ func copyConns(group interface{}) { wg.Add(2) var in, out int64 remoteAddr := conns.conn2.RemoteAddr().String() - _ = connCopyPool.Invoke(newConnGroup(conns.conn1, conns.conn2, wg, &in, conns.flow, conns.task, remoteAddr)) + _ = connCopyPool.Invoke(newConnGroup(conns.conn1, conns.conn2, wg, &in, conns.flow, conns.task, conns.host, remoteAddr)) // outside to mux : incoming - _ = connCopyPool.Invoke(newConnGroup(conns.conn2, conns.conn1, wg, &out, conns.flow, conns.task, remoteAddr)) + _ = connCopyPool.Invoke(newConnGroup(conns.conn2, conns.conn1, wg, &out, conns.flow, conns.task, conns.host, remoteAddr)) // mux to outside : outgoing wg.Wait() //if conns.flow != nil { diff --git a/server/proxy/base.go b/server/proxy/base.go index 1188154..323e584 100644 --- a/server/proxy/base.go +++ b/server/proxy/base.go @@ -96,7 +96,7 @@ func in(target string, str_array []string) bool { // create a new connection and start bytes copying func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string, - rb []byte, tp string, f func(), flow *file.Flow, localProxy bool, task *file.Tunnel) error { + rb []byte, tp string, f func(), flow *file.Flow, localProxy bool, task *file.Tunnel, host *file.Host) error { // 判断访问地址是否在全局黑名单内 if IsGlobalBlackIp(c.RemoteAddr().String()) { @@ -124,7 +124,7 @@ func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string, if f != nil { f() } - conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, client.Rate, flow, true, rb, task) + conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, client.Rate, flow, true, rb, task, host) } return nil } diff --git a/server/proxy/http.go b/server/proxy/http.go index aa386d0..78788e3 100644 --- a/server/proxy/http.go +++ b/server/proxy/http.go @@ -262,7 +262,7 @@ reset: } }() - err1 := goroutine.CopyBuffer(c, connClient, host.Client.Flow, nil, "") + err1 := goroutine.CopyBuffer(c, connClient, host.Client.Flow, nil, host, "") if err1 != nil { return } @@ -292,6 +292,7 @@ reset: } logs.Trace("%s request, method %s, host %s, url %s, remote address %s, return cache", r.URL.Scheme, r.Method, r.Host, r.URL.Path, c.RemoteAddr().String()) host.Client.Flow.Add(int64(n), int64(n)) + host.Flow.Add(int64(n), int64(n)) //if return cache and does not create a new conn with client and Connection is not set or close, close the connection. if strings.ToLower(r.Header.Get("Connection")) == "close" || strings.ToLower(r.Header.Get("Connection")) == "" { break @@ -313,6 +314,7 @@ reset: break } host.Client.Flow.Add(int64(lenConn.Len), int64(lenConn.Len)) + host.Flow.Add(int64(lenConn.Len), int64(lenConn.Len)) readReq: //read req from connection diff --git a/server/proxy/https.go b/server/proxy/https.go index a5aa67f..ea2a66e 100644 --- a/server/proxy/https.go +++ b/server/proxy/https.go @@ -259,7 +259,7 @@ func (https *HttpsServer) handleHttps2(c net.Conn, hostName string, rb []byte, r logs.Warn(err.Error()) } logs.Info("new https connection,clientId %d,host %s,remote address %s", host.Client.Id, r.Host, c.RemoteAddr().String()) - https.DealClient(conn.NewConn(c), host.Client, targetAddr, rb, common.CONN_TCP, nil, host.Client.Flow, host.Target.LocalProxy, nil) + https.DealClient(conn.NewConn(c), host.Client, targetAddr, rb, common.CONN_TCP, nil, host.Client.Flow, host.Target.LocalProxy, nil, host) } // close @@ -302,7 +302,7 @@ func (https *HttpsServer) handleHttps(c net.Conn) { logs.Warn(err.Error()) } logs.Trace("new https connection,clientId %d,host %s,remote address %s", host.Client.Id, r.Host, c.RemoteAddr().String()) - https.DealClient(conn.NewConn(c), host.Client, targetAddr, rb, common.CONN_TCP, nil, host.Client.Flow, host.Target.LocalProxy, nil) + https.DealClient(conn.NewConn(c), host.Client, targetAddr, rb, common.CONN_TCP, nil, host.Client.Flow, host.Target.LocalProxy, nil, host) } type HttpsListener struct { diff --git a/server/proxy/socks5.go b/server/proxy/socks5.go index 9fb3eb0..4dfb16d 100755 --- a/server/proxy/socks5.go +++ b/server/proxy/socks5.go @@ -172,7 +172,7 @@ func (s *Sock5ModeServer) doConnect(c net.Conn, command uint8) { } s.DealClient(conn.NewConn(c), s.task.Client, addr, nil, ltype, func() { s.sendReply(c, succeeded) - }, s.task.Flow, s.task.Target.LocalProxy, nil) + }, s.task.Flow, s.task.Target.LocalProxy, nil, nil) return } diff --git a/server/proxy/tcp.go b/server/proxy/tcp.go index 3efed8a..68b90e7 100755 --- a/server/proxy/tcp.go +++ b/server/proxy/tcp.go @@ -106,7 +106,7 @@ func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error { logs.Warn("tcp port %d ,client id %d,task id %d connect error %s", s.task.Port, s.task.Client.Id, s.task.Id, err.Error()) return err } - return s.DealClient(c, s.task.Client, targetAddr, nil, common.CONN_TCP, nil, s.task.Client.Flow, s.task.Target.LocalProxy, s.task) + return s.DealClient(c, s.task.Client, targetAddr, nil, common.CONN_TCP, nil, s.task.Client.Flow, s.task.Target.LocalProxy, s.task, nil) } // http proxy @@ -125,6 +125,6 @@ func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error { if err := s.auth(r, c, s.task.Client.Cnf.U, s.task.Client.Cnf.P); err != nil { return err } - return s.DealClient(c, s.task.Client, addr, rb, common.CONN_TCP, nil, s.task.Client.Flow, s.task.Target.LocalProxy, nil) + return s.DealClient(c, s.task.Client, addr, rb, common.CONN_TCP, nil, s.task.Client.Flow, s.task.Target.LocalProxy, nil, nil) } diff --git a/server/proxy/transport.go b/server/proxy/transport.go index 163c9ea..ff93b3b 100644 --- a/server/proxy/transport.go +++ b/server/proxy/transport.go @@ -16,7 +16,7 @@ func HandleTrans(c *conn.Conn, s *TunnelModeServer) error { if addr, err := getAddress(c.Conn); err != nil { return err } else { - return s.DealClient(c, s.task.Client, addr, nil, common.CONN_TCP, nil, s.task.Flow, s.task.Target.LocalProxy, nil) + return s.DealClient(c, s.task.Client, addr, nil, common.CONN_TCP, nil, s.task.Flow, s.task.Target.LocalProxy, nil, nil) } } diff --git a/server/proxy/udp.go b/server/proxy/udp.go index 5360b78..6066e59 100755 --- a/server/proxy/udp.go +++ b/server/proxy/udp.go @@ -144,6 +144,7 @@ func (s *UdpModeServer) dispatch(key string, sess *udpSession, data []byte, n in } sess.touch() s.task.Client.Flow.Add(int64(n), int64(n)) + s.task.Flow.Add(int64(n), int64(n)) } // runSession 由占位赢家执行:建立到 npc 的 stream、发送首包、运行下行读循环。 @@ -191,6 +192,7 @@ func (s *UdpModeServer) runSession(addr *net.UDPAddr, key string, sess *udpSessi } common.BufPoolUdp.Put(buf) s.task.Client.Flow.Add(int64(n), int64(n)) + s.task.Flow.Add(int64(n), int64(n)) // 下行读循环 rbuf := common.BufPoolUdp.Get().([]byte) @@ -211,6 +213,7 @@ func (s *UdpModeServer) runSession(addr *net.UDPAddr, key string, sess *udpSessi return } s.task.Client.Flow.Add(int64(rn), int64(rn)) + s.task.Flow.Add(int64(rn), int64(rn)) } } diff --git a/server/proxy/websocket.go b/server/proxy/websocket.go index 0454dc5..7cb8ef5 100644 --- a/server/proxy/websocket.go +++ b/server/proxy/websocket.go @@ -257,7 +257,7 @@ func Join(c1 io.ReadWriteCloser, c2 io.ReadWriteCloser, host *file.Host) (inCoun defer to.Close() defer from.Close() defer wait.Done() - goroutine.CopyBuffer(to, from, host.Client.Flow, nil, "") + goroutine.CopyBuffer(to, from, host.Client.Flow, nil, host, "") //*count, _ = io.Copy(to, from) } diff --git a/server/server.go b/server/server.go index 160f0c3..a01977f 100644 --- a/server/server.go +++ b/server/server.go @@ -71,7 +71,7 @@ func DealBridgeTask() { logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr()) if t := file.GetDb().GetTaskByMd5Password(s.Password); t != nil { if t.Status { - go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Client, t.Target.TargetStr, nil, common.CONN_TCP, nil, t.Flow, t.Target.LocalProxy, nil) + go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Client, t.Target.TargetStr, nil, common.CONN_TCP, nil, t.Flow, t.Target.LocalProxy, nil, nil) } else { s.Conn.Close() logs.Trace("This key %s cannot be processed,status is close", s.Password)