refactor: симметрия, вынос серверных хендлеров
- internal/proxy/udpserver: handleUDPConnection вынесен из server-main - internal/proxy/tcpfwdserver: handleVLESSConnection + prefixedConn + pipeConn вынесены из server-main - cmd/vk-turn-server/main.go: 396 → 134 строк, глобалы убраны - internal/proxy/udprelay: добавлен entrypoint Run() симметрично tcpfwd.Run; Pool → packetPool (unexported); listener+fan-in вынесены из client-main - cmd/vk-turn-client/main.go: 262 → 130 строк, package-глобалы убраны - dtlsdial.Dialer: кеш self-signed сертификата на Dialer (sync.Once) вместо регенерации на каждый handshake - captcha.solveCaptchaPoW: переиспользование буфера (вместо 10M конкатенаций строк), ctx-проверка каждые 1024 итерации (было 4096) Тесты + lint проходят.
This commit is contained in:
1 parent
ed6825976a
commit
51bda46446
7 files changed
+465
-460
No files matched your search
+23
-151
@@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -18,31 +17,13 @@ import (
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/client/vkauth"
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/config"
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/logx"
|
||||
bondclient "github.com/cacggghp/vk-turn-proxy/internal/proxy/bondclient"
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/proxy/bondclient"
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/proxy/tcpfwd"
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/proxy/udprelay"
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/transport/dtlsdial"
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/wire/srtpmimicry"
|
||||
)
|
||||
|
||||
type getCredsFunc func(ctx context.Context, link string, streamID int) (string, string, string, error)
|
||||
|
||||
// Global state trackers
|
||||
var (
|
||||
activeLocalPeer atomic.Value
|
||||
connectedStreams atomic.Int32
|
||||
udpDtlsDialer = &dtlsdial.Dialer{
|
||||
HandshakeTimeout: 20 * time.Second,
|
||||
HandshakeSem: make(chan struct{}, 3),
|
||||
}
|
||||
vlessDtlsDialer = &dtlsdial.Dialer{HandshakeTimeout: 30 * time.Second}
|
||||
)
|
||||
|
||||
var appDialer net.Dialer
|
||||
|
||||
// vkAuth is the lazily-initialized VK auth client. Set once in main().
|
||||
var vkAuth *vkauth.Client
|
||||
|
||||
// manualCaptchaSolver bridges the vkauth.ManualSolveFunc contract to the
|
||||
// local captcha bouncer (internal/client/captcha/manual).
|
||||
func manualCaptchaSolver(_ context.Context, e *captcha.Error, d net.Dialer) (string, string, error) {
|
||||
@@ -57,15 +38,6 @@ func manualCaptchaSolver(_ context.Context, e *captcha.Error, d net.Dialer) (str
|
||||
return "", "", fmt.Errorf("no redirect_uri or captcha_img")
|
||||
}
|
||||
|
||||
type turnParams struct {
|
||||
host string
|
||||
port string
|
||||
link string
|
||||
udp bool
|
||||
wrapKey []byte
|
||||
getCreds getCredsFunc
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@@ -91,7 +63,7 @@ func main() {
|
||||
dnsdial.SetUDPDNSServers(cfg.DNS.Servers)
|
||||
log.Printf("[DNS] using custom UDP servers: %v", cfg.DNS.Servers)
|
||||
}
|
||||
appDialer = dnsdial.AppDialer(cfg.DNS.Mode)
|
||||
appDialer := dnsdial.AppDialer(cfg.DNS.Mode)
|
||||
dnsdial.InstallGlobalResolver(cfg.DNS.Mode)
|
||||
if cfg.Obf.GenWrapKey {
|
||||
key, gerr := srtpmimicry.GenKeyHex()
|
||||
@@ -112,7 +84,9 @@ func main() {
|
||||
logger := logx.New(cfg.Log.Debug)
|
||||
manualcaptcha.Debug = cfg.Log.Debug
|
||||
|
||||
vkAuth = vkauth.New(vkauth.Config{
|
||||
var connectedStreams atomic.Int32
|
||||
|
||||
vkAuth := vkauth.New(vkauth.Config{
|
||||
Dialer: appDialer,
|
||||
ManualOnly: cfg.VK.ManualCaptcha,
|
||||
StreamsPerCache: cfg.VK.StreamsPerCred,
|
||||
@@ -121,18 +95,8 @@ func main() {
|
||||
Log: logger,
|
||||
})
|
||||
|
||||
getCreds := getCredsFunc(vkAuth.GetCredentials)
|
||||
|
||||
params := &turnParams{
|
||||
host: cfg.TURN.Host,
|
||||
port: cfg.TURN.Port,
|
||||
link: cfg.VK.Link,
|
||||
udp: cfg.TURN.UDP,
|
||||
wrapKey: cfg.Obf.WrapKey,
|
||||
getCreds: getCreds,
|
||||
}
|
||||
|
||||
if cfg.Proxy.Mode != config.ProxyModeUDP {
|
||||
vlessDtlsDialer := &dtlsdial.Dialer{HandshakeTimeout: 30 * time.Second}
|
||||
bondH := &bondclient.Handler{Deps: bondclient.Deps{Log: logger}}
|
||||
vlessDeps := &tcpfwd.Deps{
|
||||
DTLSDialer: vlessDtlsDialer,
|
||||
@@ -140,120 +104,28 @@ func main() {
|
||||
BondHandler: bondH.Handle,
|
||||
}
|
||||
vlessParams := &tcpfwd.Params{
|
||||
Host: params.host,
|
||||
Port: params.port,
|
||||
Link: params.link,
|
||||
UDP: params.udp,
|
||||
WrapKey: params.wrapKey,
|
||||
GetCreds: tcpfwd.GetCredsFunc(params.getCreds),
|
||||
Host: cfg.TURN.Host,
|
||||
Port: cfg.TURN.Port,
|
||||
Link: cfg.VK.Link,
|
||||
UDP: cfg.TURN.UDP,
|
||||
WrapKey: cfg.Obf.WrapKey,
|
||||
GetCreds: tcpfwd.GetCredsFunc(vkAuth.GetCredentials),
|
||||
}
|
||||
tcpfwd.Run(ctx, vlessDeps, vlessParams, peer, cfg.Proxy.Listen, cfg.TURN.N, (cfg.Proxy.Mode == config.ProxyModeTCPFwdBond))
|
||||
tcpfwd.Run(ctx, vlessDeps, vlessParams, peer, cfg.Proxy.Listen, cfg.TURN.N, cfg.Proxy.Mode == config.ProxyModeTCPFwdBond)
|
||||
return
|
||||
}
|
||||
|
||||
listenConn, err := net.ListenPacket("udp", cfg.Proxy.Listen)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to listen: %s", err)
|
||||
}
|
||||
context.AfterFunc(ctx, func() {
|
||||
if closeErr := listenConn.Close(); closeErr != nil {
|
||||
log.Printf("Failed to close local connection: %s", closeErr)
|
||||
}
|
||||
})
|
||||
|
||||
numStreams := cfg.TURN.N
|
||||
if numStreams <= 0 {
|
||||
numStreams = 1
|
||||
}
|
||||
|
||||
// Shared Worker Pool Queue for Aggregation
|
||||
inboundChan := make(chan *udprelay.Packet, 2000)
|
||||
|
||||
go func() {
|
||||
// Pointer-cache for the last seen local peer addr. Avoids the
|
||||
// per-packet addr.String() allocation pair on the hot WG ingest path:
|
||||
// most packets come from the same UDPAddr instance, so a pointer
|
||||
// equality check covers the fast path. The slow path (new instance
|
||||
// from ReadFrom for the same ip:port) does one String compare and
|
||||
// then refreshes the cache.
|
||||
var lastAddr net.Addr
|
||||
var lastAddrStr string
|
||||
for {
|
||||
pktIface := udprelay.Pool.Get()
|
||||
pkt, ok := pktIface.(*udprelay.Packet)
|
||||
if !ok {
|
||||
log.Printf("packetPool returned unexpected type: %T", pktIface)
|
||||
continue
|
||||
}
|
||||
nRead, addr, err := listenConn.ReadFrom(pkt.Data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if addr != lastAddr {
|
||||
s := addr.String()
|
||||
if s != lastAddrStr {
|
||||
activeLocalPeer.Store(addr)
|
||||
lastAddrStr = s
|
||||
}
|
||||
lastAddr = addr
|
||||
}
|
||||
|
||||
pkt.N = nRead
|
||||
|
||||
select {
|
||||
case inboundChan <- pkt:
|
||||
default:
|
||||
// Drop the packet only if the global queue is completely full
|
||||
udprelay.Pool.Put(pkt)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
wg1 := sync.WaitGroup{}
|
||||
t := time.Tick(200 * time.Millisecond)
|
||||
|
||||
udpDeps := &udprelay.Deps{
|
||||
DTLSDialer: udpDtlsDialer,
|
||||
Auth: vkAuth,
|
||||
Log: logger,
|
||||
ActiveLocalPeer: &activeLocalPeer,
|
||||
ConnectedStreams: &connectedStreams,
|
||||
AppCancel: cancel,
|
||||
udpDtlsDialer := &dtlsdial.Dialer{
|
||||
HandshakeTimeout: 20 * time.Second,
|
||||
HandshakeSem: make(chan struct{}, 3),
|
||||
}
|
||||
udpParams := &udprelay.Params{
|
||||
Host: params.host,
|
||||
Port: params.port,
|
||||
Link: params.link,
|
||||
UDP: params.udp,
|
||||
WrapKey: params.wrapKey,
|
||||
GetCreds: udprelay.GetCredsFunc(params.getCreds),
|
||||
Host: cfg.TURN.Host,
|
||||
Port: cfg.TURN.Port,
|
||||
Link: cfg.VK.Link,
|
||||
UDP: cfg.TURN.UDP,
|
||||
WrapKey: cfg.Obf.WrapKey,
|
||||
GetCreds: udprelay.GetCredsFunc(vkAuth.GetCredentials),
|
||||
}
|
||||
|
||||
okchan := make(chan struct{})
|
||||
connchan := make(chan net.PacketConn)
|
||||
wg1.Go(func() {
|
||||
udprelay.DTLSLoop(ctx, udpDeps, peer, listenConn, inboundChan, connchan, okchan, 1)
|
||||
})
|
||||
wg1.Go(func() {
|
||||
udprelay.TURNLoop(ctx, udpDeps, udpParams, peer, connchan, t, 1)
|
||||
})
|
||||
|
||||
select {
|
||||
case <-okchan:
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
for i := 1; i < numStreams; i++ {
|
||||
cchan := make(chan net.PacketConn)
|
||||
streamID := i
|
||||
wg1.Go(func() {
|
||||
udprelay.DTLSLoop(ctx, udpDeps, peer, listenConn, inboundChan, cchan, nil, streamID)
|
||||
})
|
||||
wg1.Go(func() {
|
||||
udprelay.TURNLoop(ctx, udpDeps, udpParams, peer, cchan, t, streamID)
|
||||
})
|
||||
}
|
||||
|
||||
wg1.Wait()
|
||||
udprelay.Run(ctx, udpDtlsDialer, vkAuth, logger, &connectedStreams, cancel, udpParams, peer, cfg.Proxy.Listen, cfg.TURN.N)
|
||||
}
|
||||
Reference in new issue
Block a user