feat(proxy): tcp-режим для xray/sing-box поверх turn
This commit is contained in:
1 parent
e63279db18
commit
f7129099f7
60 files changed
+2953
-135
No files matched your search
@@ -3,6 +3,7 @@ package stats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
type Stats struct {
|
||||
tx atomic.Uint64
|
||||
rx atomic.Uint64
|
||||
wireRx atomic.Uint64
|
||||
enabled bool
|
||||
}
|
||||
|
||||
@@ -22,6 +24,19 @@ func (s *Stats) Counters() (tx, rx uint64) {
|
||||
return s.tx.Load(), s.rx.Load()
|
||||
}
|
||||
|
||||
// LivenessRx - приём на проводе: служебные кадры транспорта тоже подтверждают живость
|
||||
// канала, а прикладной счётчик на простое стоит.
|
||||
func (s *Stats) LivenessRx() uint64 {
|
||||
return s.rx.Load() + s.wireRx.Load()
|
||||
}
|
||||
|
||||
func (s *Stats) AddWireRx(n int) {
|
||||
if n <= 0 || !s.enabled {
|
||||
return
|
||||
}
|
||||
s.wireRx.Add(uint64(n))
|
||||
}
|
||||
|
||||
func (s *Stats) AddTx(n int) {
|
||||
if n <= 0 {
|
||||
return
|
||||
@@ -57,3 +72,45 @@ func FormatBitsPerSecond(bytes uint64, interval time.Duration) string {
|
||||
}
|
||||
return fmt.Sprintf("%.0f bit/s", bps)
|
||||
}
|
||||
|
||||
// FormatByteCount форматирует количество байт в человекочитаемый вид (B, KiB, MiB).
|
||||
func FormatByteCount(bytes uint64) string {
|
||||
if bytes >= 1024*1024 {
|
||||
return fmt.Sprintf("%.2f MiB", float64(bytes)/(1024*1024))
|
||||
}
|
||||
if bytes >= 1024 {
|
||||
return fmt.Sprintf("%.1f KiB", float64(bytes)/1024)
|
||||
}
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
|
||||
// CountingConn учитывает прикладные байты - те, что видит пользователь в UI.
|
||||
type CountingConn struct {
|
||||
net.Conn
|
||||
Stats *Stats
|
||||
}
|
||||
|
||||
func (c *CountingConn) Read(p []byte) (int, error) {
|
||||
n, err := c.Conn.Read(p)
|
||||
c.Stats.AddRx(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (c *CountingConn) Write(p []byte) (int, error) {
|
||||
n, err := c.Conn.Write(p)
|
||||
c.Stats.AddTx(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// WireConn учитывает байты на проводе (с оверхедом транспорта и ретрансмитами) - только
|
||||
// для проверки живости, в пользовательские счётчики они не идут.
|
||||
type WireConn struct {
|
||||
net.Conn
|
||||
Stats *Stats
|
||||
}
|
||||
|
||||
func (c *WireConn) Read(p []byte) (int, error) {
|
||||
n, err := c.Conn.Read(p)
|
||||
c.Stats.AddWireRx(n)
|
||||
return n, err
|
||||
}
|
||||
Reference in new issue
Block a user