Refactor proxy structure and parser implementations to streamline protocol handling; remove unused marshaler interfaces and improve YAML serialization for various proxy types.

This commit is contained in:
2025-06-12 10:27:22 +10:00
parent 2d8508f390
commit cdf69ce65f
23 changed files with 235 additions and 548 deletions

View File

@ -1,5 +1,7 @@
package proxy
import "fmt"
type HTTPOptions struct {
Method string `yaml:"method,omitempty"`
Path []string `yaml:"path,omitempty"`
@ -31,76 +33,6 @@ type SmuxStruct struct {
Enabled bool `yaml:"enable"`
}
type Proxy struct {
Name string `yaml:"name,omitempty"`
Server string `yaml:"server,omitempty"`
Port int `yaml:"port,omitempty"`
Type string `yaml:"type,omitempty"`
Cipher string `yaml:"cipher,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
UDP bool `yaml:"udp,omitempty"`
UUID string `yaml:"uuid,omitempty"`
Network string `yaml:"network,omitempty"`
Flow string `yaml:"flow,omitempty"`
TLS bool `yaml:"tls,omitempty"`
ClientFingerprint string `yaml:"client-fingerprint,omitempty"`
Plugin string `yaml:"plugin,omitempty"`
PluginOpts map[string]any `yaml:"plugin-opts,omitempty"`
Smux SmuxStruct `yaml:"smux,omitempty"`
Sni string `yaml:"sni,omitempty"`
AllowInsecure bool `yaml:"allow-insecure,omitempty"`
Fingerprint string `yaml:"fingerprint,omitempty"`
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
Alpn []string `yaml:"alpn,omitempty"`
XUDP bool `yaml:"xudp,omitempty"`
Servername string `yaml:"servername,omitempty"`
WSOpts WSOptions `yaml:"ws-opts,omitempty"`
AlterID int `yaml:"alterId,omitempty"`
GrpcOpts GrpcOptions `yaml:"grpc-opts,omitempty"`
RealityOpts RealityOptions `yaml:"reality-opts,omitempty"`
Protocol string `yaml:"protocol,omitempty"`
Obfs string `yaml:"obfs,omitempty"`
ObfsParam string `yaml:"obfs-param,omitempty"`
ProtocolParam string `yaml:"protocol-param,omitempty"`
Remarks []string `yaml:"remarks,omitempty"`
HTTPOpts HTTPOptions `yaml:"http-opts,omitempty"`
HTTP2Opts HTTP2Options `yaml:"h2-opts,omitempty"`
PacketAddr bool `yaml:"packet-addr,omitempty"`
PacketEncoding string `yaml:"packet-encoding,omitempty"`
GlobalPadding bool `yaml:"global-padding,omitempty"`
AuthenticatedLength bool `yaml:"authenticated-length,omitempty"`
UDPOverTCP bool `yaml:"udp-over-tcp,omitempty"`
UDPOverTCPVersion int `yaml:"udp-over-tcp-version,omitempty"`
SubName string `yaml:"-"`
Up string `yaml:"up,omitempty"`
Down string `yaml:"down,omitempty"`
CustomCA string `yaml:"ca,omitempty"`
CustomCAString string `yaml:"ca-str,omitempty"`
CWND int `yaml:"cwnd,omitempty"`
Auth string `yaml:"auth,omitempty"`
ReceiveWindowConn int `yaml:"recv-window-conn,omitempty"`
ReceiveWindow int `yaml:"recv-window,omitempty"`
DisableMTUDiscovery bool `yaml:"disable-mtu-discovery,omitempty"`
FastOpen bool `yaml:"fast-open,omitempty"`
HopInterval int `yaml:"hop-interval,omitempty"`
Ports string `yaml:"ports,omitempty"`
AuthStringOLD string `yaml:"auth_str,omitempty"`
AuthString string `yaml:"auth-str,omitempty"`
Ip string `yaml:"ip,omitempty"`
Ipv6 string `yaml:"ipv6,omitempty"`
PrivateKey string `yaml:"private-key,omitempty"`
Workers int `yaml:"workers,omitempty"`
MTU int `yaml:"mtu,omitempty"`
PersistentKeepalive int `yaml:"persistent-keepalive,omitempty"`
Peers []WireGuardPeerOption `yaml:"peers,omitempty"`
RemoteDnsResolve bool `yaml:"remote-dns-resolve,omitempty"`
Dns []string `yaml:"dns,omitempty"`
IdleSessionCheckInterval int `yaml:"idle-session-check-interval,omitempty"`
IdleSessionTimeout int `yaml:"idle-session-timeout,omitempty"`
MinIdleSession int `yaml:"min-idle-session,omitempty"`
}
type WireGuardPeerOption struct {
Server string `yaml:"server"`
Port int `yaml:"port"`
@ -110,33 +42,104 @@ type WireGuardPeerOption struct {
AllowedIPs []string `yaml:"allowed-ips,omitempty"`
}
type _Proxy Proxy
type Proxy struct {
Type string
Name string
SubName string `yaml:"-"`
Anytls
Hysteria
Hysteria2
ShadowSocks
ShadowSocksR
Trojan
Vless
Vmess
Socks
}
func (p Proxy) MarshalYAML() (interface{}, error) {
// 尝试使用注册的序列化器
if marshaler, exists := GetMarshaler(p.Type); exists {
return marshaler.MarshalProxy(p)
}
// 保持向后兼容,对于未注册的类型使用原有逻辑
func (p Proxy) MarshalYAML() (any, error) {
switch p.Type {
case "vmess":
return ProxyToVmess(p), nil
case "ss":
return ProxyToShadowSocks(p), nil
case "ssr":
return ProxyToShadowSocksR(p), nil
case "vless":
return ProxyToVless(p), nil
case "trojan":
return ProxyToTrojan(p), nil
case "hysteria":
return ProxyToHysteria(p), nil
case "hysteria2":
return ProxyToHysteria2(p), nil
case "anytls":
return ProxyToAnytls(p), nil
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
Anytls `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
Anytls: p.Anytls,
}, nil
case "hysteria":
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
Hysteria `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
Hysteria: p.Hysteria,
}, nil
case "hysteria2":
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
Hysteria2 `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
Hysteria2: p.Hysteria2,
}, nil
case "ss":
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
ShadowSocks `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
ShadowSocks: p.ShadowSocks,
}, nil
case "ssr":
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
ShadowSocksR `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
ShadowSocksR: p.ShadowSocksR,
}, nil
case "trojan":
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
Trojan `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
Trojan: p.Trojan,
}, nil
case "vless":
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
Vless `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
Vless: p.Vless,
}, nil
case "vmess":
return struct {
Type string `yaml:"type"`
Name string `yaml:"name"`
Vmess `yaml:",inline"`
}{
Type: p.Type,
Name: p.Name,
Vmess: p.Vmess,
}, nil
default:
return _Proxy(p), nil
return nil, fmt.Errorf("unsupported proxy type: %s", p.Type)
}
}