dashboard_api.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 int64 `json:"vhost_http_port"`
  33. VhostHttpsPort int64 `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. Status string `json:"status"`
  80. }
  81. type GetProxyInfoResp struct {
  82. GeneralResponse
  83. Proxies []*ProxyStatsInfo `json:"proxies"`
  84. }
  85. // api/proxy/tcp
  86. func apiProxyTcp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  87. var (
  88. buf []byte
  89. res GetProxyInfoResp
  90. )
  91. defer func() {
  92. log.Info("Http response [/api/proxy/tcp]: code [%d]", res.Code)
  93. }()
  94. log.Info("Http request: [/api/proxy/tcp]")
  95. res.Proxies = getProxyStatsByType(consts.TcpProxy)
  96. buf, _ = json.Marshal(&res)
  97. w.Write(buf)
  98. }
  99. // api/proxy/udp
  100. func apiProxyUdp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  101. var (
  102. buf []byte
  103. res GetProxyInfoResp
  104. )
  105. defer func() {
  106. log.Info("Http response [/api/proxy/udp]: code [%d]", res.Code)
  107. }()
  108. log.Info("Http request: [/api/proxy/udp]")
  109. res.Proxies = getProxyStatsByType(consts.UdpProxy)
  110. buf, _ = json.Marshal(&res)
  111. w.Write(buf)
  112. }
  113. // api/proxy/http
  114. func apiProxyHttp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  115. var (
  116. buf []byte
  117. res GetProxyInfoResp
  118. )
  119. defer func() {
  120. log.Info("Http response [/api/proxy/http]: code [%d]", res.Code)
  121. }()
  122. log.Info("Http request: [/api/proxy/http]")
  123. res.Proxies = getProxyStatsByType(consts.HttpProxy)
  124. buf, _ = json.Marshal(&res)
  125. w.Write(buf)
  126. }
  127. // api/proxy/https
  128. func apiProxyHttps(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  129. var (
  130. buf []byte
  131. res GetProxyInfoResp
  132. )
  133. defer func() {
  134. log.Info("Http response [/api/proxy/https]: code [%d]", res.Code)
  135. }()
  136. log.Info("Http request: [/api/proxy/https]")
  137. res.Proxies = getProxyStatsByType(consts.HttpsProxy)
  138. buf, _ = json.Marshal(&res)
  139. w.Write(buf)
  140. }
  141. func getProxyStatsByType(proxyType string) (proxyInfos []*ProxyStatsInfo) {
  142. proxyStats := StatsGetProxiesByType(proxyType)
  143. proxyInfos = make([]*ProxyStatsInfo, 0, len(proxyStats))
  144. for _, ps := range proxyStats {
  145. proxyInfo := &ProxyStatsInfo{}
  146. if pxy, ok := ServerService.pxyManager.GetByName(ps.Name); ok {
  147. proxyInfo.Conf = pxy.GetConf()
  148. proxyInfo.Status = consts.Online
  149. } else {
  150. proxyInfo.Status = consts.Offline
  151. }
  152. proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
  153. proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
  154. proxyInfo.CurConns = ps.CurConns
  155. proxyInfo.Name = ps.Name
  156. proxyInfos = append(proxyInfos, proxyInfo)
  157. }
  158. return
  159. }
  160. // api/proxy/traffic/:name
  161. type GetProxyTrafficResp struct {
  162. GeneralResponse
  163. Name string `json:"name"`
  164. TrafficIn []int64 `json:"traffic_in"`
  165. TrafficOut []int64 `json:"traffic_out"`
  166. }
  167. func apiProxyTraffic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  168. var (
  169. buf []byte
  170. res GetProxyTrafficResp
  171. )
  172. name := params.ByName("name")
  173. defer func() {
  174. log.Info("Http response [/api/proxy/traffic/:name]: code [%d]", res.Code)
  175. }()
  176. log.Info("Http request: [/api/proxy/traffic/:name]")
  177. res.Name = name
  178. proxyTrafficInfo := StatsGetProxyTraffic(name)
  179. if proxyTrafficInfo == nil {
  180. res.Code = 1
  181. res.Msg = "no proxy info found"
  182. } else {
  183. res.TrafficIn = proxyTrafficInfo.TrafficIn
  184. res.TrafficOut = proxyTrafficInfo.TrafficOut
  185. }
  186. buf, _ = json.Marshal(&res)
  187. w.Write(buf)
  188. }