Files
free-turn-proxy-orig/internal/proxy/bondserver/bondserver.go
T

364 lines
9.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package bondserver реализует серверную сторону bonded VLESS lane:
// одно backend TCP-соединение, мультиплексированное по N smux-потокам с общим
// ConnID. Wire-формат фреймов — internal/wire/bondframe; пакет соединяет
// copy-loop backend TCP ↔ lanes и реестр per-ConnID.
package bondserver
import (
"context"
"errors"
"io"
"net"
"sync"
"time"
"github.com/samosvalishe/btp/internal/auth"
"github.com/samosvalishe/btp/internal/logx"
"github.com/samosvalishe/btp/internal/wire/bondframe"
"github.com/xtaci/smux"
)
// laneStream — подмножество *smux.Stream, нужное serverLane. Определено как
// интерфейс, чтобы юнит-тесты могли подменять in-memory pipe без смоделированной
// smux-сессии.
type laneStream interface {
io.Reader
io.Writer
SetDeadline(time.Time) error
}
// Deps — зависимости хост-процесса для bond-сервера.
type Deps struct {
Log logx.Logger
}
func (d *Deps) log() logx.Logger {
if d.Log == nil {
return logx.Nop()
}
return d.Log
}
// connKey — составной ключ дедупликации для карты Registry.
// TenantID в ключе гарантирует, что ConnID разных тенантов никогда не пересекаются
// (обязательно для multi-tenant). В single-tenant режиме у всех ключей Tenant == auth.Anonymous.
type connKey struct {
Tenant auth.TenantID
ConnID uint64
}
// Registry дедуплицирует одновременные lane с одинаковой парой (TenantID, ConnID)
// в одно backend TCP-соединение.
type Registry struct {
deps Deps
mu sync.Mutex
conns map[connKey]*serverConn
}
func NewRegistry(deps Deps) *Registry {
return &Registry{deps: deps, conns: make(map[connKey]*serverConn)}
}
// HandleStreamAfterMagic принимает smux-поток, у которого первые 4 magic-байта
// уже прочитаны (server-side multiplex pre-peek), читает Hello, прикрепляет
// lane и блокируется до завершения bond-соединения.
//
// tenant ограничивает ключ дедупликации. В single-tenant — [auth.Anonymous];
// в multi-tenant — значение от реального Authenticator.
func (r *Registry) HandleStreamAfterMagic(ctx context.Context, tenant auth.TenantID, stream *smux.Stream, connectAddr string, magic [4]byte) {
r.handleStream(ctx, tenant, stream, connectAddr, func(rd io.Reader) (bondframe.Hello, error) {
return bondframe.ReadHelloAfterMagic(rd, magic)
})
}
func (r *Registry) handleStream(ctx context.Context, tenant auth.TenantID, stream *smux.Stream, connectAddr string, readHello func(io.Reader) (bondframe.Hello, error)) {
defer func() {
if err := stream.Close(); err != nil && !errors.Is(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, tenant, 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, tenant auth.TenantID, id uint64, connectAddr string) *serverConn {
r.mu.Lock()
defer r.mu.Unlock()
key := connKey{Tenant: tenant, ConnID: id}
if c := r.conns[key]; c != nil {
return c
}
connCtx, cancel := context.WithCancel(ctx)
c := &serverConn{
deps: &r.deps,
tenantID: tenant,
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[key] = c
go func() {
<-c.done
r.mu.Lock()
if r.conns[key] == c {
delete(r.conns, key)
}
r.mu.Unlock()
}()
return c
}
type serverLane struct {
index uint16
stream laneStream
mu sync.Mutex
}
type serverConn struct {
deps *Deps
tenantID auth.TenantID
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 {
// writeToNextLane — синхронная запись (WriteFrame возвращается до
// переключения lane), поэтому buf[:n] передаётся напрямую — аналог bondclient.
if writeErr := c.writeToNextLane(bondframe.FrameData, seq, buf[:n], &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()
}
}