diff --git a/README.md b/README.md index 5f08bee..cb24ffb 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务 * [配置文件说明](#服务端配置文件) * [使用https](#使用https) * [与nginx配合](#与nginx配合) + * [web使用Caddy代理](#web使用Caddy代理) * [关闭http|https代理](#关闭代理) * [将nps安装到系统](#将nps安装到系统) * [流量数据持久化](#流量数据持久化) @@ -307,6 +308,7 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务 web_port | web管理端口 web_password | web界面管理密码 web_username | web界面管理账号 +web_base_url | web管理主路径,用于将web管理置于代理子路径后面 bridge_port | 服务端客户端通信端口 https_proxy_port | 域名代理https代理监听端口 http_proxy_port | 域名代理http代理监听端口 @@ -365,6 +367,29 @@ server { } } ``` + +### web使用Caddy代理 + +如果将web配置到Caddy代理,实现子路径访问nps,可以这样配置. + +假设我们想通过 `http://caddy_ip:caddy_port/nps` 来访问后台, Caddyfile 这样配置: + +```Caddyfile +caddy_ip:caddy_port/nps { + #server_ip 为 nps 服务器IP + #web_port 为 nps 后台端口 + proxy / http://server_ip:web_port/nps { + transparent + } +} +``` + +nps.conf 修改 `web_base_url` 为 `/nps` 即可 +``` +web_base_url=/nps +``` + + ### 关闭代理 如需关闭http代理可在配置文件中将http_proxy_port设置为空,如需关闭https代理可在配置文件中将https_proxy_port设置为空。 diff --git a/cmd/nps/nps.go b/cmd/nps/nps.go index 2cb9a28..369dcf1 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -14,7 +14,7 @@ import ( "github.com/cnlh/nps/server/tool" "github.com/cnlh/nps/vender/github.com/astaxie/beego" "github.com/cnlh/nps/vender/github.com/astaxie/beego/logs" - _ "github.com/cnlh/nps/web/routers" + "github.com/cnlh/nps/web/routers" "log" "os" "path/filepath" @@ -28,6 +28,7 @@ var ( func main() { flag.Parse() beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf")) + routers.Init() if len(os.Args) > 1 { switch os.Args[1] { case "test": diff --git a/conf/nps.conf b/conf/nps.conf index c75a430..8c22ab1 100755 --- a/conf/nps.conf +++ b/conf/nps.conf @@ -41,6 +41,9 @@ web_username=admin web_password=123 web_port = 8080 web_ip=0.0.0.0 +web_base_url= +# if web under proxy use sub path. like http://host/nps need this. +#web_base_url=/nps #Web API unauthenticated IP address(the len of auth_crypt_key must be 16) auth_key=test diff --git a/server/proxy/tcp.go b/server/proxy/tcp.go index b7c7267..e263a82 100755 --- a/server/proxy/tcp.go +++ b/server/proxy/tcp.go @@ -62,7 +62,7 @@ func (s *WebServer) Start() error { <-stop } beego.BConfig.WebConfig.Session.SessionOn = true - beego.SetStaticPath("/static", filepath.Join(common.GetRunPath(), "web", "static")) + beego.SetStaticPath(beego.AppConfig.String("web_base_url")+"/static", filepath.Join(common.GetRunPath(), "web", "static")) beego.SetViewsPath(filepath.Join(common.GetRunPath(), "web", "views")) if l, err := connection.GetWebManagerListener(); err == nil { beego.InitBeforeHTTPRun() diff --git a/web/controllers/base.go b/web/controllers/base.go index cbfa1cc..e58d37d 100755 --- a/web/controllers/base.go +++ b/web/controllers/base.go @@ -21,6 +21,7 @@ type BaseController struct { //初始化参数 func (s *BaseController) Prepare() { + s.Data["web_base_url"] = beego.AppConfig.String("web_base_url") controllerName, actionName := s.GetControllerAndAction() s.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10]) s.actionName = strings.ToLower(actionName) @@ -33,7 +34,7 @@ func (s *BaseController) Prepare() { timeNowUnix := time.Now().Unix() if !((math.Abs(float64(timeNowUnix-int64(timestamp))) <= 20) && (crypt.Md5(configKey+strconv.Itoa(timestamp)) == md5Key)) { if s.GetSession("auth") != true { - s.Redirect("/login/index", 302) + s.Redirect(beego.AppConfig.String("web_base_url")+"/login/index", 302) } } if s.GetSession("isAdmin") != nil && !s.GetSession("isAdmin").(bool) { @@ -59,6 +60,7 @@ func (s *BaseController) Prepare() { //加载模板 func (s *BaseController) display(tpl ...string) { + s.Data["web_base_url"] = beego.AppConfig.String("web_base_url") var tplname string if s.Data["menu"] == nil { s.Data["menu"] = s.actionName @@ -82,6 +84,7 @@ func (s *BaseController) display(tpl ...string) { //错误 func (s *BaseController) error() { + s.Data["web_base_url"] = beego.AppConfig.String("web_base_url") s.Layout = "public/layout.html" s.TplName = "public/error.html" } diff --git a/web/controllers/index.go b/web/controllers/index.go index 5ecdb15..ff7370a 100755 --- a/web/controllers/index.go +++ b/web/controllers/index.go @@ -4,6 +4,7 @@ import ( "github.com/cnlh/nps/lib/file" "github.com/cnlh/nps/server" "github.com/cnlh/nps/server/tool" + "github.com/cnlh/nps/vender/github.com/astaxie/beego" ) type IndexController struct { @@ -11,6 +12,7 @@ type IndexController struct { } func (s *IndexController) Index() { + s.Data["web_base_url"] = beego.AppConfig.String("web_base_url") s.Data["data"] = server.GetDashboardData() s.SetInfo("dashboard") s.display("index/index") diff --git a/web/controllers/login.go b/web/controllers/login.go index 7b8a55d..6c62a0d 100755 --- a/web/controllers/login.go +++ b/web/controllers/login.go @@ -13,6 +13,7 @@ type LoginController struct { } func (self *LoginController) Index() { + self.Data["web_base_url"] = beego.AppConfig.String("web_base_url") self.Data["register_allow"], _ = beego.AppConfig.Bool("allow_user_register") self.TplName = "login/index.html" } @@ -59,6 +60,7 @@ func (self *LoginController) Verify() { } func (self *LoginController) Register() { if self.Ctx.Request.Method == "GET" { + self.Data["web_base_url"] = beego.AppConfig.String("web_base_url") self.TplName = "login/register.html" } else { if b, err := beego.AppConfig.Bool("allow_user_register"); err != nil || !b { @@ -90,5 +92,5 @@ func (self *LoginController) Register() { func (self *LoginController) Out() { self.SetSession("auth", false) - self.Redirect("/login/index", 302) + self.Redirect(beego.AppConfig.String("web_base_url")+"/login/index", 302) } diff --git a/web/routers/router.go b/web/routers/router.go index 690df3b..82d4ad2 100755 --- a/web/routers/router.go +++ b/web/routers/router.go @@ -5,10 +5,22 @@ import ( "github.com/cnlh/nps/web/controllers" ) -func init() { - beego.Router("/", &controllers.IndexController{}, "*:Index") - beego.AutoRouter(&controllers.IndexController{}) - beego.AutoRouter(&controllers.LoginController{}) - beego.AutoRouter(&controllers.ClientController{}) - beego.AutoRouter(&controllers.AuthController{}) +func Init() { + web_base_url := beego.AppConfig.String("web_base_url") + if len(web_base_url) > 0 { + ns := beego.NewNamespace(web_base_url, + beego.NSRouter("/", &controllers.IndexController{}, "*:Index"), + beego.NSAutoRouter(&controllers.IndexController{}), + beego.NSAutoRouter(&controllers.LoginController{}), + beego.NSAutoRouter(&controllers.ClientController{}), + beego.NSAutoRouter(&controllers.AuthController{}), + ) + beego.AddNamespace(ns) + } else { + beego.Router("/", &controllers.IndexController{}, "*:Index") + beego.AutoRouter(&controllers.IndexController{}) + beego.AutoRouter(&controllers.LoginController{}) + beego.AutoRouter(&controllers.ClientController{}) + beego.AutoRouter(&controllers.AuthController{}) + } } diff --git a/web/static/js/langchange.js b/web/static/js/langchange.js index 54961d7..e7a60fb 100644 --- a/web/static/js/langchange.js +++ b/web/static/js/langchange.js @@ -12,7 +12,7 @@ $.ajax({ type: "GET", - url: defaults.file, + url: window.nps.web_base_url + defaults.file, dataType: "xml", success: function (xml) { $(xml).find('text').each(function () { @@ -65,4 +65,4 @@ $(document).ready(function () { setCookie("lang", "zh") $("body").cloudLang({lang: "zh", file: "/static/page/lang-example.xml"}); }); -}); \ No newline at end of file +}); diff --git a/web/views/client/add.html b/web/views/client/add.html index 07719bd..30563c2 100755 --- a/web/views/client/add.html +++ b/web/views/client/add.html @@ -134,7 +134,7 @@ $("#add").on("click", function () { $.ajax({ type: "POST", - url: "/client/add", + url: "{{.web_base_url}}/client/add", data: $("form").serializeArray(), success: function (res) { alert(res.msg) diff --git a/web/views/client/edit.html b/web/views/client/edit.html index 20db215..0ae06a2 100755 --- a/web/views/client/edit.html +++ b/web/views/client/edit.html @@ -145,7 +145,7 @@ $("#add").on("click", function () { $.ajax({ type: "POST", - url: "/client/edit", + url: "{{.web_base_url}}/client/edit", data: $("form").serializeArray(), success: function (res) { alert(res.msg) diff --git a/web/views/client/list.html b/web/views/client/list.html index ce49bfe..60a4fc6 100755 --- a/web/views/client/list.html +++ b/web/views/client/list.html @@ -20,7 +20,7 @@