refactor(config): сгруппировать опции по доменам
Заменяет плоские структуры config.Client / config.Server на
доменно-сгруппированные под-структуры, чтобы форма повторяла концептуальные
слои прокси:
type Client struct {
TURN TURNOpts // Host, Port, UDP, N
Obf ObfOpts // WrapMode, WrapKey, GenWrapKey
Proxy ProxyOpts // Mode, Listen, Peer
VK VKOpts // Link, StreamsPerCred, ManualCaptcha
DNS DNSOpts // Mode, Servers
Log LogOpts // Debug
}
type Server struct { Obf; Proxy{Mode, Listen, Connect}; Log }
Enum ProxyMode (udp | tcpfwd | tcpfwd-bond) заменяет два параллельных bool
VLESSMode / VLESSBond в коде вызывающих. Поверхность CLI (-vless, -vless-bond)
не изменена; clientProxyMode / serverProxyMode в Parse* мапят bool в enum.
cmd/vk-turn-{client,server}/main.go: каждый cfg.X теперь cfg.Group.X. Тесты
обновлены; новый TestParseClient_ProxyMode покрывает все четыре комбинации
флагов (включая -vless-bond без -vless, который молча игнорируется как и раньше).
Без изменений флагов CLI, дефолтов, поведения и wire-формата. go build /
go vet / go test / gofmt чисто.
This commit is contained in:
1 parent
e212465f83
commit
6c9bfbae82
5 files changed
+240
-137
No files matched your search
+136
-64
@@ -3,6 +3,11 @@
|
||||
// Parse* functions are side-effect free: they validate inputs and decode the
|
||||
// wrap key, but do not touch the network, DNS, or process state. main() is
|
||||
// responsible for wiring those side effects after Parse* returns.
|
||||
//
|
||||
// V2-7: options are grouped by domain (TURN, Obf, Proxy, VK, DNS, Log) so the
|
||||
// struct shape mirrors the conceptual layers of the proxy instead of being a
|
||||
// flat bag of bools. CLI flag names, defaults, and behavior are bit-exact
|
||||
// compatible with the pre-V2-7 surface.
|
||||
package config
|
||||
|
||||
import (
|
||||
@@ -15,9 +20,6 @@ import (
|
||||
"github.com/cacggghp/vk-turn-proxy/internal/wire/srtpmimicry"
|
||||
)
|
||||
|
||||
// Mirrors of constants defined in internal/client/* packages, duplicated here
|
||||
// because internal/config cannot import internal/client/* (those packages
|
||||
// import internal/config indirectly through dependents — avoid the cycle).
|
||||
const (
|
||||
dnsModeUDP = "udp"
|
||||
dnsModeDoH = "doh"
|
||||
@@ -25,36 +27,73 @@ const (
|
||||
defaultStreamsPerCache = 10
|
||||
)
|
||||
|
||||
// ProxyMode selects the app-layer payload carried over the TURN tunnel.
|
||||
// On the client it can be all three; on the server only UDP / TCPFwd
|
||||
// (bond is auto-detected per stream by the magic prefix).
|
||||
type ProxyMode string
|
||||
|
||||
const (
|
||||
ProxyModeUDP ProxyMode = "udp" // -vless=false: UDP packet relay (WireGuard)
|
||||
ProxyModeTCPFwd ProxyMode = "tcpfwd" // -vless=true: TCP forwarder over smux
|
||||
ProxyModeTCPFwdBond ProxyMode = "tcpfwd-bond" // -vless=true -vless-bond=true: bonded TCP across N smux sessions
|
||||
)
|
||||
|
||||
// TURNOpts groups TURN-server-side options (where and how to reach TURN).
|
||||
type TURNOpts struct {
|
||||
Host string // -turn: override the TURN server IP/host
|
||||
Port string // -port: override the TURN port
|
||||
UDP bool // -udp: dial TURN via UDP (default: TCP/TLS)
|
||||
N int // -n: number of TURN streams (client only)
|
||||
}
|
||||
|
||||
// ObfOpts groups TURN-payload obfuscation options (SRTP-mimicry wrap).
|
||||
type ObfOpts struct {
|
||||
WrapMode bool // -wrap: enable SRTP-mimicry AEAD wrap on TURN payload
|
||||
WrapKey []byte // -wrap-key (decoded): 32-byte shared key; nil unless WrapMode
|
||||
GenWrapKey bool // -gen-wrap-key: print a fresh key and exit
|
||||
}
|
||||
|
||||
// ProxyOpts groups app-layer proxy options.
|
||||
type ProxyOpts struct {
|
||||
Mode ProxyMode // udp | tcpfwd | tcpfwd-bond (server: udp | tcpfwd)
|
||||
Listen string // -listen: local bind addr (client: WG/TCP entry; server: TURN entry)
|
||||
Connect string // -connect: upstream backend addr (server only)
|
||||
Peer string // -peer: server-side proxy addr the client dials (client only)
|
||||
}
|
||||
|
||||
// VKOpts groups VK-credentials and captcha options (client only).
|
||||
type VKOpts struct {
|
||||
Link string // -vk-link (sanitized to the join-code suffix)
|
||||
StreamsPerCred int // -streams-per-cred
|
||||
ManualCaptcha bool // -manual-captcha
|
||||
}
|
||||
|
||||
// DNSOpts groups DNS-resolution options (client only).
|
||||
type DNSOpts struct {
|
||||
Mode string // -dns: udp | doh | auto
|
||||
Servers []string // -dns-servers (comma-split); nil when flag empty
|
||||
}
|
||||
|
||||
// LogOpts groups logging options.
|
||||
type LogOpts struct {
|
||||
Debug bool // -debug
|
||||
}
|
||||
|
||||
// Client holds parsed and validated client CLI options.
|
||||
type Client struct {
|
||||
Host string
|
||||
Port string
|
||||
Listen string
|
||||
VKLink string // sanitized: stripped past "join/" and trimmed at /?#
|
||||
Peer string
|
||||
N int
|
||||
UDP bool
|
||||
VLESSMode bool
|
||||
VLESSBond bool
|
||||
WrapMode bool
|
||||
WrapKey []byte // nil unless WrapMode
|
||||
GenWrapKey bool
|
||||
StreamsPerCred int
|
||||
Debug bool
|
||||
ManualCaptcha bool
|
||||
DNSMode string
|
||||
DNSServers []string // nil when -dns-servers empty
|
||||
TURN TURNOpts
|
||||
Obf ObfOpts
|
||||
Proxy ProxyOpts
|
||||
VK VKOpts
|
||||
DNS DNSOpts
|
||||
Log LogOpts
|
||||
}
|
||||
|
||||
// Server holds parsed and validated server CLI options.
|
||||
type Server struct {
|
||||
Listen string
|
||||
Connect string
|
||||
VLESSMode bool
|
||||
WrapMode bool
|
||||
WrapKey []byte
|
||||
GenWrapKey bool
|
||||
Debug bool
|
||||
Obf ObfOpts
|
||||
Proxy ProxyOpts
|
||||
Log LogOpts
|
||||
}
|
||||
|
||||
// ParseClient parses args (excluding program name) into a Client.
|
||||
@@ -88,53 +127,62 @@ func ParseClient(args []string, errOut io.Writer) (*Client, error) {
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
Host: *host,
|
||||
Port: *port,
|
||||
Listen: *listen,
|
||||
Peer: *peerAddr,
|
||||
N: *n,
|
||||
UDP: *udp,
|
||||
VLESSMode: *vlessMode,
|
||||
VLESSBond: *vlessBond,
|
||||
WrapMode: *wrapMode,
|
||||
GenWrapKey: *genWrapKey,
|
||||
StreamsPerCred: *streamsPerCredFlag,
|
||||
Debug: *debugFlag,
|
||||
ManualCaptcha: *manualCaptchaFlag,
|
||||
DNSMode: *dnsFlag,
|
||||
TURN: TURNOpts{
|
||||
Host: *host,
|
||||
Port: *port,
|
||||
UDP: *udp,
|
||||
N: *n,
|
||||
},
|
||||
Obf: ObfOpts{
|
||||
WrapMode: *wrapMode,
|
||||
GenWrapKey: *genWrapKey,
|
||||
},
|
||||
Proxy: ProxyOpts{
|
||||
Mode: clientProxyMode(*vlessMode, *vlessBond),
|
||||
Listen: *listen,
|
||||
Peer: *peerAddr,
|
||||
},
|
||||
VK: VKOpts{
|
||||
StreamsPerCred: *streamsPerCredFlag,
|
||||
ManualCaptcha: *manualCaptchaFlag,
|
||||
},
|
||||
DNS: DNSOpts{
|
||||
Mode: *dnsFlag,
|
||||
},
|
||||
Log: LogOpts{
|
||||
Debug: *debugFlag,
|
||||
},
|
||||
}
|
||||
|
||||
switch c.DNSMode {
|
||||
switch c.DNS.Mode {
|
||||
case dnsModeUDP, dnsModeDoH, dnsModeAuto:
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid -dns value %q: must be udp | doh | auto", c.DNSMode)
|
||||
return nil, fmt.Errorf("invalid -dns value %q: must be udp | doh | auto", c.DNS.Mode)
|
||||
}
|
||||
if *dnsServersFlag != "" {
|
||||
c.DNSServers = strings.Split(*dnsServersFlag, ",")
|
||||
c.DNS.Servers = strings.Split(*dnsServersFlag, ",")
|
||||
}
|
||||
|
||||
// gen-wrap-key short-circuits: caller emits the key and exits, so skip
|
||||
// the rest of validation (no peer / vk-link needed for key gen).
|
||||
if c.GenWrapKey {
|
||||
if c.Obf.GenWrapKey {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
if c.Peer == "" {
|
||||
if c.Proxy.Peer == "" {
|
||||
return nil, errors.New("need peer address")
|
||||
}
|
||||
if *vklink == "" {
|
||||
return nil, errors.New("need vk-link")
|
||||
}
|
||||
key, err := srtpmimicry.DecodeKey(c.WrapMode, *wrapKeyHex)
|
||||
key, err := srtpmimicry.DecodeKey(c.Obf.WrapMode, *wrapKeyHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.WrapKey = key
|
||||
if c.StreamsPerCred <= 0 {
|
||||
c.Obf.WrapKey = key
|
||||
if c.VK.StreamsPerCred <= 0 {
|
||||
return nil, fmt.Errorf("-streams-per-cred must be positive")
|
||||
}
|
||||
if c.N <= 0 {
|
||||
c.N = 10
|
||||
if c.TURN.N <= 0 {
|
||||
c.TURN.N = 10
|
||||
}
|
||||
|
||||
parts := strings.Split(*vklink, "join/")
|
||||
@@ -142,7 +190,7 @@ func ParseClient(args []string, errOut io.Writer) (*Client, error) {
|
||||
if idx := strings.IndexAny(link, "/?#"); idx != -1 {
|
||||
link = link[:idx]
|
||||
}
|
||||
c.VKLink = link
|
||||
c.VK.Link = link
|
||||
|
||||
return c, nil
|
||||
}
|
||||
@@ -167,26 +215,50 @@ func ParseServer(args []string, errOut io.Writer) (*Server, error) {
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
Listen: *listen,
|
||||
Connect: *connect,
|
||||
VLESSMode: *vlessMode,
|
||||
WrapMode: *wrapMode,
|
||||
GenWrapKey: *genWrapKey,
|
||||
Debug: *debugFlag,
|
||||
Obf: ObfOpts{
|
||||
WrapMode: *wrapMode,
|
||||
GenWrapKey: *genWrapKey,
|
||||
},
|
||||
Proxy: ProxyOpts{
|
||||
Mode: serverProxyMode(*vlessMode),
|
||||
Listen: *listen,
|
||||
Connect: *connect,
|
||||
},
|
||||
Log: LogOpts{
|
||||
Debug: *debugFlag,
|
||||
},
|
||||
}
|
||||
|
||||
if s.GenWrapKey {
|
||||
if s.Obf.GenWrapKey {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
if s.Connect == "" {
|
||||
if s.Proxy.Connect == "" {
|
||||
return nil, fmt.Errorf("server address is required")
|
||||
}
|
||||
key, err := srtpmimicry.DecodeKey(s.WrapMode, *wrapKeyHex)
|
||||
key, err := srtpmimicry.DecodeKey(s.Obf.WrapMode, *wrapKeyHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.WrapKey = key
|
||||
s.Obf.WrapKey = key
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func clientProxyMode(vless, bond bool) ProxyMode {
|
||||
switch {
|
||||
case vless && bond:
|
||||
return ProxyModeTCPFwdBond
|
||||
case vless:
|
||||
return ProxyModeTCPFwd
|
||||
default:
|
||||
return ProxyModeUDP
|
||||
}
|
||||
}
|
||||
|
||||
func serverProxyMode(vless bool) ProxyMode {
|
||||
if vless {
|
||||
return ProxyModeTCPFwd
|
||||
}
|
||||
return ProxyModeUDP
|
||||
}
|
||||
@@ -21,23 +21,26 @@ func TestParseClient_Defaults(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
if c.Listen != "127.0.0.1:9000" {
|
||||
t.Errorf("Listen default: %q", c.Listen)
|
||||
if c.Proxy.Listen != "127.0.0.1:9000" {
|
||||
t.Errorf("Proxy.Listen default: %q", c.Proxy.Listen)
|
||||
}
|
||||
if c.N != 10 {
|
||||
t.Errorf("N default: %d", c.N)
|
||||
if c.TURN.N != 10 {
|
||||
t.Errorf("TURN.N default: %d", c.TURN.N)
|
||||
}
|
||||
if c.DNSMode != "auto" {
|
||||
t.Errorf("DNSMode default: %q", c.DNSMode)
|
||||
if c.DNS.Mode != "auto" {
|
||||
t.Errorf("DNS.Mode default: %q", c.DNS.Mode)
|
||||
}
|
||||
if c.StreamsPerCred != defaultStreamsPerCache {
|
||||
t.Errorf("StreamsPerCred default: %d", c.StreamsPerCred)
|
||||
if c.VK.StreamsPerCred != defaultStreamsPerCache {
|
||||
t.Errorf("VK.StreamsPerCred default: %d", c.VK.StreamsPerCred)
|
||||
}
|
||||
if c.VKLink != "abcdef" {
|
||||
t.Errorf("VKLink: %q (expected abcdef)", c.VKLink)
|
||||
if c.VK.Link != "abcdef" {
|
||||
t.Errorf("VK.Link: %q (expected abcdef)", c.VK.Link)
|
||||
}
|
||||
if c.WrapKey != nil {
|
||||
t.Errorf("WrapKey should be nil when -wrap absent")
|
||||
if c.Obf.WrapKey != nil {
|
||||
t.Errorf("Obf.WrapKey should be nil when -wrap absent")
|
||||
}
|
||||
if c.Proxy.Mode != ProxyModeUDP {
|
||||
t.Errorf("Proxy.Mode default: %q (expected udp)", c.Proxy.Mode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,8 +53,8 @@ func TestParseClient_VKLinkStrip(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.VKLink != "CODE123" {
|
||||
t.Errorf("VKLink: %q (expected CODE123)", c.VKLink)
|
||||
if c.VK.Link != "CODE123" {
|
||||
t.Errorf("VK.Link: %q (expected CODE123)", c.VK.Link)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,8 +94,8 @@ func TestParseClient_WrapKeyOK(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(c.WrapKey) != 32 {
|
||||
t.Errorf("WrapKey len: %d", len(c.WrapKey))
|
||||
if len(c.Obf.WrapKey) != 32 {
|
||||
t.Errorf("Obf.WrapKey len: %d", len(c.Obf.WrapKey))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,8 +113,8 @@ func TestParseClient_NClampedToTen(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.N != 10 {
|
||||
t.Errorf("N: %d (expected 10)", c.N)
|
||||
if c.TURN.N != 10 {
|
||||
t.Errorf("TURN.N: %d (expected 10)", c.TURN.N)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,8 +124,8 @@ func TestParseClient_DNSServersSplit(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(c.DNSServers) != 2 || c.DNSServers[0] != "1.1.1.1" || c.DNSServers[1] != "8.8.8.8:53" {
|
||||
t.Errorf("DNSServers: %v", c.DNSServers)
|
||||
if len(c.DNS.Servers) != 2 || c.DNS.Servers[0] != "1.1.1.1" || c.DNS.Servers[1] != "8.8.8.8:53" {
|
||||
t.Errorf("DNS.Servers: %v", c.DNS.Servers)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,8 +134,8 @@ func TestParseClient_GenWrapKeySkipsPeerCheck(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("gen-wrap-key should not require peer/vk-link: %v", err)
|
||||
}
|
||||
if !c.GenWrapKey {
|
||||
t.Errorf("GenWrapKey not set")
|
||||
if !c.Obf.GenWrapKey {
|
||||
t.Errorf("Obf.GenWrapKey not set")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,16 +147,44 @@ func TestParseClient_HelpReturnsErrHelp(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseClient_ProxyMode(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
args []string
|
||||
want ProxyMode
|
||||
}{
|
||||
{"default-udp", nil, ProxyModeUDP},
|
||||
{"vless", []string{"-vless"}, ProxyModeTCPFwd},
|
||||
{"vless-bond", []string{"-vless", "-vless-bond"}, ProxyModeTCPFwdBond},
|
||||
{"bond-without-vless-ignored", []string{"-vless-bond"}, ProxyModeUDP},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
args := append(validClientArgs(), tc.args...)
|
||||
c, err := ParseClient(args, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.Proxy.Mode != tc.want {
|
||||
t.Errorf("Proxy.Mode = %q, want %q", c.Proxy.Mode, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseServer_Defaults(t *testing.T) {
|
||||
s, err := ParseServer([]string{"-connect", "backend:1234"}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.Listen != "0.0.0.0:56000" {
|
||||
t.Errorf("Listen default: %q", s.Listen)
|
||||
if s.Proxy.Listen != "0.0.0.0:56000" {
|
||||
t.Errorf("Proxy.Listen default: %q", s.Proxy.Listen)
|
||||
}
|
||||
if s.Connect != "backend:1234" {
|
||||
t.Errorf("Connect: %q", s.Connect)
|
||||
if s.Proxy.Connect != "backend:1234" {
|
||||
t.Errorf("Proxy.Connect: %q", s.Proxy.Connect)
|
||||
}
|
||||
if s.Proxy.Mode != ProxyModeUDP {
|
||||
t.Errorf("Proxy.Mode default: %q", s.Proxy.Mode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,8 +214,8 @@ func TestParseServer_WrapKeyOK(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(s.WrapKey) != 32 {
|
||||
t.Errorf("WrapKey len: %d", len(s.WrapKey))
|
||||
if len(s.Obf.WrapKey) != 32 {
|
||||
t.Errorf("Obf.WrapKey len: %d", len(s.Obf.WrapKey))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +224,17 @@ func TestParseServer_GenWrapKeySkipsConnectCheck(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("gen-wrap-key should not require -connect: %v", err)
|
||||
}
|
||||
if !s.GenWrapKey {
|
||||
t.Errorf("GenWrapKey not set")
|
||||
if !s.Obf.GenWrapKey {
|
||||
t.Errorf("Obf.GenWrapKey not set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseServer_ProxyMode(t *testing.T) {
|
||||
s, err := ParseServer([]string{"-connect", "x:1", "-vless"}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.Proxy.Mode != ProxyModeTCPFwd {
|
||||
t.Errorf("Proxy.Mode = %q, want tcpfwd", s.Proxy.Mode)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user