dashboard_api.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 server
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "github.com/fatedier/frp/g"
  19. "github.com/fatedier/frp/models/config"
  20. "github.com/fatedier/frp/models/consts"
  21. "github.com/fatedier/frp/utils/log"
  22. "github.com/fatedier/frp/utils/version"
  23. "github.com/julienschmidt/httprouter"
  24. )
  25. type GeneralResponse struct {
  26. Code int64 `json:"code"`
  27. Msg string `json:"msg"`
  28. }
  29. // api/serverinfo
  30. type ServerInfoResp struct {
  31. GeneralResponse
  32. Version string `json:"version"`
  33. VhostHttpPort int `json:"vhost_http_port"`
  34. VhostHttpsPort int `json:"vhost_https_port"`
  35. AuthTimeout int64 `json:"auth_timeout"`
  36. SubdomainHost string `json:"subdomain_host"`
  37. MaxPoolCount int64 `json:"max_pool_count"`
  38. HeartBeatTimeout int64 `json:"heart_beat_timeout"`
  39. TotalTrafficIn int64 `json:"total_traffic_in"`
  40. TotalTrafficOut int64 `json:"total_traffic_out"`
  41. CurConns int64 `json:"cur_conns"`
  42. ClientCounts int64 `json:"client_counts"`
  43. ProxyTypeCounts map[string]int64 `json:"proxy_type_count"`
  44. }
  45. func apiServerInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  46. var (
  47. buf []byte
  48. res ServerInfoResp
  49. )
  50. defer func() {
  51. log.Info("Http response [/api/serverinfo]: code [%d]", res.Code)
  52. }()
  53. log.Info("Http request: [/api/serverinfo]")
  54. cfg := &g.GlbServerCfg.ServerCommonConf
  55. serverStats := StatsGetServer()
  56. res = ServerInfoResp{
  57. Version: version.Full(),
  58. VhostHttpPort: cfg.VhostHttpPort,
  59. VhostHttpsPort: cfg.VhostHttpsPort,
  60. AuthTimeout: cfg.AuthTimeout,
  61. SubdomainHost: cfg.SubDomainHost,
  62. MaxPoolCount: cfg.MaxPoolCount,
  63. HeartBeatTimeout: cfg.HeartBeatTimeout,
  64. TotalTrafficIn: serverStats.TotalTrafficIn,
  65. TotalTrafficOut: serverStats.TotalTrafficOut,
  66. CurConns: serverStats.CurConns,
  67. ClientCounts: serverStats.ClientCounts,
  68. ProxyTypeCounts: serverStats.ProxyTypeCounts,
  69. }
  70. buf, _ = json.Marshal(&res)
  71. w.Write(buf)
  72. }
  73. // Get proxy info.
  74. type ProxyStatsInfo struct {
  75. Name string `json:"name"`
  76. Conf config.ProxyConf `json:"conf"`
  77. TodayTrafficIn int64 `json:"today_traffic_in"`
  78. TodayTrafficOut int64 `json:"today_traffic_out"`
  79. CurConns int64 `json:"cur_conns"`
  80. LastStartTime string `json:"last_start_time"`
  81. LastCloseTime string `json:"last_close_time"`
  82. Status string `json:"status"`
  83. }
  84. type GetProxyInfoResp struct {
  85. GeneralResponse
  86. Proxies []*ProxyStatsInfo `json:"proxies"`
  87. }
  88. // api/proxy/tcp
  89. func apiProxyTcp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  90. var (
  91. buf []byte
  92. res GetProxyInfoResp
  93. )
  94. defer func() {
  95. log.Info("Http response [/api/proxy/tcp]: code [%d]", res.Code)
  96. }()
  97. log.Info("Http request: [/api/proxy/tcp]")
  98. res.Proxies = getProxyStatsByType(consts.TcpProxy)
  99. buf, _ = json.Marshal(&res)
  100. w.Write(buf)
  101. }
  102. // api/proxy/udp
  103. func apiProxyUdp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  104. var (
  105. buf []byte
  106. res GetProxyInfoResp
  107. )
  108. defer func() {
  109. log.Info("Http response [/api/proxy/udp]: code [%d]", res.Code)
  110. }()
  111. log.Info("Http request: [/api/proxy/udp]")
  112. res.Proxies = getProxyStatsByType(consts.UdpProxy)
  113. buf, _ = json.Marshal(&res)
  114. w.Write(buf)
  115. }
  116. // api/proxy/http
  117. func apiProxyHttp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  118. var (
  119. buf []byte
  120. res GetProxyInfoResp
  121. )
  122. defer func() {
  123. log.Info("Http response [/api/proxy/http]: code [%d]", res.Code)
  124. }()
  125. log.Info("Http request: [/api/proxy/http]")
  126. res.Proxies = getProxyStatsByType(consts.HttpProxy)
  127. buf, _ = json.Marshal(&res)
  128. w.Write(buf)
  129. }
  130. // api/proxy/https
  131. func apiProxyHttps(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  132. var (
  133. buf []byte
  134. res GetProxyInfoResp
  135. )
  136. defer func() {
  137. log.Info("Http response [/api/proxy/https]: code [%d]", res.Code)
  138. }()
  139. log.Info("Http request: [/api/proxy/https]")
  140. res.Proxies = getProxyStatsByType(consts.HttpsProxy)
  141. buf, _ = json.Marshal(&res)
  142. w.Write(buf)
  143. }
  144. func getProxyStatsByType(proxyType string) (proxyInfos []*ProxyStatsInfo) {
  145. proxyStats := StatsGetProxiesByType(proxyType)
  146. proxyInfos = make([]*ProxyStatsInfo, 0, len(proxyStats))
  147. for _, ps := range proxyStats {
  148. proxyInfo := &ProxyStatsInfo{}
  149. if pxy, ok := ServerService.pxyManager.GetByName(ps.Name); ok {
  150. proxyInfo.Conf = pxy.GetConf()
  151. proxyInfo.Status = consts.Online
  152. } else {
  153. proxyInfo.Status = consts.Offline
  154. }
  155. proxyInfo.Name = ps.Name
  156. proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
  157. proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
  158. proxyInfo.CurConns = ps.CurConns
  159. proxyInfo.LastStartTime = ps.LastStartTime
  160. proxyInfo.LastCloseTime = ps.LastCloseTime
  161. proxyInfos = append(proxyInfos, proxyInfo)
  162. }
  163. return
  164. }
  165. // Get proxy info by name.
  166. type GetProxyStatsResp struct {
  167. GeneralResponse
  168. Name string `json:"name"`
  169. Conf config.ProxyConf `json:"conf"`
  170. TodayTrafficIn int64 `json:"today_traffic_in"`
  171. TodayTrafficOut int64 `json:"today_traffic_out"`
  172. CurConns int64 `json:"cur_conns"`
  173. LastStartTime string `json:"last_start_time"`
  174. LastCloseTime string `json:"last_close_time"`
  175. Status string `json:"status"`
  176. }
  177. // api/proxy/tcp/:name
  178. func apiProxyTcpByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  179. var (
  180. buf []byte
  181. res GetProxyStatsResp
  182. )
  183. name := params.ByName("name")
  184. defer func() {
  185. log.Info("Http response [/api/proxy/tcp/:name]: code [%d]", res.Code)
  186. }()
  187. log.Info("Http request: [/api/proxy/tcp/:name]")
  188. res = getProxyStatsByTypeAndName(consts.TcpProxy, name)
  189. buf, _ = json.Marshal(&res)
  190. w.Write(buf)
  191. }
  192. // api/proxy/udp/:name
  193. func apiProxyUdpByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  194. var (
  195. buf []byte
  196. res GetProxyStatsResp
  197. )
  198. name := params.ByName("name")
  199. defer func() {
  200. log.Info("Http response [/api/proxy/udp/:name]: code [%d]", res.Code)
  201. }()
  202. log.Info("Http request: [/api/proxy/udp/:name]")
  203. res = getProxyStatsByTypeAndName(consts.UdpProxy, name)
  204. buf, _ = json.Marshal(&res)
  205. w.Write(buf)
  206. }
  207. // api/proxy/http/:name
  208. func apiProxyHttpByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  209. var (
  210. buf []byte
  211. res GetProxyStatsResp
  212. )
  213. name := params.ByName("name")
  214. defer func() {
  215. log.Info("Http response [/api/proxy/http/:name]: code [%d]", res.Code)
  216. }()
  217. log.Info("Http request: [/api/proxy/http/:name]")
  218. res = getProxyStatsByTypeAndName(consts.HttpProxy, name)
  219. buf, _ = json.Marshal(&res)
  220. w.Write(buf)
  221. }
  222. // api/proxy/https/:name
  223. func apiProxyHttpsByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  224. var (
  225. buf []byte
  226. res GetProxyStatsResp
  227. )
  228. name := params.ByName("name")
  229. defer func() {
  230. log.Info("Http response [/api/proxy/https/:name]: code [%d]", res.Code)
  231. }()
  232. log.Info("Http request: [/api/proxy/https/:name]")
  233. res = getProxyStatsByTypeAndName(consts.HttpsProxy, name)
  234. buf, _ = json.Marshal(&res)
  235. w.Write(buf)
  236. }
  237. func getProxyStatsByTypeAndName(proxyType string, proxyName string) (proxyInfo GetProxyStatsResp) {
  238. proxyInfo.Name = proxyName
  239. ps := StatsGetProxiesByTypeAndName(proxyType, proxyName)
  240. if ps == nil {
  241. proxyInfo.Code = 1
  242. proxyInfo.Msg = "no proxy info found"
  243. } else {
  244. if pxy, ok := ServerService.pxyManager.GetByName(proxyName); ok {
  245. proxyInfo.Conf = pxy.GetConf()
  246. proxyInfo.Status = consts.Online
  247. } else {
  248. proxyInfo.Status = consts.Offline
  249. }
  250. proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
  251. proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
  252. proxyInfo.CurConns = ps.CurConns
  253. proxyInfo.LastStartTime = ps.LastStartTime
  254. proxyInfo.LastCloseTime = ps.LastCloseTime
  255. }
  256. return
  257. }
  258. // api/proxy/traffic/:name
  259. type GetProxyTrafficResp struct {
  260. GeneralResponse
  261. Name string `json:"name"`
  262. TrafficIn []int64 `json:"traffic_in"`
  263. TrafficOut []int64 `json:"traffic_out"`
  264. }
  265. func apiProxyTraffic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  266. var (
  267. buf []byte
  268. res GetProxyTrafficResp
  269. )
  270. name := params.ByName("name")
  271. defer func() {
  272. log.Info("Http response [/api/proxy/traffic/:name]: code [%d]", res.Code)
  273. }()
  274. log.Info("Http request: [/api/proxy/traffic/:name]")
  275. res.Name = name
  276. proxyTrafficInfo := StatsGetProxyTraffic(name)
  277. if proxyTrafficInfo == nil {
  278. res.Code = 1
  279. res.Msg = "no proxy info found"
  280. } else {
  281. res.TrafficIn = proxyTrafficInfo.TrafficIn
  282. res.TrafficOut = proxyTrafficInfo.TrafficOut
  283. }
  284. buf, _ = json.Marshal(&res)
  285. w.Write(buf)
  286. }