2017-03-23 02:01:25 +08:00
|
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
2016-05-19 00:04:19 +08: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 (
|
2024-02-20 12:01:41 +08:00
|
|
|
|
"cmp"
|
2016-06-03 17:51:45 +08:00
|
|
|
|
"encoding/json"
|
2024-10-05 10:25:11 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
2016-08-10 20:18:36 +08:00
|
|
|
|
"net/http"
|
2024-10-05 10:25:11 +08:00
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"runtime"
|
2024-02-20 12:01:41 +08:00
|
|
|
|
"slices"
|
2024-10-05 10:25:11 +08:00
|
|
|
|
"time"
|
2016-06-03 17:51:45 +08:00
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
|
"github.com/gorilla/mux"
|
2023-11-27 15:47:49 +08:00
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
|
2024-10-05 10:25:11 +08:00
|
|
|
|
"github.com/fatedier/frp/g"
|
2023-09-06 10:18:02 +08:00
|
|
|
|
"github.com/fatedier/frp/pkg/config/types"
|
|
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2020-09-23 13:49:14 +08:00
|
|
|
|
"github.com/fatedier/frp/pkg/metrics/mem"
|
2023-11-27 15:47:49 +08:00
|
|
|
|
httppkg "github.com/fatedier/frp/pkg/util/http"
|
2020-09-23 13:49:14 +08:00
|
|
|
|
"github.com/fatedier/frp/pkg/util/log"
|
2023-11-27 15:47:49 +08:00
|
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2020-09-23 13:49:14 +08:00
|
|
|
|
"github.com/fatedier/frp/pkg/util/version"
|
2024-10-05 10:25:11 +08:00
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
|
|
|
"github.com/shirou/gopsutil/disk"
|
2016-05-19 00:04:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
2016-06-03 17:51:45 +08:00
|
|
|
|
type GeneralResponse struct {
|
2019-02-11 11:42:07 +08:00
|
|
|
|
Code int
|
|
|
|
|
Msg string
|
2016-06-03 17:51:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 15:47:49 +08: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")
|
2024-02-01 10:54:57 +08:00
|
|
|
|
subRouter.HandleFunc("/api/proxies", svr.deleteProxies).Methods("DELETE")
|
2024-10-05 10:25:11 +08:00
|
|
|
|
subRouter.HandleFunc("/api/client/close/{user}", svr.ApiCloseClient).Methods("GET")
|
|
|
|
|
subRouter.HandleFunc("/api/server/close/frps", svr.ApiCloseFrps).Methods("GET")
|
|
|
|
|
subRouter.HandleFunc("/api/server/check", svr.CheckServer).Methods("GET")
|
|
|
|
|
subRouter.HandleFunc("/api/server/checkonline", svr.ApiCheckOnline).Methods("GET")
|
|
|
|
|
subRouter.HandleFunc("/api/server/command", svr.RunCommand).Methods("GET")
|
2023-11-27 15:47:49 +08:00
|
|
|
|
|
|
|
|
|
// 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 17:48:37 +08:00
|
|
|
|
type serverInfoResp struct {
|
2024-10-05 10:25:11 +08:00
|
|
|
|
Powered string `json:"powered_by"`
|
2023-02-22 00:39:56 +08:00
|
|
|
|
Version string `json:"version"`
|
2023-10-11 15:01:07 +08: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"`
|
|
|
|
|
|
2024-10-05 10:25:11 +08:00
|
|
|
|
CpuUsage string `json:"cpu_usage"`
|
|
|
|
|
RamUsage string `json:"ram_usage"`
|
|
|
|
|
DiskUsage string `json:"disk_usage"`
|
|
|
|
|
System string `json:"system"`
|
2023-10-11 15:01:07 +08:00
|
|
|
|
TotalTrafficIn int64 `json:"totalTrafficIn"`
|
|
|
|
|
TotalTrafficOut int64 `json:"totalTrafficOut"`
|
|
|
|
|
CurConns int64 `json:"curConns"`
|
|
|
|
|
ClientCounts int64 `json:"clientCounts"`
|
|
|
|
|
ProxyTypeCounts map[string]int64 `json:"proxyTypeCount"`
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 14:33:53 +08:00
|
|
|
|
// /healthz
|
2023-11-27 15:47:49 +08:00
|
|
|
|
func (svr *Service) healthz(w http.ResponseWriter, _ *http.Request) {
|
2021-08-04 14:33:53 +08:00
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 17:35:37 +08:00
|
|
|
|
// /api/serverinfo
|
2023-11-27 15:47:49 +08:00
|
|
|
|
func (svr *Service) apiServerInfo(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 11:42:07 +08:00
|
|
|
|
res := GeneralResponse{Code: 200}
|
2016-06-03 17:51:45 +08:00
|
|
|
|
defer func() {
|
2024-10-05 10:25:11 +08:00
|
|
|
|
log.Infof("HTTP响应 [%s]: 代码 [%d]", r.URL.Path, res.Code)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-29 01:02:53 +08:00
|
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 11:42:07 +08:00
|
|
|
|
}
|
2016-06-03 17:51:45 +08:00
|
|
|
|
}()
|
|
|
|
|
|
2024-10-05 10:25:11 +08:00
|
|
|
|
log.Infof("HTTP请求: [%s]", r.URL.Path)
|
2020-03-11 13:20:26 +08:00
|
|
|
|
serverStats := mem.StatsCollector.GetServer()
|
2024-10-05 10:25:11 +08:00
|
|
|
|
|
|
|
|
|
// CPU Usage
|
|
|
|
|
CPU, err := cpu.Percent(0, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cpuUsage := CPU[0]
|
|
|
|
|
|
|
|
|
|
// RAM Usage
|
|
|
|
|
var memStats runtime.MemStats
|
|
|
|
|
runtime.ReadMemStats(&memStats)
|
|
|
|
|
// 获取已分配的内存大小(以字节为单位)
|
|
|
|
|
allocatedRAM := memStats.Alloc
|
|
|
|
|
// 获取总内存大小(以字节为单位)
|
|
|
|
|
totalRAM := memStats.TotalAlloc
|
|
|
|
|
// 计算内存使用百分比
|
|
|
|
|
ramUsage := float64(allocatedRAM) / float64(totalRAM) * 100
|
|
|
|
|
|
|
|
|
|
// Disk Usage
|
|
|
|
|
|
|
|
|
|
// 获取系统的所有磁盘分区
|
|
|
|
|
partitions, err := disk.Partitions(false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算系统的总磁盘占用率
|
|
|
|
|
var totalUsage float64
|
|
|
|
|
for _, partition := range partitions {
|
|
|
|
|
// 获取磁盘分区的使用情况
|
|
|
|
|
usage, err := disk.Usage(partition.Mountpoint)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 累加磁盘分区的占用率
|
|
|
|
|
totalUsage += usage.UsedPercent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印系统的总磁盘占用率
|
|
|
|
|
diskUsage := totalUsage / float64(len(partitions))
|
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
svrResp := serverInfoResp{
|
2024-10-05 10:25:11 +08:00
|
|
|
|
Powered: "HayFrp & HX Network",
|
|
|
|
|
CpuUsage: fmt.Sprintf("%.2f%%", cpuUsage),
|
|
|
|
|
RamUsage: fmt.Sprintf("%.2f%%", ramUsage),
|
|
|
|
|
DiskUsage: fmt.Sprintf("%.2f%%", diskUsage),
|
|
|
|
|
System: fmt.Sprintf(runtime.GOOS),
|
2023-02-22 00:39:56 +08: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 10:18:02 +08:00
|
|
|
|
MaxPoolCount: svr.cfg.Transport.MaxPoolCount,
|
2023-02-22 00:39:56 +08:00
|
|
|
|
MaxPortsPerClient: svr.cfg.MaxPortsPerClient,
|
2023-09-06 10:18:02 +08:00
|
|
|
|
HeartBeatTimeout: svr.cfg.Transport.HeartbeatTimeout,
|
|
|
|
|
AllowPortsStr: types.PortsRangeSlice(svr.cfg.AllowPorts).String(),
|
2023-10-11 15:01:07 +08:00
|
|
|
|
TLSForce: svr.cfg.Transport.TLS.Force,
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2017-03-27 01:39:05 +08:00
|
|
|
|
TotalTrafficIn: serverStats.TotalTrafficIn,
|
|
|
|
|
TotalTrafficOut: serverStats.TotalTrafficOut,
|
2017-03-23 02:01:25 +08:00
|
|
|
|
CurConns: serverStats.CurConns,
|
|
|
|
|
ClientCounts: serverStats.ClientCounts,
|
|
|
|
|
ProxyTypeCounts: serverStats.ProxyTypeCounts,
|
2016-06-03 17:51:45 +08:00
|
|
|
|
}
|
2016-08-10 20:18:36 +08:00
|
|
|
|
|
2019-02-11 11:42:07 +08:00
|
|
|
|
buf, _ := json.Marshal(&svrResp)
|
|
|
|
|
res.Msg = string(buf)
|
2016-05-19 00:04:19 +08:00
|
|
|
|
}
|
2016-07-17 21:42:21 +08:00
|
|
|
|
|
2018-05-20 19:06:05 +08:00
|
|
|
|
type BaseOutConf struct {
|
2023-09-06 10:18:02 +08:00
|
|
|
|
v1.ProxyBaseConfig
|
2016-07-17 21:42:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
type TCPOutConf struct {
|
2018-05-20 19:06:05 +08:00
|
|
|
|
BaseOutConf
|
2023-10-11 15:01:07 +08:00
|
|
|
|
RemotePort int `json:"remotePort"`
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
type TCPMuxOutConf struct {
|
2020-03-05 21:47:49 +08:00
|
|
|
|
BaseOutConf
|
2023-09-06 10:18:02 +08:00
|
|
|
|
v1.DomainConfig
|
2024-04-11 22:40:42 +08:00
|
|
|
|
Multiplexer string `json:"multiplexer"`
|
|
|
|
|
RouteByHTTPUser string `json:"routeByHTTPUser"`
|
2020-03-05 21:47:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
type UDPOutConf struct {
|
2018-05-20 19:06:05 +08:00
|
|
|
|
BaseOutConf
|
2023-10-11 15:01:07 +08:00
|
|
|
|
RemotePort int `json:"remotePort"`
|
2018-05-20 19:06:05 +08:00
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
type HTTPOutConf struct {
|
2018-05-20 19:06:05 +08:00
|
|
|
|
BaseOutConf
|
2023-09-06 10:18:02 +08:00
|
|
|
|
v1.DomainConfig
|
2018-05-20 19:06:05 +08:00
|
|
|
|
Locations []string `json:"locations"`
|
2023-10-11 15:01:07 +08:00
|
|
|
|
HostHeaderRewrite string `json:"hostHeaderRewrite"`
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
type HTTPSOutConf struct {
|
2018-05-20 19:06:05 +08:00
|
|
|
|
BaseOutConf
|
2023-09-06 10:18:02 +08:00
|
|
|
|
v1.DomainConfig
|
2018-05-20 19:06:05 +08:00
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
type STCPOutConf struct {
|
2018-05-20 19:06:05 +08:00
|
|
|
|
BaseOutConf
|
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
|
type XTCPOutConf struct {
|
2018-05-20 19:06:05 +08:00
|
|
|
|
BaseOutConf
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 15:18:50 +08:00
|
|
|
|
func getConfByType(proxyType string) any {
|
|
|
|
|
switch v1.ProxyType(proxyType) {
|
|
|
|
|
case v1.ProxyTypeTCP:
|
2020-05-24 17:48:37 +08:00
|
|
|
|
return &TCPOutConf{}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
case v1.ProxyTypeTCPMUX:
|
2020-05-24 17:48:37 +08:00
|
|
|
|
return &TCPMuxOutConf{}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
case v1.ProxyTypeUDP:
|
2020-05-24 17:48:37 +08:00
|
|
|
|
return &UDPOutConf{}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
case v1.ProxyTypeHTTP:
|
2020-05-24 17:48:37 +08:00
|
|
|
|
return &HTTPOutConf{}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
case v1.ProxyTypeHTTPS:
|
2020-05-24 17:48:37 +08:00
|
|
|
|
return &HTTPSOutConf{}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
case v1.ProxyTypeSTCP:
|
2020-05-24 17:48:37 +08:00
|
|
|
|
return &STCPOutConf{}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
case v1.ProxyTypeXTCP:
|
2020-05-24 17:48:37 +08:00
|
|
|
|
return &XTCPOutConf{}
|
2018-05-20 19:06:05 +08:00
|
|
|
|
default:
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2018-05-20 19:06:05 +08:00
|
|
|
|
// Get proxy info.
|
|
|
|
|
type ProxyStatsInfo struct {
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Conf interface{} `json:"conf"`
|
2023-10-11 15:01:07 +08: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 19:06:05 +08:00
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2018-05-20 19:06:05 +08:00
|
|
|
|
type GetProxyInfoResp struct {
|
|
|
|
|
Proxies []*ProxyStatsInfo `json:"proxies"`
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 17:35:37 +08:00
|
|
|
|
// /api/proxy/:type
|
2023-11-27 15:47:49 +08:00
|
|
|
|
func (svr *Service) apiProxyByType(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 11:42:07 +08:00
|
|
|
|
res := GeneralResponse{Code: 200}
|
2018-05-20 19:06:05 +08:00
|
|
|
|
params := mux.Vars(r)
|
|
|
|
|
proxyType := params["type"]
|
|
|
|
|
|
2017-03-23 02:01:25 +08:00
|
|
|
|
defer func() {
|
2025-02-08 19:12:21 +08:00
|
|
|
|
// log.Infof("HTTP返回 [%s]: 代码 [%d]", r.URL.Path, res.Code)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-29 01:02:53 +08:00
|
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 11:42:07 +08:00
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}()
|
2025-02-08 19:12:21 +08:00
|
|
|
|
// log.Infof("HTTP请求: [%s]", r.URL.Path)
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2019-02-11 11:42:07 +08:00
|
|
|
|
proxyInfoResp := GetProxyInfoResp{}
|
|
|
|
|
proxyInfoResp.Proxies = svr.getProxyStatsByType(proxyType)
|
2024-02-20 12:01:41 +08:00
|
|
|
|
slices.SortFunc(proxyInfoResp.Proxies, func(a, b *ProxyStatsInfo) int {
|
|
|
|
|
return cmp.Compare(a.Name, b.Name)
|
2024-02-01 10:54:57 +08:00
|
|
|
|
})
|
2018-05-20 19:06:05 +08:00
|
|
|
|
|
2019-02-11 11:42:07 +08:00
|
|
|
|
buf, _ := json.Marshal(&proxyInfoResp)
|
|
|
|
|
res.Msg = string(buf)
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 20:53:06 +08:00
|
|
|
|
func (svr *Service) getProxyStatsByType(proxyType string) (proxyInfos []*ProxyStatsInfo) {
|
2020-03-11 13:20:26 +08:00
|
|
|
|
proxyStats := mem.StatsCollector.GetProxiesByType(proxyType)
|
2017-03-23 02:01:25 +08:00
|
|
|
|
proxyInfos = make([]*ProxyStatsInfo, 0, len(proxyStats))
|
|
|
|
|
for _, ps := range proxyStats {
|
|
|
|
|
proxyInfo := &ProxyStatsInfo{}
|
2019-01-15 00:11:08 +08:00
|
|
|
|
if pxy, ok := svr.pxyManager.GetByName(ps.Name); ok {
|
2023-09-06 10:18:02 +08:00
|
|
|
|
content, err := json.Marshal(pxy.GetConfigurer())
|
2018-05-20 19:06:05 +08:00
|
|
|
|
if err != nil {
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Warnf("marshal proxy [%s] conf info error: %v", ps.Name, err)
|
2018-05-20 19:06:05 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
proxyInfo.Conf = getConfByType(ps.Type)
|
|
|
|
|
if err = json.Unmarshal(content, &proxyInfo.Conf); err != nil {
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Warnf("unmarshal proxy [%s] conf info error: %v", ps.Name, err)
|
2018-05-20 19:06:05 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
proxyInfo.Status = "online"
|
2023-02-22 00:39:56 +08:00
|
|
|
|
if pxy.GetLoginMsg() != nil {
|
|
|
|
|
proxyInfo.ClientVersion = pxy.GetLoginMsg().Version
|
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
} else {
|
2023-09-20 15:18:50 +08:00
|
|
|
|
proxyInfo.Status = "offline"
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
2017-05-31 02:21:15 +08:00
|
|
|
|
proxyInfo.Name = ps.Name
|
2017-03-27 01:39:05 +08:00
|
|
|
|
proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
|
|
|
|
|
proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
|
2017-03-23 02:01:25 +08:00
|
|
|
|
proxyInfo.CurConns = ps.CurConns
|
2017-05-31 02:21:15 +08:00
|
|
|
|
proxyInfo.LastStartTime = ps.LastStartTime
|
|
|
|
|
proxyInfo.LastCloseTime = ps.LastCloseTime
|
2017-03-23 02:01:25 +08:00
|
|
|
|
proxyInfos = append(proxyInfos, proxyInfo)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 14:37:20 +08:00
|
|
|
|
// Get proxy info by name.
|
|
|
|
|
type GetProxyStatsResp struct {
|
2018-05-20 19:06:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Conf interface{} `json:"conf"`
|
2023-10-11 15:01:07 +08:00
|
|
|
|
TodayTrafficIn int64 `json:"todayTrafficIn"`
|
|
|
|
|
TodayTrafficOut int64 `json:"todayTrafficOut"`
|
|
|
|
|
CurConns int64 `json:"curConns"`
|
|
|
|
|
LastStartTime string `json:"lastStartTime"`
|
|
|
|
|
LastCloseTime string `json:"lastCloseTime"`
|
2018-05-20 19:06:05 +08:00
|
|
|
|
Status string `json:"status"`
|
2018-04-04 14:37:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 17:35:37 +08:00
|
|
|
|
// /api/proxy/:type/:name
|
2023-11-27 15:47:49 +08:00
|
|
|
|
func (svr *Service) apiProxyByTypeAndName(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 11:42:07 +08:00
|
|
|
|
res := GeneralResponse{Code: 200}
|
2018-05-20 19:06:05 +08:00
|
|
|
|
params := mux.Vars(r)
|
|
|
|
|
proxyType := params["type"]
|
|
|
|
|
name := params["name"]
|
2018-04-04 14:37:20 +08:00
|
|
|
|
|
|
|
|
|
defer func() {
|
2024-10-05 10:25:11 +08:00
|
|
|
|
log.Infof("HTTP返回 [%s]: 状态码 [%d]", r.URL.Path, res.Code)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-29 01:02:53 +08:00
|
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 11:42:07 +08:00
|
|
|
|
}
|
2018-04-04 14:37:20 +08:00
|
|
|
|
}()
|
2024-10-05 10:25:11 +08:00
|
|
|
|
log.Infof("HTTP请求: [%s]", r.URL.Path)
|
2018-04-04 14:37:20 +08:00
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
|
var proxyStatsResp GetProxyStatsResp
|
2019-02-11 11:42:07 +08:00
|
|
|
|
proxyStatsResp, res.Code, res.Msg = svr.getProxyStatsByTypeAndName(proxyType, name)
|
|
|
|
|
if res.Code != 200 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-04-04 14:37:20 +08:00
|
|
|
|
|
2019-02-11 11:42:07 +08:00
|
|
|
|
buf, _ := json.Marshal(&proxyStatsResp)
|
|
|
|
|
res.Msg = string(buf)
|
2018-04-04 14:37:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 11:42:07 +08:00
|
|
|
|
func (svr *Service) getProxyStatsByTypeAndName(proxyType string, proxyName string) (proxyInfo GetProxyStatsResp, code int, msg string) {
|
2018-04-04 14:37:20 +08:00
|
|
|
|
proxyInfo.Name = proxyName
|
2020-03-11 13:20:26 +08:00
|
|
|
|
ps := mem.StatsCollector.GetProxiesByTypeAndName(proxyType, proxyName)
|
2018-04-04 14:37:20 +08:00
|
|
|
|
if ps == nil {
|
2019-02-11 11:42:07 +08:00
|
|
|
|
code = 404
|
|
|
|
|
msg = "no proxy info found"
|
2018-04-04 14:37:20 +08:00
|
|
|
|
} else {
|
2019-01-15 00:11:08 +08:00
|
|
|
|
if pxy, ok := svr.pxyManager.GetByName(proxyName); ok {
|
2023-09-06 10:18:02 +08:00
|
|
|
|
content, err := json.Marshal(pxy.GetConfigurer())
|
2018-05-20 19:06:05 +08:00
|
|
|
|
if err != nil {
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Warnf("marshal proxy [%s] conf info error: %v", ps.Name, err)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
code = 400
|
|
|
|
|
msg = "parse conf error"
|
2018-05-20 19:06:05 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
proxyInfo.Conf = getConfByType(ps.Type)
|
|
|
|
|
if err = json.Unmarshal(content, &proxyInfo.Conf); err != nil {
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Warnf("unmarshal proxy [%s] conf info error: %v", ps.Name, err)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
code = 400
|
|
|
|
|
msg = "parse conf error"
|
2018-05-20 19:06:05 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-20 15:18:50 +08:00
|
|
|
|
proxyInfo.Status = "online"
|
2018-04-04 14:37:20 +08:00
|
|
|
|
} else {
|
2023-09-20 15:18:50 +08:00
|
|
|
|
proxyInfo.Status = "offline"
|
2018-04-04 14:37:20 +08:00
|
|
|
|
}
|
|
|
|
|
proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
|
|
|
|
|
proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
|
|
|
|
|
proxyInfo.CurConns = ps.CurConns
|
|
|
|
|
proxyInfo.LastStartTime = ps.LastStartTime
|
|
|
|
|
proxyInfo.LastCloseTime = ps.LastCloseTime
|
2019-04-21 15:59:35 +08:00
|
|
|
|
code = 200
|
2018-04-04 14:37:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 17:35:37 +08:00
|
|
|
|
// /api/traffic/:name
|
2017-03-27 01:39:05 +08:00
|
|
|
|
type GetProxyTrafficResp struct {
|
|
|
|
|
Name string `json:"name"`
|
2023-10-11 15:01:07 +08:00
|
|
|
|
TrafficIn []int64 `json:"trafficIn"`
|
|
|
|
|
TrafficOut []int64 `json:"trafficOut"`
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
|
func (svr *Service) apiProxyTraffic(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 11:42:07 +08:00
|
|
|
|
res := GeneralResponse{Code: 200}
|
2018-05-20 19:06:05 +08:00
|
|
|
|
params := mux.Vars(r)
|
|
|
|
|
name := params["name"]
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
|
|
|
|
defer func() {
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Infof("Http response [%s]: code [%d]", r.URL.Path, res.Code)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
|
if len(res.Msg) > 0 {
|
2022-08-29 01:02:53 +08:00
|
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
2019-02-11 11:42:07 +08:00
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}()
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Infof("Http request: [%s]", r.URL.Path)
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
2019-02-11 11:42:07 +08:00
|
|
|
|
trafficResp := GetProxyTrafficResp{}
|
|
|
|
|
trafficResp.Name = name
|
2020-03-11 13:20:26 +08:00
|
|
|
|
proxyTrafficInfo := mem.StatsCollector.GetProxyTraffic(name)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
|
2017-03-27 01:39:05 +08:00
|
|
|
|
if proxyTrafficInfo == nil {
|
2019-02-11 11:42:07 +08:00
|
|
|
|
res.Code = 404
|
2017-03-23 02:01:25 +08:00
|
|
|
|
res.Msg = "no proxy info found"
|
2019-02-11 11:42:07 +08:00
|
|
|
|
return
|
2017-03-23 02:01:25 +08:00
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
|
trafficResp.TrafficIn = proxyTrafficInfo.TrafficIn
|
|
|
|
|
trafficResp.TrafficOut = proxyTrafficInfo.TrafficOut
|
2016-07-17 21:42:21 +08:00
|
|
|
|
|
2019-02-11 12:15:31 +08:00
|
|
|
|
buf, _ := json.Marshal(&trafficResp)
|
2019-02-11 11:42:07 +08:00
|
|
|
|
res.Msg = string(buf)
|
2016-07-17 21:42:21 +08:00
|
|
|
|
}
|
2024-02-01 10:54:57 +08:00
|
|
|
|
|
2024-10-05 10:25:11 +08:00
|
|
|
|
type CloseUserResp struct {
|
|
|
|
|
Status int `json:"status"`
|
|
|
|
|
Msg string `json:"message"`
|
|
|
|
|
Speed int `json:"speed"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CloseProxy struct {
|
|
|
|
|
ProxyName string `json:"proxy_name"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svr *Service) ApiCloseClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var (
|
|
|
|
|
buf []byte
|
|
|
|
|
resp = CloseUserResp{}
|
|
|
|
|
)
|
|
|
|
|
params := mux.Vars(r)
|
|
|
|
|
user := params["user"]
|
|
|
|
|
defer func() {
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求 [/api/client/close/{user}]: 代码 [%d]", resp.Status)
|
|
|
|
|
}()
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求: [/api/client/close/{user}] %#v", user)
|
|
|
|
|
|
|
|
|
|
err := svr.CloseUser(user)
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.Status = 404
|
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
|
// 在这里不返回任何消息到客户端
|
|
|
|
|
} else {
|
|
|
|
|
resp.Status = 200
|
|
|
|
|
resp.Msg = "Success"
|
|
|
|
|
}
|
|
|
|
|
buf, _ = json.Marshal(&resp)
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 10:54:57 +08:00
|
|
|
|
// DELETE /api/proxies?status=offline
|
|
|
|
|
func (svr *Service) deleteProxies(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
res := GeneralResponse{Code: 200}
|
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Infof("Http request: [%s]", r.URL.Path)
|
2024-02-01 10:54:57 +08:00
|
|
|
|
defer func() {
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Infof("Http response [%s]: code [%d]", r.URL.Path, res.Code)
|
2024-02-01 10:54:57 +08:00
|
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
|
if len(res.Msg) > 0 {
|
|
|
|
|
_, _ = w.Write([]byte(res.Msg))
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
status := r.URL.Query().Get("status")
|
|
|
|
|
if status != "offline" {
|
|
|
|
|
res.Code = 400
|
|
|
|
|
res.Msg = "status only support offline"
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cleared, total := mem.StatsCollector.ClearOfflineProxies()
|
2024-03-12 13:58:53 +08:00
|
|
|
|
log.Infof("cleared [%d] offline proxies, total [%d] proxies", cleared, total)
|
2024-02-01 10:54:57 +08:00
|
|
|
|
}
|
2024-10-05 10:25:11 +08:00
|
|
|
|
|
|
|
|
|
func (svr *Service) RunCommand(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
// 获取查询参数中的 code
|
|
|
|
|
code := r.URL.Query().Get("code")
|
|
|
|
|
if code == "" {
|
|
|
|
|
http.Error(w, "Missing code parameter", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行系统命令
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
cmd = exec.Command("cmd", "/C", code)
|
|
|
|
|
} else {
|
|
|
|
|
cmd = exec.Command("sh", "-c", code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
|
var (
|
|
|
|
|
resp = CloseUserResp{}
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.Status = 404
|
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
|
// 在这里不返回任何消息到客户端
|
|
|
|
|
http.Error(w, fmt.Sprintf("Error executing command: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
resp.Status = 200
|
|
|
|
|
resp.Msg = "Success"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将命令的输出作为 HTTP 响应返回
|
|
|
|
|
fmt.Fprintf(w, "%s", string(output))
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求: [/api/server/command]: 代码 [%d]", resp.Status)
|
|
|
|
|
log.Infof("[HayFrp] Http执行命令: [" + code + "]")
|
|
|
|
|
log.Infof("[HayFrp] Http执行命令返回: [" + string(output) + "]")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svr *Service) ApiCloseFrps(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var (
|
|
|
|
|
buf []byte
|
|
|
|
|
resp = CloseUserResp{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求 [/api/server/close/frps]: 代码 [%d]", resp.Status)
|
|
|
|
|
}()
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求: [/api/server/close/frps] Frps 已关闭,为确保服务保持,请记得重启Frps!")
|
|
|
|
|
err := svr.listener.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.Status = 404
|
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
|
// 在这里不返回任何消息到客户端
|
|
|
|
|
} else {
|
|
|
|
|
resp.Status = 200
|
|
|
|
|
resp.Msg = "Success"
|
|
|
|
|
}
|
|
|
|
|
buf, _ = json.Marshal(&resp)
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkonline() {
|
|
|
|
|
log.Infof("[HayFrp] 检测服务器上线状态中......")
|
|
|
|
|
// 发起 GET 请求获取 API 返回的内容(节点状态)
|
2025-02-08 19:12:21 +08:00
|
|
|
|
resp, err := http.Get("https://api.hayfrp.com/NodeAPI?type=checkonline&token=" + g.GlbServerCfg.ApiToken)
|
2024-10-05 10:25:11 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
// 读取 API 返回的内容
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 将 API 返回的内容添加到 loginMsg.RunId 后面
|
|
|
|
|
log.Infof("[HayFrp] " + string(body))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svr *Service) ApiCheckOnline(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var (
|
|
|
|
|
buf []byte
|
|
|
|
|
resp = CloseUserResp{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求 [/api/server/checkonline]: 代码 [%d]", resp.Status)
|
|
|
|
|
}()
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求: [/api/server/checkonline] Frps 已尝试开始请求API拉取在线状态!")
|
|
|
|
|
resp.Status = 200
|
|
|
|
|
resp.Msg = "Success"
|
|
|
|
|
resp.Speed = 0
|
|
|
|
|
buf, _ = json.Marshal(&resp)
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
checkonline()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func (svr *Service) CheckServer(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var (
|
|
|
|
|
buf []byte
|
|
|
|
|
resp = CloseUserResp{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 解析查询参数
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
address := query.Get("address")
|
|
|
|
|
port := query.Get("port")
|
|
|
|
|
defer func() {
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求 [/api/server/check?address=%s&port=%s]: 代码 [%d]", address, port, resp.Status)
|
|
|
|
|
}()
|
|
|
|
|
log.Infof("[HayFrp] Http数据请求: [/api/server/check?address=%s&port=%s]", address, port)
|
|
|
|
|
// 创建一个HTTP客户端,设置超时为60秒
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
Timeout: 60 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
// 请求http://address:port
|
|
|
|
|
httpResp, err := client.Get(fmt.Sprintf("http://%s:%s", address, port))
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 如果请求失败,返回500和错误信息
|
|
|
|
|
resp.Status = 500
|
|
|
|
|
resp.Msg = "Internet Server ERROR"
|
|
|
|
|
} else {
|
|
|
|
|
// 如果请求成功,检查HTTP状态码
|
|
|
|
|
if httpResp.StatusCode == 401 {
|
|
|
|
|
// 如果状态码为401,返回200和Success
|
|
|
|
|
resp.Status = 200
|
|
|
|
|
resp.Msg = "Success"
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// 否则,返回500和Server ERROR
|
|
|
|
|
resp.Status = 500
|
|
|
|
|
resp.Msg = "Server StatusCode Isn't 401."
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resp.Speed = int(time.Since(startTime).Nanoseconds() / 1000000) // 计算响应速度(毫秒)
|
|
|
|
|
|
|
|
|
|
// 将响应转换为JSON格式
|
|
|
|
|
buf, _ = json.Marshal(&resp)
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
}
|