|
@@ -29,57 +29,53 @@ import (
|
|
|
)
|
|
|
|
|
|
type GeneralResponse struct {
|
|
|
- Code int64 `json:"code"`
|
|
|
- Msg string `json:"msg"`
|
|
|
+ Code int
|
|
|
+ Msg string
|
|
|
}
|
|
|
|
|
|
// GET api/reload
|
|
|
-type ReloadResp struct {
|
|
|
- GeneralResponse
|
|
|
-}
|
|
|
|
|
|
func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
|
|
|
- var (
|
|
|
- buf []byte
|
|
|
- res ReloadResp
|
|
|
- )
|
|
|
+ res := GeneralResponse{Code: 200}
|
|
|
|
|
|
- log.Info("Http request: [/api/reload]")
|
|
|
+ log.Info("Http request [/api/reload]")
|
|
|
defer func() {
|
|
|
- log.Info("Http response [/api/reload]: code [%d]", res.Code)
|
|
|
- buf, _ = json.Marshal(&res)
|
|
|
- w.Write(buf)
|
|
|
+ log.Info("Http response [/api/reload], code [%d]", res.Code)
|
|
|
+ w.WriteHeader(res.Code)
|
|
|
+ if len(res.Msg) > 0 {
|
|
|
+ w.Write([]byte(res.Msg))
|
|
|
+ }
|
|
|
}()
|
|
|
|
|
|
content, err := config.GetRenderedConfFromFile(g.GlbClientCfg.CfgFile)
|
|
|
if err != nil {
|
|
|
- res.Code = 1
|
|
|
+ res.Code = 400
|
|
|
res.Msg = err.Error()
|
|
|
- log.Warn("reload frpc config file error: %v", err)
|
|
|
+ log.Warn("reload frpc config file error: %s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
newCommonCfg, err := config.UnmarshalClientConfFromIni(nil, content)
|
|
|
if err != nil {
|
|
|
- res.Code = 2
|
|
|
+ res.Code = 400
|
|
|
res.Msg = err.Error()
|
|
|
- log.Warn("reload frpc common section error: %v", err)
|
|
|
+ log.Warn("reload frpc common section error: %s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
pxyCfgs, visitorCfgs, err := config.LoadAllConfFromIni(g.GlbClientCfg.User, content, newCommonCfg.Start)
|
|
|
if err != nil {
|
|
|
- res.Code = 3
|
|
|
+ res.Code = 400
|
|
|
res.Msg = err.Error()
|
|
|
- log.Warn("reload frpc proxy config error: %v", err)
|
|
|
+ log.Warn("reload frpc proxy config error: %s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
err = svr.ReloadConf(pxyCfgs, visitorCfgs)
|
|
|
if err != nil {
|
|
|
- res.Code = 4
|
|
|
+ res.Code = 500
|
|
|
res.Msg = err.Error()
|
|
|
- log.Warn("reload frpc proxy config error: %v", err)
|
|
|
+ log.Warn("reload frpc proxy config error: %s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
log.Info("success reload conf")
|
|
@@ -177,7 +173,7 @@ func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
res.Stcp = make([]ProxyStatusResp, 0)
|
|
|
res.Xtcp = make([]ProxyStatusResp, 0)
|
|
|
|
|
|
- log.Info("Http request: [/api/status]")
|
|
|
+ log.Info("Http request [/api/status]")
|
|
|
defer func() {
|
|
|
log.Info("Http response [/api/status]")
|
|
|
buf, _ = json.Marshal(&res)
|
|
@@ -212,36 +208,29 @@ func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
// GET api/config
|
|
|
func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
- var (
|
|
|
- buf []byte
|
|
|
- res GeneralResponse
|
|
|
- )
|
|
|
+ res := GeneralResponse{Code: 200}
|
|
|
|
|
|
- log.Info("Http get request: [/api/config]")
|
|
|
+ log.Info("Http get request [/api/config]")
|
|
|
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 response [/api/config], code [%d]", res.Code)
|
|
|
+ w.WriteHeader(res.Code)
|
|
|
+ if len(res.Msg) > 0 {
|
|
|
+ w.Write([]byte(res.Msg))
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
if g.GlbClientCfg.CfgFile == "" {
|
|
|
- w.WriteHeader(400)
|
|
|
- res.Code = 1
|
|
|
- res.Msg = "frpc don't configure a config file path"
|
|
|
+ res.Code = 400
|
|
|
+ res.Msg = "frpc has no config file path"
|
|
|
log.Warn("%s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
content, err := config.GetRenderedConfFromFile(g.GlbClientCfg.CfgFile)
|
|
|
if err != nil {
|
|
|
- w.WriteHeader(400)
|
|
|
- res.Code = 2
|
|
|
+ res.Code = 400
|
|
|
res.Msg = err.Error()
|
|
|
- log.Warn("load frpc config file error: %v", err)
|
|
|
+ log.Warn("load frpc config file error: %s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -254,34 +243,33 @@ func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
newRows = append(newRows, row)
|
|
|
}
|
|
|
- buf = []byte(strings.Join(newRows, "\n"))
|
|
|
+ res.Msg = strings.Join(newRows, "\n")
|
|
|
}
|
|
|
|
|
|
// PUT api/config
|
|
|
func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
- var (
|
|
|
- buf []byte
|
|
|
- res GeneralResponse
|
|
|
- )
|
|
|
+ res := GeneralResponse{Code: 200}
|
|
|
|
|
|
- log.Info("Http put request: [/api/config]")
|
|
|
+ log.Info("Http put request [/api/config]")
|
|
|
defer func() {
|
|
|
- log.Info("Http put response: [/api/config]")
|
|
|
- buf, _ = json.Marshal(&res)
|
|
|
- w.Write(buf)
|
|
|
+ log.Info("Http put response [/api/config], code [%d]", res.Code)
|
|
|
+ w.WriteHeader(res.Code)
|
|
|
+ if len(res.Msg) > 0 {
|
|
|
+ w.Write([]byte(res.Msg))
|
|
|
+ }
|
|
|
}()
|
|
|
|
|
|
// get new config content
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
if err != nil {
|
|
|
- res.Code = 1
|
|
|
+ res.Code = 400
|
|
|
res.Msg = fmt.Sprintf("read request body error: %v", err)
|
|
|
log.Warn("%s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if len(body) == 0 {
|
|
|
- res.Code = 2
|
|
|
+ res.Code = 400
|
|
|
res.Msg = "body can't be empty"
|
|
|
log.Warn("%s", res.Msg)
|
|
|
return
|
|
@@ -291,9 +279,9 @@ func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
token := ""
|
|
|
b, err := ioutil.ReadFile(g.GlbClientCfg.CfgFile)
|
|
|
if err != nil {
|
|
|
- res.Code = 3
|
|
|
+ res.Code = 400
|
|
|
res.Msg = err.Error()
|
|
|
- log.Warn("load frpc config file error: %v", err)
|
|
|
+ log.Warn("load frpc config file error: %s", res.Msg)
|
|
|
return
|
|
|
}
|
|
|
content := string(b)
|
|
@@ -328,7 +316,7 @@ func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
err = ioutil.WriteFile(g.GlbClientCfg.CfgFile, []byte(content), 0644)
|
|
|
if err != nil {
|
|
|
- res.Code = 4
|
|
|
+ res.Code = 500
|
|
|
res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)
|
|
|
log.Warn("%s", res.Msg)
|
|
|
return
|