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:
samosvalishe committed 2026-05-19 00:29:47 +03:00
1 parent 948108a33a
commit ada0c746aa
8 files changed
+817 -580

No files matched your search

+133
View File
@@ -0,0 +1,133 @@
// Package bond contains the wire format used by the VLESS bond multi-lane
// transport (hello + framed data/FIN). Both the client (initiator) and the
// server (acceptor) speak the same encoding.
package bond
import (
"encoding/binary"
"fmt"
"io"
"net"
"time"
)
const (
Version uint8 = 1
Magic = "VLB1"
FrameData byte = 1
FrameFIN byte = 2
MaxChunk = 16 * 1024
LaneAttachTimeout = 300 * time.Millisecond
)
// Hello is the per-lane handshake header sent right after a smux stream opens.
type Hello struct {
ConnID uint64
LaneIndex uint16
LaneCount uint16
}
// Frame is a single bonded data or FIN unit, identified by Seq within a ConnID.
type Frame struct {
Type byte
Seq uint64
Data []byte
}
// WriteHello encodes and writes a Hello to w using the bond wire format.
func WriteHello(w io.Writer, connID uint64, laneIndex, laneCount uint16) error {
var hdr [17]byte
copy(hdr[0:4], Magic)
hdr[4] = Version
binary.BigEndian.PutUint64(hdr[5:13], connID)
binary.BigEndian.PutUint16(hdr[13:15], laneIndex)
binary.BigEndian.PutUint16(hdr[15:17], laneCount)
_, err := w.Write(hdr[:])
return err
}
// ReadHelloAfterMagic finishes reading a Hello whose first 4 magic bytes have
// already been consumed (server pre-peeks the magic to multiplex protocols).
func ReadHelloAfterMagic(r io.Reader, magic [4]byte) (Hello, error) {
var hdr [17]byte
copy(hdr[0:4], magic[:])
if _, err := io.ReadFull(r, hdr[4:]); err != nil {
return Hello{}, err
}
return ParseHelloHeader(hdr[:])
}
// ParseHelloHeader decodes a 17-byte Hello header from hdr.
func ParseHelloHeader(hdr []byte) (Hello, error) {
if len(hdr) != 17 {
return Hello{}, fmt.Errorf("bad bond hello size: %d", len(hdr))
}
if string(hdr[0:4]) != Magic {
return Hello{}, fmt.Errorf("bad bond magic")
}
if hdr[4] != Version {
return Hello{}, fmt.Errorf("unsupported bond version: %d", hdr[4])
}
return Hello{
ConnID: binary.BigEndian.Uint64(hdr[5:13]),
LaneIndex: binary.BigEndian.Uint16(hdr[13:15]),
LaneCount: binary.BigEndian.Uint16(hdr[15:17]),
}, nil
}
// WriteFrame writes a single Frame to w (header + payload).
func WriteFrame(w io.Writer, typ byte, seq uint64, data []byte) error {
var hdr [13]byte
hdr[0] = typ
binary.BigEndian.PutUint64(hdr[1:9], seq)
binary.BigEndian.PutUint32(hdr[9:13], uint32(len(data)))
if _, err := w.Write(hdr[:]); err != nil {
return err
}
if len(data) == 0 {
return nil
}
_, err := w.Write(data)
return err
}
// ReadFrame reads one Frame from r. Payloads over 4 MiB are rejected.
func ReadFrame(r io.Reader) (Frame, error) {
var hdr [13]byte
if _, err := io.ReadFull(r, hdr[:]); err != nil {
return Frame{}, err
}
size := binary.BigEndian.Uint32(hdr[9:13])
if size > 4*1024*1024 {
return Frame{}, fmt.Errorf("bond frame too large: %d", size)
}
f := Frame{
Type: hdr[0],
Seq: binary.BigEndian.Uint64(hdr[1:9]),
}
if size > 0 {
f.Data = make([]byte, size)
if _, err := io.ReadFull(r, f.Data); err != nil {
return Frame{}, err
}
}
return f, nil
}
// CloseWrite half-closes the write side of conn if the underlying type
// supports it (TCPConn, smux.Stream, …); otherwise it is a no-op. errf is
// invoked with the error if CloseWrite fails; callers typically pass a
// debug-gated log func.
func CloseWrite(conn net.Conn, errf func(format string, v ...any)) {
type closeWriter interface {
CloseWrite() error
}
if cw, ok := conn.(closeWriter); ok {
if err := cw.CloseWrite(); err != nil && errf != nil {
errf("CloseWrite failed: %v", err)
}
}
}
+128
View File
@@ -0,0 +1,128 @@
package bond
import (
"bytes"
"encoding/binary"
"io"
"testing"
)
func TestHelloRoundTrip(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
if err := WriteHello(&buf, 0xCAFEBABEDEADBEEF, 3, 7); err != nil {
t.Fatalf("WriteHello: %v", err)
}
if buf.Len() != 17 {
t.Fatalf("hello size = %d, want 17", buf.Len())
}
var magic [4]byte
copy(magic[:], buf.Bytes()[:4])
if string(magic[:]) != Magic {
t.Fatalf("magic = %q, want %q", magic, Magic)
}
// Consume magic to simulate the server pre-peek path.
buf.Next(4)
got, err := ReadHelloAfterMagic(&buf, magic)
if err != nil {
t.Fatalf("ReadHelloAfterMagic: %v", err)
}
want := Hello{ConnID: 0xCAFEBABEDEADBEEF, LaneIndex: 3, LaneCount: 7}
if got != want {
t.Fatalf("hello = %+v, want %+v", got, want)
}
}
func TestParseHelloHeader_BadInputs(t *testing.T) {
t.Parallel()
if _, err := ParseHelloHeader(make([]byte, 16)); err == nil {
t.Errorf("short header should fail")
}
bad := make([]byte, 17)
copy(bad[0:4], "XXXX")
if _, err := ParseHelloHeader(bad); err == nil {
t.Errorf("bad magic should fail")
}
bad = make([]byte, 17)
copy(bad[0:4], Magic)
bad[4] = 99
if _, err := ParseHelloHeader(bad); err == nil {
t.Errorf("unsupported version should fail")
}
}
func TestFrameRoundTrip(t *testing.T) {
t.Parallel()
cases := []Frame{
{Type: FrameData, Seq: 0, Data: []byte("hello")},
{Type: FrameData, Seq: 0xFFFFFFFFFFFFFFFF, Data: bytes.Repeat([]byte{0xAB}, 1500)},
{Type: FrameFIN, Seq: 42, Data: nil},
}
for _, want := range cases {
var buf bytes.Buffer
if err := WriteFrame(&buf, want.Type, want.Seq, want.Data); err != nil {
t.Fatalf("WriteFrame: %v", err)
}
got, err := ReadFrame(&buf)
if err != nil {
t.Fatalf("ReadFrame: %v", err)
}
if got.Type != want.Type || got.Seq != want.Seq || !bytes.Equal(got.Data, want.Data) {
t.Fatalf("frame = %+v, want %+v", got, want)
}
}
}
func TestReadFrame_RejectsOversize(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
var hdr [13]byte
hdr[0] = FrameData
binary.BigEndian.PutUint32(hdr[9:13], 4*1024*1024+1)
buf.Write(hdr[:])
if _, err := ReadFrame(&buf); err == nil {
t.Fatalf("oversize frame should be rejected")
}
}
func TestReadFrame_TruncatedHeader(t *testing.T) {
t.Parallel()
buf := bytes.NewReader([]byte{1, 2, 3})
_, err := ReadFrame(buf)
if err == nil {
t.Fatalf("truncated header should fail")
}
}
func TestReadFrame_TruncatedPayload(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
if err := WriteFrame(&buf, FrameData, 1, []byte("hello world")); err != nil {
t.Fatalf("WriteFrame: %v", err)
}
truncated := buf.Bytes()[:13+5] // header + 5 of 11 payload bytes
if _, err := ReadFrame(bytes.NewReader(truncated)); err == nil {
t.Fatalf("truncated payload should fail")
}
}
// Sanity: ReadHelloAfterMagic surfaces underlying EOF, not a silent zero.
func TestReadHelloAfterMagic_EOF(t *testing.T) {
t.Parallel()
var magic [4]byte
copy(magic[:], Magic)
if _, err := ReadHelloAfterMagic(bytes.NewReader(nil), magic); err == nil || err == io.ErrShortBuffer {
t.Fatalf("expected EOF-like error, got %v", err)
}
}