From 3585e456d4d3b1053f96e4668edd21f6a8d40e70 Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 31 Jan 2019 17:17:34 +0800 Subject: [PATCH] frpc: add api GET api/config --- client/admin.go | 1 + client/admin_api.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/client/admin.go b/client/admin.go index e8d49bad..f3ed248f 100644 --- a/client/admin.go +++ b/client/admin.go @@ -41,6 +41,7 @@ func (svr *Service) RunAdminServer(addr string, port int) (err error) { // api, see dashboard_api.go router.HandleFunc("/api/reload", svr.apiReload).Methods("GET") router.HandleFunc("/api/status", svr.apiStatus).Methods("GET") + router.HandleFunc("/api/config", svr.apiGetConfig).Methods("GET") address := fmt.Sprintf("%s:%d", addr, port) server := &http.Server{ diff --git a/client/admin_api.go b/client/admin_api.go index 5bf41fd9..1d06cdcf 100644 --- a/client/admin_api.go +++ b/client/admin_api.go @@ -208,3 +208,48 @@ func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) { sort.Sort(ByProxyStatusResp(res.Xtcp)) return } + +// api/config +func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) { + var ( + buf []byte + res GeneralResponse + ) + defer func() { + log.Info("Http get response [/api/config]") + if len(buf) > 0 { + w.Write(buf) + } else { + buf, _ = json.Marshal(&res) + w.Write(buf) + } + }() + log.Info("Http get request: [/api/config]") + + if g.GlbClientCfg.CfgFile == "" { + w.WriteHeader(400) + res.Code = 1 + res.Msg = "frpc don't configure a config file path" + return + } + + content, err := config.GetRenderedConfFromFile(g.GlbClientCfg.CfgFile) + if err != nil { + w.WriteHeader(400) + res.Code = 2 + res.Msg = err.Error() + log.Error("load frpc config file error: %v", err) + return + } + + rows := strings.Split(content, "\n") + newRows := make([]string, 0, len(rows)) + for _, row := range rows { + row = strings.TrimSpace(row) + if strings.HasPrefix(row, "token") { + continue + } + newRows = append(newRows, row) + } + buf = []byte(strings.Join(newRows, "\n")) +}