From 1e8a79c2d21b37d45ce097bfe352d31042827f7c Mon Sep 17 00:00:00 2001 From: nite Date: Fri, 17 Oct 2025 18:13:49 +1100 Subject: [PATCH] #69 --- model/proxy/anytls.go | 26 +++++------ model/proxy/hysteria.go | 48 ++++++++++---------- model/proxy/hysteria2.go | 36 +++++++-------- model/proxy/proxy.go | 21 +++++++++ model/proxy/shadowsocks.go | 2 +- model/proxy/shadowsocksr.go | 18 ++++---- model/proxy/socks.go | 16 +++---- model/proxy/trojan.go | 2 +- model/proxy/vless.go | 2 +- model/proxy/vmess.go | 4 +- model/subscription.go | 90 ++++++++++++++++++------------------- parser/anytls.go | 2 +- parser/hysteria.go | 2 +- parser/hysteria2.go | 2 +- parser/shadowsocks.go | 2 +- parser/shadowsocksr.go | 2 +- parser/socks.go | 2 +- parser/trojan.go | 2 +- parser/vless.go | 2 +- parser/vmess.go | 4 +- test/yaml_test.go | 33 ++++++++++++++ 21 files changed, 186 insertions(+), 132 deletions(-) create mode 100644 test/yaml_test.go diff --git a/model/proxy/anytls.go b/model/proxy/anytls.go index d8efa27..1c949ef 100644 --- a/model/proxy/anytls.go +++ b/model/proxy/anytls.go @@ -2,17 +2,17 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/anytls.go type Anytls struct { - Server string `yaml:"server"` - Port int `yaml:"port"` - Password string `yaml:"password"` - ALPN []string `yaml:"alpn,omitempty"` - SNI string `yaml:"sni,omitempty"` - ECHOpts ECHOptions `yaml:"ech-opts,omitempty"` - ClientFingerprint string `yaml:"client-fingerprint,omitempty"` - SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` - Fingerprint string `yaml:"fingerprint,omitempty"` - UDP bool `yaml:"udp,omitempty"` - IdleSessionCheckInterval int `yaml:"idle-session-check-interval,omitempty"` - IdleSessionTimeout int `yaml:"idle-session-timeout,omitempty"` - MinIdleSession int `yaml:"min-idle-session,omitempty"` + Server string `yaml:"server"` + Port IntOrString `yaml:"port"` + Password string `yaml:"password"` + ALPN []string `yaml:"alpn,omitempty"` + SNI string `yaml:"sni,omitempty"` + ECHOpts ECHOptions `yaml:"ech-opts,omitempty"` + ClientFingerprint string `yaml:"client-fingerprint,omitempty"` + SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` + Fingerprint string `yaml:"fingerprint,omitempty"` + UDP bool `yaml:"udp,omitempty"` + IdleSessionCheckInterval int `yaml:"idle-session-check-interval,omitempty"` + IdleSessionTimeout int `yaml:"idle-session-timeout,omitempty"` + MinIdleSession int `yaml:"min-idle-session,omitempty"` } diff --git a/model/proxy/hysteria.go b/model/proxy/hysteria.go index 2d48fb5..11ee4f2 100644 --- a/model/proxy/hysteria.go +++ b/model/proxy/hysteria.go @@ -2,28 +2,28 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria.go type Hysteria struct { - Server string `yaml:"server"` - Port int `yaml:"port,omitempty"` - Ports string `yaml:"ports,omitempty"` - Protocol string `yaml:"protocol,omitempty"` - ObfsProtocol string `yaml:"obfs-protocol,omitempty"` // compatible with Stash - Up string `yaml:"up"` - UpSpeed int `yaml:"up-speed,omitempty"` // compatible with Stash - Down string `yaml:"down"` - DownSpeed int `yaml:"down-speed,omitempty"` // compatible with Stash - Auth string `yaml:"auth,omitempty"` - AuthString string `yaml:"auth-str,omitempty"` - Obfs string `yaml:"obfs,omitempty"` - SNI string `yaml:"sni,omitempty"` - ECHOpts ECHOptions `yaml:"ech-opts,omitempty"` - SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` - Fingerprint string `yaml:"fingerprint,omitempty"` - ALPN []string `yaml:"alpn,omitempty"` - CustomCA string `yaml:"ca,omitempty"` - CustomCAString string `yaml:"ca-str,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"` + Server string `yaml:"server"` + Port IntOrString `yaml:"port,omitempty"` + Ports string `yaml:"ports,omitempty"` + Protocol string `yaml:"protocol,omitempty"` + ObfsProtocol string `yaml:"obfs-protocol,omitempty"` // compatible with Stash + Up string `yaml:"up"` + UpSpeed int `yaml:"up-speed,omitempty"` // compatible with Stash + Down string `yaml:"down"` + DownSpeed int `yaml:"down-speed,omitempty"` // compatible with Stash + Auth string `yaml:"auth,omitempty"` + AuthString string `yaml:"auth-str,omitempty"` + Obfs string `yaml:"obfs,omitempty"` + SNI string `yaml:"sni,omitempty"` + ECHOpts ECHOptions `yaml:"ech-opts,omitempty"` + SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` + Fingerprint string `yaml:"fingerprint,omitempty"` + ALPN []string `yaml:"alpn,omitempty"` + CustomCA string `yaml:"ca,omitempty"` + CustomCAString string `yaml:"ca-str,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"` } diff --git a/model/proxy/hysteria2.go b/model/proxy/hysteria2.go index c1cae50..f63705c 100644 --- a/model/proxy/hysteria2.go +++ b/model/proxy/hysteria2.go @@ -2,24 +2,24 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria2.go type Hysteria2 struct { - Server string `yaml:"server"` - Port int `yaml:"port,omitempty"` - Ports string `yaml:"ports,omitempty"` - HopInterval int `yaml:"hop-interval,omitempty"` - Up string `yaml:"up,omitempty"` - Down string `yaml:"down,omitempty"` - Password string `yaml:"password,omitempty"` - Obfs string `yaml:"obfs,omitempty"` - ObfsPassword string `yaml:"obfs-password,omitempty"` - SNI string `yaml:"sni,omitempty"` - ECHOpts ECHOptions `yaml:"ech-opts,omitempty"` - SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` - Fingerprint string `yaml:"fingerprint,omitempty"` - ALPN []string `yaml:"alpn,omitempty"` - CustomCA string `yaml:"ca,omitempty"` - CustomCAString string `yaml:"ca-str,omitempty"` - CWND int `yaml:"cwnd,omitempty"` - UdpMTU int `yaml:"udp-mtu,omitempty"` + Server string `yaml:"server"` + Port IntOrString `yaml:"port,omitempty"` + Ports string `yaml:"ports,omitempty"` + HopInterval int `yaml:"hop-interval,omitempty"` + Up string `yaml:"up,omitempty"` + Down string `yaml:"down,omitempty"` + Password string `yaml:"password,omitempty"` + Obfs string `yaml:"obfs,omitempty"` + ObfsPassword string `yaml:"obfs-password,omitempty"` + SNI string `yaml:"sni,omitempty"` + ECHOpts ECHOptions `yaml:"ech-opts,omitempty"` + SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` + Fingerprint string `yaml:"fingerprint,omitempty"` + ALPN []string `yaml:"alpn,omitempty"` + CustomCA string `yaml:"ca,omitempty"` + CustomCAString string `yaml:"ca-str,omitempty"` + CWND int `yaml:"cwnd,omitempty"` + UdpMTU int `yaml:"udp-mtu,omitempty"` // quic-go special config InitialStreamReceiveWindow uint64 `yaml:"initial-stream-receive-window,omitempty"` diff --git a/model/proxy/proxy.go b/model/proxy/proxy.go index 76599a9..fe904b1 100644 --- a/model/proxy/proxy.go +++ b/model/proxy/proxy.go @@ -2,10 +2,31 @@ package proxy import ( "fmt" + "strconv" "gopkg.in/yaml.v3" ) +type IntOrString int + +func (i *IntOrString) UnmarshalYAML(value *yaml.Node) error { + intVal := 0 + err := yaml.Unmarshal([]byte(value.Value), &intVal) + if err == nil { + *i = IntOrString(intVal) + } + strVal := "" + err = yaml.Unmarshal([]byte(value.Value), &strVal) + if err == nil { + _int, err := strconv.ParseInt(strVal, 10, 64) + if err != nil { + *i = IntOrString(_int) + } + return err + } + return nil +} + type HTTPOptions struct { Method string `yaml:"method,omitempty"` Path []string `yaml:"path,omitempty"` diff --git a/model/proxy/shadowsocks.go b/model/proxy/shadowsocks.go index 127a246..38d4911 100644 --- a/model/proxy/shadowsocks.go +++ b/model/proxy/shadowsocks.go @@ -3,7 +3,7 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocks.go type ShadowSocks struct { Server string `yaml:"server"` - Port int `yaml:"port"` + Port IntOrString `yaml:"port"` Password string `yaml:"password"` Cipher string `yaml:"cipher"` UDP bool `yaml:"udp,omitempty"` diff --git a/model/proxy/shadowsocksr.go b/model/proxy/shadowsocksr.go index 74d669e..231b7c2 100644 --- a/model/proxy/shadowsocksr.go +++ b/model/proxy/shadowsocksr.go @@ -2,13 +2,13 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocksr.go type ShadowSocksR struct { - Server string `yaml:"server"` - Port int `yaml:"port"` - Password string `yaml:"password"` - Cipher string `yaml:"cipher"` - Obfs string `yaml:"obfs"` - ObfsParam string `yaml:"obfs-param,omitempty"` - Protocol string `yaml:"protocol"` - ProtocolParam string `yaml:"protocol-param,omitempty"` - UDP bool `yaml:"udp,omitempty"` + Server string `yaml:"server"` + Port IntOrString `yaml:"port"` + Password string `yaml:"password"` + Cipher string `yaml:"cipher"` + Obfs string `yaml:"obfs"` + ObfsParam string `yaml:"obfs-param,omitempty"` + Protocol string `yaml:"protocol"` + ProtocolParam string `yaml:"protocol-param,omitempty"` + UDP bool `yaml:"udp,omitempty"` } diff --git a/model/proxy/socks.go b/model/proxy/socks.go index c06c449..7dd7138 100644 --- a/model/proxy/socks.go +++ b/model/proxy/socks.go @@ -2,12 +2,12 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/socks5.go type Socks struct { - Server string `yaml:"server"` - Port int `yaml:"port"` - UserName string `yaml:"username,omitempty"` - Password string `yaml:"password,omitempty"` - TLS bool `yaml:"tls,omitempty"` - UDP bool `yaml:"udp,omitempty"` - SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` - Fingerprint string `yaml:"fingerprint,omitempty"` + Server string `yaml:"server"` + Port IntOrString `yaml:"port"` + UserName string `yaml:"username,omitempty"` + Password string `yaml:"password,omitempty"` + TLS bool `yaml:"tls,omitempty"` + UDP bool `yaml:"udp,omitempty"` + SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"` + Fingerprint string `yaml:"fingerprint,omitempty"` } diff --git a/model/proxy/trojan.go b/model/proxy/trojan.go index 20b71e0..cf8ec97 100644 --- a/model/proxy/trojan.go +++ b/model/proxy/trojan.go @@ -3,7 +3,7 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/trojan.go type Trojan struct { Server string `yaml:"server"` - Port int `yaml:"port"` + Port IntOrString `yaml:"port"` Password string `yaml:"password"` ALPN []string `yaml:"alpn,omitempty"` SNI string `yaml:"sni,omitempty"` diff --git a/model/proxy/vless.go b/model/proxy/vless.go index 72692e0..9f81992 100644 --- a/model/proxy/vless.go +++ b/model/proxy/vless.go @@ -3,7 +3,7 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vless.go type Vless struct { Server string `yaml:"server"` - Port int `yaml:"port"` + Port IntOrString `yaml:"port"` UUID string `yaml:"uuid"` Flow string `yaml:"flow,omitempty"` TLS bool `yaml:"tls,omitempty"` diff --git a/model/proxy/vmess.go b/model/proxy/vmess.go index b45bdf2..42d6d96 100644 --- a/model/proxy/vmess.go +++ b/model/proxy/vmess.go @@ -3,9 +3,9 @@ package proxy // https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vmess.go type Vmess struct { Server string `yaml:"server"` - Port int `yaml:"port"` + Port IntOrString `yaml:"port"` UUID string `yaml:"uuid"` - AlterID int `yaml:"alterId"` + AlterID IntOrString `yaml:"alterId"` Cipher string `yaml:"cipher"` UDP bool `yaml:"udp,omitempty"` Network string `yaml:"network,omitempty"` diff --git a/model/subscription.go b/model/subscription.go index 88aa5f1..5e1b886 100644 --- a/model/subscription.go +++ b/model/subscription.go @@ -14,51 +14,51 @@ type NodeList struct { // https://github.com/MetaCubeX/mihomo/blob/Meta/config/config.go RawConfig type Subscription struct { - Port int `yaml:"port,omitempty" json:"port"` - SocksPort int `yaml:"socks-port,omitempty" json:"socks-port"` - RedirPort int `yaml:"redir-port,omitempty" json:"redir-port"` - TProxyPort int `yaml:"tproxy-port,omitempty" json:"tproxy-port"` - MixedPort int `yaml:"mixed-port,omitempty" json:"mixed-port"` - ShadowSocksConfig string `yaml:"ss-config,omitempty" json:"ss-config"` - VmessConfig string `yaml:"vmess-config,omitempty" json:"vmess-config"` - InboundTfo bool `yaml:"inbound-tfo,omitempty" json:"inbound-tfo"` - InboundMPTCP bool `yaml:"inbound-mptcp,omitempty" json:"inbound-mptcp"` - Authentication []string `yaml:"authentication,omitempty" json:"authentication"` - SkipAuthPrefixes []netip.Prefix `yaml:"skip-auth-prefixes,omitempty" json:"skip-auth-prefixes"` - LanAllowedIPs []netip.Prefix `yaml:"lan-allowed-ips,omitempty" json:"lan-allowed-ips"` - LanDisAllowedIPs []netip.Prefix `yaml:"lan-disallowed-ips,omitempty" json:"lan-disallowed-ips"` - AllowLan bool `yaml:"allow-lan,omitempty" json:"allow-lan"` - BindAddress string `yaml:"bind-address,omitempty" json:"bind-address"` - Mode string `yaml:"mode,omitempty" json:"mode"` - UnifiedDelay bool `yaml:"unified-delay,omitempty" json:"unified-delay"` - LogLevel string `yaml:"log-level,omitempty" json:"log-level"` - IPv6 bool `yaml:"ipv6,omitempty" json:"ipv6"` - ExternalController string `yaml:"external-controller,omitempty" json:"external-controller"` - ExternalControllerPipe string `yaml:"external-controller-pipe,omitempty" json:"external-controller-pipe"` - ExternalControllerUnix string `yaml:"external-controller-unix,omitempty" json:"external-controller-unix"` - ExternalControllerTLS string `yaml:"external-controller-tls,omitempty" json:"external-controller-tls"` - ExternalControllerCors C.RawCors `yaml:"external-controller-cors,omitempty" json:"external-controller-cors"` - ExternalUI string `yaml:"external-ui,omitempty" json:"external-ui"` - ExternalUIURL string `yaml:"external-ui-url,omitempty" json:"external-ui-url"` - ExternalUIName string `yaml:"external-ui-name,omitempty" json:"external-ui-name"` - ExternalDohServer string `yaml:"external-doh-server,omitempty" json:"external-doh-server"` - Secret string `yaml:"secret,omitempty" json:"secret"` - Interface string `yaml:"interface-name,omitempty" json:"interface-name"` - RoutingMark int `yaml:"routing-mark,omitempty" json:"routing-mark"` - Tunnels []LC.Tunnel `yaml:"tunnels,omitempty" json:"tunnels"` - GeoAutoUpdate bool `yaml:"geo-auto-update,omitempty" json:"geo-auto-update"` - GeoUpdateInterval int `yaml:"geo-update-interval,omitempty" json:"geo-update-interval"` - GeodataMode bool `yaml:"geodata-mode,omitempty" json:"geodata-mode"` - GeodataLoader string `yaml:"geodata-loader,omitempty" json:"geodata-loader"` - GeositeMatcher string `yaml:"geosite-matcher,omitempty" json:"geosite-matcher"` - TCPConcurrent bool `yaml:"tcp-concurrent,omitempty" json:"tcp-concurrent"` - FindProcessMode string `yaml:"find-process-mode,omitempty" json:"find-process-mode"` - GlobalClientFingerprint string `yaml:"global-client-fingerprint,omitempty" json:"global-client-fingerprint"` - GlobalUA string `yaml:"global-ua,omitempty" json:"global-ua"` - ETagSupport bool `yaml:"etag-support,omitempty" json:"etag-support"` - KeepAliveIdle int `yaml:"keep-alive-idle,omitempty" json:"keep-alive-idle"` - KeepAliveInterval int `yaml:"keep-alive-interval,omitempty" json:"keep-alive-interval"` - DisableKeepAlive bool `yaml:"disable-keep-alive,omitempty" json:"disable-keep-alive"` + Port proxy.IntOrString `yaml:"port,omitempty" json:"port"` + SocksPort proxy.IntOrString `yaml:"socks-port,omitempty" json:"socks-port"` + RedirPort proxy.IntOrString `yaml:"redir-port,omitempty" json:"redir-port"` + TProxyPort proxy.IntOrString `yaml:"tproxy-port,omitempty" json:"tproxy-port"` + MixedPort proxy.IntOrString `yaml:"mixed-port,omitempty" json:"mixed-port"` + ShadowSocksConfig string `yaml:"ss-config,omitempty" json:"ss-config"` + VmessConfig string `yaml:"vmess-config,omitempty" json:"vmess-config"` + InboundTfo bool `yaml:"inbound-tfo,omitempty" json:"inbound-tfo"` + InboundMPTCP bool `yaml:"inbound-mptcp,omitempty" json:"inbound-mptcp"` + Authentication []string `yaml:"authentication,omitempty" json:"authentication"` + SkipAuthPrefixes []netip.Prefix `yaml:"skip-auth-prefixes,omitempty" json:"skip-auth-prefixes"` + LanAllowedIPs []netip.Prefix `yaml:"lan-allowed-ips,omitempty" json:"lan-allowed-ips"` + LanDisAllowedIPs []netip.Prefix `yaml:"lan-disallowed-ips,omitempty" json:"lan-disallowed-ips"` + AllowLan bool `yaml:"allow-lan,omitempty" json:"allow-lan"` + BindAddress string `yaml:"bind-address,omitempty" json:"bind-address"` + Mode string `yaml:"mode,omitempty" json:"mode"` + UnifiedDelay bool `yaml:"unified-delay,omitempty" json:"unified-delay"` + LogLevel string `yaml:"log-level,omitempty" json:"log-level"` + IPv6 bool `yaml:"ipv6,omitempty" json:"ipv6"` + ExternalController string `yaml:"external-controller,omitempty" json:"external-controller"` + ExternalControllerPipe string `yaml:"external-controller-pipe,omitempty" json:"external-controller-pipe"` + ExternalControllerUnix string `yaml:"external-controller-unix,omitempty" json:"external-controller-unix"` + ExternalControllerTLS string `yaml:"external-controller-tls,omitempty" json:"external-controller-tls"` + ExternalControllerCors C.RawCors `yaml:"external-controller-cors,omitempty" json:"external-controller-cors"` + ExternalUI string `yaml:"external-ui,omitempty" json:"external-ui"` + ExternalUIURL string `yaml:"external-ui-url,omitempty" json:"external-ui-url"` + ExternalUIName string `yaml:"external-ui-name,omitempty" json:"external-ui-name"` + ExternalDohServer string `yaml:"external-doh-server,omitempty" json:"external-doh-server"` + Secret string `yaml:"secret,omitempty" json:"secret"` + Interface string `yaml:"interface-name,omitempty" json:"interface-name"` + RoutingMark int `yaml:"routing-mark,omitempty" json:"routing-mark"` + Tunnels []LC.Tunnel `yaml:"tunnels,omitempty" json:"tunnels"` + GeoAutoUpdate bool `yaml:"geo-auto-update,omitempty" json:"geo-auto-update"` + GeoUpdateInterval int `yaml:"geo-update-interval,omitempty" json:"geo-update-interval"` + GeodataMode bool `yaml:"geodata-mode,omitempty" json:"geodata-mode"` + GeodataLoader string `yaml:"geodata-loader,omitempty" json:"geodata-loader"` + GeositeMatcher string `yaml:"geosite-matcher,omitempty" json:"geosite-matcher"` + TCPConcurrent bool `yaml:"tcp-concurrent,omitempty" json:"tcp-concurrent"` + FindProcessMode string `yaml:"find-process-mode,omitempty" json:"find-process-mode"` + GlobalClientFingerprint string `yaml:"global-client-fingerprint,omitempty" json:"global-client-fingerprint"` + GlobalUA string `yaml:"global-ua,omitempty" json:"global-ua"` + ETagSupport bool `yaml:"etag-support,omitempty" json:"etag-support"` + KeepAliveIdle int `yaml:"keep-alive-idle,omitempty" json:"keep-alive-idle"` + KeepAliveInterval int `yaml:"keep-alive-interval,omitempty" json:"keep-alive-interval"` + DisableKeepAlive bool `yaml:"disable-keep-alive,omitempty" json:"disable-keep-alive"` ProxyProvider map[string]map[string]any `yaml:"proxy-providers,omitempty" json:"proxy-providers"` RuleProvider map[string]RuleProvider `yaml:"rule-providers,omitempty" json:"rule-providers"` diff --git a/parser/anytls.go b/parser/anytls.go index 76d1653..2c5d6eb 100644 --- a/parser/anytls.go +++ b/parser/anytls.go @@ -68,7 +68,7 @@ func (p *AnytlsParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) Name: remarks, Anytls: P.Anytls{ Server: server, - Port: port, + Port: P.IntOrString(port), Password: password, SNI: sni, SkipCertVerify: insecureBool, diff --git a/parser/hysteria.go b/parser/hysteria.go index e39d800..9f75ab9 100644 --- a/parser/hysteria.go +++ b/parser/hysteria.go @@ -76,7 +76,7 @@ func (p *HysteriaParser) Parse(config ParseConfig, proxy string) (P.Proxy, error Name: remarks, Hysteria: P.Hysteria{ Server: server, - Port: port, + Port: P.IntOrString(port), Up: upmbps, Down: downmbps, Auth: auth, diff --git a/parser/hysteria2.go b/parser/hysteria2.go index f11ccc3..669bf98 100644 --- a/parser/hysteria2.go +++ b/parser/hysteria2.go @@ -68,7 +68,7 @@ func (p *Hysteria2Parser) Parse(config ParseConfig, proxy string) (P.Proxy, erro Name: remarks, Hysteria2: P.Hysteria2{ Server: server, - Port: port, + Port: P.IntOrString(port), Password: password, Obfs: obfs, ObfsPassword: obfsPassword, diff --git a/parser/shadowsocks.go b/parser/shadowsocks.go index 25a2b5a..05b89a6 100644 --- a/parser/shadowsocks.go +++ b/parser/shadowsocks.go @@ -108,7 +108,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er Cipher: method, Password: password, Server: server, - Port: port, + Port: P.IntOrString(port), UDP: config.UseUDP, }, } diff --git a/parser/shadowsocksr.go b/parser/shadowsocksr.go index f4db56e..73da865 100644 --- a/parser/shadowsocksr.go +++ b/parser/shadowsocksr.go @@ -94,7 +94,7 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e Name: remarks, ShadowSocksR: P.ShadowSocksR{ Server: server, - Port: port, + Port: P.IntOrString(port), Protocol: protocol, Cipher: method, Obfs: obfs, diff --git a/parser/socks.go b/parser/socks.go index 2a69c76..dc420c7 100644 --- a/parser/socks.go +++ b/parser/socks.go @@ -79,7 +79,7 @@ func (p *SocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) { Name: remarks, Socks: P.Socks{ Server: server, - Port: port, + Port: P.IntOrString(port), UserName: username, Password: password, TLS: tls == "true", diff --git a/parser/trojan.go b/parser/trojan.go index bc8cf31..24b059c 100644 --- a/parser/trojan.go +++ b/parser/trojan.go @@ -63,7 +63,7 @@ func (p *TrojanParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) insecureBool := insecure == "1" result := P.Trojan{ Server: server, - Port: port, + Port: P.IntOrString(port), Password: password, Network: network, UDP: udp == "true", diff --git a/parser/vless.go b/parser/vless.go index ad51c33..519c3ff 100644 --- a/parser/vless.go +++ b/parser/vless.go @@ -66,7 +66,7 @@ func (p *VlessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) { result := P.Vless{ Server: server, - Port: port, + Port: P.IntOrString(port), UUID: uuid, Flow: flow, UDP: udp == "true", diff --git a/parser/vmess.go b/parser/vmess.go index a7afe4a..9d1874b 100644 --- a/parser/vmess.go +++ b/parser/vmess.go @@ -109,9 +109,9 @@ func (p *VmessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) { result := P.Vmess{ Server: vmess.Add, - Port: port, + Port: P.IntOrString(port), UUID: vmess.Id, - AlterID: aid, + AlterID: P.IntOrString(aid), Cipher: vmess.Scy, UDP: config.UseUDP, } diff --git a/test/yaml_test.go b/test/yaml_test.go new file mode 100644 index 0000000..520bddc --- /dev/null +++ b/test/yaml_test.go @@ -0,0 +1,33 @@ +package test + +import ( + "testing" + + "github.com/bestnite/sub2clash/model/proxy" + "gopkg.in/yaml.v3" +) + +type testStruct struct { + A proxy.IntOrString `yaml:"a"` +} + +func TestUnmarshal(t *testing.T) { + yamlData1 := `a: 123` + res := testStruct{} + err := yaml.Unmarshal([]byte(yamlData1), &res) + if err != nil { + t.Errorf("failed to unmarshal yaml: %v", err) + } + if res.A != 123 { + t.Errorf("expected 123, but got %v", res.A) + } + + yamlData2 := `a: "123"` + err = yaml.Unmarshal([]byte(yamlData2), &res) + if err != nil { + t.Errorf("failed to unmarshal yaml: %v", err) + } + if res.A != 123 { + t.Errorf("expected 123, but got %v", res.A) + } +}