style: перевести комментарии на русский, убрать очевидные
This commit is contained in:
1 parent
ada9b949a4
commit
e417dac087
48 files changed
+659
-671
No files matched your search
@@ -9,29 +9,28 @@ import (
|
||||
|
||||
const inboundQueueCap = 2000
|
||||
|
||||
// Packet is a pooled UDP datagram carried from the listener to the per-stream
|
||||
// DTLS worker. N is the populated prefix of Data.
|
||||
// Packet — пулированная UDP-датаграмма, передаваемая из listener'а в per-stream
|
||||
// DTLS-воркер. N — заполненный префикс Data.
|
||||
type Packet struct {
|
||||
Data []byte
|
||||
N int
|
||||
}
|
||||
|
||||
// packetPool reuses Packet buffers across the inbound hot path. Buffer size
|
||||
// matches the 2048-byte default the listener loop expects.
|
||||
// packetPool переиспользует Packet-буферы на горячем inbound пути. Размер буфера
|
||||
// соответствует 2048 байт, которые ожидает цикл listener'а.
|
||||
var packetPool = sync.Pool{
|
||||
New: func() any { return &Packet{Data: make([]byte, 2048)} },
|
||||
}
|
||||
|
||||
// runListener reads packets from listenConn, refreshes the active-peer cache,
|
||||
// and posts each packet to inboundChan. Packets are dropped when the channel
|
||||
// is full to keep the read loop wait-free.
|
||||
// runListener читает пакеты из listenConn, обновляет кэш active-peer
|
||||
// и публикует каждый пакет в inboundChan. При переполнении канала пакет
|
||||
// отбрасывается — цикл чтения остаётся wait-free.
|
||||
func runListener(ctx context.Context, listenConn net.PacketConn, activeLocalPeer *atomic.Value, inboundChan chan<- *Packet) {
|
||||
// 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.
|
||||
// Pointer-кэш последнего виденного адреса local peer. Позволяет избежать
|
||||
// per-packet аллокации addr.String() на горячем WG ingest пути:
|
||||
// большинство пакетов приходит от одного UDPAddr, поэтому проверка
|
||||
// по указателю покрывает fast path. Медленный путь (новый экземпляр
|
||||
// от ReadFrom для того же ip:port) делает одно String-сравнение и обновляет кэш.
|
||||
var lastAddr net.Addr
|
||||
var lastAddrStr string
|
||||
for {
|
||||
|
||||
@@ -17,11 +17,11 @@ import (
|
||||
"github.com/samosvalishe/btp/internal/wire/srtpmimicry"
|
||||
)
|
||||
|
||||
// DTLSLoop keeps a single DTLS termination alive for streamID, restarting it
|
||||
// on failure with a 10-30s backoff (skipped while a captcha lockout is active
|
||||
// and the prior error was a deadline). connchan is fed a fresh AsyncPacketPipe
|
||||
// half on each attempt; okchan (non-nil only for stream 1) signals the first
|
||||
// successful handshake.
|
||||
// DTLSLoop поддерживает единственное DTLS-подключение для streamID, перезапуская
|
||||
// его при сбое с backoff 10-30s (пропускается при активной captcha-блокировке,
|
||||
// если предыдущая ошибка — дедлайн). connchan получает свежую половину
|
||||
// AsyncPacketPipe на каждой попытке; okchan (non-nil только для потока 1)
|
||||
// сигнализирует о первом успешном handshake.
|
||||
func DTLSLoop(ctx context.Context, deps *Deps, peer *net.UDPAddr, listenConn net.PacketConn, inboundChan <-chan *Packet, connchan chan<- net.PacketConn, okchan chan<- struct{}, streamID int) {
|
||||
for {
|
||||
select {
|
||||
@@ -29,9 +29,9 @@ func DTLSLoop(ctx context.Context, deps *Deps, peer *net.UDPAddr, listenConn net
|
||||
return
|
||||
default:
|
||||
err := oneDTLS(ctx, deps, peer, listenConn, inboundChan, connchan, okchan, streamID)
|
||||
// During captcha lockout the handshake deadline fires before
|
||||
// auth retries can succeed; back off briefly to avoid a tight
|
||||
// retry spin until the lockout clears.
|
||||
// При captcha-блокировке дедлайн handshake срабатывает раньше,
|
||||
// чем auth-retry успевает отработать; делаем краткий backoff,
|
||||
// чтобы не крутиться в tight spin до снятия блокировки.
|
||||
if err != nil && time.Now().Unix() < deps.Auth.LockoutUntilUnix() && errors.Is(err, context.DeadlineExceeded) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -51,9 +51,9 @@ func DTLSLoop(ctx context.Context, deps *Deps, peer *net.UDPAddr, listenConn net
|
||||
}
|
||||
}
|
||||
|
||||
// TURNLoop drives the TURN allocation half. It waits for a fresh conn2 from
|
||||
// the DTLS loop, throttles via t (the global 200ms tick), runs one TURN
|
||||
// session, and reacts to FATAL_CAPTCHA / CAPTCHA_WAIT_REQUIRED accordingly.
|
||||
// TURNLoop ведёт половину TURN-аллокации. Ждёт свежий conn2 от DTLS-цикла,
|
||||
// тормозит через t (глобальный тик 200ms), выполняет одну TURN-сессию
|
||||
// и реагирует на FATAL_CAPTCHA / CAPTCHA_WAIT_REQUIRED соответственно.
|
||||
func TURNLoop(ctx context.Context, deps *Deps, params *Params, peer *net.UDPAddr, connchan <-chan net.PacketConn, t <-chan time.Time, streamID int) {
|
||||
for {
|
||||
select {
|
||||
@@ -127,9 +127,8 @@ func oneDTLS(ctx context.Context, deps *Deps, peer *net.UDPAddr, listenConn net.
|
||||
defer dtlscancel()
|
||||
|
||||
conn1, conn2 := connutil.AsyncPacketPipe()
|
||||
// TURNLoop may restart oneTURN several times within a single DTLS lifetime,
|
||||
// re-reading conn2 on each restart; keep publishing until the DTLS attempt
|
||||
// itself ends.
|
||||
// TURNLoop может перезапускать oneTURN несколько раз в рамках одного DTLS
|
||||
// соединения, каждый раз перечитывая conn2; публикуем до завершения DTLS.
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
@@ -230,8 +229,8 @@ func oneTURN(ctx context.Context, deps *Deps, params *Params, peer *net.UDPAddr,
|
||||
relayConn := stream.Relay
|
||||
deps.log().Debugf("[STREAM %d] TURN server IP: %s", streamID, stream.ServerUDPAddr.IP)
|
||||
|
||||
// Increment before ResetErrors so concurrent HandleAuthError observers see
|
||||
// the stream as connected before its error counter clears.
|
||||
// Инкремент до ResetErrors — конкурентные наблюдатели HandleAuthError видят
|
||||
// поток подключённым до сброса счётчика ошибок.
|
||||
deps.ConnectedStreams.Add(1)
|
||||
deps.Auth.ResetErrors(streamID)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Package udprelay implements the UDP-mode proxy loop: it terminates DTLS from a
|
||||
// local peer (WireGuard) and relays its packets through a per-stream TURN
|
||||
// allocation back to a remote peer. Run is the entrypoint; it owns the local
|
||||
// listener, the inbound dispatch fan-in, and the per-stream DTLS/TURN loops.
|
||||
// Package udprelay реализует UDP-режим прокси: терминирует DTLS от локального
|
||||
// пира (WireGuard) и ретранслирует пакеты через per-stream TURN-аллокацию
|
||||
// обратно к удалённому пиру. Run — точка входа; владеет локальным listener,
|
||||
// fan-in входящего dispatch и per-stream DTLS/TURN циклами.
|
||||
package udprelay
|
||||
|
||||
import (
|
||||
@@ -18,13 +18,12 @@ import (
|
||||
"github.com/samosvalishe/btp/internal/transport/dtlsdial"
|
||||
)
|
||||
|
||||
// GetCredsFunc is re-exported from common so callers can keep their imports
|
||||
// scoped to this package.
|
||||
// GetCredsFunc реэкспортирован из common, чтобы вызывающие не выходили за пределы импортов пакета.
|
||||
type GetCredsFunc = common.GetCredsFunc
|
||||
|
||||
// AuthHandler is the subset of vkauth.Client this package needs. Defined as
|
||||
// an interface so tests can inject fakes; the production wiring still imports
|
||||
// vkauth for its sentinel errors (ErrFatalCaptchaNoStreams, etc.).
|
||||
// AuthHandler — подмножество vkauth.Client, необходимое пакету. Определено как
|
||||
// интерфейс, чтобы тесты могли подменять fake; prod-код всё равно импортирует
|
||||
// vkauth ради sentinel-ошибок (ErrFatalCaptchaNoStreams и т.д.).
|
||||
type AuthHandler interface {
|
||||
IsAuthError(err error) bool
|
||||
HandleAuthError(streamID int) bool
|
||||
@@ -32,7 +31,7 @@ type AuthHandler interface {
|
||||
LockoutUntilUnix() int64
|
||||
}
|
||||
|
||||
// Params is the per-stream TURN/wrap configuration shared by the DTLS and TURN loops.
|
||||
// Params — per-stream конфигурация TURN/wrap, общая для DTLS и TURN циклов.
|
||||
type Params struct {
|
||||
Host string
|
||||
Port string
|
||||
@@ -42,23 +41,23 @@ type Params struct {
|
||||
GetCreds GetCredsFunc
|
||||
}
|
||||
|
||||
// ErrFatal is returned by Run when a stream encounters a condition that
|
||||
// requires the entire application to exit (e.g. manual captcha solver failed
|
||||
// with no connected streams). Callers should check with errors.Is and call
|
||||
// os.Exit themselves — udprelay does not reach into the host process.
|
||||
// ErrFatal возвращается из Run, когда поток встречает условие, требующее
|
||||
// завершения всего приложения (напр. ручной решатель captcha провалился без
|
||||
// подключённых потоков). Вызывающий должен проверить через errors.Is и вызвать
|
||||
// os.Exit сам — udprelay не вмешивается в хост-процесс.
|
||||
var ErrFatal = errors.New("udprelay: fatal error")
|
||||
|
||||
// Deps groups everything the loops need from the host process. The atomics
|
||||
// are owned by Run and exposed here so DTLSLoop/TURNLoop can share them when
|
||||
// called directly (Run wires them automatically).
|
||||
// Deps объединяет всё, что циклы берут из хост-процесса. Атомики принадлежат
|
||||
// Run и экспонированы здесь, чтобы DTLSLoop/TURNLoop могли разделять их при
|
||||
// прямом вызове (Run подключает их автоматически).
|
||||
type Deps struct {
|
||||
DTLSDialer *dtlsdial.Dialer
|
||||
Auth AuthHandler
|
||||
Log logx.Logger
|
||||
ActiveLocalPeer *atomic.Value
|
||||
ConnectedStreams *atomic.Int32
|
||||
// fatalCh is an internal signalling channel; set by Run, written by
|
||||
// TURNLoop, and drained by Run to propagate the fatal error up.
|
||||
// fatalCh — внутренний сигнальный канал; устанавливается Run, пишется
|
||||
// TURNLoop, читается Run для проброса фатальной ошибки наверх.
|
||||
fatalCh chan error
|
||||
}
|
||||
|
||||
@@ -69,13 +68,13 @@ func (d *Deps) log() logx.Logger {
|
||||
return d.Log
|
||||
}
|
||||
|
||||
// Run is the UDP-mode entrypoint. It binds listenAddr, fans inbound packets
|
||||
// into a shared queue, and spawns numStreams pairs of (DTLSLoop, TURNLoop).
|
||||
// connectedStreams is owned by the caller (vkauth reads it via StreamsAlive)
|
||||
// and incremented/decremented by oneTURN.
|
||||
// Returns after all stream loops exit (i.e. when ctx is cancelled).
|
||||
// If a fatal captcha condition is encountered, Run returns ErrFatal so the
|
||||
// caller can perform os.Exit without udprelay reaching into the host process.
|
||||
// Run — точка входа UDP-режима. Биндит listenAddr, распределяет входящие пакеты
|
||||
// в общую очередь и запускает numStreams пар (DTLSLoop, TURNLoop).
|
||||
// connectedStreams принадлежит вызывающему (vkauth читает через StreamsAlive)
|
||||
// и инкрементируется/декрементируется в oneTURN.
|
||||
// Возвращается после выхода всех потоков (т.е. при отмене ctx).
|
||||
// При фатальном captcha-условии возвращает ErrFatal — вызывающий делает os.Exit
|
||||
// без вмешательства udprelay в хост-процесс.
|
||||
func Run(ctx context.Context, dtlsDialer *dtlsdial.Dialer, auth AuthHandler, logger logx.Logger, connectedStreams *atomic.Int32, params *Params, peer *net.UDPAddr, listenAddr string, numStreams int) error {
|
||||
listenConn, err := net.ListenPacket("udp", listenAddr)
|
||||
if err != nil {
|
||||
@@ -102,9 +101,9 @@ func Run(ctx context.Context, dtlsDialer *dtlsdial.Dialer, auth AuthHandler, log
|
||||
fatalCh: fatalCh,
|
||||
}
|
||||
|
||||
// runCtx is cancelled when a fatal error is detected (via fatalCh), which
|
||||
// propagates cancellation into all stream loops without requiring them to
|
||||
// hold a reference to the host-process cancel function.
|
||||
// runCtx отменяется при обнаружении фатальной ошибки (через fatalCh),
|
||||
// распространяя отмену во все потоковые циклы без необходимости хранить
|
||||
// ссылку на cancel-функцию хост-процесса.
|
||||
runCtx, runCancel := context.WithCancel(ctx)
|
||||
defer runCancel()
|
||||
|
||||
@@ -115,9 +114,9 @@ func Run(ctx context.Context, dtlsDialer *dtlsdial.Dialer, auth AuthHandler, log
|
||||
})
|
||||
t := time.Tick(200 * time.Millisecond)
|
||||
|
||||
// Stream 1 gets okchan so it can signal the first successful handshake to
|
||||
// the log. All streams start concurrently — no gate between stream 1 and
|
||||
// the rest, so a slow DTLS handshake on stream 1 never delays streams 2..N.
|
||||
// Поток 1 получает okchan для сигнализации о первом успешном handshake в лог.
|
||||
// Все потоки стартуют одновременно — нет барьера между потоком 1 и остальными,
|
||||
// поэтому медленный DTLS handshake потока 1 не задерживает потоки 2..N.
|
||||
okchan := make(chan struct{}, 1)
|
||||
for i := 0; i < numStreams; i++ {
|
||||
cchan := make(chan net.PacketConn)
|
||||
@@ -134,9 +133,9 @@ func Run(ctx context.Context, dtlsDialer *dtlsdial.Dialer, auth AuthHandler, log
|
||||
})
|
||||
}
|
||||
|
||||
// If a fatal error was sent, cancel remaining goroutines and propagate up.
|
||||
// watcherDone synchronises the watcher goroutine with Run's return so the
|
||||
// fatalErr load happens-after the store.
|
||||
// При фатальной ошибке отменяем остальные горутины и пробрасываем наверх.
|
||||
// watcherDone синхронизирует watcher-горутину с возвратом Run, обеспечивая
|
||||
// happens-after между store и load fatalErr.
|
||||
var fatalErr atomic.Pointer[error]
|
||||
watcherDone := make(chan struct{})
|
||||
go func() {
|
||||
|
||||
Reference in new issue
Block a user