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
|
|
|
|
2022-08-28 19:02:53 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-11-27 08:47:49 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-08-28 19:02:53 +02:00
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
"github.com/fatedier/frp/pkg/config/types"
|
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/metrics/mem"
|
2023-11-27 08:47:49 +01:00
|
|
|
httppkg "github.com/fatedier/frp/pkg/util/http"
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/util/log"
|
2023-11-27 08:47:49 +01:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/util/version"
|
2016-05-18 18:04:19 +02:00
|
|
|
)
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
// TODO(fatedier): add an API to clean status of all offline proxies.
|
|
|
|
|
2016-06-03 11:51:45 +02:00
|
|
|
type GeneralResponse struct {
|
2019-02-11 04:42:07 +01:00
|
|
|
Code int
|
|
|
|
Msg string
|
2016-06-03 11:51:45 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper) {
|
|
|
|
helper.Router.HandleFunc("/healthz", svr.healthz)
|
|
|
|
subRouter := helper.Router.NewRoute().Subrouter()
|
|
|
|
|
|
|
|
subRouter.Use(helper.AuthMiddleware.Middleware)
|
|
|
|
|
|
|
|
// metrics
|
|
|
|
if svr.cfg.EnablePrometheus {
|
|
|
|
subRouter.Handle("/metrics", promhttp.Handler())
|
|
|
|
}
|
|
|
|
|
|
|
|
// apis
|
|
|
|
subRouter.HandleFunc("/api/serverinfo", svr.apiServerInfo).Methods("GET")
|
|
|
|
subRouter.HandleFunc("/api/proxy/{type}", svr.apiProxyByType).Methods("GET")
|
|
|
|
subRouter.HandleFunc("/api/proxy/{type}/{name}", svr.apiProxyByTypeAndName).Methods("GET")
|
|
|
|
subRouter.HandleFunc("/api/traffic/{name}", svr.apiProxyTraffic).Methods("GET")
|
|
|
|
|
|
|
|
// view
|
|
|
|
subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET")
|
|
|
|
subRouter.PathPrefix("/static/").Handler(
|
|
|
|
netpkg.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(helper.AssetsFS))),
|
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type serverInfoResp struct {
|
2023-02-21 17:39:56 +01:00
|
|
|
Version string `json:"version"`
|
2023-10-11 09:01:07 +02:00
|
|
|
BindPort int `json:"bindPort"`
|
|
|
|
VhostHTTPPort int `json:"vhostHTTPPort"`
|
|
|
|
VhostHTTPSPort int `json:"vhostHTTPSPort"`
|
|
|
|
TCPMuxHTTPConnectPort int `json:"tcpmuxHTTPConnectPort"`
|
|
|
|
KCPBindPort int `json:"kcpBindPort"`
|
|
|
|
QUICBindPort int `json:"quicBindPort"`
|
|
|
|
SubdomainHost string `json:"subdomainHost"`
|
|
|
|
MaxPoolCount int64 `json:"maxPoolCount"`
|
|
|
|
MaxPortsPerClient int64 `json:"maxPortsPerClient"`
|
|
|
|
HeartBeatTimeout int64 `json:"heartbeatTimeout"`
|
|
|
|
AllowPortsStr string `json:"allowPortsStr,omitempty"`
|
|
|
|
TLSForce bool `json:"tlsForce,omitempty"`
|
|
|
|
|
|
|
|
TotalTrafficIn int64 `json:"totalTrafficIn"`
|
|
|
|
TotalTrafficOut int64 `json:"totalTrafficOut"`
|
|
|
|
CurConns int64 `json:"curConns"`
|
|
|
|
ClientCounts int64 `json:"clientCounts"`
|
|
|
|
ProxyTypeCounts map[string]int64 `json:"proxyTypeCount"`
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2021-08-04 08:33:53 +02:00
|
|
|
// /healthz
|
2023-11-27 08:47:49 +01:00
|
|
|
func (svr *Service) healthz(w http.ResponseWriter, _ *http.Request) {
|
2021-08-04 08:33:53 +02:00
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:35:37 +02:00
|
|
|
// /api/serverinfo
|
2023-11-27 08:47:49 +01:00
|
|
|
func (svr *Service) apiServerInfo(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 04:42:07 +01:00
|
|
|
res := GeneralResponse{Code: 200}
|
2016-06-03 11:51:45 +02:00
|
|
|
defer func() {
|
2018-05-20 13:06:05 +02:00
|
|
|
log.Info("Http response [%s]: code [%d]", r.URL.Path, res.Code)
|
2019-02-11 04:42:07 +01:00
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-28 19:02:53 +02:00
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 04:42:07 +01:00
|
|
|
}
|
2016-06-03 11:51:45 +02:00
|
|
|
}()
|
|
|
|
|
2018-05-20 13:06:05 +02:00
|
|
|
log.Info("Http request: [%s]", r.URL.Path)
|
2020-03-11 06:20:26 +01:00
|
|
|
serverStats := mem.StatsCollector.GetServer()
|
2020-05-24 11:48:37 +02:00
|
|
|
svrResp := serverInfoResp{
|
2023-02-21 17:39:56 +01:00
|
|
|
Version: version.Full(),
|
|
|
|
BindPort: svr.cfg.BindPort,
|
|
|
|
VhostHTTPPort: svr.cfg.VhostHTTPPort,
|
|
|
|
VhostHTTPSPort: svr.cfg.VhostHTTPSPort,
|
|
|
|
TCPMuxHTTPConnectPort: svr.cfg.TCPMuxHTTPConnectPort,
|
|
|
|
KCPBindPort: svr.cfg.KCPBindPort,
|
|
|
|
QUICBindPort: svr.cfg.QUICBindPort,
|
|
|
|
SubdomainHost: svr.cfg.SubDomainHost,
|
2023-09-06 04:18:02 +02:00
|
|
|
MaxPoolCount: svr.cfg.Transport.MaxPoolCount,
|
2023-02-21 17:39:56 +01:00
|
|
|
MaxPortsPerClient: svr.cfg.MaxPortsPerClient,
|
2023-09-06 04:18:02 +02:00
|
|
|
HeartBeatTimeout: svr.cfg.Transport.HeartbeatTimeout,
|
|
|
|
AllowPortsStr: types.PortsRangeSlice(svr.cfg.AllowPorts).String(),
|
2023-10-11 09:01:07 +02:00
|
|
|
TLSForce: svr.cfg.Transport.TLS.Force,
|
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,
|
2016-06-03 11:51:45 +02:00
|
|
|
}
|
2016-08-10 14:18:36 +02:00
|
|
|
|
2019-02-11 04:42:07 +01:00
|
|
|
buf, _ := json.Marshal(&svrResp)
|
|
|
|
res.Msg = string(buf)
|
2016-05-18 18:04:19 +02:00
|
|
|
}
|
2016-07-17 15:42:21 +02:00
|
|
|
|
2018-05-20 13:06:05 +02:00
|
|
|
type BaseOutConf struct {
|
2023-09-06 04:18:02 +02:00
|
|
|
v1.ProxyBaseConfig
|
2016-07-17 15:42:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type TCPOutConf struct {
|
2018-05-20 13:06:05 +02:00
|
|
|
BaseOutConf
|
2023-10-11 09:01:07 +02:00
|
|
|
RemotePort int `json:"remotePort"`
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type TCPMuxOutConf struct {
|
2020-03-05 14:47:49 +01:00
|
|
|
BaseOutConf
|
2023-09-06 04:18:02 +02:00
|
|
|
v1.DomainConfig
|
2020-03-05 14:47:49 +01:00
|
|
|
Multiplexer string `json:"multiplexer"`
|
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type UDPOutConf struct {
|
2018-05-20 13:06:05 +02:00
|
|
|
BaseOutConf
|
2023-10-11 09:01:07 +02:00
|
|
|
RemotePort int `json:"remotePort"`
|
2018-05-20 13:06:05 +02:00
|
|
|
}
|
2017-03-22 19:01:25 +01:00
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type HTTPOutConf struct {
|
2018-05-20 13:06:05 +02:00
|
|
|
BaseOutConf
|
2023-09-06 04:18:02 +02:00
|
|
|
v1.DomainConfig
|
2018-05-20 13:06:05 +02:00
|
|
|
Locations []string `json:"locations"`
|
2023-10-11 09:01:07 +02:00
|
|
|
HostHeaderRewrite string `json:"hostHeaderRewrite"`
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type HTTPSOutConf struct {
|
2018-05-20 13:06:05 +02:00
|
|
|
BaseOutConf
|
2023-09-06 04:18:02 +02:00
|
|
|
v1.DomainConfig
|
2018-05-20 13:06:05 +02:00
|
|
|
}
|
2017-03-22 19:01:25 +01:00
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type STCPOutConf struct {
|
2018-05-20 13:06:05 +02:00
|
|
|
BaseOutConf
|
|
|
|
}
|
2017-03-22 19:01:25 +01:00
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type XTCPOutConf struct {
|
2018-05-20 13:06:05 +02:00
|
|
|
BaseOutConf
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2023-09-20 09:18:50 +02:00
|
|
|
func getConfByType(proxyType string) any {
|
|
|
|
switch v1.ProxyType(proxyType) {
|
|
|
|
case v1.ProxyTypeTCP:
|
2020-05-24 11:48:37 +02:00
|
|
|
return &TCPOutConf{}
|
2023-09-20 09:18:50 +02:00
|
|
|
case v1.ProxyTypeTCPMUX:
|
2020-05-24 11:48:37 +02:00
|
|
|
return &TCPMuxOutConf{}
|
2023-09-20 09:18:50 +02:00
|
|
|
case v1.ProxyTypeUDP:
|
2020-05-24 11:48:37 +02:00
|
|
|
return &UDPOutConf{}
|
2023-09-20 09:18:50 +02:00
|
|
|
case v1.ProxyTypeHTTP:
|
2020-05-24 11:48:37 +02:00
|
|
|
return &HTTPOutConf{}
|
2023-09-20 09:18:50 +02:00
|
|
|
case v1.ProxyTypeHTTPS:
|
2020-05-24 11:48:37 +02:00
|
|
|
return &HTTPSOutConf{}
|
2023-09-20 09:18:50 +02:00
|
|
|
case v1.ProxyTypeSTCP:
|
2020-05-24 11:48:37 +02:00
|
|
|
return &STCPOutConf{}
|
2023-09-20 09:18:50 +02:00
|
|
|
case v1.ProxyTypeXTCP:
|
2020-05-24 11:48:37 +02:00
|
|
|
return &XTCPOutConf{}
|
2018-05-20 13:06:05 +02:00
|
|
|
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"`
|
2023-10-11 09:01:07 +02:00
|
|
|
ClientVersion string `json:"clientVersion,omitempty"`
|
|
|
|
TodayTrafficIn int64 `json:"todayTrafficIn"`
|
|
|
|
TodayTrafficOut int64 `json:"todayTrafficOut"`
|
|
|
|
CurConns int64 `json:"curConns"`
|
|
|
|
LastStartTime string `json:"lastStartTime"`
|
|
|
|
LastCloseTime string `json:"lastCloseTime"`
|
2018-05-20 13:06:05 +02:00
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
2017-03-22 19:01:25 +01:00
|
|
|
|
2018-05-20 13:06:05 +02:00
|
|
|
type GetProxyInfoResp struct {
|
|
|
|
Proxies []*ProxyStatsInfo `json:"proxies"`
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2023-06-30 11:35:37 +02:00
|
|
|
// /api/proxy/:type
|
2023-11-27 08:47:49 +01:00
|
|
|
func (svr *Service) apiProxyByType(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 04:42:07 +01:00
|
|
|
res := GeneralResponse{Code: 200}
|
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)
|
2019-02-11 04:42:07 +01:00
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-28 19:02:53 +02:00
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 04:42:07 +01:00
|
|
|
}
|
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
|
|
|
|
2019-02-11 04:42:07 +01:00
|
|
|
proxyInfoResp := GetProxyInfoResp{}
|
|
|
|
proxyInfoResp.Proxies = svr.getProxyStatsByType(proxyType)
|
2018-05-20 13:06:05 +02:00
|
|
|
|
2019-02-11 04:42:07 +01:00
|
|
|
buf, _ := json.Marshal(&proxyInfoResp)
|
|
|
|
res.Msg = string(buf)
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:53:06 +01:00
|
|
|
func (svr *Service) getProxyStatsByType(proxyType string) (proxyInfos []*ProxyStatsInfo) {
|
2020-03-11 06:20:26 +01:00
|
|
|
proxyStats := mem.StatsCollector.GetProxiesByType(proxyType)
|
2017-03-22 19:01:25 +01:00
|
|
|
proxyInfos = make([]*ProxyStatsInfo, 0, len(proxyStats))
|
|
|
|
for _, ps := range proxyStats {
|
|
|
|
proxyInfo := &ProxyStatsInfo{}
|
2019-01-14 17:11:08 +01:00
|
|
|
if pxy, ok := svr.pxyManager.GetByName(ps.Name); ok {
|
2023-09-06 04:18:02 +02:00
|
|
|
content, err := json.Marshal(pxy.GetConfigurer())
|
2018-05-20 13:06:05 +02:00
|
|
|
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
|
|
|
|
}
|
2023-09-20 09:18:50 +02:00
|
|
|
proxyInfo.Status = "online"
|
2023-02-21 17:39:56 +01:00
|
|
|
if pxy.GetLoginMsg() != nil {
|
|
|
|
proxyInfo.ClientVersion = pxy.GetLoginMsg().Version
|
|
|
|
}
|
2017-03-22 19:01:25 +01:00
|
|
|
} else {
|
2023-09-20 09:18:50 +02:00
|
|
|
proxyInfo.Status = "offline"
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
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 {
|
2018-05-20 13:06:05 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Conf interface{} `json:"conf"`
|
2023-10-11 09:01:07 +02:00
|
|
|
TodayTrafficIn int64 `json:"todayTrafficIn"`
|
|
|
|
TodayTrafficOut int64 `json:"todayTrafficOut"`
|
|
|
|
CurConns int64 `json:"curConns"`
|
|
|
|
LastStartTime string `json:"lastStartTime"`
|
|
|
|
LastCloseTime string `json:"lastCloseTime"`
|
2018-05-20 13:06:05 +02:00
|
|
|
Status string `json:"status"`
|
2018-04-04 08:37:20 +02:00
|
|
|
}
|
|
|
|
|
2023-06-30 11:35:37 +02:00
|
|
|
// /api/proxy/:type/:name
|
2023-11-27 08:47:49 +01:00
|
|
|
func (svr *Service) apiProxyByTypeAndName(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 04:42:07 +01:00
|
|
|
res := GeneralResponse{Code: 200}
|
2018-05-20 13:06:05 +02:00
|
|
|
params := mux.Vars(r)
|
|
|
|
proxyType := params["type"]
|
|
|
|
name := params["name"]
|
2018-04-04 08:37:20 +02:00
|
|
|
|
|
|
|
defer func() {
|
2018-05-20 13:06:05 +02:00
|
|
|
log.Info("Http response [%s]: code [%d]", r.URL.Path, res.Code)
|
2019-02-11 04:42:07 +01:00
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-28 19:02:53 +02:00
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 04:42:07 +01:00
|
|
|
}
|
2018-04-04 08:37:20 +02:00
|
|
|
}()
|
2018-05-20 13:06:05 +02:00
|
|
|
log.Info("Http request: [%s]", r.URL.Path)
|
2018-04-04 08:37:20 +02:00
|
|
|
|
2022-08-28 19:02:53 +02:00
|
|
|
var proxyStatsResp GetProxyStatsResp
|
2019-02-11 04:42:07 +01:00
|
|
|
proxyStatsResp, res.Code, res.Msg = svr.getProxyStatsByTypeAndName(proxyType, name)
|
|
|
|
if res.Code != 200 {
|
|
|
|
return
|
|
|
|
}
|
2018-04-04 08:37:20 +02:00
|
|
|
|
2019-02-11 04:42:07 +01:00
|
|
|
buf, _ := json.Marshal(&proxyStatsResp)
|
|
|
|
res.Msg = string(buf)
|
2018-04-04 08:37:20 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 04:42:07 +01:00
|
|
|
func (svr *Service) getProxyStatsByTypeAndName(proxyType string, proxyName string) (proxyInfo GetProxyStatsResp, code int, msg string) {
|
2018-04-04 08:37:20 +02:00
|
|
|
proxyInfo.Name = proxyName
|
2020-03-11 06:20:26 +01:00
|
|
|
ps := mem.StatsCollector.GetProxiesByTypeAndName(proxyType, proxyName)
|
2018-04-04 08:37:20 +02:00
|
|
|
if ps == nil {
|
2019-02-11 04:42:07 +01:00
|
|
|
code = 404
|
|
|
|
msg = "no proxy info found"
|
2018-04-04 08:37:20 +02:00
|
|
|
} else {
|
2019-01-14 17:11:08 +01:00
|
|
|
if pxy, ok := svr.pxyManager.GetByName(proxyName); ok {
|
2023-09-06 04:18:02 +02:00
|
|
|
content, err := json.Marshal(pxy.GetConfigurer())
|
2018-05-20 13:06:05 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("marshal proxy [%s] conf info error: %v", ps.Name, err)
|
2019-02-11 04:42:07 +01:00
|
|
|
code = 400
|
|
|
|
msg = "parse conf error"
|
2018-05-20 13:06:05 +02:00
|
|
|
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)
|
2019-02-11 04:42:07 +01:00
|
|
|
code = 400
|
|
|
|
msg = "parse conf error"
|
2018-05-20 13:06:05 +02:00
|
|
|
return
|
|
|
|
}
|
2023-09-20 09:18:50 +02:00
|
|
|
proxyInfo.Status = "online"
|
2018-04-04 08:37:20 +02:00
|
|
|
} else {
|
2023-09-20 09:18:50 +02:00
|
|
|
proxyInfo.Status = "offline"
|
2018-04-04 08:37:20 +02:00
|
|
|
}
|
|
|
|
proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
|
|
|
|
proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
|
|
|
|
proxyInfo.CurConns = ps.CurConns
|
|
|
|
proxyInfo.LastStartTime = ps.LastStartTime
|
|
|
|
proxyInfo.LastCloseTime = ps.LastCloseTime
|
2019-04-21 09:59:35 +02:00
|
|
|
code = 200
|
2018-04-04 08:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:35:37 +02:00
|
|
|
// /api/traffic/:name
|
2017-03-26 19:39:05 +02:00
|
|
|
type GetProxyTrafficResp struct {
|
|
|
|
Name string `json:"name"`
|
2023-10-11 09:01:07 +02:00
|
|
|
TrafficIn []int64 `json:"trafficIn"`
|
|
|
|
TrafficOut []int64 `json:"trafficOut"`
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
|
|
|
|
2023-11-27 08:47:49 +01:00
|
|
|
func (svr *Service) apiProxyTraffic(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 04:42:07 +01:00
|
|
|
res := GeneralResponse{Code: 200}
|
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)
|
2019-02-11 04:42:07 +01:00
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-28 19:02:53 +02:00
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 04:42:07 +01:00
|
|
|
}
|
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
|
|
|
|
2019-02-11 04:42:07 +01:00
|
|
|
trafficResp := GetProxyTrafficResp{}
|
|
|
|
trafficResp.Name = name
|
2020-03-11 06:20:26 +01:00
|
|
|
proxyTrafficInfo := mem.StatsCollector.GetProxyTraffic(name)
|
2019-02-11 04:42:07 +01:00
|
|
|
|
2017-03-26 19:39:05 +02:00
|
|
|
if proxyTrafficInfo == nil {
|
2019-02-11 04:42:07 +01:00
|
|
|
res.Code = 404
|
2017-03-22 19:01:25 +01:00
|
|
|
res.Msg = "no proxy info found"
|
2019-02-11 04:42:07 +01:00
|
|
|
return
|
2017-03-22 19:01:25 +01:00
|
|
|
}
|
2020-05-24 11:48:37 +02:00
|
|
|
trafficResp.TrafficIn = proxyTrafficInfo.TrafficIn
|
|
|
|
trafficResp.TrafficOut = proxyTrafficInfo.TrafficOut
|
2016-07-17 15:42:21 +02:00
|
|
|
|
2019-02-11 05:15:31 +01:00
|
|
|
buf, _ := json.Marshal(&trafficResp)
|
2019-02-11 04:42:07 +01:00
|
|
|
res.Msg = string(buf)
|
2016-07-17 15:42:21 +02:00
|
|
|
}
|