diff --git a/README.md b/README.md index 4967d8e..37703c5 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,15 @@ https://natnps.com/ # 更新日志 +- 2025-11-051 v0.26.27 + 新增: + - 1.客户端配置移除【压缩】和【加密】功能,此功能会显著提高内存和CPU占用,利大于弊。 + - 2.客户端新增【IP白名单】,可通过两种方式添加IP: + - 方式1️⃣:在【客户端】新建/修改页面,配置IP授权密码,不在IP白名单的外网访问将被跳转到 IP 授权页面,输入正确的授权密码,即可完成添加 IP。 + - 方式2️⃣:在【客户端】新建/修改页面,直接添加IP到白名单中。 + - 3.客户端默认不再提供配置文件,强烈推荐使用无配置文件模式启动客户端 + + - 2025-08-15 v0.26.26 修复: - windows注册服务 使用tls无法连接 [269](https://github.com/yisier/nps/issues/269) diff --git a/build.sh b/build.sh index 7185101..376b100 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #/bash/sh -export VERSION=0.26.26 +export VERSION=0.26.27 export GOPROXY=direct sudo apt-get update diff --git a/lib/common/const.go b/lib/common/const.go index ffb2fa6..b9a7617 100644 --- a/lib/common/const.go +++ b/lib/common/const.go @@ -34,5 +34,8 @@ WWW-Authenticate: Basic realm="easyProxy" 401 Unauthorized` ConnectionFailBytes = `HTTP/1.1 404 Not Found +` + Unauthorized = `HTTP/1.1 401 Unauthorized + ` ) diff --git a/lib/common/util.go b/lib/common/util.go index 27b249f..e6a75c9 100755 --- a/lib/common/util.go +++ b/lib/common/util.go @@ -310,6 +310,17 @@ func IsBlackIp(ipPort, vkey string, blackIpList []string) bool { return false } +// 判断访问地址是否在白名单内 +func IsAuthIp(ipPort, vkey string, authIpList []string) bool { + ip := GetIpByAddr(ipPort) + + if in(ip, authIpList) { + return false + } + logs.Error("IP地址[" + ip + "]不在隧道[" + vkey + "]白名单列表内") + return true +} + func CopyBuffer(dst io.Writer, src io.Reader, label ...string) (written int64, err error) { buf := CopyBuff.Get() defer CopyBuff.Put(buf) diff --git a/lib/file/db.go b/lib/file/db.go index c126b20..d6b7cbf 100644 --- a/lib/file/db.go +++ b/lib/file/db.go @@ -317,6 +317,24 @@ func (s *DbUtils) GetClientIdByVkey(vkey string) (id int, err error) { return } +func (s *DbUtils) GetClientByVkey(vkey string) (c *Client, err error) { + var exist bool + s.JsonDb.Clients.Range(func(key, value interface{}) bool { + v := value.(*Client) + if v.VerifyKey == vkey { + exist = true + c = v + return false + } + return true + }) + if exist { + return + } + err = errors.New("未找到客户端") + return +} + func (s *DbUtils) GetHostById(id int) (h *Host, err error) { if v, ok := s.JsonDb.Hosts.Load(id); ok { h = v.(*Host) diff --git a/lib/file/obj.go b/lib/file/obj.go index f093917..f8016b2 100644 --- a/lib/file/obj.go +++ b/lib/file/obj.go @@ -54,6 +54,9 @@ type Client struct { BlackIpList []string CreateTime string LastOnlineTime string + IpWhite bool // 是否启用ip白名单 + IpWhitePass string // ip授权密码 + IpWhiteList []string // ip白名单 sync.RWMutex } @@ -149,7 +152,6 @@ type Tunnel struct { Remark string TargetAddr string NoStore bool - IsHttp bool LocalPath string StripPre string ProtoVersion string diff --git a/lib/goroutine/pool.go b/lib/goroutine/pool.go index 3d89edb..04c7d27 100644 --- a/lib/goroutine/pool.go +++ b/lib/goroutine/pool.go @@ -3,10 +3,13 @@ package goroutine import ( "ehang.io/nps/lib/common" "ehang.io/nps/lib/file" + "fmt" + "github.com/astaxie/beego" "github.com/astaxie/beego/logs" "github.com/panjf2000/ants/v2" "io" "net" + "path/filepath" "strings" "sync" ) @@ -51,24 +54,18 @@ func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel } nr, er := src.Read(buf) - //if len(pr)>0 && pr[0] && nr > 50 { - // logs.Warn(string(buf[:50])) - //} - if task != nil { - task.IsHttp = false - firstLine := string(buf[0:nr]) - if len(firstLine) > 3 { - method := firstLine[0:3] - if method != "" && (method == "HTT" || method == "GET" || method == "POS" || method == "HEA" || method == "PUT" || method == "DEL") { - if method != "HTT" { - heads := strings.Split(firstLine, "\r\n") - if len(heads) >= 2 { - logs.Info("HTTP Request method %s, %s, remote address %s, target %s", heads[0], heads[1], remote, task.Target.TargetStr) - } - } + if task.Client.IpWhite && task.Client.IpWhitePass != "" { + if common.IsAuthIp(remote, task.Client.VerifyKey, task.Client.IpWhiteList) { + errorContent, _ := common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "auth.html")) + authHtml := string(errorContent) + authHtml = strings.ReplaceAll(authHtml, "${ip}", common.GetIpByAddr(remote)) + authHtml = strings.ReplaceAll(authHtml, "${vkey}", task.Client.VerifyKey) + authHtml = strings.ReplaceAll(authHtml, "${port}", beego.AppConfig.String("web_port")) - task.IsHttp = true + response := fmt.Sprintf("HTTP/1.1 401 Unauthorized\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(authHtml), authHtml) + dst.Write([]byte(response)) + break } } } diff --git a/lib/version/version.go b/lib/version/version.go index 8f75daa..423050a 100644 --- a/lib/version/version.go +++ b/lib/version/version.go @@ -1,6 +1,6 @@ package version -const VERSION = "0.26.26" +const VERSION = "0.26.27" // Compulsory minimum version, Minimum downward compatibility to this version func GetVersion() string { diff --git a/server/proxy/base.go b/server/proxy/base.go index 5f33f65..1188154 100644 --- a/server/proxy/base.go +++ b/server/proxy/base.go @@ -29,6 +29,7 @@ type BaseServer struct { bridge NetBridge task *file.Tunnel errorContent []byte + errorCode int sync.Mutex } diff --git a/server/proxy/http.go b/server/proxy/http.go index 1a5f95d..2ea8582 100644 --- a/server/proxy/http.go +++ b/server/proxy/http.go @@ -10,6 +10,7 @@ import ( "ehang.io/nps/lib/file" "ehang.io/nps/lib/goroutine" "ehang.io/nps/server/connection" + "github.com/astaxie/beego" "github.com/astaxie/beego/logs" "io" "net" @@ -196,6 +197,19 @@ reset: return } + if host.Client.IpWhite && host.Client.IpWhitePass != "" { + if common.IsAuthIp(c.RemoteAddr().String(), host.Client.VerifyKey, host.Client.IpWhiteList) { + errorContent, _ := common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "auth.html")) + authHtml := string(errorContent) + authHtml = strings.ReplaceAll(authHtml, "${ip}", common.GetIpByAddr(c.RemoteAddr().String())) + authHtml = strings.ReplaceAll(authHtml, "${vkey}", host.Client.VerifyKey) + authHtml = strings.ReplaceAll(authHtml, "${port}", beego.AppConfig.String("web_port")) + s.errorContent, err = []byte(authHtml), err + s.errorCode = 401 + return + } + } + // 判断访问地址是否在黑名单内 if common.IsBlackIp(c.RemoteAddr().String(), host.Client.VerifyKey, host.Client.BlackIpList) { c.Close() diff --git a/web/controllers/auth.go b/web/controllers/auth.go index 9eb22ca..21282fc 100644 --- a/web/controllers/auth.go +++ b/web/controllers/auth.go @@ -1,7 +1,9 @@ package controllers import ( + "ehang.io/nps/lib/file" "encoding/hex" + "html" "time" "ehang.io/nps/lib/crypt" @@ -40,3 +42,52 @@ func (s *AuthController) GetTime() { s.Data["json"] = m s.ServeJSON() } + +func (s *AuthController) IpWhiteAuth() { + s.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") + s.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") + s.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "Content-Type") + + vkey := s.getEscapeString("vkey") + ip := s.getEscapeString("ip") + password := s.getEscapeString("pass") + + if vkey == "" || ip == "" || password == "" { + s.Data["json"] = map[string]interface{}{"success": false, "message": "参数错误"} + s.ServeJSON() + return + } + + c, err := file.GetDb().GetClientByVkey(vkey) + if err != nil { + s.Data["json"] = map[string]interface{}{"success": false, "message": "客户端密钥错误"} + s.ServeJSON() + return + } + + if c.IpWhitePass != password { + s.Data["json"] = map[string]interface{}{"success": false, "message": "授权密码错误"} + s.ServeJSON() + return + } + + ipExists := false + for _, existingIp := range c.IpWhiteList { + if existingIp == ip { + ipExists = true + break + } + } + + if !ipExists { + c.IpWhiteList = append(c.IpWhiteList, ip) + file.GetDb().UpdateClient(c) + } + + s.Data["json"] = map[string]interface{}{"success": true, "message": "授权成功"} + s.ServeJSON() +} + +func (s *AuthController) getEscapeString(key string) string { + return html.EscapeString(s.GetString(key)) +} diff --git a/web/controllers/client.go b/web/controllers/client.go index 2cf1f6e..5fc8cad 100644 --- a/web/controllers/client.go +++ b/web/controllers/client.go @@ -69,6 +69,9 @@ func (s *ClientController) Add() { FlowLimit: int64(s.GetIntNoErr("flow_limit")), }, BlackIpList: RemoveRepeatedElement(strings.Split(s.getEscapeString("blackiplist"), "\r\n")), + IpWhite: s.GetBoolNoErr("ipwhite"), + IpWhitePass: s.getEscapeString("ipwhitepass"), + IpWhiteList: RemoveRepeatedElement(strings.Split(s.getEscapeString("ipwhitelist"), "\r\n")), CreateTime: time.Now().Format("2006-01-02 15:04:05"), } if err := file.GetDb().NewClient(t); err != nil { @@ -102,6 +105,7 @@ func (s *ClientController) Edit() { } else { s.Data["c"] = c s.Data["BlackIpList"] = strings.Join(c.BlackIpList, "\r\n") + s.Data["IpWhiteList"] = strings.Join(c.IpWhiteList, "\r\n") } s.SetInfo("edit client") s.display() @@ -139,6 +143,9 @@ func (s *ClientController) Edit() { } c.WebPassword = s.getEscapeString("web_password") c.ConfigConnAllow = s.GetBoolNoErr("config_conn_allow") + c.IpWhite = s.GetBoolNoErr("ipwhite") + c.IpWhitePass = s.getEscapeString("ipwhitepass") + c.IpWhiteList = RemoveRepeatedElement(strings.Split(s.getEscapeString("ipwhitelist"), "\r\n")) if c.Rate != nil { c.Rate.Stop() } diff --git a/web/routers/router.go b/web/routers/router.go index be289db..e4add32 100755 --- a/web/routers/router.go +++ b/web/routers/router.go @@ -14,6 +14,7 @@ func Init() { beego.NSAutoRouter(&controllers.LoginController{}), beego.NSAutoRouter(&controllers.ClientController{}), beego.NSAutoRouter(&controllers.AuthController{}), + beego.NSRouter("/auth/ipwhiteauth", &controllers.AuthController{}, "*:IpWhiteAuth"), beego.NSAutoRouter(&controllers.GlobalController{}), ) beego.AddNamespace(ns) @@ -23,6 +24,7 @@ func Init() { beego.AutoRouter(&controllers.LoginController{}) beego.AutoRouter(&controllers.ClientController{}) beego.AutoRouter(&controllers.AuthController{}) + beego.Router("/auth/ipwhiteauth", &controllers.AuthController{}, "*:IpWhiteAuth") beego.AutoRouter(&controllers.GlobalController{}) } diff --git a/web/static/page/auth.html b/web/static/page/auth.html new file mode 100644 index 0000000..1964293 --- /dev/null +++ b/web/static/page/auth.html @@ -0,0 +1,234 @@ + + + + + + + NPS访问验证 + + + + + + +
+
+

