2017-03-22 19:01:25 +01:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
2016-05-18 18:04:19 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-06-03 11:51:45 +02:00
|
|
|
"encoding/json"
|
2016-08-10 14:18:36 +02:00
|
|
|
"net/http"
|
2016-06-03 11:51:45 +02:00
|
|
|
|
2018-04-10 11:46:49 +02:00
|
|
|
"github.com/fatedier/frp/g"
|
2017-03-22 19:01:25 +01:00
|
|
|
"github.com/fatedier/frp/models/config"
|
|
|
|
"github.com/fatedier/frp/models/consts"
|
|
|
|
"github.com/fatedier/frp/utils/log"
|
2017-05-26 06:05:39 +02:00
|
|
|
"github.com/fatedier/frp/utils/version"
|
2017-03-22 19:01:25 +01:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2016-05-18 18:04:19 +02:00
|
|
|
)
|
|
|
|
|
2016-06-03 11:51:45 +02:00
|
|
|
type GeneralResponse struct {
|
|
|
|
Code int64 `json:"code"`
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
}
|
|
|
|
|
2017-03-22 19:01:25 +01:00
|
|
|
// api/serverinfo
|
|
|
|
type ServerInfoResp struct {
|
|
|
|
GeneralResponse
|
|
|
|
|
2017-05-26 06:05:39 +02:00
|
|
|
Version string `json:"version"`
|
2018-01-17 14:49:37 +01:00
|
|
|
VhostHttpPort int `json:"vhost_http_port"`
|
|
|
|
VhostHttpsPort int `json:"vhost_https_port"`
|
2017-03-22 19:01:25 +01:00
|
|
|
AuthTimeout int64 `json:"auth_timeout"`
|
|
|
|
SubdomainHost string `json:"subdomain_host"`
|
|
|
|
MaxPoolCount int64 `json:"max_pool_count"`
|
|
|
|
HeartBeatTimeout int64 `json:"heart_beat_timeout"`
|
|
|
|
|
2017-03-26 19:39:05 +02:00
|
|
|
TotalTrafficIn int64 `json:"total_traffic_in"`
|
|
|
|
TotalTrafficOut int64 `json:"total_traffic_out"`
|
2017-03-22 19:01:25 +01:00
|
|
|
CurConns int64 `json:"cur_conns"`
|
|
|
|
ClientCounts int64 `json:"client_counts"`
|
|
|
|
ProxyTypeCounts map[string]int64 `json:"proxy_type_count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func apiServerInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res ServerInfoResp
|
|
|
|
)
|
2016-06-03 11:51:45 +02:00
|
|
|
defer func() {
|
2017-03-22 19:01:25 +01:00
|
|
|
log.Info("Http response [/api/serverinfo]: code [%d]", res.Code)
|
2016-06-03 11:51:45 +02:00
|
|
|
}()
|
|
|
|
|
2017-03-22 19:01:25 +01:00
|
|
|
log.Info("Http request: [/api/serverinfo]")
|
2018-04-10 11:46:49 +02:00
|
|
|
cfg := &g.GlbServerCfg.ServerCommonConf
|
2017-03-22 19:01:25 +01:00
|
|
|
serverStats := StatsGetServer()
|
|
|
|
res = ServerInfoResp{
|
2017-05-26 06:05:39 +02:00
|
|
|
Version: version.Full(),
|
2017-03-22 19:01:25 +01:00
|
|
|
VhostHttpPort: cfg.VhostHttpPort,
|
|
|
|
VhostHttpsPort: cfg.VhostHttpsPort,
|
|
|
|
AuthTimeout: cfg.AuthTimeout,
|
|
|
|
SubdomainHost: cfg.SubDomainHost,
|
|
|
|
MaxPoolCount: cfg.MaxPoolCount,
|
|
|
|
HeartBeatTimeout: cfg.HeartBeatTimeout,
|
|
|
|
|
2017-03-26 19:39:05 +02:00
|
|
|
TotalTrafficIn: serverStats.TotalTrafficIn,
|
|
|
|
TotalTrafficOut: serverStats.TotalTrafficOut,
|
2017-03-22 19:01:25 +01:00
|
|
|
CurConns: serverStats.CurConns,
|
|
|
|
ClientCounts: serverStats.ClientCounts,
|
|
|
|
ProxyTypeCounts: serverStats.ProxyTypeCounts,
|
2016-06-03 11:51:45 +02:00
|
|
|
}
|
2016-08-10 14:18:36 +02:00
|
|
|
|
2017-03-22 19:01:25 +01:00
|
|
|
buf, _ = json.Marshal(&res)
|
2016-08-10 14:18:36 +02:00
|
|
|
w.Write(buf)
|
2016-05-18 18:04:19 +02:00
|
|
|
}
|
2016-07-17 15:42:21 +02:00
|
|
|
|
2017-03-22 19:01:25 +01:00
|
|
|
// Get proxy info.
|
|
|
|
type ProxyStatsInfo struct {
|
2017-03-26 19:39:05 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Conf config.ProxyConf `json:"conf"`
|
|
|
|
TodayTrafficIn int64 `json:"today_traffic_in"`
|
|
|
|
TodayTrafficOut int64 `json:"today_traffic_out"`
|
|
|
|
CurConns int64 `json:"cur_conns"`
|
2017-05-30 20:21:15 +02:00
|
|
|
LastStartTime string `json:"last_start_time"`
|
|
|
|
LastCloseTime string `json:"last_close_time"`
|
2017-03-26 19:39:05 +02:00
|
|
|
Status string `json:"status"`
|
2016-07-17 15:42:21 +02:00
|
|
|
}
|
|
|
|
|
2017-03-22 19:01:25 +01:00
|
|
|
type GetProxyInfoResp struct {
|
|
|
|
GeneralResponse
|
|
|
|
Proxies []*ProxyStatsInfo `json:"proxies"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/tcp
|
|
|
|
func apiProxyTcp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyInfoResp
|
|
|
|
)
|
2016-07-17 15:42:21 +02:00
|
|
|
defer func() {
|
2017-03-22 19:01:25 +01:00
|
|
|
log.Info("Http response [/api/proxy/tcp]: code [%d]", res.Code)
|
2016-07-17 15:42:21 +02:00
|
|
|
}()
|
2017-03-22 19:01:25 +01:00
|
|
|
log.Info("Http request: [/api/proxy/tcp]")
|
|
|
|
|
|
|
|
res.Proxies = getProxyStatsByType(consts.TcpProxy)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/udp
|
|
|
|
func apiProxyUdp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyInfoResp
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/proxy/udp]: code [%d]", res.Code)
|
|
|
|
}()
|
|
|
|
log.Info("Http request: [/api/proxy/udp]")
|
|
|
|
|
|
|
|
res.Proxies = getProxyStatsByType(consts.UdpProxy)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/http
|
|
|
|
func apiProxyHttp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyInfoResp
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/proxy/http]: code [%d]", res.Code)
|
|
|
|
}()
|
|
|
|
log.Info("Http request: [/api/proxy/http]")
|
|
|
|
|
|
|
|
res.Proxies = getProxyStatsByType(consts.HttpProxy)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/https
|
|
|
|
func apiProxyHttps(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyInfoResp
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/proxy/https]: code [%d]", res.Code)
|
|
|
|
}()
|
|
|
|
log.Info("Http request: [/api/proxy/https]")
|
|
|
|
|
|
|
|
res.Proxies = getProxyStatsByType(consts.HttpsProxy)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProxyStatsByType(proxyType string) (proxyInfos []*ProxyStatsInfo) {
|
|
|
|
proxyStats := StatsGetProxiesByType(proxyType)
|
|
|
|
proxyInfos = make([]*ProxyStatsInfo, 0, len(proxyStats))
|
|
|
|
for _, ps := range proxyStats {
|
|
|
|
proxyInfo := &ProxyStatsInfo{}
|
|
|
|
if pxy, ok := ServerService.pxyManager.GetByName(ps.Name); ok {
|
|
|
|
proxyInfo.Conf = pxy.GetConf()
|
|
|
|
proxyInfo.Status = consts.Online
|
|
|
|
} else {
|
|
|
|
proxyInfo.Status = consts.Offline
|
|
|
|
}
|
2017-05-30 20:21:15 +02:00
|
|
|
proxyInfo.Name = ps.Name
|
2017-03-26 19:39:05 +02:00
|
|
|
proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
|
|
|
|
proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
|
2017-03-22 19:01:25 +01:00
|
|
|
proxyInfo.CurConns = ps.CurConns
|
2017-05-30 20:21:15 +02:00
|
|
|
proxyInfo.LastStartTime = ps.LastStartTime
|
|
|
|
proxyInfo.LastCloseTime = ps.LastCloseTime
|
2017-03-22 19:01:25 +01:00
|
|
|
proxyInfos = append(proxyInfos, proxyInfo)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-04 08:37:20 +02:00
|
|
|
// Get proxy info by name.
|
|
|
|
type GetProxyStatsResp struct {
|
|
|
|
GeneralResponse
|
|
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
Conf config.ProxyConf `json:"conf"`
|
|
|
|
TodayTrafficIn int64 `json:"today_traffic_in"`
|
|
|
|
TodayTrafficOut int64 `json:"today_traffic_out"`
|
|
|
|
CurConns int64 `json:"cur_conns"`
|
|
|
|
LastStartTime string `json:"last_start_time"`
|
|
|
|
LastCloseTime string `json:"last_close_time"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/tcp/:name
|
|
|
|
func apiProxyTcpByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyStatsResp
|
|
|
|
)
|
|
|
|
name := params.ByName("name")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/proxy/tcp/:name]: code [%d]", res.Code)
|
|
|
|
}()
|
|
|
|
log.Info("Http request: [/api/proxy/tcp/:name]")
|
|
|
|
|
|
|
|
res = getProxyStatsByTypeAndName(consts.TcpProxy, name)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/udp/:name
|
|
|
|
func apiProxyUdpByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyStatsResp
|
|
|
|
)
|
|
|
|
name := params.ByName("name")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/proxy/udp/:name]: code [%d]", res.Code)
|
|
|
|
}()
|
|
|
|
log.Info("Http request: [/api/proxy/udp/:name]")
|
|
|
|
|
|
|
|
res = getProxyStatsByTypeAndName(consts.UdpProxy, name)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/http/:name
|
|
|
|
func apiProxyHttpByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyStatsResp
|
|
|
|
)
|
|
|
|
name := params.ByName("name")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/proxy/http/:name]: code [%d]", res.Code)
|
|
|
|
}()
|
|
|
|
log.Info("Http request: [/api/proxy/http/:name]")
|
|
|
|
|
|
|
|
res = getProxyStatsByTypeAndName(consts.HttpProxy, name)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// api/proxy/https/:name
|
|
|
|
func apiProxyHttpsByName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GetProxyStatsResp
|
|
|
|
)
|
|
|
|
name := params.ByName("name")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/proxy/https/:name]: code [%d]", res.Code)
|
|
|
|
}()
|
|
|
|
log.Info("Http request: [/api/proxy/https/:name]")
|
|
|
|
|
|
|
|
res = getProxyStatsByTypeAndName(consts.HttpsProxy, name)
|
|
|
|
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProxyStatsByTypeAndName(proxyType string, proxyName string) (proxyInfo GetProxyStatsResp) {
|
|
|
|
proxyInfo.Name = proxyName
|
|
|
|
ps := StatsGetProxiesByTypeAndName(proxyType, proxyName)
|
|
|
|
if ps == nil {
|
|
|
|
proxyInfo.Code = 1
|
|
|
|
proxyInfo.Msg = "no proxy info found"
|
|
|
|
} else {
|
|
|
|
if pxy, ok := ServerService.pxyManager.GetByName(proxyName); ok {
|
|
|
|
proxyInfo.Conf = pxy.GetConf()
|
|
|
|
proxyInfo.Status = consts.Online
|
|
|
|
} else {
|
|
|
|
proxyInfo.Status = consts.Offline
|
|
|
|
}
|
|
|
|
proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
|
|
|
|
proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
|
|
|
|
proxyInfo.CurConns = ps.CurConns
|
|
|
|
proxyInfo.LastStartTime = ps.LastStartTime
|
|
|
|
proxyInfo.LastCloseTime = ps.LastCloseTime
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:39:05 +02:00
|
|
|
// api/proxy/traffic/:name
|
|
|
|
type GetProxyTrafficResp struct {
|
2017-03-22 19:01:25 +01:00
|
|
|
GeneralResponse
|
|
|
|
|
2017-03-26 19:39:05 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
TrafficIn []int64 `json:"traffic_in"`
|
|
|
|
TrafficOut []int64 `json:"traffic_out"`
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-26 19:39:05 +02:00
|
|
|
func apiProxyTraffic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
2017-03-22 19:01:25 +01:00
|
|
|
var (
|
|
|
|
buf []byte
|
2017-03-26 19:39:05 +02:00
|
|
|
res GetProxyTrafficResp
|
2017-03-22 19:01:25 +01:00
|
|
|
)
|
|
|
|
name := params.ByName("name")
|
|
|
|
|
|
|
|
defer func() {
|
2017-03-26 19:39:05 +02:00
|
|
|
log.Info("Http response [/api/proxy/traffic/:name]: code [%d]", res.Code)
|
2017-03-22 19:01:25 +01:00
|
|
|
}()
|
2017-03-26 19:39:05 +02:00
|
|
|
log.Info("Http request: [/api/proxy/traffic/:name]")
|
2017-03-22 19:01:25 +01:00
|
|
|
|
|
|
|
res.Name = name
|
2017-03-26 19:39:05 +02:00
|
|
|
proxyTrafficInfo := StatsGetProxyTraffic(name)
|
|
|
|
if proxyTrafficInfo == nil {
|
2017-03-22 19:01:25 +01:00
|
|
|
res.Code = 1
|
|
|
|
res.Msg = "no proxy info found"
|
|
|
|
} else {
|
2017-03-26 19:39:05 +02:00
|
|
|
res.TrafficIn = proxyTrafficInfo.TrafficIn
|
|
|
|
res.TrafficOut = proxyTrafficInfo.TrafficOut
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
2016-07-17 15:42:21 +02:00
|
|
|
|
2017-03-22 19:01:25 +01:00
|
|
|
buf, _ = json.Marshal(&res)
|
2016-08-10 14:18:36 +02:00
|
|
|
w.Write(buf)
|
2016-07-17 15:42:21 +02:00
|
|
|
}
|