All checks were successful
Build Multi-Platform Binaries / build-frontend (push) Successful in 38s
Build Multi-Platform Binaries / build-binaries (amd64, linux, client, true) (push) Successful in 1m47s
Build Multi-Platform Binaries / build-binaries (amd64, darwin, server, false) (push) Successful in 1m48s
Build Multi-Platform Binaries / build-binaries (amd64, windows, client, true) (push) Successful in 1m29s
Build Multi-Platform Binaries / build-binaries (amd64, linux, server, true) (push) Successful in 1m53s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, client, true) (push) Successful in 1m14s
Build Multi-Platform Binaries / build-binaries (amd64, windows, server, true) (push) Successful in 1m40s
Build Multi-Platform Binaries / build-binaries (arm64, darwin, server, false) (push) Successful in 1m36s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, server, true) (push) Successful in 1m51s
Build Multi-Platform Binaries / build-binaries (arm64, linux, client, true) (push) Successful in 1m17s
Build Multi-Platform Binaries / build-binaries (arm64, linux, server, true) (push) Successful in 1m50s
Build Multi-Platform Binaries / build-binaries (arm64, windows, server, false) (push) Successful in 1m18s
- 在WebServer中添加TrafficStore存储接口 - 将Web配置从根级别移动到Server.Web子结构下 - 移除Web配置中的BindAddr字段并调整默认值逻辑 - 在前端HomeView中替换模拟流量数据显示真实统计数据 - 添加流量统计API接口(/traffic/stats和/traffic/hourly) - 实现SQLite数据库流量统计表创建和CRUD操作 - 在Relay包中添加带流量统计的数据转发功能 - 在设置页面添加服务器配置编辑和保存功能 - 创建流量统计处理器和相关数据模型定义
69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package relay
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
const bufferSize = 32 * 1024
|
|
|
|
// TrafficStats 流量统计
|
|
type TrafficStats struct {
|
|
Inbound int64
|
|
Outbound int64
|
|
}
|
|
|
|
// Relay 双向数据转发
|
|
func Relay(c1, c2 net.Conn) {
|
|
var wg sync.WaitGroup
|
|
wg.Add(2)
|
|
|
|
copyConn := func(dst, src net.Conn) {
|
|
defer wg.Done()
|
|
buf := make([]byte, bufferSize)
|
|
_, _ = io.CopyBuffer(dst, src, buf)
|
|
if tc, ok := dst.(*net.TCPConn); ok {
|
|
tc.CloseWrite()
|
|
}
|
|
}
|
|
|
|
go copyConn(c1, c2)
|
|
go copyConn(c2, c1)
|
|
wg.Wait()
|
|
}
|
|
|
|
// RelayWithStats 带流量统计的双向数据转发
|
|
func RelayWithStats(c1, c2 net.Conn, onStats func(in, out int64)) {
|
|
var wg sync.WaitGroup
|
|
var inbound, outbound int64
|
|
wg.Add(2)
|
|
|
|
copyWithCount := func(dst, src net.Conn, counter *int64) {
|
|
defer wg.Done()
|
|
buf := make([]byte, bufferSize)
|
|
for {
|
|
n, err := src.Read(buf)
|
|
if n > 0 {
|
|
atomic.AddInt64(counter, int64(n))
|
|
dst.Write(buf[:n])
|
|
}
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
if tc, ok := dst.(*net.TCPConn); ok {
|
|
tc.CloseWrite()
|
|
}
|
|
}
|
|
|
|
go copyWithCount(c1, c2, &inbound)
|
|
go copyWithCount(c2, c1, &outbound)
|
|
wg.Wait()
|
|
|
|
if onStats != nil {
|
|
onStats(inbound, outbound)
|
|
}
|
|
}
|