访问验证

+
当前 IP (${ip}) 未经授权, 已禁止访问
+
+ + + +
+
+
+ + + + + \ No newline at end of file diff --git a/web/static/page/languages.xml b/web/static/page/languages.xml index bd7b759..3bd47bb 100644 --- a/web/static/page/languages.xml +++ b/web/static/page/languages.xml @@ -599,6 +599,12 @@ 唯一值,不填将自动生成 Unique, non-filling will be generated automatically + + + IP授权密码,不在IP白名单的访问将被跳转到IP授权页面,输入正确的授权密码,即可完成添加IP。 + IP White Password, access that is not in the IP white list will be redirected to the IP authorization page, enter the correct authorization password, and you can complete the addition of IP. + + 通提供一个公网可访问的本地文件服务,此模式仅客户端使用配置文件模式方可启动。 Provide a local file service accessible to the public network, which can only be started by the client using the profile mode. @@ -732,6 +738,12 @@ Empty means to be unrestricted + + 留空表示不跳转IP授权页面 + Empty means not to jump to the IP authorization page + + + IP黑名单(防暴力破解) @@ -770,6 +782,21 @@ Last Online Time + + 启用IP白名单 + Enable IP White + + + + IP白名单授权密码 + IP White Password + + + + IP白名单 + IP White List + + 你确定你要删除它吗? diff --git a/web/views/client/add.html b/web/views/client/add.html index 26b39b2..2f8318d 100644 --- a/web/views/client/add.html +++ b/web/views/client/add.html @@ -88,22 +88,49 @@ -
- + + + + + + + + + + + + + + + + + + + +
+
-
-
- + + + + + @@ -128,3 +155,20 @@
+ + diff --git a/web/views/client/edit.html b/web/views/client/edit.html index 076769e..8157dac 100644 --- a/web/views/client/edit.html +++ b/web/views/client/edit.html @@ -8,96 +8,108 @@
- +
- {{if eq true .isAdmin}} - {{if eq true .allow_flow_limit}} + {{if eq true .isAdmin}} + {{if eq true .allow_flow_limit}}
- + : M
- {{end}} - {{if eq true .allow_rate_limit}} + {{end}} + {{if eq true .allow_rate_limit}}
- + : KB/S
- {{end}} - {{if eq true .allow_connection_num_limit}} + {{end}} + {{if eq true .allow_connection_num_limit}}
- +
- {{end}} - {{if eq true .allow_tunnel_num_limit}} + {{end}} + {{if eq true .allow_tunnel_num_limit}}
- +
- {{end}} - {{end}} + {{end}} + {{end}}
- +
- +
- {{if eq true .isAdmin}} + {{if eq true .isAdmin}}
- +
- {{end}} - {{if eq true .allow_user_login}} - {{if or (eq true .allow_user_change_username) (eq true .isAdmin)}} + {{end}} + {{if eq true .allow_user_login}} + {{if or (eq true .allow_user_change_username) (eq true .isAdmin)}}
- +
- {{end}} + {{end}}
- +
- {{end}} + {{end}}
-
+ + +
+ +
+ +
+
+ + + + +
- +
@@ -127,8 +169,9 @@
-
@@ -137,3 +180,24 @@
+ + + \ No newline at end of file