frp/server/dashboard_api.go

324 lines
8.5 KiB
Go
Raw Normal View History

2017-03-22 19:01:25 +01:00
// Copyright 2017 fatedier, fatedier@gmail.com
//
// 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 (
"encoding/json"
"net/http"
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"
"github.com/fatedier/frp/utils/version"
2017-03-22 19:01:25 +01:00
2018-05-20 13:06:05 +02:00
"github.com/gorilla/mux"
)
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
2018-05-11 11:14:16 +02:00
Version string `json:"version"`
BindPort int `json:"bind_port"`
BindUdpPort int `json:"bind_udp_port"`
VhostHttpPort int `json:"vhost_http_port"`
VhostHttpsPort int `json:"vhost_https_port"`
KcpBindPort int `json:"kcp_bind_port"`
AuthTimeout int64 `json:"auth_timeout"`
SubdomainHost string `json:"subdomain_host"`
MaxPoolCount int64 `json:"max_pool_count"`
MaxPortsPerClient int64 `json:"max_ports_per_client"`
HeartBeatTimeout int64 `json:"heart_beat_timeout"`
2017-03-22 19:01:25 +01:00
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"`
}
2018-05-20 13:06:05 +02:00
func apiServerInfo(w http.ResponseWriter, r *http.Request) {
2017-03-22 19:01:25 +01:00
var (
buf []byte
res ServerInfoResp
)
defer func() {
2018-05-20 13:06:05 +02:00
log.Info("Http response [%s]: code [%d]", r.URL.Path, res.Code)
}()
2018-05-20 13:06:05 +02:00
log.Info("Http request: [%s]", r.URL.Path)
2018-04-10 11:46:49 +02:00
cfg := &g.GlbServerCfg.ServerCommonConf
2017-03-22 19:01:25 +01:00
serverStats := StatsGetServer()
res = ServerInfoResp{
2018-05-11 11:14:16 +02:00
Version: version.Full(),
BindPort: cfg.BindPort,
BindUdpPort: cfg.BindUdpPort,
VhostHttpPort: cfg.VhostHttpPort,
VhostHttpsPort: cfg.VhostHttpsPort,
KcpBindPort: cfg.KcpBindPort,
AuthTimeout: cfg.AuthTimeout,
SubdomainHost: cfg.SubDomainHost,
MaxPoolCount: cfg.MaxPoolCount,
MaxPortsPerClient: cfg.MaxPortsPerClient,
HeartBeatTimeout: cfg.HeartBeatTimeout,
2017-03-22 19:01:25 +01:00
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,
}
2017-03-22 19:01:25 +01:00
buf, _ = json.Marshal(&res)
w.Write(buf)
}
2018-05-20 13:06:05 +02:00
type BaseOutConf struct {
config.BaseProxyConf
}
2018-05-20 13:06:05 +02:00
type TcpOutConf struct {
BaseOutConf
RemotePort int `json:"remote_port"`
2017-03-22 19:01:25 +01:00
}
2018-05-20 13:06:05 +02:00
type UdpOutConf struct {
BaseOutConf
RemotePort int `json:"remote_port"`
}
2017-03-22 19:01:25 +01:00
2018-05-20 13:06:05 +02:00
type HttpOutConf struct {
BaseOutConf
config.DomainConf
Locations []string `json:"locations"`
HostHeaderRewrite string `json:"host_header_rewrite"`
2017-03-22 19:01:25 +01:00
}
2018-05-20 13:06:05 +02:00
type HttpsOutConf struct {
BaseOutConf
config.DomainConf
}
2017-03-22 19:01:25 +01:00
2018-05-20 13:06:05 +02:00
type StcpOutConf struct {
BaseOutConf
}
2017-03-22 19:01:25 +01:00
2018-05-20 13:06:05 +02:00
type XtcpOutConf struct {
BaseOutConf
2017-03-22 19:01:25 +01:00
}
2018-05-20 13:06:05 +02:00
func getConfByType(proxyType string) interface{} {
switch proxyType {
case consts.TcpProxy:
return &TcpOutConf{}
case consts.UdpProxy:
return &UdpOutConf{}
case consts.HttpProxy:
return &HttpOutConf{}
case consts.HttpsProxy:
return &HttpsOutConf{}
case consts.StcpProxy:
return &StcpOutConf{}
case consts.XtcpProxy:
return &XtcpOutConf{}
default:
return nil
}
}
2017-03-22 19:01:25 +01:00
2018-05-20 13:06:05 +02:00
// Get proxy info.
type ProxyStatsInfo struct {
Name string `json:"name"`
Conf interface{} `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"`
}
2017-03-22 19:01:25 +01:00
2018-05-20 13:06:05 +02:00
type GetProxyInfoResp struct {
GeneralResponse
Proxies []*ProxyStatsInfo `json:"proxies"`
2017-03-22 19:01:25 +01:00
}
2018-05-20 13:06:05 +02:00
// api/proxy/:type
func apiProxyByType(w http.ResponseWriter, r *http.Request) {
2017-03-22 19:01:25 +01:00
var (
buf []byte
res GetProxyInfoResp
)
2018-05-20 13:06:05 +02:00
params := mux.Vars(r)
proxyType := params["type"]
2017-03-22 19:01:25 +01:00
defer func() {
2018-05-20 13:06:05 +02:00
log.Info("Http response [%s]: code [%d]", r.URL.Path, res.Code)
log.Info(r.URL.Path)
log.Info(r.URL.RawPath)
2017-03-22 19:01:25 +01:00
}()
2018-05-20 13:06:05 +02:00
log.Info("Http request: [%s]", r.URL.Path)
2017-03-22 19:01:25 +01:00
2018-05-20 13:06:05 +02:00
res.Proxies = getProxyStatsByType(proxyType)
2017-03-22 19:01:25 +01:00
buf, _ = json.Marshal(&res)
w.Write(buf)
2018-05-20 13:06:05 +02:00
2017-03-22 19:01:25 +01:00
}
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 {
2018-05-20 13:06:05 +02:00
content, err := json.Marshal(pxy.GetConf())
if err != nil {
log.Warn("marshal proxy [%s] conf info error: %v", ps.Name, err)
continue
}
proxyInfo.Conf = getConfByType(ps.Type)
if err = json.Unmarshal(content, &proxyInfo.Conf); err != nil {
log.Warn("unmarshal proxy [%s] conf info error: %v", ps.Name, err)
continue
}
2017-03-22 19:01:25 +01:00
proxyInfo.Status = consts.Online
} else {
proxyInfo.Status = consts.Offline
}
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
proxyInfo.LastStartTime = ps.LastStartTime
proxyInfo.LastCloseTime = ps.LastCloseTime
2017-03-22 19:01:25 +01:00
proxyInfos = append(proxyInfos, proxyInfo)
}
return
}
// Get proxy info by name.
type GetProxyStatsResp struct {
GeneralResponse
2018-05-20 13:06:05 +02:00
Name string `json:"name"`
Conf interface{} `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"`
}
2018-05-20 13:06:05 +02:00
// api/proxy/:type/:name
func apiProxyByTypeAndName(w http.ResponseWriter, r *http.Request) {
var (
buf []byte
res GetProxyStatsResp
)
2018-05-20 13:06:05 +02:00
params := mux.Vars(r)
proxyType := params["type"]
name := params["name"]
defer func() {
2018-05-20 13:06:05 +02:00
log.Info("Http response [%s]: code [%d]", r.URL.Path, res.Code)
}()
2018-05-20 13:06:05 +02:00
log.Info("Http request: [%s]", r.URL.Path)
2018-05-20 13:06:05 +02:00
res = getProxyStatsByTypeAndName(proxyType, 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 {
2018-05-20 13:06:05 +02:00
content, err := json.Marshal(pxy.GetConf())
if err != nil {
log.Warn("marshal proxy [%s] conf info error: %v", ps.Name, err)
proxyInfo.Code = 2
proxyInfo.Msg = "parse conf error"
return
}
proxyInfo.Conf = getConfByType(ps.Type)
if err = json.Unmarshal(content, &proxyInfo.Conf); err != nil {
log.Warn("unmarshal proxy [%s] conf info error: %v", ps.Name, err)
proxyInfo.Code = 2
proxyInfo.Msg = "parse conf error"
return
}
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
}
2018-05-20 13:06:05 +02:00
// api/traffic/:name
2017-03-26 19:39:05 +02:00
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
}
2018-05-20 13:06:05 +02:00
func apiProxyTraffic(w http.ResponseWriter, r *http.Request) {
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
)
2018-05-20 13:06:05 +02:00
params := mux.Vars(r)
name := params["name"]
2017-03-22 19:01:25 +01:00
defer func() {
2018-05-20 13:06:05 +02:00
log.Info("Http response [%s]: code [%d]", r.URL.Path, res.Code)
2017-03-22 19:01:25 +01:00
}()
2018-05-20 13:06:05 +02:00
log.Info("Http request: [%s]", r.URL.Path)
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
}
2017-03-22 19:01:25 +01:00
buf, _ = json.Marshal(&res)
w.Write(buf)
}