admin_api.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. "fmt"
  18. "io"
  19. "net"
  20. "net/http"
  21. "os"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "github.com/samber/lo"
  27. "github.com/fatedier/frp/client/proxy"
  28. "github.com/fatedier/frp/pkg/config"
  29. "github.com/fatedier/frp/pkg/config/v1/validation"
  30. "github.com/fatedier/frp/pkg/util/log"
  31. )
  32. type GeneralResponse struct {
  33. Code int
  34. Msg string
  35. }
  36. // /healthz
  37. func (svr *Service) healthz(w http.ResponseWriter, _ *http.Request) {
  38. w.WriteHeader(200)
  39. }
  40. // GET /api/reload
  41. func (svr *Service) apiReload(w http.ResponseWriter, _ *http.Request) {
  42. res := GeneralResponse{Code: 200}
  43. log.Info("api request [/api/reload]")
  44. defer func() {
  45. log.Info("api response [/api/reload], code [%d]", res.Code)
  46. w.WriteHeader(res.Code)
  47. if len(res.Msg) > 0 {
  48. _, _ = w.Write([]byte(res.Msg))
  49. }
  50. }()
  51. cliCfg, pxyCfgs, visitorCfgs, _, err := config.LoadClientConfig(svr.cfgFile)
  52. if err != nil {
  53. res.Code = 400
  54. res.Msg = err.Error()
  55. log.Warn("reload frpc proxy config error: %s", res.Msg)
  56. return
  57. }
  58. if _, err := validation.ValidateAllClientConfig(cliCfg, pxyCfgs, visitorCfgs); err != nil {
  59. res.Code = 400
  60. res.Msg = err.Error()
  61. log.Warn("reload frpc proxy config error: %s", res.Msg)
  62. return
  63. }
  64. if err := svr.ReloadConf(pxyCfgs, visitorCfgs); err != nil {
  65. res.Code = 500
  66. res.Msg = err.Error()
  67. log.Warn("reload frpc proxy config error: %s", res.Msg)
  68. return
  69. }
  70. log.Info("success reload conf")
  71. }
  72. // POST /api/stop
  73. func (svr *Service) apiStop(w http.ResponseWriter, _ *http.Request) {
  74. res := GeneralResponse{Code: 200}
  75. log.Info("api request [/api/stop]")
  76. defer func() {
  77. log.Info("api response [/api/stop], code [%d]", res.Code)
  78. w.WriteHeader(res.Code)
  79. if len(res.Msg) > 0 {
  80. _, _ = w.Write([]byte(res.Msg))
  81. }
  82. }()
  83. go svr.GracefulClose(100 * time.Millisecond)
  84. }
  85. type StatusResp map[string][]ProxyStatusResp
  86. type ProxyStatusResp struct {
  87. Name string `json:"name"`
  88. Type string `json:"type"`
  89. Status string `json:"status"`
  90. Err string `json:"err"`
  91. LocalAddr string `json:"local_addr"`
  92. Plugin string `json:"plugin"`
  93. RemoteAddr string `json:"remote_addr"`
  94. }
  95. func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxyStatusResp {
  96. psr := ProxyStatusResp{
  97. Name: status.Name,
  98. Type: status.Type,
  99. Status: status.Phase,
  100. Err: status.Err,
  101. }
  102. baseCfg := status.Cfg.GetBaseConfig()
  103. if baseCfg.LocalPort != 0 {
  104. psr.LocalAddr = net.JoinHostPort(baseCfg.LocalIP, strconv.Itoa(baseCfg.LocalPort))
  105. }
  106. psr.Plugin = baseCfg.Plugin.Type
  107. if status.Err == "" {
  108. psr.RemoteAddr = status.RemoteAddr
  109. if lo.Contains([]string{"tcp", "udp"}, status.Type) {
  110. psr.RemoteAddr = serverAddr + psr.RemoteAddr
  111. }
  112. }
  113. return psr
  114. }
  115. // GET /api/status
  116. func (svr *Service) apiStatus(w http.ResponseWriter, _ *http.Request) {
  117. var (
  118. buf []byte
  119. res StatusResp = make(map[string][]ProxyStatusResp)
  120. )
  121. log.Info("Http request [/api/status]")
  122. defer func() {
  123. log.Info("Http response [/api/status]")
  124. buf, _ = json.Marshal(&res)
  125. _, _ = w.Write(buf)
  126. }()
  127. svr.ctlMu.RLock()
  128. ctl := svr.ctl
  129. svr.ctlMu.RUnlock()
  130. if ctl == nil {
  131. return
  132. }
  133. ps := ctl.pm.GetAllProxyStatus()
  134. for _, status := range ps {
  135. res[status.Type] = append(res[status.Type], NewProxyStatusResp(status, svr.cfg.ServerAddr))
  136. }
  137. for _, arrs := range res {
  138. if len(arrs) <= 1 {
  139. continue
  140. }
  141. sort.Slice(arrs, func(i, j int) bool {
  142. return strings.Compare(arrs[i].Name, arrs[j].Name) < 0
  143. })
  144. }
  145. }
  146. // GET /api/config
  147. func (svr *Service) apiGetConfig(w http.ResponseWriter, _ *http.Request) {
  148. res := GeneralResponse{Code: 200}
  149. log.Info("Http get request [/api/config]")
  150. defer func() {
  151. log.Info("Http get response [/api/config], code [%d]", res.Code)
  152. w.WriteHeader(res.Code)
  153. if len(res.Msg) > 0 {
  154. _, _ = w.Write([]byte(res.Msg))
  155. }
  156. }()
  157. if svr.cfgFile == "" {
  158. res.Code = 400
  159. res.Msg = "frpc has no config file path"
  160. log.Warn("%s", res.Msg)
  161. return
  162. }
  163. content, err := os.ReadFile(svr.cfgFile)
  164. if err != nil {
  165. res.Code = 400
  166. res.Msg = err.Error()
  167. log.Warn("load frpc config file error: %s", res.Msg)
  168. return
  169. }
  170. res.Msg = string(content)
  171. }
  172. // PUT /api/config
  173. func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
  174. res := GeneralResponse{Code: 200}
  175. log.Info("Http put request [/api/config]")
  176. defer func() {
  177. log.Info("Http put response [/api/config], code [%d]", res.Code)
  178. w.WriteHeader(res.Code)
  179. if len(res.Msg) > 0 {
  180. _, _ = w.Write([]byte(res.Msg))
  181. }
  182. }()
  183. // get new config content
  184. body, err := io.ReadAll(r.Body)
  185. if err != nil {
  186. res.Code = 400
  187. res.Msg = fmt.Sprintf("read request body error: %v", err)
  188. log.Warn("%s", res.Msg)
  189. return
  190. }
  191. if len(body) == 0 {
  192. res.Code = 400
  193. res.Msg = "body can't be empty"
  194. log.Warn("%s", res.Msg)
  195. return
  196. }
  197. if err := os.WriteFile(svr.cfgFile, body, 0o644); err != nil {
  198. res.Code = 500
  199. res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)
  200. log.Warn("%s", res.Msg)
  201. return
  202. }
  203. }