From 2b04bf8b5a2eb0478700f01d88936c6931368f88 Mon Sep 17 00:00:00 2001 From: yisier <1340976576@qq.com> Date: Sat, 23 May 2026 16:34:08 -0600 Subject: [PATCH] =?UTF-8?q?=20=20-=20[#184](https://github.com/yisier/nps/?= =?UTF-8?q?issues/184)=20=E9=99=90=E9=80=9F=E5=AF=BC=E8=87=B4=E9=9A=A7?= =?UTF-8?q?=E9=81=93=E4=B8=AD=E6=96=AD=EF=BC=9AUpdateClient=20=E5=9C=A8=20?= =?UTF-8?q?RateLimit=3D0=20=E6=97=B6=E5=88=9B=E5=BB=BA=E9=9B=B6=E9=80=9F?= =?UTF-8?q?=E7=8E=87=E4=BB=A4=E7=89=8C=E6=A1=B6=E5=AF=BC=E8=87=B4=E6=B0=B8?= =?UTF-8?q?=E4=B9=85=E9=98=BB=E5=A1=9E=20=20=20=20=20-=20[#159](https://gi?= =?UTF-8?q?thub.com/yisier/nps/issues/159)=20=E5=85=B3=E9=97=AD=E9=9A=A7?= =?UTF-8?q?=E9=81=93=E7=AB=AF=E5=8F=A3=E4=BB=8D=E5=8F=AF=E7=94=A8=EF=BC=9A?= =?UTF-8?q?StopServer=20=E9=94=99=E8=AF=AF=E6=97=B6=E6=8F=90=E5=89=8D?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=9C=AA=E6=9B=B4=E6=96=B0=E7=8A=B6=E6=80=81?= =?UTF-8?q?=20+=20=E9=87=8D=E5=A4=8D=20OpenTask=20case=20=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E9=9A=A7=E9=81=93=E8=A2=AB=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E5=90=AF=20=20=20=20=20-=20[#170](https://github.com/yisier/np?= =?UTF-8?q?s/issues/170)=20TCP=20=E8=B4=9F=E8=BD=BD=E5=9D=87=E8=A1=A1?= =?UTF-8?q?=E5=8F=AA=E6=9C=89=E4=B8=80=E4=B8=AA=E5=90=8E=E7=AB=AF=E5=8F=AF?= =?UTF-8?q?=E8=BE=BE=EF=BC=9AGetRandomTarget=20=E6=9C=AA=E5=A4=84=E7=90=86?= =?UTF-8?q?=20\r=20=E6=8D=A2=E8=A1=8C=E7=AC=A6=E5=92=8C=E5=B0=BE=E9=83=A8?= =?UTF-8?q?=E7=A9=BA=E8=A1=8C=20=20=20=20=20-=20[#319](https://github.com/?= =?UTF-8?q?yisier/nps/issues/319)=20=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E4=B8=8D=E6=98=BE=E7=A4=BA=E5=AE=9E=E6=97=B6=E7=BD=91?= =?UTF-8?q?=E9=80=9F=EF=BC=9ANowRate=20=E8=AE=A1=E7=AE=97=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E9=94=99=E8=AF=AF=EF=BC=8C=E6=94=B9=E4=B8=BA=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=AF=8F=E7=A7=92=E5=AE=9E=E9=99=85=E6=B6=88=E8=80=97?= =?UTF-8?q?=E5=AD=97=E8=8A=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ lib/file/db.go | 2 +- lib/file/obj.go | 20 +++++++++++++------- lib/rate/rate.go | 7 ++++--- server/server.go | 15 ++++----------- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 76687d9..9c730f6 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,11 @@ - web 静态文件打包进可执行文件:启动时若 web/static、web/views 目录不存在,自动释放内嵌资源,部署不再需要单独拷贝 web 目录 - 精简发布包:去除打包时冗余的 conf/nps.conf、web 目录 - 清理失效配置项:移除 appname、runmode、https_default_cert_file、https_default_key_file、https_just_proxy + - 修复: + - [#184](https://github.com/yisier/nps/issues/184) 限速导致隧道中断:UpdateClient 在 RateLimit=0 时创建零速率令牌桶导致永久阻塞 + - [#159](https://github.com/yisier/nps/issues/159) 关闭隧道端口仍可用:StopServer 错误时提前返回未更新状态 + 重复 OpenTask case 导致隧道被自动重启 + - [#170](https://github.com/yisier/nps/issues/170) TCP 负载均衡只有一个后端可达:GetRandomTarget 未处理 \r 换行符和尾部空行 + - [#319](https://github.com/yisier/nps/issues/319) 客户端列表不显示实时网速:NowRate 计算逻辑错误,改为统计每秒实际消耗字节数 - 2026-03-27 v0.26.32 - 修复: diff --git a/lib/file/db.go b/lib/file/db.go index aca7cfe..6ff76fa 100644 --- a/lib/file/db.go +++ b/lib/file/db.go @@ -273,7 +273,7 @@ func (s *DbUtils) VerifyUserName(username string, id int) (res bool) { func (s *DbUtils) UpdateClient(t *Client) error { s.JsonDb.Clients.Store(t.Id, t) if t.RateLimit == 0 { - t.Rate = rate.NewRate(0) + t.Rate = rate.NewRate(int64((2 << 23) * 1024)) t.Rate.Start() } return nil diff --git a/lib/file/obj.go b/lib/file/obj.go index 99e5888..73d3d27 100644 --- a/lib/file/obj.go +++ b/lib/file/obj.go @@ -207,22 +207,28 @@ type MultiAccount struct { } func (s *Target) GetRandomTarget() (string, error) { + s.Lock() if s.TargetArr == nil { - s.TargetArr = strings.Split(s.TargetStr, "\n") - } - if len(s.TargetArr) == 1 { - return s.TargetArr[0], nil + arr := strings.Split(s.TargetStr, "\n") + s.TargetArr = make([]string, 0, len(arr)) + for _, v := range arr { + v = strings.TrimRight(v, "\r") + if v != "" { + s.TargetArr = append(s.TargetArr, v) + } + } } if len(s.TargetArr) == 0 { + s.Unlock() return "", errors.New("all inward-bending targets are offline") } - s.Lock() - defer s.Unlock() if s.nowIndex >= len(s.TargetArr)-1 { s.nowIndex = -1 } s.nowIndex++ - return s.TargetArr[s.nowIndex], nil + addr := s.TargetArr[s.nowIndex] + s.Unlock() + return addr, nil } type Glob struct { diff --git a/lib/rate/rate.go b/lib/rate/rate.go index 6b22a18..91c934b 100644 --- a/lib/rate/rate.go +++ b/lib/rate/rate.go @@ -2,6 +2,7 @@ package rate import ( "context" + "sync/atomic" "time" xrate "golang.org/x/time/rate" @@ -11,6 +12,7 @@ type Rate struct { limiter *xrate.Limiter addSize int64 stopChan chan struct{} + consumed int64 NowRate int64 } @@ -19,7 +21,6 @@ func NewRate(addSize int64) *Rate { limiter: xrate.NewLimiter(xrate.Limit(addSize), int(addSize)), addSize: addSize, stopChan: make(chan struct{}), - NowRate: addSize, } } @@ -30,7 +31,7 @@ func (s *Rate) Start() { for { select { case <-ticker.C: - s.NowRate = s.addSize - (int64(s.limiter.Tokens())) + s.NowRate = atomic.SwapInt64(&s.consumed, 0) case <-s.stopChan: return } @@ -55,5 +56,5 @@ func (s *Rate) Get(size int64) { } ctx := context.Background() _ = s.limiter.WaitN(ctx, int(size)) - s.NowRate = s.addSize - (int64(s.limiter.Tokens())) + atomic.AddInt64(&s.consumed, size) } diff --git a/server/server.go b/server/server.go index 582b9c4..444389d 100644 --- a/server/server.go +++ b/server/server.go @@ -57,7 +57,7 @@ func DealBridgeTask() { for { select { case t := <-Bridge.OpenTask: - AddTask(t) + StartTask(t.Id) case t := <-Bridge.CloseTask: StopServer(t.Id) case id := <-Bridge.CloseClient: @@ -67,8 +67,6 @@ func DealBridgeTask() { file.GetDb().DelClient(id) } } - case tunnel := <-Bridge.OpenTask: - StartTask(tunnel.Id) case s := <-Bridge.SecretChan: logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr()) if t := file.GetDb().GetTaskByMd5Password(s.Password); t != nil { @@ -160,25 +158,20 @@ func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service { // stop server func StopServer(id int) error { - //if v, ok := RunList[id]; ok { if v, ok := RunList.Load(id); ok { if svr, ok := v.(proxy.Service); ok { if err := svr.Close(); err != nil { - return err + logs.Error("stop server id %d error", id, err) } - logs.Info("stop server id %d", id) } else { logs.Warn("stop server id %d error", id) } - if t, err := file.GetDb().GetTask(id); err != nil { - return err - } else { + RunList.Delete(id) + if t, err := file.GetDb().GetTask(id); err == nil { t.Status = false logs.Info("close port %d,remark %s,client id %d,task id %d", t.Port, t.Remark, t.Client.Id, t.Id) file.GetDb().UpdateTask(t) } - //delete(RunList, id) - RunList.Delete(id) return nil } return errors.New("task is not running")