You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.4 KiB
121 lines
3.4 KiB
package proxy
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// ProxyStats 代理统计信息
|
|
type ProxyStats struct {
|
|
StartTime time.Time
|
|
TotalConnections int64
|
|
ActiveConnections int64
|
|
SuccessfulRequests int64
|
|
FailedRequests int64
|
|
BytesSent int64
|
|
BytesReceived int64
|
|
SOCKS5Errors map[string]int64
|
|
mutex sync.RWMutex
|
|
}
|
|
|
|
// NewProxyStats 创建新的统计实例
|
|
func NewProxyStats() *ProxyStats {
|
|
return &ProxyStats{
|
|
StartTime: time.Now(),
|
|
SOCKS5Errors: make(map[string]int64),
|
|
}
|
|
}
|
|
|
|
// IncrementConnections 增加连接计数
|
|
func (s *ProxyStats) IncrementConnections() {
|
|
atomic.AddInt64(&s.TotalConnections, 1)
|
|
atomic.AddInt64(&s.ActiveConnections, 1)
|
|
}
|
|
|
|
// DecrementActiveConnections 减少活跃连接计数
|
|
func (s *ProxyStats) DecrementActiveConnections() {
|
|
atomic.AddInt64(&s.ActiveConnections, -1)
|
|
}
|
|
|
|
// IncrementSuccessfulRequests 增加成功请求计数
|
|
func (s *ProxyStats) IncrementSuccessfulRequests() {
|
|
atomic.AddInt64(&s.SuccessfulRequests, 1)
|
|
}
|
|
|
|
// IncrementFailedRequests 增加失败请求计数
|
|
func (s *ProxyStats) IncrementFailedRequests() {
|
|
atomic.AddInt64(&s.FailedRequests, 1)
|
|
}
|
|
|
|
// AddBytesTransferred 添加传输字节数
|
|
func (s *ProxyStats) AddBytesTransferred(sent, received int64) {
|
|
atomic.AddInt64(&s.BytesSent, sent)
|
|
atomic.AddInt64(&s.BytesReceived, received)
|
|
}
|
|
|
|
// IncrementSOCKS5Error 增加SOCKS5错误计数
|
|
func (s *ProxyStats) IncrementSOCKS5Error(errorType string) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
s.SOCKS5Errors[errorType]++
|
|
}
|
|
|
|
// GetStats 获取统计快照
|
|
func (s *ProxyStats) GetStats() ProxyStatsSnapshot {
|
|
s.mutex.RLock()
|
|
defer s.mutex.RUnlock()
|
|
|
|
errors := make(map[string]int64)
|
|
for k, v := range s.SOCKS5Errors {
|
|
errors[k] = v
|
|
}
|
|
|
|
return ProxyStatsSnapshot{
|
|
StartTime: s.StartTime,
|
|
Uptime: time.Since(s.StartTime),
|
|
TotalConnections: atomic.LoadInt64(&s.TotalConnections),
|
|
ActiveConnections: atomic.LoadInt64(&s.ActiveConnections),
|
|
SuccessfulRequests: atomic.LoadInt64(&s.SuccessfulRequests),
|
|
FailedRequests: atomic.LoadInt64(&s.FailedRequests),
|
|
BytesSent: atomic.LoadInt64(&s.BytesSent),
|
|
BytesReceived: atomic.LoadInt64(&s.BytesReceived),
|
|
SOCKS5Errors: errors,
|
|
}
|
|
}
|
|
|
|
// ProxyStatsSnapshot 统计快照
|
|
type ProxyStatsSnapshot struct {
|
|
StartTime time.Time `json:"start_time"`
|
|
Uptime time.Duration `json:"uptime"`
|
|
TotalConnections int64 `json:"total_connections"`
|
|
ActiveConnections int64 `json:"active_connections"`
|
|
SuccessfulRequests int64 `json:"successful_requests"`
|
|
FailedRequests int64 `json:"failed_requests"`
|
|
BytesSent int64 `json:"bytes_sent"`
|
|
BytesReceived int64 `json:"bytes_received"`
|
|
SOCKS5Errors map[string]int64 `json:"socks5_errors"`
|
|
}
|
|
|
|
// GetSuccessRate 获取成功率
|
|
func (s *ProxyStatsSnapshot) GetSuccessRate() float64 {
|
|
total := s.SuccessfulRequests + s.FailedRequests
|
|
if total == 0 {
|
|
return 0
|
|
}
|
|
return float64(s.SuccessfulRequests) / float64(total) * 100
|
|
}
|
|
|
|
// GetTotalBytes 获取总传输字节数
|
|
func (s *ProxyStatsSnapshot) GetTotalBytes() int64 {
|
|
return s.BytesSent + s.BytesReceived
|
|
}
|
|
|
|
// GetAverageConnectionsPerHour 获取每小时平均连接数
|
|
func (s *ProxyStatsSnapshot) GetAverageConnectionsPerHour() float64 {
|
|
hours := s.Uptime.Hours()
|
|
if hours == 0 {
|
|
return 0
|
|
}
|
|
return float64(s.TotalConnections) / hours
|
|
}
|
|
|