// Package bondserver implements the server side of the bonded VLESS lane: // a single backend TCP connection multiplexed across N smux streams that all // share a ConnID. Frame wire-format lives in internal/wire/bondframe; this package // wires the backend TCP <-> lanes copy loops and the per-ConnID registry. package bondserver import ( "context" "io" "net" "sync" "time" "github.com/samosvalishe/btp/internal/logx" "github.com/samosvalishe/btp/internal/wire/bondframe" "github.com/xtaci/smux" ) // laneStream is the subset of *smux.Stream that serverLane needs. Defined as // an interface so unit tests can inject in-memory pipes without spinning up a // real smux session. type laneStream interface { io.Reader io.Writer SetDeadline(time.Time) error } // Deps groups host-process dependencies needed by the bond server. type Deps struct { Log logx.Logger } func (d *Deps) log() logx.Logger { if d.Log == nil { return logx.Nop() } return d.Log } // Registry deduplicates concurrent lanes that share a ConnID into a single // backend TCP connection. type Registry struct { deps Deps mu sync.Mutex conns map[uint64]*serverConn } // NewRegistry creates an empty Registry. func NewRegistry(deps Deps) *Registry { return &Registry{deps: deps, conns: make(map[uint64]*serverConn)} } // HandleStreamAfterMagic accepts a smux stream whose first 4 magic bytes have // already been consumed (server-side multiplex pre-peek), reads the Hello, // attaches as a lane, and blocks until the underlying bond connection is done. func (r *Registry) HandleStreamAfterMagic(ctx context.Context, stream *smux.Stream, connectAddr string, magic [4]byte) { r.handleStream(ctx, stream, connectAddr, func(rd io.Reader) (bondframe.Hello, error) { return bondframe.ReadHelloAfterMagic(rd, magic) }) } func (r *Registry) handleStream(ctx context.Context, stream *smux.Stream, connectAddr string, readHello func(io.Reader) (bondframe.Hello, error)) { defer func() { if err := stream.Close(); err != nil && err != smux.ErrGoAway { r.deps.log().Errorf("bondserver: close smux stream: %v", err) } }() hello, err := readHello(stream) if err != nil { r.deps.log().Errorf("bondserver: bond hello: %v", err) return } conn := r.get(ctx, hello.ConnID, connectAddr) conn.addLane(&serverLane{ index: hello.LaneIndex, stream: stream, }, hello.LaneCount) select { case <-ctx.Done(): case <-conn.done: } } func (r *Registry) get(ctx context.Context, id uint64, connectAddr string) *serverConn { r.mu.Lock() defer r.mu.Unlock() if c := r.conns[id]; c != nil { return c } connCtx, cancel := context.WithCancel(ctx) c := &serverConn{ deps: &r.deps, id: id, connectAddr: connectAddr, ctx: connCtx, cancel: cancel, done: make(chan struct{}), ready: make(chan struct{}, 1), recvCh: make(chan bondframe.Frame, 1024), } r.conns[id] = c go func() { <-c.done r.mu.Lock() if r.conns[id] == c { delete(r.conns, id) } r.mu.Unlock() }() return c } type serverLane struct { index uint16 stream laneStream mu sync.Mutex } type serverConn struct { deps *Deps id uint64 connectAddr string ctx context.Context cancel context.CancelFunc done chan struct{} lanesMu sync.RWMutex lanes []*serverLane want uint16 ready chan struct{} recvCh chan bondframe.Frame once sync.Once } func (c *serverConn) addLane(l *serverLane, laneCount uint16) { c.lanesMu.Lock() if laneCount > c.want { c.want = laneCount } c.lanes = append(c.lanes, l) count := len(c.lanes) c.lanesMu.Unlock() c.deps.log().Debugf("[bond %d] lane %d attached (lanes=%d)", c.id, l.index, count) select { case c.ready <- struct{}{}: default: } go c.readLane(l) c.once.Do(func() { go c.run() }) } func (c *serverConn) snapshotLanes() []*serverLane { c.lanesMu.RLock() defer c.lanesMu.RUnlock() out := make([]*serverLane, len(c.lanes)) copy(out, c.lanes) return out } func (c *serverConn) removeLane(l *serverLane) int { c.lanesMu.Lock() defer c.lanesMu.Unlock() for i, lane := range c.lanes { if lane == l { c.lanes = append(c.lanes[:i], c.lanes[i+1:]...) break } } return len(c.lanes) } func (c *serverConn) waitForInitialLanes() { timer := time.NewTimer(bondframe.LaneAttachTimeout) defer timer.Stop() for { c.lanesMu.RLock() count := len(c.lanes) want := int(c.want) c.lanesMu.RUnlock() if want <= 0 || count >= want { return } select { case <-c.ctx.Done(): return case <-c.ready: case <-timer.C: c.deps.log().Debugf("[bond %d] starting with %d/%d lanes after attach timeout", c.id, count, want) return } } } func (c *serverConn) readLane(l *serverLane) { for { f, err := bondframe.ReadFrame(l.stream) if err != nil { left := c.removeLane(l) select { case <-c.ctx.Done(): default: if err != io.EOF { c.deps.log().Debugf("[bond %d] lane %d read error: %v (lanes=%d)", c.id, l.index, err, left) } if left == 0 { c.cancel() } } return } select { case c.recvCh <- f: case <-c.ctx.Done(): return } } } func (c *serverConn) run() { defer close(c.done) defer c.cancel() c.waitForInitialLanes() backendConn, err := net.DialTimeout("tcp", c.connectAddr, 10*time.Second) if err != nil { c.deps.log().Errorf("[bond %d] backend dial: %s", c.id, err) return } defer func() { if err := backendConn.Close(); err != nil { c.deps.log().Errorf("[bond %d] close backend connection: %v", c.id, err) } }() context.AfterFunc(c.ctx, func() { now := time.Now() if err := backendConn.SetDeadline(now); err != nil { c.deps.log().Errorf("[bond %d] backend deadline: %v", c.id, err) } for _, lane := range c.snapshotLanes() { if err := lane.stream.SetDeadline(now); err != nil { c.deps.log().Errorf("[bond %d] lane %d deadline: %v", c.id, lane.index, err) } } }) c.deps.log().Debugf("[bond %d] backend connected", c.id) var wg sync.WaitGroup wg.Go(func() { c.copyBondToBackend(backendConn) }) wg.Go(func() { defer c.cancel() c.copyBackendToBond(backendConn) }) wg.Wait() } func (c *serverConn) copyBondToBackend(backendConn net.Conn) { chunks := bondframe.Reorder(c.ctx, backendConn, c.recvCh, bondframe.ReorderHooks{ OnOverflow: func(have int) { c.deps.log().Errorf("[bond %d] pending map overflow (>%d), closing", c.id, bondframe.PendingCap) }, OnUnknownType: func(typ byte) { c.deps.log().Errorf("[bond %d] unknown frame type %d", c.id, typ) }, OnWriteError: func(err error) { c.deps.log().Errorf("[bond %d] backend write error: %v", c.id, err) }, OnCloseWrite: c.deps.log().Debugf, }) c.deps.log().Debugf("[bond %d] upload to backend finished chunks=%d", c.id, chunks) } func (c *serverConn) copyBackendToBond(backendConn net.Conn) { buf := make([]byte, bondframe.MaxChunk) var seq uint64 var laneIdx uint64 for { n, err := backendConn.Read(buf) if n > 0 { data := make([]byte, n) copy(data, buf[:n]) if writeErr := c.writeToNextLane(bondframe.FrameData, seq, data, &laneIdx); writeErr != nil { c.deps.log().Errorf("[bond %d] lane write data error: %v", c.id, writeErr) return } seq++ } if err != nil { lanes := c.snapshotLanes() for _, lane := range lanes { lane.mu.Lock() writeErr := bondframe.WriteFrame(lane.stream, bondframe.FrameFIN, seq, nil) lane.mu.Unlock() if writeErr != nil && c.ctx.Err() == nil { c.deps.log().Errorf("[bond %d] lane %d write FIN error: %v", c.id, lane.index, writeErr) } } c.deps.log().Debugf("[bond %d] download from backend finished chunks=%d", c.id, seq) return } select { case <-c.ctx.Done(): return default: } } } func (c *serverConn) writeToNextLane(typ byte, seq uint64, data []byte, laneIdx *uint64) error { lanes := c.snapshotLanes() for { if len(lanes) == 0 { select { case <-c.ctx.Done(): return c.ctx.Err() case <-time.After(10 * time.Millisecond): } lanes = c.snapshotLanes() continue } written := false for range lanes { lane := lanes[*laneIdx%uint64(len(lanes))] *laneIdx++ lane.mu.Lock() err := bondframe.WriteFrame(lane.stream, typ, seq, data) lane.mu.Unlock() if err == nil { written = true break } left := c.removeLane(lane) c.deps.log().Errorf("[bond %d] lane %d write error: %v (lanes=%d)", c.id, lane.index, err, left) if left == 0 { c.cancel() return err } } if written { return nil } lanes = c.snapshotLanes() } }