admin_api.go 8.4 KB

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