refactor: вынести stats, netadapt, bond в internal/
- internal/stats: throughputStats/countingConn/форматтеры, общие для клиента и сервера - internal/netadapt: DirectNet, ConnectedUDPConn, SplitFirstWriteConn - internal/bond: wire-кодек (Frame/Hello, Read/Write, CloseWrite) Убирает дублирование между client/main.go и server/main.go. Wire-формат и поведение CLI не изменены; round-trip кодека покрыт тестами.
This commit is contained in:
1 parent
948108a33a
commit
ada0c746aa
8 files changed
+817
-580
No files matched your search
@@ -0,0 +1,150 @@
|
||||
// Package netadapt holds small net.Conn / transport.Net adapters used by both
|
||||
// client and server: a passthrough transport.Net (for pion turn),
|
||||
// ConnectedUDPConn (Write-only WriteTo on a dialed UDPConn), and
|
||||
// SplitFirstWriteConn (DPI evasion via first-segment split).
|
||||
package netadapt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/pion/transport/v4"
|
||||
)
|
||||
|
||||
// DirectNet implements transport.Net by delegating to the std net package.
|
||||
type DirectNet struct{}
|
||||
|
||||
// New returns a transport.Net that delegates to the standard net package.
|
||||
func New() transport.Net {
|
||||
return DirectNet{}
|
||||
}
|
||||
|
||||
type directDialer struct {
|
||||
*net.Dialer
|
||||
}
|
||||
|
||||
type directListenConfig struct {
|
||||
*net.ListenConfig
|
||||
}
|
||||
|
||||
type directTCPListener struct {
|
||||
*net.TCPListener
|
||||
}
|
||||
|
||||
func (DirectNet) ListenPacket(network string, address string) (net.PacketConn, error) {
|
||||
return net.ListenPacket(network, address)
|
||||
}
|
||||
|
||||
func (DirectNet) ListenUDP(network string, locAddr *net.UDPAddr) (transport.UDPConn, error) {
|
||||
return net.ListenUDP(network, locAddr)
|
||||
}
|
||||
|
||||
func (DirectNet) ListenTCP(network string, laddr *net.TCPAddr) (transport.TCPListener, error) {
|
||||
listener, err := net.ListenTCP(network, laddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return directTCPListener{listener}, nil
|
||||
}
|
||||
|
||||
func (DirectNet) Dial(network, address string) (net.Conn, error) {
|
||||
return net.Dial(network, address)
|
||||
}
|
||||
|
||||
func (DirectNet) DialUDP(network string, laddr, raddr *net.UDPAddr) (transport.UDPConn, error) {
|
||||
return net.DialUDP(network, laddr, raddr)
|
||||
}
|
||||
|
||||
func (DirectNet) DialTCP(network string, laddr, raddr *net.TCPAddr) (transport.TCPConn, error) {
|
||||
return net.DialTCP(network, laddr, raddr)
|
||||
}
|
||||
|
||||
func (DirectNet) ResolveIPAddr(network, address string) (*net.IPAddr, error) {
|
||||
return net.ResolveIPAddr(network, address)
|
||||
}
|
||||
|
||||
func (DirectNet) ResolveUDPAddr(network, address string) (*net.UDPAddr, error) {
|
||||
return net.ResolveUDPAddr(network, address)
|
||||
}
|
||||
|
||||
func (DirectNet) ResolveTCPAddr(network, address string) (*net.TCPAddr, error) {
|
||||
return net.ResolveTCPAddr(network, address)
|
||||
}
|
||||
|
||||
func (DirectNet) Interfaces() ([]*transport.Interface, error) {
|
||||
return nil, transport.ErrNotSupported
|
||||
}
|
||||
|
||||
func (DirectNet) InterfaceByIndex(index int) (*transport.Interface, error) {
|
||||
return nil, fmt.Errorf("%w: index=%d", transport.ErrInterfaceNotFound, index)
|
||||
}
|
||||
|
||||
func (DirectNet) InterfaceByName(name string) (*transport.Interface, error) {
|
||||
return nil, fmt.Errorf("%w: %s", transport.ErrInterfaceNotFound, name)
|
||||
}
|
||||
|
||||
func (DirectNet) CreateDialer(dialer *net.Dialer) transport.Dialer {
|
||||
return directDialer{Dialer: dialer}
|
||||
}
|
||||
|
||||
func (DirectNet) CreateListenConfig(listenerConfig *net.ListenConfig) transport.ListenConfig {
|
||||
return directListenConfig{ListenConfig: listenerConfig}
|
||||
}
|
||||
|
||||
func (d directDialer) Dial(network, address string) (net.Conn, error) {
|
||||
return d.Dialer.Dial(network, address)
|
||||
}
|
||||
|
||||
func (d directListenConfig) Listen(ctx context.Context, network, address string) (net.Listener, error) {
|
||||
return d.ListenConfig.Listen(ctx, network, address)
|
||||
}
|
||||
|
||||
func (d directListenConfig) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
|
||||
return d.ListenConfig.ListenPacket(ctx, network, address)
|
||||
}
|
||||
|
||||
func (l directTCPListener) AcceptTCP() (transport.TCPConn, error) {
|
||||
return l.TCPListener.AcceptTCP()
|
||||
}
|
||||
|
||||
// ConnectedUDPConn lets a dialed (connected) *net.UDPConn satisfy
|
||||
// net.PacketConn semantics: WriteTo ignores the destination since the kernel
|
||||
// already has it from connect().
|
||||
type ConnectedUDPConn struct {
|
||||
*net.UDPConn
|
||||
}
|
||||
|
||||
// WriteTo discards the addr (UDP is already connected) and writes p.
|
||||
func (c *ConnectedUDPConn) WriteTo(p []byte, _ net.Addr) (int, error) {
|
||||
return c.Write(p)
|
||||
}
|
||||
|
||||
// SplitFirstWriteConn wraps a TCP net.Conn and splits the very first Write
|
||||
// into two segments (SplitAt bytes + remainder) with an optional Delay between
|
||||
// them. This breaks DPI rules that match a fixed offset in the first segment
|
||||
// without TCP reassembly (e.g. the STUN magic cookie at offset 4-7).
|
||||
type SplitFirstWriteConn struct {
|
||||
net.Conn
|
||||
SplitAt int
|
||||
Delay time.Duration
|
||||
done atomic.Bool
|
||||
}
|
||||
|
||||
// Write performs the one-shot split on the first call, then forwards directly.
|
||||
func (s *SplitFirstWriteConn) Write(b []byte) (int, error) {
|
||||
if s.done.CompareAndSwap(false, true) && len(b) > s.SplitAt {
|
||||
n1, err := s.Conn.Write(b[:s.SplitAt])
|
||||
if err != nil {
|
||||
return n1, err
|
||||
}
|
||||
if s.Delay > 0 {
|
||||
time.Sleep(s.Delay)
|
||||
}
|
||||
n2, err := s.Conn.Write(b[s.SplitAt:])
|
||||
return n1 + n2, err
|
||||
}
|
||||
return s.Conn.Write(b)
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package netadapt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// pipeRecorder is a net.Conn that records the slice handed to each Write.
|
||||
type pipeRecorder struct {
|
||||
net.Conn
|
||||
mu sync.Mutex
|
||||
writes [][]byte
|
||||
}
|
||||
|
||||
func (p *pipeRecorder) Write(b []byte) (int, error) {
|
||||
p.mu.Lock()
|
||||
cp := make([]byte, len(b))
|
||||
copy(cp, b)
|
||||
p.writes = append(p.writes, cp)
|
||||
p.mu.Unlock()
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func newRecorder() *pipeRecorder {
|
||||
a, b := net.Pipe()
|
||||
go func() { _ = b.Close() }()
|
||||
_ = a
|
||||
return &pipeRecorder{Conn: dummyConn{}}
|
||||
}
|
||||
|
||||
type dummyConn struct{ net.Conn }
|
||||
|
||||
func (dummyConn) Close() error { return nil }
|
||||
func (dummyConn) LocalAddr() net.Addr { return nil }
|
||||
func (dummyConn) RemoteAddr() net.Addr { return nil }
|
||||
func (dummyConn) SetDeadline(time.Time) error { return nil }
|
||||
func (dummyConn) SetReadDeadline(time.Time) error { return nil }
|
||||
func (dummyConn) SetWriteDeadline(time.Time) error { return nil }
|
||||
func (dummyConn) Read(b []byte) (int, error) { return 0, nil }
|
||||
|
||||
func TestSplitFirstWriteConn_SplitsFirstWrite(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rec := newRecorder()
|
||||
c := &SplitFirstWriteConn{Conn: rec, SplitAt: 6}
|
||||
payload := []byte("ABCDEF01234567")
|
||||
n, err := c.Write(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Write err: %v", err)
|
||||
}
|
||||
if n != len(payload) {
|
||||
t.Fatalf("Write n=%d want %d", n, len(payload))
|
||||
}
|
||||
if len(rec.writes) != 2 {
|
||||
t.Fatalf("expected 2 underlying writes, got %d", len(rec.writes))
|
||||
}
|
||||
if !bytes.Equal(rec.writes[0], payload[:6]) {
|
||||
t.Fatalf("first segment = %q, want %q", rec.writes[0], payload[:6])
|
||||
}
|
||||
if !bytes.Equal(rec.writes[1], payload[6:]) {
|
||||
t.Fatalf("second segment = %q, want %q", rec.writes[1], payload[6:])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitFirstWriteConn_SubsequentWritesPassThrough(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rec := newRecorder()
|
||||
c := &SplitFirstWriteConn{Conn: rec, SplitAt: 6}
|
||||
if _, err := c.Write([]byte("ABCDEF01234567")); err != nil {
|
||||
t.Fatalf("first Write: %v", err)
|
||||
}
|
||||
if _, err := c.Write([]byte("XYZXYZ")); err != nil {
|
||||
t.Fatalf("second Write: %v", err)
|
||||
}
|
||||
if len(rec.writes) != 3 {
|
||||
t.Fatalf("expected 3 underlying writes after second call (2+1), got %d", len(rec.writes))
|
||||
}
|
||||
if !bytes.Equal(rec.writes[2], []byte("XYZXYZ")) {
|
||||
t.Fatalf("third write = %q, want XYZXYZ", rec.writes[2])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitFirstWriteConn_ShortFirstWriteNoSplit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rec := newRecorder()
|
||||
c := &SplitFirstWriteConn{Conn: rec, SplitAt: 6}
|
||||
if _, err := c.Write([]byte("ABC")); err != nil { // len < SplitAt
|
||||
t.Fatalf("Write: %v", err)
|
||||
}
|
||||
if len(rec.writes) != 1 {
|
||||
t.Fatalf("expected single underlying write, got %d", len(rec.writes))
|
||||
}
|
||||
if !bytes.Equal(rec.writes[0], []byte("ABC")) {
|
||||
t.Fatalf("write = %q, want ABC", rec.writes[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitFirstWriteConn_DelayApplied(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rec := newRecorder()
|
||||
c := &SplitFirstWriteConn{Conn: rec, SplitAt: 4, Delay: 25 * time.Millisecond}
|
||||
start := time.Now()
|
||||
if _, err := c.Write([]byte("ABCDEFGH")); err != nil {
|
||||
t.Fatalf("Write: %v", err)
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
if elapsed < 20*time.Millisecond {
|
||||
t.Fatalf("expected delay >= 20ms, got %v", elapsed)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user