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:
1 parent
7fd2d9594d
commit
1fb869b3e1
27 files changed
+178
-147
No files matched your search
@@ -0,0 +1,65 @@
|
||||
// Package dtlsdial wraps pion-dtls client setup (self-signed cert, EMS,
|
||||
// AES-128-GCM, send-only CID) plus an optional concurrency gate around the
|
||||
// handshake. Used by the client UDP and VLESS pipelines.
|
||||
package dtlsdial
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pion/dtls/v3"
|
||||
"github.com/pion/dtls/v3/pkg/crypto/selfsign"
|
||||
)
|
||||
|
||||
// Dialer configures the DTLS client handshake.
|
||||
type Dialer struct {
|
||||
// HandshakeTimeout caps the handshake context. Zero means no timeout.
|
||||
HandshakeTimeout time.Duration
|
||||
// HandshakeSem, if non-nil, gates concurrent handshakes (Dial blocks
|
||||
// until a slot is available or ctx fires).
|
||||
HandshakeSem chan struct{}
|
||||
}
|
||||
|
||||
// Dial generates a fresh self-signed cert, acquires the optional handshake
|
||||
// slot, and performs a DTLS client handshake over pc to peer. On success
|
||||
// returns the connected *dtls.Conn. Caller closes it.
|
||||
func (d *Dialer) Dial(ctx context.Context, pc net.PacketConn, peer *net.UDPAddr) (*dtls.Conn, error) {
|
||||
certificate, err := selfsign.GenerateSelfSigned()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d.HandshakeSem != nil {
|
||||
select {
|
||||
case d.HandshakeSem <- struct{}{}:
|
||||
defer func() { <-d.HandshakeSem }()
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
hsCtx := ctx
|
||||
if d.HandshakeTimeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
hsCtx, cancel = context.WithTimeout(ctx, d.HandshakeTimeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
dtlsConn, err := dtls.ClientWithOptions(
|
||||
pc,
|
||||
peer,
|
||||
dtls.WithCertificates(certificate),
|
||||
dtls.WithInsecureSkipVerify(true),
|
||||
dtls.WithExtendedMasterSecret(dtls.RequireExtendedMasterSecret),
|
||||
dtls.WithCipherSuites(dtls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
|
||||
dtls.WithConnectionIDGenerator(dtls.OnlySendCIDGenerator()),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := dtlsConn.HandshakeContext(hsCtx); err != nil {
|
||||
_ = dtlsConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
return dtlsConn, nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package dtlsdial
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakePC blocks forever on ReadFrom and accepts writes — enough to keep the
|
||||
// DTLS handshake stuck so we can exercise the gate/cancel paths.
|
||||
type fakePC struct{}
|
||||
|
||||
func (fakePC) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
select {} // block forever
|
||||
}
|
||||
func (fakePC) WriteTo(b []byte, _ net.Addr) (int, error) { return len(b), nil }
|
||||
func (fakePC) Close() error { return nil }
|
||||
func (fakePC) LocalAddr() net.Addr { return &net.UDPAddr{IP: net.IPv4zero} }
|
||||
func (fakePC) SetDeadline(time.Time) error { return nil }
|
||||
func (fakePC) SetReadDeadline(time.Time) error { return nil }
|
||||
func (fakePC) SetWriteDeadline(time.Time) error { return nil }
|
||||
|
||||
// When HandshakeSem is full and ctx fires, Dial must return ctx.Err()
|
||||
// without ever touching the underlying PacketConn.
|
||||
func TestDial_SemBlocksUntilCtx(t *testing.T) {
|
||||
sem := make(chan struct{}, 1)
|
||||
sem <- struct{}{} // pre-fill: no slots free
|
||||
d := &Dialer{HandshakeSem: sem, HandshakeTimeout: 5 * time.Second}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
peer := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1}
|
||||
_, err := d.Dial(ctx, fakePC{}, peer)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("expected DeadlineExceeded, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var _ = errors.New
|
||||
Reference in new issue
Block a user