dashboard_api.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/julienschmidt/httprouter"
  22. )
  23. type GeneralResponse struct {
  24. Code int64 `json:"code"`
  25. Msg string `json:"msg"`
  26. }
  27. // api/serverinfo
  28. type ServerInfoResp struct {
  29. GeneralResponse
  30. VhostHttpPort int64 `json:"vhost_http_port"`
  31. VhostHttpsPort int64 `json:"vhost_https_port"`
  32. AuthTimeout int64 `json:"auth_timeout"`
  33. SubdomainHost string `json:"subdomain_host"`
  34. MaxPoolCount int64 `json:"max_pool_count"`
  35. HeartBeatTimeout int64 `json:"heart_beat_timeout"`
  36. TotalTrafficIn int64 `json:"total_traffic_in"`
  37. TotalTrafficOut int64 `json:"total_traffic_out"`
  38. CurConns int64 `json:"cur_conns"`
  39. ClientCounts int64 `json:"client_counts"`
  40. ProxyTypeCounts map[string]int64 `json:"proxy_type_count"`
  41. }
  42. func apiServerInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  43. var (
  44. buf []byte
  45. res ServerInfoResp
  46. )
  47. defer func() {
  48. log.Info("Http response [/api/serverinfo]: code [%d]", res.Code)
  49. }()
  50. log.Info("Http request: [/api/serverinfo]")
  51. cfg := config.ServerCommonCfg
  52. serverStats := StatsGetServer()
  53. res = ServerInfoResp{
  54. VhostHttpPort: cfg.VhostHttpPort,
  55. VhostHttpsPort: cfg.VhostHttpsPort,
  56. AuthTimeout: cfg.AuthTimeout,
  57. SubdomainHost: cfg.SubDomainHost,
  58. MaxPoolCount: cfg.MaxPoolCount,
  59. HeartBeatTimeout: cfg.HeartBeatTimeout,
  60. TotalTrafficIn: serverStats.TotalTrafficIn,
  61. TotalTrafficOut: serverStats.TotalTrafficOut,
  62. CurConns: serverStats.CurConns,
  63. ClientCounts: serverStats.ClientCounts,
  64. ProxyTypeCounts: serverStats.ProxyTypeCounts,
  65. }
  66. buf, _ = json.Marshal(&res)
  67. w.Write(buf)
  68. }
  69. // Get proxy info.
  70. type ProxyStatsInfo struct {
  71. Name string `json:"name"`
  72. Conf config.ProxyConf `json:"conf"`
  73. TodayTrafficIn int64 `json:"today_traffic_in"`
  74. TodayTrafficOut int64 `json:"today_traffic_out"`
  75. CurConns int64 `json:"cur_conns"`
  76. Status string `json:"status"`
  77. }
  78. type GetProxyInfoResp struct {
  79. GeneralResponse
  80. Proxies []*ProxyStatsInfo `json:"proxies"`
  81. }
  82. // api/proxy/tcp
  83. func apiProxyTcp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  84. var (
  85. buf []byte
  86. res GetProxyInfoResp
  87. )
  88. defer func() {
  89. log.Info("Http response [/api/proxy/tcp]: code [%d]", res.Code)
  90. }()
  91. log.Info("Http request: [/api/proxy/tcp]")
  92. res.Proxies = getProxyStatsByType(consts.TcpProxy)
  93. buf, _ = json.Marshal(&res)
  94. w.Write(buf)
  95. }
  96. // api/proxy/udp
  97. func apiProxyUdp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  98. var (
  99. buf []byte
  100. res GetProxyInfoResp
  101. )
  102. defer func() {
  103. log.Info("Http response [/api/proxy/udp]: code [%d]", res.Code)
  104. }()
  105. log.Info("Http request: [/api/proxy/udp]")
  106. res.Proxies = getProxyStatsByType(consts.UdpProxy)
  107. buf, _ = json.Marshal(&res)
  108. w.Write(buf)
  109. }
  110. // api/proxy/http
  111. func apiProxyHttp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  112. var (
  113. buf []byte
  114. res GetProxyInfoResp
  115. )
  116. defer func() {
  117. log.Info("Http response [/api/proxy/http]: code [%d]", res.Code)
  118. }()
  119. log.Info("Http request: [/api/proxy/http]")
  120. res.Proxies = getProxyStatsByType(consts.HttpProxy)
  121. buf, _ = json.Marshal(&res)
  122. w.Write(buf)
  123. }
  124. // api/proxy/https
  125. func apiProxyHttps(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  126. var (
  127. buf []byte
  128. res GetProxyInfoResp
  129. )
  130. defer func() {
  131. log.Info("Http response [/api/proxy/https]: code [%d]", res.Code)
  132. }()
  133. log.Info("Http request: [/api/proxy/https]")
  134. res.Proxies = getProxyStatsByType(consts.HttpsProxy)
  135. buf, _ = json.Marshal(&res)
  136. w.Write(buf)
  137. }
  138. func getProxyStatsByType(proxyType string) (proxyInfos []*ProxyStatsInfo) {
  139. proxyStats := StatsGetProxiesByType(proxyType)
  140. proxyInfos = make([]*ProxyStatsInfo, 0, len(proxyStats))
  141. for _, ps := range proxyStats {
  142. proxyInfo := &ProxyStatsInfo{}
  143. if pxy, ok := ServerService.pxyManager.GetByName(ps.Name); ok {
  144. proxyInfo.Conf = pxy.GetConf()
  145. proxyInfo.Status = consts.Online
  146. } else {
  147. proxyInfo.Status = consts.Offline
  148. }
  149. proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
  150. proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
  151. proxyInfo.CurConns = ps.CurConns
  152. proxyInfo.Name = ps.Name
  153. proxyInfos = append(proxyInfos, proxyInfo)
  154. }
  155. return
  156. }
  157. // api/proxy/traffic/:name
  158. type GetProxyTrafficResp struct {
  159. GeneralResponse
  160. Name string `json:"name"`
  161. TrafficIn []int64 `json:"traffic_in"`
  162. TrafficOut []int64 `json:"traffic_out"`
  163. }
  164. func apiProxyTraffic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  165. var (
  166. buf []byte
  167. res GetProxyTrafficResp
  168. )
  169. name := params.ByName("name")
  170. defer func() {
  171. log.Info("Http response [/api/proxy/traffic/:name]: code [%d]", res.Code)
  172. }()
  173. log.Info("Http request: [/api/proxy/traffic/:name]")
  174. res.Name = name
  175. proxyTrafficInfo := StatsGetProxyTraffic(name)
  176. if proxyTrafficInfo == nil {
  177. res.Code = 1
  178. res.Msg = "no proxy info found"
  179. } else {
  180. res.TrafficIn = proxyTrafficInfo.TrafficIn
  181. res.TrafficOut = proxyTrafficInfo.TrafficOut
  182. }
  183. buf, _ = json.Marshal(&res)
  184. w.Write(buf)
  185. }