frpc: add api GET api/config

This commit is contained in:
fatedier 2019-01-31 17:17:34 +08:00
parent 1de8c3fc87
commit 3585e456d4
2 changed files with 46 additions and 0 deletions

View File

@ -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{

View File

@ -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"))
}