1
0

admin_api.go 5.6 KB

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