admin_api.go 8.3 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/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. }
  80. type ProxyStatusResp struct {
  81. Name string `json:"name"`
  82. Type string `json:"type"`
  83. Status string `json:"status"`
  84. Err string `json:"err"`
  85. LocalAddr string `json:"local_addr"`
  86. Plugin string `json:"plugin"`
  87. RemoteAddr string `json:"remote_addr"`
  88. }
  89. type ByProxyStatusResp []ProxyStatusResp
  90. func (a ByProxyStatusResp) Len() int { return len(a) }
  91. func (a ByProxyStatusResp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  92. func (a ByProxyStatusResp) Less(i, j int) bool { return strings.Compare(a[i].Name, a[j].Name) < 0 }
  93. func NewProxyStatusResp(status *proxy.ProxyStatus, serverAddr string) ProxyStatusResp {
  94. psr := ProxyStatusResp{
  95. Name: status.Name,
  96. Type: status.Type,
  97. Status: status.Status,
  98. Err: status.Err,
  99. }
  100. switch cfg := status.Cfg.(type) {
  101. case *config.TcpProxyConf:
  102. if cfg.LocalPort != 0 {
  103. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
  104. }
  105. psr.Plugin = cfg.Plugin
  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.UdpProxyConf:
  112. if cfg.LocalPort != 0 {
  113. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
  114. }
  115. if status.Err != "" {
  116. psr.RemoteAddr = fmt.Sprintf("%s:%d", serverAddr, cfg.RemotePort)
  117. } else {
  118. psr.RemoteAddr = serverAddr + status.RemoteAddr
  119. }
  120. case *config.HttpProxyConf:
  121. if cfg.LocalPort != 0 {
  122. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
  123. }
  124. psr.Plugin = cfg.Plugin
  125. psr.RemoteAddr = status.RemoteAddr
  126. case *config.HttpsProxyConf:
  127. if cfg.LocalPort != 0 {
  128. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
  129. }
  130. psr.Plugin = cfg.Plugin
  131. psr.RemoteAddr = status.RemoteAddr
  132. case *config.StcpProxyConf:
  133. if cfg.LocalPort != 0 {
  134. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
  135. }
  136. psr.Plugin = cfg.Plugin
  137. case *config.XtcpProxyConf:
  138. if cfg.LocalPort != 0 {
  139. psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
  140. }
  141. psr.Plugin = cfg.Plugin
  142. }
  143. return psr
  144. }
  145. // GET api/status
  146. func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) {
  147. var (
  148. buf []byte
  149. res StatusResp
  150. )
  151. res.Tcp = make([]ProxyStatusResp, 0)
  152. res.Udp = make([]ProxyStatusResp, 0)
  153. res.Http = make([]ProxyStatusResp, 0)
  154. res.Https = make([]ProxyStatusResp, 0)
  155. res.Stcp = make([]ProxyStatusResp, 0)
  156. res.Xtcp = make([]ProxyStatusResp, 0)
  157. log.Info("Http request [/api/status]")
  158. defer func() {
  159. log.Info("Http response [/api/status]")
  160. buf, _ = json.Marshal(&res)
  161. w.Write(buf)
  162. }()
  163. ps := svr.ctl.pm.GetAllProxyStatus()
  164. for _, status := range ps {
  165. switch status.Type {
  166. case "tcp":
  167. res.Tcp = append(res.Tcp, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  168. case "udp":
  169. res.Udp = append(res.Udp, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  170. case "http":
  171. res.Http = append(res.Http, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  172. case "https":
  173. res.Https = append(res.Https, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  174. case "stcp":
  175. res.Stcp = append(res.Stcp, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  176. case "xtcp":
  177. res.Xtcp = append(res.Xtcp, NewProxyStatusResp(status, svr.cfg.ServerAddr))
  178. }
  179. }
  180. sort.Sort(ByProxyStatusResp(res.Tcp))
  181. sort.Sort(ByProxyStatusResp(res.Udp))
  182. sort.Sort(ByProxyStatusResp(res.Http))
  183. sort.Sort(ByProxyStatusResp(res.Https))
  184. sort.Sort(ByProxyStatusResp(res.Stcp))
  185. sort.Sort(ByProxyStatusResp(res.Xtcp))
  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(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 := ioutil.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 := ioutil.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 = ioutil.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. }