1
0

admin_api.go 8.7 KB

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