From 149ea92e8b7a4d0d4a097c4584e092f20c55c5f8 Mon Sep 17 00:00:00 2001 From: fatih Date: Wed, 4 Sep 2019 11:57:53 +0800 Subject: [PATCH] add dashboard url --- cmd/frps/root.go | 3 +++ conf/frps_full.ini | 1 + models/config/server_common.go | 11 +++++++++++ server/dashboard.go | 6 ++++-- server/service.go | 4 ++-- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/frps/root.go b/cmd/frps/root.go index fd6cdbde..85e784e4 100644 --- a/cmd/frps/root.go +++ b/cmd/frps/root.go @@ -44,6 +44,7 @@ var ( vhostHttpPort int vhostHttpsPort int vhostHttpTimeout int64 + dashboardUrl string dashboardAddr string dashboardPort int dashboardUser string @@ -73,6 +74,7 @@ func init() { rootCmd.PersistentFlags().IntVarP(&vhostHttpPort, "vhost_http_port", "", 0, "vhost http port") rootCmd.PersistentFlags().IntVarP(&vhostHttpsPort, "vhost_https_port", "", 0, "vhost https port") rootCmd.PersistentFlags().Int64VarP(&vhostHttpTimeout, "vhost_http_timeout", "", 60, "vhost http response header timeout") + rootCmd.PersistentFlags().StringVarP(&dashboardUrl, "dashboard_url", "", "/", "dasboard root url") rootCmd.PersistentFlags().StringVarP(&dashboardAddr, "dashboard_addr", "", "0.0.0.0", "dasboard address") rootCmd.PersistentFlags().IntVarP(&dashboardPort, "dashboard_port", "", 0, "dashboard port") rootCmd.PersistentFlags().StringVarP(&dashboardUser, "dashboard_user", "", "admin", "dashboard user") @@ -164,6 +166,7 @@ func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) { cfg.VhostHttpPort = vhostHttpPort cfg.VhostHttpsPort = vhostHttpsPort cfg.VhostHttpTimeout = vhostHttpTimeout + cfg.DashboardUrl = dashboardUrl cfg.DashboardAddr = dashboardAddr cfg.DashboardPort = dashboardPort cfg.DashboardUser = dashboardUser diff --git a/conf/frps_full.ini b/conf/frps_full.ini index ed507cef..44aacf9c 100644 --- a/conf/frps_full.ini +++ b/conf/frps_full.ini @@ -26,6 +26,7 @@ vhost_https_port = 443 # set dashboard_addr and dashboard_port to view dashboard of frps # dashboard_addr's default value is same with bind_addr # dashboard is available only if dashboard_port is set +dashboard_url = / dashboard_addr = 0.0.0.0 dashboard_port = 7500 diff --git a/models/config/server_common.go b/models/config/server_common.go index a190a61a..34c278b3 100644 --- a/models/config/server_common.go +++ b/models/config/server_common.go @@ -60,6 +60,10 @@ type ServerCommonConf struct { // HTTP server, in seconds. By default, this value is 60. VhostHttpTimeout int64 `json:"vhost_http_timeout"` + // DashboardUrl specifies the root path of the dashboard url. By + // default, this value is "/". + DashboardUrl string `json:"dashboard_url"` + // DashboardAddr specifies the address that the dashboard binds to. By // default, this value is "0.0.0.0". DashboardAddr string `json:"dashboard_addr"` @@ -148,6 +152,7 @@ func GetDefaultServerConf() ServerCommonConf { VhostHttpPort: 0, VhostHttpsPort: 0, VhostHttpTimeout: 60, + DashboardUrl: "/", DashboardAddr: "0.0.0.0", DashboardPort: 0, DashboardUser: "admin", @@ -255,6 +260,12 @@ func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error } } + if tmpStr, ok = conf.Get("common", "dashboard_url"); ok { + cfg.DashboardUrl = tmpStr + } else { + cfg.DashboardUrl = "/" + } + if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok { cfg.DashboardAddr = tmpStr } else { diff --git a/server/dashboard.go b/server/dashboard.go index 0a65dbb2..bdfb865c 100644 --- a/server/dashboard.go +++ b/server/dashboard.go @@ -31,9 +31,11 @@ var ( httpServerWriteTimeout = 10 * time.Second ) -func (svr *Service) RunDashboardServer(addr string, port int) (err error) { +func (svr *Service) RunDashboardServer(url string, addr string, port int) (err error) { // url router - router := mux.NewRouter() + r := mux.NewRouter() + r.Host(url) + router := r.PathPrefix(url).Subrouter() user, passwd := svr.cfg.DashboardUser, svr.cfg.DashboardPwd router.Use(frpNet.NewHttpAuthMiddleware(user, passwd).Middleware) diff --git a/server/service.go b/server/service.go index 6d561016..d2abf2e8 100644 --- a/server/service.go +++ b/server/service.go @@ -231,12 +231,12 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { return } - err = svr.RunDashboardServer(cfg.DashboardAddr, cfg.DashboardPort) + err = svr.RunDashboardServer(cfg.DashboardUrl, cfg.DashboardAddr, cfg.DashboardPort) if err != nil { err = fmt.Errorf("Create dashboard web server error, %v", err) return } - log.Info("Dashboard listen on %s:%d", cfg.DashboardAddr, cfg.DashboardPort) + log.Info("Dashboard listen on %s:%d%s", cfg.DashboardAddr, cfg.DashboardPort, cfg.DashboardUrl) statsEnable = true }