1
0

dashboard_api.go 9.0 KB

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