From 383dbd1b7b8afa712d9493197e0a28cc34340c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Fri, 10 May 2019 16:29:55 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20web=5Fbase=5Furl=20?= =?UTF-8?q?=E9=85=8D=E7=BD=AE,=20=E7=94=A8=E4=BA=8E=E9=85=8D=E7=BD=AE=20we?= =?UTF-8?q?b=20=E5=90=8E=E5=8F=B0=E5=8F=AF=E7=BD=AE=E4=BA=8E=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=AD=90=E8=B7=AF=E5=BE=84=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 ++++++++++++++++++ cmd/nps/nps.go | 3 ++- conf/nps.conf | 3 +++ server/proxy/tcp.go | 2 +- web/controllers/base.go | 5 +++- web/controllers/index.go | 2 ++ web/controllers/login.go | 4 ++- web/routers/router.go | 24 ++++++++++++----- web/static/js/langchange.js | 4 +-- web/views/client/add.html | 2 +- web/views/client/edit.html | 2 +- web/views/client/list.html | 18 ++++++------- web/views/index/add.html | 2 +- web/views/index/edit.html | 2 +- web/views/index/hadd.html | 2 +- web/views/index/hedit.html | 2 +- web/views/index/hlist.html | 6 ++--- web/views/index/list.html | 12 ++++----- web/views/login/index.html | 14 +++++----- web/views/login/register.html | 14 +++++----- web/views/public/layout.html | 49 +++++++++++++++++++---------------- 21 files changed, 124 insertions(+), 73 deletions(-) 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 @@
@@ -42,7 +42,7 @@ if (confirm("Are you sure you want to delete it??")) { $.ajax({ type: "POST", - url: "/client/del", + url: "{{.web_base_url}}/client/del", data: {"id": id}, success: function (res) { alert(res.msg) @@ -58,7 +58,7 @@ if (confirm("Are you sure you want to start it??")) { $.ajax({ type: "POST", - url: "/client/changestatus", + url: "{{.web_base_url}}/client/changestatus", data: {"id": id, "status": 1}, success: function (res) { alert(res.msg) @@ -74,7 +74,7 @@ if (confirm("Are you sure you want to stop it?")) { $.ajax({ type: "POST", - url: "/client/changestatus", + url: "{{.web_base_url}}/client/changestatus", data: { "id": id, "status": 0 }, @@ -90,26 +90,26 @@ } function edit(id) { - window.location.href = "/client/edit?id=" + id + window.location.href = "{{.web_base_url}}/client/edit?id=" + id } function add() { - window.location.href = "/client/add" + window.location.href = "{{.web_base_url}}/client/add" } function tunnel(id) { - window.location.href = "/index/all?client_id=" + id + window.location.href = "{{.web_base_url}}/index/all?client_id=" + id } function host(id) { - window.location.href = "/index/hostlist?client_id=" + id + window.location.href = "{{.web_base_url}}/index/hostlist?client_id=" + id } /*bootstrap table*/ $('#table').bootstrapTable({ toolbar: "#toolbar", method: 'post', // 服务器数据的请求方式 get or post - url: "/client/list", // 服务器数据的加载地址 + url: "{{.web_base_url}}/client/list", // 服务器数据的加载地址 contentType: "application/x-www-form-urlencoded", striped: true, // 设置为true会有隔行变色效果 search: true, diff --git a/web/views/index/add.html b/web/views/index/add.html index c2208a7..302553a 100755 --- a/web/views/index/add.html +++ b/web/views/index/add.html @@ -162,7 +162,7 @@ $("#add").on("click", function () { $.ajax({ type: "POST", - url: "/index/add", + url: "{{.web_base_url}}/index/add", data: $("form").serializeArray(), success: function (res) { alert(res.msg) diff --git a/web/views/index/edit.html b/web/views/index/edit.html index ef48ad0..32e3c3f 100755 --- a/web/views/index/edit.html +++ b/web/views/index/edit.html @@ -160,7 +160,7 @@ $("#add").on("click", function () { $.ajax({ type: "POST", - url: "/index/edit", + url: "{{.web_base_url}}/index/edit", data: $("form").serializeArray(), success: function (res) { alert(res.msg) diff --git a/web/views/index/hadd.html b/web/views/index/hadd.html index ac2a5cc..1a4482d 100755 --- a/web/views/index/hadd.html +++ b/web/views/index/hadd.html @@ -112,7 +112,7 @@ $("#add").on("click", function () { $.ajax({ type: "POST", - url: "/index/addhost", + url: "{{.web_base_url}}/index/addhost", data: $("form").serializeArray(), success: function (res) { alert(res.msg) diff --git a/web/views/index/hedit.html b/web/views/index/hedit.html index 5b430e9..9e17f45 100644 --- a/web/views/index/hedit.html +++ b/web/views/index/hedit.html @@ -116,7 +116,7 @@ $("#add").on("click", function () { $.ajax({ type: "POST", - url: "/index/edithost", + url: "{{.web_base_url}}/index/edithost", data: $("form").serializeArray(), success: function (res) { alert(res.msg) diff --git a/web/views/index/hlist.html b/web/views/index/hlist.html index 6d2ccaa..d2ec24e 100755 --- a/web/views/index/hlist.html +++ b/web/views/index/hlist.html @@ -18,7 +18,7 @@
- 新增
@@ -158,7 +158,7 @@ if (confirm("Are you sure you want to delete it??")) { $.ajax({ type: "POST", - url: "/index/delhost", + url: "{{.web_base_url}}/index/delhost", data: {"id": id}, success: function (res) { alert(res.msg) @@ -171,7 +171,7 @@ } function edit(id) { - window.location.href = "/index/edithost?id=" + id + window.location.href = "{{.web_base_url}}/index/edithost?id=" + id } diff --git a/web/views/index/list.html b/web/views/index/list.html index 5537f67..248d2cb 100755 --- a/web/views/index/list.html +++ b/web/views/index/list.html @@ -18,7 +18,7 @@
- 新增
diff --git a/web/views/login/index.html b/web/views/login/index.html index a28ae87..ffe89d0 100755 --- a/web/views/login/index.html +++ b/web/views/login/index.html @@ -8,10 +8,10 @@ nps admin login - - + + - + @@ -49,7 +49,7 @@ {{if eq true .register_allow}} - register + register {{end}} @@ -59,7 +59,7 @@ - + @@ -68,11 +68,11 @@ function login() { $.ajax({ type: "POST", - url: "/login/verify", + url: "{{.web_base_url}}/login/verify", data: $("form").serializeArray(), success: function (res) { if (res.status) { - window.location.href = "/index/index" + window.location.href = "{{.web_base_url}}/index/index" } else { alert(res.msg) } diff --git a/web/views/login/register.html b/web/views/login/register.html index 88d6e3b..a67e8c5 100644 --- a/web/views/login/register.html +++ b/web/views/login/register.html @@ -8,10 +8,10 @@ nps register - - + + - + @@ -34,22 +34,22 @@ - login + login - + + + + + - - - + + + - + - - + + - + @@ -35,7 +35,7 @@ @@ -115,7 +115,7 @@
  • - + logout
  • @@ -142,6 +142,9 @@