admin_api.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "github.com/julienschmidt/httprouter"
  19. ini "github.com/vaughan0/go-ini"
  20. "github.com/fatedier/frp/models/config"
  21. "github.com/fatedier/frp/utils/log"
  22. )
  23. type GeneralResponse struct {
  24. Code int64 `json:"code"`
  25. Msg string `json:"msg"`
  26. }
  27. // api/reload
  28. type ReloadResp struct {
  29. GeneralResponse
  30. }
  31. func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  32. var (
  33. buf []byte
  34. res ReloadResp
  35. )
  36. defer func() {
  37. log.Info("Http response [/api/reload]: code [%d]", res.Code)
  38. buf, _ = json.Marshal(&res)
  39. w.Write(buf)
  40. }()
  41. log.Info("Http request: [/api/reload]")
  42. conf, err := ini.LoadFile(config.ClientCommonCfg.ConfigFile)
  43. if err != nil {
  44. res.Code = 1
  45. res.Msg = err.Error()
  46. log.Error("reload frpc config file error: %v", err)
  47. return
  48. }
  49. newCommonCfg, err := config.LoadClientCommonConf(conf)
  50. if err != nil {
  51. res.Code = 2
  52. res.Msg = err.Error()
  53. log.Error("reload frpc common section error: %v", err)
  54. return
  55. }
  56. pxyCfgs, visitorCfgs, err := config.LoadProxyConfFromFile(config.ClientCommonCfg.User, conf, newCommonCfg.Start)
  57. if err != nil {
  58. res.Code = 3
  59. res.Msg = err.Error()
  60. log.Error("reload frpc proxy config error: %v", err)
  61. return
  62. }
  63. svr.ctl.reloadConf(pxyCfgs, visitorCfgs)
  64. log.Info("success reload conf")
  65. return
  66. }