admin_api.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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/g"
  24. "github.com/fatedier/frp/models/config"
  25. "github.com/fatedier/frp/utils/log"
  26. )
  27. type GeneralResponse struct {
  28. Code int
  29. Msg string
  30. }
  31. // GET api/reload
  32. func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
  33. res := GeneralResponse{Code: 200}
  34. log.Info("Http request [/api/reload]")
  35. defer func() {
  36. log.Info("Http response [/api/reload], code [%d]", res.Code)
  37. w.WriteHeader(res.Code)
  38. if len(res.Msg) > 0 {
  39. w.Write([]byte(res.Msg))
  40. }
  41. }()
  42. content, err := config.GetRenderedConfFromFile(g.GlbClientCfg.CfgFile)
  43. if err != nil {
  44. res.Code = 400
  45. res.Msg = err.Error()
  46. log.Warn("reload frpc config file error: %s", res.Msg)
  47. return
  48. }
  49. newCommonCfg, err := config.UnmarshalClientConfFromIni(nil, content)
  50. if err != nil {
  51. res.Code = 400
  52. res.Msg = err.Error()
  53. log.Warn("reload frpc common section error: %s", res.Msg)
  54. return
  55. }
  56. pxyCfgs, visitorCfgs, err := config.LoadAllConfFromIni(g.GlbClientCfg.User, content, newCommonCfg.Start)
  57. if err != nil {
  58. res.Code = 400
  59. res.Msg = err.Error()
  60. log.Warn("reload frpc proxy config error: %s", res.Msg)
  61. return
  62. }
  63. err = svr.ReloadConf(pxyCfgs, visitorCfgs)
  64. if err != nil {
  65. res.Code = 500
  66. res.Msg = err.Error()
  67. log.Warn("reload frpc proxy config error: %s", res.Msg)
  68. return
  69. }
  70. log.Info("success reload conf")
  71. return
  72. }
  73. type StatusResp struct {
  74. Tcp []ProxyStatusResp `json:"tcp"`
  75. Udp []ProxyStatusResp `json:"udp"`
  76. Http []ProxyStatusResp `json:"http"`
  77. Https []ProxyStatusResp `json:"https"`
  78. Stcp []ProxyStatusResp `json:"stcp"`
  79. Xtcp []ProxyStatusResp `json:"xtcp"`
  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.ProxyStatus) ProxyStatusResp {
  95. psr := ProxyStatusResp{
  96. Name: status.Name,
  97. Type: status.Type,
  98. Status: status.Status,
  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", g.GlbClientCfg.ServerAddr, cfg.RemotePort)
  109. } else {
  110. psr.RemoteAddr = g.GlbClientCfg.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", g.GlbClientCfg.ServerAddr, cfg.RemotePort)
  118. } else {
  119. psr.RemoteAddr = g.GlbClientCfg.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. }
  144. return psr
  145. }
  146. // GET api/status
  147. func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) {
  148. var (
  149. buf []byte
  150. res StatusResp
  151. )
  152. res.Tcp = make([]ProxyStatusResp, 0)
  153. res.Udp = make([]ProxyStatusResp, 0)
  154. res.Http = make([]ProxyStatusResp, 0)
  155. res.Https = make([]ProxyStatusResp, 0)
  156. res.Stcp = make([]ProxyStatusResp, 0)
  157. res.Xtcp = make([]ProxyStatusResp, 0)
  158. log.Info("Http request [/api/status]")
  159. defer func() {
  160. log.Info("Http response [/api/status]")
  161. buf, _ = json.Marshal(&res)
  162. w.Write(buf)
  163. }()
  164. ps := svr.ctl.pm.GetAllProxyStatus()
  165. for _, status := range ps {
  166. switch status.Type {
  167. case "tcp":
  168. res.Tcp = append(res.Tcp, NewProxyStatusResp(status))
  169. case "udp":
  170. res.Udp = append(res.Udp, NewProxyStatusResp(status))
  171. case "http":
  172. res.Http = append(res.Http, NewProxyStatusResp(status))
  173. case "https":
  174. res.Https = append(res.Https, NewProxyStatusResp(status))
  175. case "stcp":
  176. res.Stcp = append(res.Stcp, NewProxyStatusResp(status))
  177. case "xtcp":
  178. res.Xtcp = append(res.Xtcp, NewProxyStatusResp(status))
  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. return
  188. }
  189. // GET api/config
  190. func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) {
  191. res := GeneralResponse{Code: 200}
  192. log.Info("Http get request [/api/config]")
  193. defer func() {
  194. log.Info("Http get response [/api/config], code [%d]", res.Code)
  195. w.WriteHeader(res.Code)
  196. if len(res.Msg) > 0 {
  197. w.Write([]byte(res.Msg))
  198. }
  199. }()
  200. if g.GlbClientCfg.CfgFile == "" {
  201. res.Code = 400
  202. res.Msg = "frpc has no config file path"
  203. log.Warn("%s", res.Msg)
  204. return
  205. }
  206. content, err := config.GetRenderedConfFromFile(g.GlbClientCfg.CfgFile)
  207. if err != nil {
  208. res.Code = 400
  209. res.Msg = err.Error()
  210. log.Warn("load frpc config file error: %s", res.Msg)
  211. return
  212. }
  213. rows := strings.Split(content, "\n")
  214. newRows := make([]string, 0, len(rows))
  215. for _, row := range rows {
  216. row = strings.TrimSpace(row)
  217. if strings.HasPrefix(row, "token") {
  218. continue
  219. }
  220. newRows = append(newRows, row)
  221. }
  222. res.Msg = strings.Join(newRows, "\n")
  223. }
  224. // PUT api/config
  225. func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
  226. res := GeneralResponse{Code: 200}
  227. log.Info("Http put request [/api/config]")
  228. defer func() {
  229. log.Info("Http put response [/api/config], code [%d]", res.Code)
  230. w.WriteHeader(res.Code)
  231. if len(res.Msg) > 0 {
  232. w.Write([]byte(res.Msg))
  233. }
  234. }()
  235. // get new config content
  236. body, err := ioutil.ReadAll(r.Body)
  237. if err != nil {
  238. res.Code = 400
  239. res.Msg = fmt.Sprintf("read request body error: %v", err)
  240. log.Warn("%s", res.Msg)
  241. return
  242. }
  243. if len(body) == 0 {
  244. res.Code = 400
  245. res.Msg = "body can't be empty"
  246. log.Warn("%s", res.Msg)
  247. return
  248. }
  249. // get token from origin content
  250. token := ""
  251. b, err := ioutil.ReadFile(g.GlbClientCfg.CfgFile)
  252. if err != nil {
  253. res.Code = 400
  254. res.Msg = err.Error()
  255. log.Warn("load frpc config file error: %s", res.Msg)
  256. return
  257. }
  258. content := string(b)
  259. for _, row := range strings.Split(content, "\n") {
  260. row = strings.TrimSpace(row)
  261. if strings.HasPrefix(row, "token") {
  262. token = row
  263. break
  264. }
  265. }
  266. tmpRows := make([]string, 0)
  267. for _, row := range strings.Split(string(body), "\n") {
  268. row = strings.TrimSpace(row)
  269. if strings.HasPrefix(row, "token") {
  270. continue
  271. }
  272. tmpRows = append(tmpRows, row)
  273. }
  274. newRows := make([]string, 0)
  275. if token != "" {
  276. for _, row := range tmpRows {
  277. newRows = append(newRows, row)
  278. if strings.HasPrefix(row, "[common]") {
  279. newRows = append(newRows, token)
  280. }
  281. }
  282. }
  283. content = strings.Join(newRows, "\n")
  284. err = ioutil.WriteFile(g.GlbClientCfg.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. }