- 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 проходят.
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
// Package dtlsdial wraps pion-dtls client setup (self-signed cert, EMS,
|
|
// AES-128-GCM, send-only CID) plus an optional concurrency gate around the
|
|
// handshake. Used by the client UDP and VLESS pipelines.
|
|
package dtlsdial
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/pion/dtls/v3"
|
|
"github.com/pion/dtls/v3/pkg/crypto/selfsign"
|
|
)
|
|
|
|
// Dialer configures the DTLS client handshake.
|
|
type Dialer struct {
|
|
// HandshakeTimeout caps the handshake context. Zero means no timeout.
|
|
HandshakeTimeout time.Duration
|
|
// HandshakeSem, if non-nil, gates concurrent handshakes (Dial blocks
|
|
// until a slot is available or ctx fires).
|
|
HandshakeSem chan struct{}
|
|
|
|
certOnce sync.Once
|
|
cert tls.Certificate
|
|
certErr error
|
|
}
|
|
|
|
// Dial acquires the optional handshake slot and performs a DTLS client
|
|
// handshake over pc to peer. On success returns the connected *dtls.Conn.
|
|
// Caller closes it. The self-signed cert is generated once per Dialer and
|
|
// reused across handshakes (DTLS is used here for obfuscation, not auth —
|
|
// see internal/transport/dtlsdial/doc.go).
|
|
func (d *Dialer) Dial(ctx context.Context, pc net.PacketConn, peer *net.UDPAddr) (*dtls.Conn, error) {
|
|
d.certOnce.Do(func() {
|
|
d.cert, d.certErr = selfsign.GenerateSelfSigned()
|
|
})
|
|
if d.certErr != nil {
|
|
return nil, d.certErr
|
|
}
|
|
certificate := d.cert
|
|
if d.HandshakeSem != nil {
|
|
select {
|
|
case d.HandshakeSem <- struct{}{}:
|
|
defer func() { <-d.HandshakeSem }()
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
}
|
|
|
|
hsCtx := ctx
|
|
if d.HandshakeTimeout > 0 {
|
|
var cancel context.CancelFunc
|
|
hsCtx, cancel = context.WithTimeout(ctx, d.HandshakeTimeout)
|
|
defer cancel()
|
|
}
|
|
|
|
dtlsConn, err := dtls.ClientWithOptions(
|
|
pc,
|
|
peer,
|
|
dtls.WithCertificates(certificate),
|
|
dtls.WithInsecureSkipVerify(true),
|
|
dtls.WithExtendedMasterSecret(dtls.RequireExtendedMasterSecret),
|
|
dtls.WithCipherSuites(dtls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
|
|
dtls.WithConnectionIDGenerator(dtls.OnlySendCIDGenerator()),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := dtlsConn.HandshakeContext(hsCtx); err != nil {
|
|
_ = dtlsConn.Close()
|
|
return nil, err
|
|
}
|
|
return dtlsConn, nil
|
|
}
|