admin_api.go 8.4 KB

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