dashboard_api.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // api/proxy/traffic/:name
  165. type GetProxyTrafficResp struct {
  166. GeneralResponse
  167. Name string `json:"name"`
  168. TrafficIn []int64 `json:"traffic_in"`
  169. TrafficOut []int64 `json:"traffic_out"`
  170. }
  171. func apiProxyTraffic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  172. var (
  173. buf []byte
  174. res GetProxyTrafficResp
  175. )
  176. name := params.ByName("name")
  177. defer func() {
  178. log.Info("Http response [/api/proxy/traffic/:name]: code [%d]", res.Code)
  179. }()
  180. log.Info("Http request: [/api/proxy/traffic/:name]")
  181. res.Name = name
  182. proxyTrafficInfo := StatsGetProxyTraffic(name)
  183. if proxyTrafficInfo == nil {
  184. res.Code = 1
  185. res.Msg = "no proxy info found"
  186. } else {
  187. res.TrafficIn = proxyTrafficInfo.TrafficIn
  188. res.TrafficOut = proxyTrafficInfo.TrafficOut
  189. }
  190. buf, _ = json.Marshal(&res)
  191. w.Write(buf)
  192. }