admin_api.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/http"
  20. "os"
  21. "sort"
  22. "strings"
  23. "github.com/fatedier/frp/client/proxy"
  24. "github.com/fatedier/frp/pkg/config"
  25. "github.com/fatedier/frp/pkg/util/log"
  26. )
  27. type GeneralResponse struct {
  28. Code int
  29. Msg string
  30. }
  31. // /healthz
  32. func (svr *Service) healthz(w http.ResponseWriter, r *http.Request) {
  33. w.WriteHeader(200)
  34. }
  35. // GET api/reload
  36. func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
  37. res := GeneralResponse{Code: 200}
  38. log.Info("api request [/api/reload]")
  39. defer func() {
  40. log.Info("api response [/api/reload], code [%d]", res.Code)
  41. w.WriteHeader(res.Code)
  42. if len(res.Msg) > 0 {
  43. w.Write([]byte(res.Msg))
  44. }
  45. }()
  46. _, pxyCfgs, visitorCfgs, err := config.ParseClientConfig(svr.cfgFile)
  47. if err != nil {
  48. res.Code = 400
  49. res.Msg = err.Error()
  50. log.Warn("reload frpc proxy config error: %s", res.Msg)
  51. return
  52. }
  53. if err = svr.ReloadConf(pxyCfgs, visitorCfgs); err != nil {
  54. res.Code = 500
  55. res.Msg = err.Error()
  56. log.Warn("reload frpc proxy config error: %s", res.Msg)
  57. return
  58. }
  59. log.Info("success reload conf")
  60. return
  61. }
  62. type StatusResp struct {
  63. TCP []ProxyStatusResp `json:"tcp"`
  64. UDP []ProxyStatusResp `json:"udp"`
  65. HTTP []ProxyStatusResp `json:"http"`
  66. HTTPS []ProxyStatusResp `json:"https"`
  67. STCP []ProxyStatusResp `json:"stcp"`
  68. XTCP []ProxyStatusResp `json:"xtcp"`
  69. SUDP []ProxyStatusResp `json:"sudp"`
  70. }
  71. type ProxyStatusResp struct {
  72. Name string `json:"name"`
  73. Type string `json:"type"`
  74. Status string `json:"status"`
  75. Err string `json:"err"`
  76. LocalAddr string `json:"local_addr"`
  77. Plugin string `json:"plugin"`
  78. RemoteAddr string `json:"remote_addr"`
  79. }
  80. type ByProxyStatusResp []ProxyStatusResp
  81. func (a ByProxyStatusResp) Len() int { return len(a) }
  82. func (a ByProxyStatusResp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  83. func (a ByProxyStatusResp) Less(i, j int) bool { return strings.Compare(a[i].Name, a[j].Name) < 0 }
  84. func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxyStatusResp {
  85. psr := ProxyStatusResp{
  86. Name: status.Name,
  87. Type: status.Type,
  88. Status: status.Phase,
  89. Err: status.Err,
  90. }
  91. switch cfg := status.Cfg.(type) {
  92. case *config.TCPProxyConf:
  93. if cfg.LocalPort != 0 {
  94. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
  95. }
  96. psr.Plugin = cfg.Plugin
  97. if status.Err != "" {
  98. psr.RemoteAddr = fmt.Sprintf("%s:%d", serverAddr, cfg.RemotePort)
  99. } else {
  100. psr.RemoteAddr = serverAddr + status.RemoteAddr
  101. }
  102. case *config.UDPProxyConf:
  103. if cfg.LocalPort != 0 {
  104. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
  105. }
  106. if status.Err != "" {
  107. psr.RemoteAddr = fmt.Sprintf("%s:%d", serverAddr, cfg.RemotePort)
  108. } else {
  109. psr.RemoteAddr = serverAddr + status.RemoteAddr
  110. }
  111. case *config.HTTPProxyConf:
  112. if cfg.LocalPort != 0 {
  113. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
  114. }
  115. psr.Plugin = cfg.Plugin
  116. psr.RemoteAddr = status.RemoteAddr
  117. case *config.HTTPSProxyConf:
  118. if cfg.LocalPort != 0 {
  119. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
  120. }
  121. psr.Plugin = cfg.Plugin
  122. psr.RemoteAddr = status.RemoteAddr
  123. case *config.STCPProxyConf:
  124. if cfg.LocalPort != 0 {
  125. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
  126. }
  127. psr.Plugin = cfg.Plugin
  128. case *config.XTCPProxyConf:
  129. if cfg.LocalPort != 0 {
  130. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
  131. }
  132. psr.Plugin = cfg.Plugin
  133. case *config.SUDPProxyConf:
  134. if cfg.LocalPort != 0 {
  135. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
  136. }
  137. psr.Plugin = cfg.Plugin
  138. }
  139. return psr
  140. }
  141. // GET api/status
  142. func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) {
  143. var (
  144. buf []byte
  145. res StatusResp
  146. )
  147. res.TCP = make([]ProxyStatusResp, 0)
  148. res.UDP = make([]ProxyStatusResp, 0)
  149. res.HTTP = make([]ProxyStatusResp, 0)
  150. res.HTTPS = make([]ProxyStatusResp, 0)
  151. res.STCP = make([]ProxyStatusResp, 0)
  152. res.XTCP = make([]ProxyStatusResp, 0)
  153. res.SUDP = make([]ProxyStatusResp, 0)
  154. log.Info("Http request [/api/status]")
  155. defer func() {
  156. log.Info("Http response [/api/status]")
  157. buf, _ = json.Marshal(&res)
  158. w.Write(buf)
  159. }()
  160. ps := svr.ctl.pm.GetAllProxyStatus()
  161. for _, status := range ps {
  162. switch status.Type {
  163. case "tcp":
  164. res.TCP = append(res.TCP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  165. case "udp":
  166. res.UDP = append(res.UDP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  167. case "http":
  168. res.HTTP = append(res.HTTP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  169. case "https":
  170. res.HTTPS = append(res.HTTPS, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  171. case "stcp":
  172. res.STCP = append(res.STCP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  173. case "xtcp":
  174. res.XTCP = append(res.XTCP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  175. case "sudp":
  176. res.SUDP = append(res.SUDP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  177. }
  178. }
  179. sort.Sort(ByProxyStatusResp(res.TCP))
  180. sort.Sort(ByProxyStatusResp(res.UDP))
  181. sort.Sort(ByProxyStatusResp(res.HTTP))
  182. sort.Sort(ByProxyStatusResp(res.HTTPS))
  183. sort.Sort(ByProxyStatusResp(res.STCP))
  184. sort.Sort(ByProxyStatusResp(res.XTCP))
  185. sort.Sort(ByProxyStatusResp(res.SUDP))
  186. return
  187. }
  188. // GET api/config
  189. func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) {
  190. res := GeneralResponse{Code: 200}
  191. log.Info("Http get request [/api/config]")
  192. defer func() {
  193. log.Info("Http get response [/api/config], code [%d]", res.Code)
  194. w.WriteHeader(res.Code)
  195. if len(res.Msg) > 0 {
  196. w.Write([]byte(res.Msg))
  197. }
  198. }()
  199. if svr.cfgFile == "" {
  200. res.Code = 400
  201. res.Msg = "frpc has no config file path"
  202. log.Warn("%s", res.Msg)
  203. return
  204. }
  205. content, err := config.GetRenderedConfFromFile(svr.cfgFile)
  206. if err != nil {
  207. res.Code = 400
  208. res.Msg = err.Error()
  209. log.Warn("load frpc config file error: %s", res.Msg)
  210. return
  211. }
  212. rows := strings.Split(string(content), "\n")
  213. newRows := make([]string, 0, len(rows))
  214. for _, row := range rows {
  215. row = strings.TrimSpace(row)
  216. if strings.HasPrefix(row, "token") {
  217. continue
  218. }
  219. newRows = append(newRows, row)
  220. }
  221. res.Msg = strings.Join(newRows, "\n")
  222. }
  223. // PUT api/config
  224. func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
  225. res := GeneralResponse{Code: 200}
  226. log.Info("Http put request [/api/config]")
  227. defer func() {
  228. log.Info("Http put response [/api/config], code [%d]", res.Code)
  229. w.WriteHeader(res.Code)
  230. if len(res.Msg) > 0 {
  231. w.Write([]byte(res.Msg))
  232. }
  233. }()
  234. // get new config content
  235. body, err := io.ReadAll(r.Body)
  236. if err != nil {
  237. res.Code = 400
  238. res.Msg = fmt.Sprintf("read request body error: %v", err)
  239. log.Warn("%s", res.Msg)
  240. return
  241. }
  242. if len(body) == 0 {
  243. res.Code = 400
  244. res.Msg = "body can't be empty"
  245. log.Warn("%s", res.Msg)
  246. return
  247. }
  248. // get token from origin content
  249. token := ""
  250. b, err := os.ReadFile(svr.cfgFile)
  251. if err != nil {
  252. res.Code = 400
  253. res.Msg = err.Error()
  254. log.Warn("load frpc config file error: %s", res.Msg)
  255. return
  256. }
  257. content := string(b)
  258. for _, row := range strings.Split(content, "\n") {
  259. row = strings.TrimSpace(row)
  260. if strings.HasPrefix(row, "token") {
  261. token = row
  262. break
  263. }
  264. }
  265. tmpRows := make([]string, 0)
  266. for _, row := range strings.Split(string(body), "\n") {
  267. row = strings.TrimSpace(row)
  268. if strings.HasPrefix(row, "token") {
  269. continue
  270. }
  271. tmpRows = append(tmpRows, row)
  272. }
  273. newRows := make([]string, 0)
  274. if token != "" {
  275. for _, row := range tmpRows {
  276. newRows = append(newRows, row)
  277. if strings.HasPrefix(row, "[common]") {
  278. newRows = append(newRows, token)
  279. }
  280. }
  281. } else {
  282. newRows = tmpRows
  283. }
  284. content = strings.Join(newRows, "\n")
  285. err = os.WriteFile(svr.cfgFile, []byte(content), 0644)
  286. if err != nil {
  287. res.Code = 500
  288. res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)
  289. log.Warn("%s", res.Msg)
  290. return
  291. }
  292. }