1
0

admin_api.go 8.2 KB

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