dashboard_api.go 5.6 KB

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