admin_api.go 8.3 KB

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