mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 23:06:36 +08:00
[优化] 优化
This commit is contained in:
@@ -3,10 +3,8 @@ package frps
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -71,108 +69,6 @@ func (m *Manager) GetStatus() string {
|
||||
return m.status
|
||||
}
|
||||
|
||||
type frpsServerInfo struct {
|
||||
Version string `json:"version"`
|
||||
BindPort int `json:"bind_port"`
|
||||
CurConns int `json:"cur_conns"`
|
||||
ClientCounts int `json:"client_counts"`
|
||||
ProxyTypeCount map[string]int `json:"proxy_type_count"`
|
||||
}
|
||||
|
||||
type frpsProxyInfo struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
ClientAddr string `json:"client_addr"`
|
||||
LastStartTime string `json:"last_start_time"`
|
||||
LastCloseTime string `json:"last_close_time"`
|
||||
}
|
||||
|
||||
type frpsProxiesResponse struct {
|
||||
Proxies []frpsProxyInfo `json:"proxies"`
|
||||
}
|
||||
|
||||
func (m *Manager) fetchFrpsTelemetry() (connections int, proxyCount int, clientCount int, proxies []service.RelayProxyStat, err error) {
|
||||
m.mu.RLock()
|
||||
activeConfig := m.activeConfig
|
||||
m.mu.RUnlock()
|
||||
|
||||
if activeConfig == nil {
|
||||
return 0, 0, 0, nil, fmt.Errorf("activeConfig is nil")
|
||||
}
|
||||
|
||||
dashboardPort := activeConfig.BindPort + 500
|
||||
client := &http.Client{
|
||||
Timeout: 2 * time.Second,
|
||||
}
|
||||
|
||||
// 1. Fetch serverinfo
|
||||
infoURL := fmt.Sprintf("http://127.0.0.1:%d/api/serverinfo", dashboardPort)
|
||||
req, err := http.NewRequest(http.MethodGet, infoURL, nil)
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
password := m.agentToken
|
||||
if password == "" {
|
||||
password = "admin"
|
||||
}
|
||||
req.SetBasicAuth("admin", password)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, 0, 0, nil, fmt.Errorf("serverinfo returned status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var info frpsServerInfo
|
||||
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
|
||||
connections = info.CurConns
|
||||
clientCount = info.ClientCounts
|
||||
|
||||
totalProxies := 0
|
||||
for _, count := range info.ProxyTypeCount {
|
||||
totalProxies += count
|
||||
}
|
||||
proxyCount = totalProxies
|
||||
|
||||
// 2. Fetch HTTP proxies list
|
||||
proxiesURL := fmt.Sprintf("http://127.0.0.1:%d/api/proxy/http", dashboardPort)
|
||||
req2, err := http.NewRequest(http.MethodGet, proxiesURL, nil)
|
||||
if err != nil {
|
||||
return connections, proxyCount, clientCount, nil, nil
|
||||
}
|
||||
req2.SetBasicAuth("admin", password)
|
||||
resp2, err := client.Do(req2)
|
||||
if err != nil {
|
||||
return connections, proxyCount, clientCount, nil, nil
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
|
||||
if resp2.StatusCode == http.StatusOK {
|
||||
var listResp frpsProxiesResponse
|
||||
if err := json.NewDecoder(resp2.Body).Decode(&listResp); err == nil {
|
||||
for _, p := range listResp.Proxies {
|
||||
proxies = append(proxies, service.RelayProxyStat{
|
||||
Name: p.Name,
|
||||
Type: p.Type,
|
||||
Status: p.Status,
|
||||
ClientAddr: p.ClientAddr,
|
||||
LastStartTime: p.LastStartTime,
|
||||
LastCloseTime: p.LastCloseTime,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return connections, proxyCount, clientCount, proxies, nil
|
||||
}
|
||||
|
||||
func (m *Manager) GetRuntimeStatus() RuntimeStatus {
|
||||
m.mu.RLock()
|
||||
status := m.status
|
||||
@@ -180,29 +76,13 @@ func (m *Manager) GetRuntimeStatus() RuntimeStatus {
|
||||
cmd := m.cmd
|
||||
m.mu.RUnlock()
|
||||
|
||||
connections := 0
|
||||
proxyCount := 0
|
||||
clientCount := 0
|
||||
var proxies []service.RelayProxyStat
|
||||
|
||||
if cmd != nil && cmd.Process != nil && status == "healthy" {
|
||||
if conns, pCount, cCount, pxs, err := m.fetchFrpsTelemetry(); err == nil {
|
||||
connections = conns
|
||||
proxyCount = pCount
|
||||
clientCount = cCount
|
||||
proxies = pxs
|
||||
} else {
|
||||
slog.Debug("failed to fetch frps telemetry", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
return RuntimeStatus{
|
||||
Status: status,
|
||||
LastError: lastError,
|
||||
Connections: connections,
|
||||
ProxyCount: proxyCount,
|
||||
ClientCount: clientCount,
|
||||
Proxies: proxies,
|
||||
Connections: 0,
|
||||
ProxyCount: 0,
|
||||
ClientCount: 0,
|
||||
Proxies: nil,
|
||||
ProcessAlive: cmd != nil && cmd.Process != nil,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user