admin_api.go 6.3 KB

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