refactor(layout): переименовать пакеты (split wire/transport/proxy)

internal/wrap → internal/wire/srtpmimicry; правки doc ("обфускация, не security").
internal/bond/{frame,client,server} → internal/wire/bondframe,
internal/proxy/bondclient, internal/proxy/bondserver.
internal/netadapt → internal/netconn.
internal/turnpipe → internal/transport/turndial; internal/dtlsdial →
internal/transport/dtlsdial.
tcputil → internal/transport/kcptun; selectedKCPProfile и selectedFEC теперь
кешируются через sync.Once (env читается один раз вместо per-connection).
internal/proxy/udp → internal/proxy/udprelay; internal/proxy/vless →
internal/proxy/tcpfwd. Флаг CLI -vless сохранён.
internal/ish → internal/client/ish; doc пакета расширен (iSH = Linux user-mode
эмулятор на iOS, только linux/386).

Чисто механическое переименование + обновление import-путей/идентификаторов.
Wire-формат, флаги CLI и поведение не изменены. go build + go test зелёные.
This commit is contained in:
samosvalishe committed 2026-05-19 20:42:46 +03:00
1 parent 7fd2d9594d
commit 1fb869b3e1
27 files changed
+178 -147

No files matched your search

+157
View File
@@ -0,0 +1,157 @@
// Package turndial centralizes the TURN dial+allocate pipeline shared by
// the client UDP (oneTurnConnection) and VLESS (createSmuxSession) modes.
//
// One call to Open performs: parse target, apply host/port overrides,
// resolve UDP addr, dial UDP-or-TCP (with SplitFirstWriteConn over TCP),
// turn.NewClient, Listen, Allocate. Returns the relay PacketConn plus a
// Close that tears down the allocation, TURN client, and underlying conn.
package turndial
import (
"context"
"fmt"
"net"
"time"
"github.com/cacggghp/vk-turn-proxy/internal/netconn"
"github.com/pion/logging"
"github.com/pion/turn/v5"
)
// Config configures one Open call.
type Config struct {
// HostOverride, if non-empty, replaces the host part returned by the
// credentials lookup.
HostOverride string
// PortOverride, if non-empty, replaces the port part returned by the
// credentials lookup.
PortOverride string
// UDP=true dials TURN over UDP; otherwise over TCP via STUNConn.
UDP bool
// DialTimeout caps the TCP dial. Zero defaults to 5s.
DialTimeout time.Duration
}
// Stream is a live TURN allocation with its dependencies. Close tears
// everything down in reverse order.
type Stream struct {
// Relay is the allocated relay PacketConn returned by turn.Client.Allocate.
Relay net.PacketConn
// ServerUDPAddr is the resolved TURN server UDP address (host:port).
ServerUDPAddr *net.UDPAddr
close func() error
}
// Close releases the allocation, TURN client and underlying transport.
// Safe to call once. Returns the first non-nil error (if any).
func (s *Stream) Close() error {
if s == nil || s.close == nil {
return nil
}
return s.close()
}
// Open dials TURN, creates a turn.Client and allocates a relay. rawAddr is
// the host:port returned by the credentials lookup; user/pass are the TURN
// long-term credentials.
func Open(ctx context.Context, cfg Config, peer *net.UDPAddr, user, pass, rawAddr string) (*Stream, error) {
urlhost, urlport, err := net.SplitHostPort(rawAddr)
if err != nil {
return nil, fmt.Errorf("parse TURN addr: %w", err)
}
if cfg.HostOverride != "" {
urlhost = cfg.HostOverride
}
if cfg.PortOverride != "" {
urlport = cfg.PortOverride
}
turnServerAddr := net.JoinHostPort(urlhost, urlport)
turnServerUDPAddr, err := net.ResolveUDPAddr("udp", turnServerAddr)
if err != nil {
return nil, fmt.Errorf("resolve TURN addr: %w", err)
}
turnServerAddr = turnServerUDPAddr.String()
dialTimeout := cfg.DialTimeout
if dialTimeout == 0 {
dialTimeout = 5 * time.Second
}
var (
turnConn net.PacketConn
closeConn func() error
)
if cfg.UDP {
c, derr := net.DialUDP("udp", nil, turnServerUDPAddr) //nolint:noctx
if derr != nil {
return nil, fmt.Errorf("dial TURN (udp): %w", derr)
}
turnConn = &netconn.ConnectedUDPConn{UDPConn: c}
closeConn = c.Close
} else {
dctx, cancel := context.WithTimeout(ctx, dialTimeout)
defer cancel()
var d net.Dialer
c, derr := d.DialContext(dctx, "tcp", turnServerAddr)
if derr != nil {
return nil, fmt.Errorf("dial TURN (tcp): %w", derr)
}
wrapped := &netconn.SplitFirstWriteConn{Conn: c, SplitAt: 6, Delay: 20 * time.Millisecond}
turnConn = turn.NewSTUNConn(wrapped)
closeConn = c.Close
}
var addrFamily turn.RequestedAddressFamily
if peer.IP.To4() != nil {
addrFamily = turn.RequestedAddressFamilyIPv4
} else {
addrFamily = turn.RequestedAddressFamilyIPv6
}
client, err := turn.NewClient(&turn.ClientConfig{
STUNServerAddr: turnServerAddr,
TURNServerAddr: turnServerAddr,
Conn: turnConn,
Net: netconn.New(),
Username: user,
Password: pass,
RequestedAddressFamily: addrFamily,
LoggerFactory: logging.NewDefaultLoggerFactory(),
})
if err != nil {
if cerr := closeConn(); cerr != nil {
err = fmt.Errorf("%w (close: %v)", err, cerr)
}
return nil, fmt.Errorf("create TURN client: %w", err)
}
if err = client.Listen(); err != nil {
client.Close()
if cerr := closeConn(); cerr != nil {
err = fmt.Errorf("%w (close: %v)", err, cerr)
}
return nil, fmt.Errorf("TURN listen: %w", err)
}
relay, err := client.Allocate()
if err != nil {
client.Close()
if cerr := closeConn(); cerr != nil {
err = fmt.Errorf("%w (close: %v)", err, cerr)
}
return nil, fmt.Errorf("TURN allocate: %w", err)
}
return &Stream{
Relay: relay,
ServerUDPAddr: turnServerUDPAddr,
close: func() error {
var firstErr error
if cerr := relay.Close(); cerr != nil {
firstErr = cerr
}
client.Close()
if cerr := closeConn(); cerr != nil && firstErr == nil {
firstErr = cerr
}
return firstErr
},
}, nil
}
@@ -0,0 +1,36 @@
package turndial
import (
"context"
"net"
"strings"
"testing"
"time"
)
// Open dialing a bogus address must surface a wrapped resolve/dial error.
func TestOpen_BadAddress(t *testing.T) {
peer := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 1}
_, err := Open(context.Background(), Config{}, peer, "u", "p", "not-a-host-port")
if err == nil {
t.Fatal("expected error for malformed addr")
}
if !strings.Contains(err.Error(), "parse TURN addr") {
t.Fatalf("unexpected error: %v", err)
}
}
// Open with overrides resolves the override host:port (still fails because
// there's no TURN server, but the dial error must mention the override).
func TestOpen_HostOverrideApplied(t *testing.T) {
peer := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 1}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
_, err := Open(ctx, Config{HostOverride: "127.0.0.1", PortOverride: "1", UDP: false, DialTimeout: 200 * time.Millisecond}, peer, "u", "p", "8.8.8.8:443")
if err == nil {
t.Fatal("expected dial error against unreachable :1")
}
if !strings.Contains(err.Error(), "dial TURN") {
t.Fatalf("expected dial error, got: %v", err)
}
}