sub-store-convert 2.44.29 → 2.44.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +274 -702
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -2537,7 +2537,7 @@ var grammars = String.raw`
2537
2537
  }
2538
2538
  }
2539
2539
 
2540
- start = (masque/anytls/shadowsocks/vmess/vless/trojan/h2_connect/https/http/snell/socks5/socks5_tls/tuic/tuic_v5/wireguard/hysteria2/ssh/trust_tunnel/direct) {
2540
+ start = (masque/anytls/shadowsocks/vmess/vless/trojan/h2_connect/https/http/snell/socks5/socks5_tls/tuic/tuic_v5/wireguard/hysteria2/ssh/trust_tunnel/direct/external) {
2541
2541
  if (
2542
2542
  ["auto", "none", "apple-26", "chromium-155"].includes(clientFingerprint) &&
2543
2543
  proxy.network !== "h3" &&
@@ -2559,7 +2559,7 @@ start = (masque/anytls/shadowsocks/vmess/vless/trojan/h2_connect/https/http/snel
2559
2559
  return proxy;
2560
2560
  }
2561
2561
 
2562
- shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/alpn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/udp_port/others)* {
2562
+ shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/udp_relay/alpn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/udp_port/proxy_policy_parameter/others)* {
2563
2563
  proxy.type = "ss";
2564
2564
  // handle obfs
2565
2565
  if (obfs.type == "http" || obfs.type === "tls") {
@@ -2570,7 +2570,7 @@ shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/
2570
2570
  }
2571
2571
  handleShadowTLS();
2572
2572
  }
2573
- vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_http_upgrade/ws_path/ws_headers/vmess_method/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2573
+ vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_http_upgrade/ws_path/ws_headers/vmess_method/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2574
2574
  proxy.type = "vmess";
2575
2575
  proxy.cipher = proxy.cipher || "auto";
2576
2576
  // Surfboard 与 Surge 默认不一致, 不管 Surfboard https://getsurfboard.com/docs/profile-format/proxy/external-proxy/vmess
@@ -2582,7 +2582,7 @@ vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_http_upgrade/ws_
2582
2582
  handleWebsocket();
2583
2583
  handleShadowTLS();
2584
2584
  }
2585
- vless = tag equals vless_address (vless_username/vless_option/reality_option/ws/ws_http_upgrade/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/xhttp/xhttp_headers/xhttp_option/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2585
+ vless = tag equals vless_address (vless_username/vless_option/reality_option/ws/ws_http_upgrade/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/xhttp/xhttp_headers/xhttp_option/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2586
2586
  proxy["packet-encoding"] = "xudp";
2587
2587
  if (proxy["reality-opts"] && proxy["reality-opts"]["public-key"]) proxy.tls = true;
2588
2588
  if (xhttpEnabled) {
@@ -2594,30 +2594,30 @@ vless = tag equals vless_address (vless_username/vless_option/reality_option/ws/
2594
2594
  handleShadowTLS();
2595
2595
  }
2596
2596
  vless_address = "vless" address { proxy.type = "vless"; }
2597
- trojan = tag equals "trojan" address (passwordk/ws/ws_http_upgrade/ws_path/ws_headers/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2597
+ trojan = tag equals "trojan" address (passwordk/ws/ws_http_upgrade/ws_path/ws_headers/tls/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2598
2598
  proxy.type = "trojan";
2599
2599
  handleWebsocket();
2600
2600
  handleShadowTLS();
2601
2601
  }
2602
- https = tag equals "https" address (username password)? (usernamek passwordk)? (headers/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2602
+ https = tag equals "https" address (username password)? (usernamek passwordk)? (headers/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2603
2603
  proxy.type = "http";
2604
2604
  proxy.tls = true;
2605
2605
  handleShadowTLS();
2606
2606
  }
2607
- h2_connect = tag equals "h2-connect" address (username password)? (usernamek passwordk)? (headers/max_streams/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2607
+ h2_connect = tag equals "h2-connect" address (username password)? (usernamek passwordk)? (headers/max_streams/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2608
2608
  proxy.type = "h2-connect";
2609
2609
  proxy.tls = true;
2610
2610
  handleShadowTLS();
2611
2611
  }
2612
- http = tag equals "http" address (username password)? (usernamek passwordk)? (headers/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2612
+ http = tag equals "http" address (username password)? (usernamek passwordk)? (headers/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2613
2613
  proxy.type = "http";
2614
2614
  handleShadowTLS();
2615
2615
  }
2616
- ssh = tag equals "ssh" address (username password)? (usernamek passwordk)? (server_fingerprint/idle_timeout/private_key/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2616
+ ssh = tag equals "ssh" address (username password)? (usernamek passwordk)? (server_fingerprint/idle_timeout/private_key/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2617
2617
  proxy.type = "ssh";
2618
2618
  handleShadowTLS();
2619
2619
  }
2620
- snell = tag equals ("snell"i/"opensnell"i) address (snell_version/snell_mode/snell_psk/obfs/obfs_host/obfs_uri/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/reuse/alpn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2620
+ snell = tag equals ("snell"i/"opensnell"i) address (snell_version/snell_mode/snell_psk/obfs/obfs_host/obfs_uri/udp_relay/reuse/alpn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2621
2621
  proxy.type = "snell";
2622
2622
  // handle obfs
2623
2623
  if (obfs.type == "http" || obfs.type === "tls") {
@@ -2627,48 +2627,71 @@ snell = tag equals ("snell"i/"opensnell"i) address (snell_version/snell_mode/sne
2627
2627
  }
2628
2628
  handleShadowTLS();
2629
2629
  }
2630
- tuic = tag equals "tuic" address (alpn/token/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/fast_open/tfo/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/others)* {
2630
+ tuic = tag equals "tuic" address (alpn/token/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/shadow_tls_version/shadow_tls_sni/shadow_tls_password/port_hopping/port_hopping_interval/proxy_policy_parameter/others)* {
2631
2631
  proxy.type = "tuic";
2632
2632
  handleShadowTLS();
2633
2633
  }
2634
- tuic_v5 = tag equals "tuic-v5" address (alpn/passwordk/uuidk/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/fast_open/tfo/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/others)* {
2634
+ tuic_v5 = tag equals "tuic-v5" address (alpn/passwordk/uuidk/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/shadow_tls_version/shadow_tls_sni/shadow_tls_password/port_hopping/port_hopping_interval/proxy_policy_parameter/others)* {
2635
2635
  proxy.type = "tuic";
2636
2636
  proxy.version = 5;
2637
2637
  handleShadowTLS();
2638
2638
  }
2639
- wireguard = tag equals "wireguard" (section_name/no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2639
+ wireguard = tag equals "wireguard" (section_name/common_policy_parameter/proxy_only_parameter/ignored_policy_parameter)* {
2640
+ if (!proxy["section-name"]) error("WireGuard requires section-name");
2640
2641
  proxy.type = "wireguard-surge";
2641
- handleShadowTLS();
2642
2642
  }
2643
- hysteria2 = tag equals "hysteria2" address (no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/sni/cert_verify_name/alpn/tls_verification/client_cert/ech_config/passwordk/tls_fingerprint/download_bandwidth/ecn/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/port_hopping_interval/salamander_password/gecko_password/others)* {
2643
+ hysteria2 = tag equals "hysteria2" address (sni/cert_verify_name/alpn/tls_verification/client_cert/ech_config/passwordk/tls_fingerprint/download_bandwidth/shadow_tls_version/shadow_tls_sni/shadow_tls_password/port_hopping/port_hopping_interval/salamander_password/gecko_password/proxy_policy_parameter/others)* {
2644
2644
  proxy.type = "hysteria2";
2645
2645
  handleShadowTLS();
2646
2646
  }
2647
- socks5 = tag equals "socks5" address (username password)? (usernamek passwordk)? (udp_relay/no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2647
+ socks5 = tag equals "socks5" address (username password)? (usernamek passwordk)? (udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2648
2648
  proxy.type = "socks5";
2649
2649
  handleShadowTLS();
2650
2650
  }
2651
- socks5_tls = tag equals "socks5-tls" address (username password)? (usernamek passwordk)? (udp_relay/no_error_alert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/ecn/others)* {
2651
+ socks5_tls = tag equals "socks5-tls" address (username password)? (usernamek passwordk)? (udp_relay/sni/cert_verify_name/alpn/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/shadow_tls_version/shadow_tls_sni/shadow_tls_password/proxy_policy_parameter/others)* {
2652
2652
  proxy.type = "socks5";
2653
2653
  proxy.tls = true;
2654
2654
  handleShadowTLS();
2655
2655
  }
2656
- anytls = tag equals "anytls" address (passwordk/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/fast_open/tfo/block_quic/ecn/others)* {
2656
+ anytls = tag equals "anytls" address (passwordk/reuse/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/proxy_policy_parameter/others)* {
2657
2657
  proxy.type = "anytls";
2658
2658
  proxy.tls = true;
2659
2659
  }
2660
- trust_tunnel = tag equals "trust-tunnel" address (usernamek/passwordk/client_random_auth_key/headers/max_streams/reuse/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/h3/fast_open/tfo/block_quic/ecn/others)* {
2660
+ trust_tunnel = tag equals "trust-tunnel" address (usernamek/passwordk/client_random_auth_key/headers/max_streams/reuse/tls_fingerprint/client_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/h3/proxy_policy_parameter/others)* {
2661
2661
  proxy.type = "trusttunnel";
2662
2662
  proxy.tls = true;
2663
2663
  }
2664
- masque = tag equals "masque" address (usernamek/passwordk/port_hopping_interval/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/fast_open/tfo/udp_relay/ecn/block_quic/others)* {
2664
+ masque = tag equals "masque" address (usernamek/passwordk/port_hopping/port_hopping_interval/tls_fingerprint/tls_verification/client_cert/ech_config/sni/cert_verify_name/alpn/udp_relay/proxy_policy_parameter/others)* {
2665
2665
  proxy.type = "masque-surge";
2666
2666
  proxy.tls = true;
2667
2667
  }
2668
2668
 
2669
- direct = tag equals "direct" (udp_relay/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/block_quic/others)* {
2669
+ direct = tag equals "direct"i (common_policy_parameter/interface_parameter/udp_relay/ignored_policy_parameter)* {
2670
2670
  proxy.type = "direct";
2671
2671
  }
2672
+
2673
+ external = tag equals "external" (external_exec/external_local_port/external_arg/external_address/udp_relay/proxy_policy_parameter/ignored_policy_parameter)* {
2674
+ proxy.type = "external";
2675
+ proxy.args = proxy.args || [];
2676
+ proxy.addresses = proxy.addresses || [];
2677
+ }
2678
+ external_exec = comma "exec" equals value:quoted_value { if (proxy.exec === undefined) proxy.exec = value; }
2679
+ external_local_port = comma "local-port" equals value:quoted_value { if (proxy["local-port"] === undefined) proxy["local-port"] = value; }
2680
+ external_arg = comma "args" equals value:quoted_value {
2681
+ proxy.args = proxy.args || [];
2682
+ proxy.args.push(value);
2683
+ }
2684
+ external_address = comma "addresses" equals value:quoted_value {
2685
+ proxy.addresses = proxy.addresses || [];
2686
+ proxy.addresses.push(value);
2687
+ }
2688
+
2689
+ proxy_policy_parameter = common_policy_parameter / interface_parameter / proxy_only_parameter
2690
+ common_policy_parameter = ip_version / hybrid / tfo / fast_open / tos / block_quic / test_url / test_timeout / test_udp
2691
+ interface_parameter = interface / allow_other_interface / dns_follow_interface
2692
+ proxy_only_parameter = no_error_alert / ecn / underlying_proxy
2693
+ ignored_policy_parameter = comma [^=,]+ equals quoted_value
2694
+
2672
2695
  address = comma server:server comma port:port {
2673
2696
  proxy.server = server;
2674
2697
  proxy.port = port;
@@ -2702,6 +2725,13 @@ port = digits:[0-9]+ {
2702
2725
  }
2703
2726
  }
2704
2727
 
2728
+ port_hopping = comma "port-hopping" equals value:(
2729
+ '"' _ list:port_hopping_list _ '"' { return list; }
2730
+ / "'" _ list:port_hopping_list _ "'" { return list; }
2731
+ / port_hopping_list
2732
+ ) &(_ ("," / !.)) { proxy.ports = value.replace(/\s+/g, "").replace(/;/g, ","); }
2733
+ port_hopping_list = $(port_hopping_range (_ [,;] _ port_hopping_range)*)
2734
+ port_hopping_range = [0-9]+ ("-" [0-9]+)?
2705
2735
  port_hopping_interval = comma "port-hopping-interval" equals match:$[0-9]+ { proxy["hop-interval"] = parseInt(match.trim()); }
2706
2736
 
2707
2737
  username = & {
@@ -2813,28 +2843,41 @@ obfs_uri = comma "obfs-uri" equals path:uri { obfs.path = path }
2813
2843
  uri = $[^,]+
2814
2844
 
2815
2845
  udp_relay = comma "udp-relay" equals flag:bool { proxy.udp = flag; }
2816
- fast_open = comma "fast-open" equals flag:bool { proxy.tfo = flag; }
2846
+ fast_open = comma "fast-open" equals flag:policy_bool { proxy.tfo = flag; }
2817
2847
  reuse = comma "reuse" equals flag:bool { proxy.reuse = flag; }
2818
- ecn = comma "ecn" equals mode:("auto" / "on" / "off" / bool) &(_ ("," / !.)) { proxy.ecn = mode; }
2819
- tfo = comma "tfo" equals flag:bool { proxy.tfo = flag; }
2820
- ip_version = comma "ip-version" equals match:[^,]+ { proxy["ip-version"] = match.join(""); }
2821
- section_name = comma "section-name" equals match:[^,]+ { proxy["section-name"] = match.join(""); }
2822
- no_error_alert = comma "no-error-alert" equals match:[^,]+ { proxy["no-error-alert"] = match.join(""); }
2823
- underlying_proxy = comma "underlying-proxy" equals value:(vless_quoted_value / uri) { proxy["underlying-proxy"] = value; }
2848
+ ecn = comma "ecn" equals mode:policy_toggle { if (mode !== undefined) proxy.ecn = mode; }
2849
+ tfo = comma "tfo" equals flag:policy_bool { proxy.tfo = flag; }
2850
+ ip_version = comma "ip-version" equals value:quoted_value { proxy["ip-version"] = value.trim(); }
2851
+ section_name = comma "section-name" equals value:quoted_value { proxy["section-name"] = value.trim(); }
2852
+ no_error_alert = comma "no-error-alert" equals value:quoted_value { proxy["no-error-alert"] = value.trim(); }
2853
+ underlying_proxy = comma "underlying-proxy" equals value:quoted_value { proxy["underlying-proxy"] = value.trim(); }
2824
2854
  download_bandwidth = comma "download-bandwidth" equals match:[^,]+ { proxy.down = match.join(""); }
2825
- test_url = comma "test-url" equals match:[^,]+ { proxy["test-url"] = match.join(""); }
2826
- test_udp = comma "test-udp" equals match:[^,]+ { proxy["test-udp"] = match.join(""); }
2827
- test_timeout = comma "test-timeout" equals match:$[0-9]+ { proxy["test-timeout"] = parseInt(match.trim()); }
2855
+ test_url = comma "test-url" equals value:quoted_value { proxy["test-url"] = value.trim(); }
2856
+ test_udp = comma "test-udp" equals value:quoted_value { proxy["test-udp"] = value.trim(); }
2857
+ test_timeout = comma "test-timeout" equals value:quoted_value {
2858
+ const seconds = value.trim();
2859
+ if (/^(?:\d+(?:\.\d*)?|\.\d+)$/.test(seconds)) proxy["test-timeout"] = Number(seconds);
2860
+ }
2828
2861
  max_streams = comma "max-streams" equals match:quoted_integer { proxy["max-streams"] = match; }
2829
2862
  quoted_integer = '"' match:$[0-9]+ '"' { return parseInt(match.trim()); } / "'" match:$[0-9]+ "'" { return parseInt(match.trim()); } / match:$[0-9]+ { return parseInt(match.trim()); }
2830
- tos = comma "tos" equals match:$[0-9]+ { proxy.tos = parseInt(match.trim()); }
2831
- interface = comma "interface" equals match:[^,]+ { proxy.interface = match.join(""); }
2832
- allow_other_interface = comma "allow-other-interface" equals flag:bool { proxy["allow-other-interface"] = flag; }
2833
- hybrid = comma "hybrid" equals flag:bool { proxy.hybrid = flag; }
2863
+ tos = comma "tos" equals value:quoted_value {
2864
+ const text = value.trim();
2865
+ proxy.tos = /^(?:\d+|0x[\da-f]+)$/i.test(text) ? Number(text) : text;
2866
+ }
2867
+ interface = comma "interface" equals value:quoted_value { proxy.interface = value.trim(); }
2868
+ allow_other_interface = comma "allow-other-interface" equals flag:policy_bool { proxy["allow-other-interface"] = flag; }
2869
+ dns_follow_interface = comma "dns-follow-interface" equals flag:policy_bool { proxy["dns-follow-interface"] = flag; }
2870
+ hybrid = comma "hybrid" equals mode:policy_toggle { if (mode !== undefined) proxy.hybrid = mode; }
2871
+ policy_bool = value:quoted_value &{ return /^(?:true|false)$/.test(value.trim()); } { return value.trim() === "true"; }
2872
+ policy_toggle = value:quoted_value {
2873
+ const mode = value.trim();
2874
+ if (mode === "true" || mode === "false") return mode === "true";
2875
+ if (["auto", "on", "off"].includes(mode)) return mode;
2876
+ }
2834
2877
  idle_timeout = comma "idle-timeout" equals match:$[0-9]+ { proxy["idle-timeout"] = parseInt(match.trim()); }
2835
2878
  private_key = comma "private-key" equals match:[^,]+ { proxy["keystore-private-key"] = stripQuotes(match.join("")); }
2836
2879
  server_fingerprint = comma "server-fingerprint" equals match:[^,]+ { proxy["server-fingerprint"] = match.join("").replace(/^"(.*)"$/, '$1'); }
2837
- block_quic = comma "block-quic" equals match:[^,]+ { proxy["block-quic"] = match.join(""); }
2880
+ block_quic = comma "block-quic" equals value:quoted_value { proxy["block-quic"] = value.trim(); }
2838
2881
  udp_port = comma "udp-port" equals match:$[0-9]+ { proxy["udp-port"] = parseInt(match.trim()); }
2839
2882
  shadow_tls_version = comma "shadow-tls-version" equals match:$[0-9]+ { shadowTLS.version = parseInt(match.trim()); }
2840
2883
  shadow_tls_sni = comma "shadow-tls-sni" equals match:[^,]+ { shadowTLS.host = match.join(""); }
@@ -2846,12 +2889,11 @@ alpn = comma "alpn" equals match:quoted_value {
2846
2889
  }
2847
2890
  h3 = comma "h3" equals flag:bool { if (flag) proxy.network = "h3"; }
2848
2891
  quoted_value
2849
- = double_quoted_vless_value
2892
+ = '"' value:$(("\\" ('"' / "\\") / [^"])*) '"' &(_ ("," / !.)) { return proxy.type === "vless" ? unescapeQuotedValue(value) : value; }
2850
2893
  / '"' match:$[^"]* '"' { return match; }
2851
2894
  / "'" match:$[^']* "'" { return proxy.type === "vless" ? unescapeQuotedValue(match) : match; }
2852
- / uri
2895
+ / value:uri { return value.trim(); }
2853
2896
  vless_quoted_value = &{ return proxy.type === "vless"; } value:quoted_value { return value.trim(); }
2854
- double_quoted_vless_value = &{ return proxy.type === "vless"; } '"' value:$(("\\" ('"' / "\\") / [^"])*) '"' &(_ ("," / !.)) { return unescapeQuotedValue(value); }
2855
2897
  uuidk = comma "uuid" equals match:[^,]+ { proxy.uuid = match.join(""); }
2856
2898
  salamander_password = comma "salamander-password" equals match:[^,]+ { proxy['obfs-password'] = match.join("").replace(/^"(.*?)"$/, '$1').replace(/^'(.*?)'$/, '$1'); proxy.obfs = 'salamander'; }
2857
2899
  gecko_password = comma "gecko-password" equals match:[^,]+ { proxy['obfs-password'] = match.join("").replace(/^"(.*?)"$/, '$1').replace(/^'(.*?)'$/, '$1'); proxy.obfs = 'gecko'; }
@@ -4180,15 +4222,6 @@ function formatSurgeVmessEncryptMethod(security) {
4180
4222
  }
4181
4223
 
4182
4224
  // src/vendors/Sub-Store/backend/src/core/proxy-utils/parsers/index.js
4183
- function surge_port_hopping(raw) {
4184
- const [parts, port_hopping] = raw.match(
4185
- /,\s*?port-hopping\s*?=\s*?["']?\s*?((\d+(-\d+)?)([,;]\d+(-\d+)?)*)\s*?["']?\s*?/
4186
- ) || [];
4187
- return {
4188
- port_hopping: port_hopping ? port_hopping.replace(/;/g, ",") : void 0,
4189
- line: parts ? raw.replace(parts, "") : raw
4190
- };
4191
- }
4192
4225
  function splitURIFragment(raw) {
4193
4226
  const [__, content, fragment] = /^(.*?)(?:#(.*?))?$/.exec(raw);
4194
4227
  return {
@@ -6285,6 +6318,8 @@ function URI_WireGuard() {
6285
6318
  proxy["public-key"] = value;
6286
6319
  } else if (/privatekey/i.test(key)) {
6287
6320
  proxy["private-key"] = value;
6321
+ } else if (/^presharedkey$/i.test(key)) {
6322
+ proxy["preshared-key"] = value;
6288
6323
  } else if (["udp"].includes(key)) {
6289
6324
  proxy[key] = /(TRUE)|1/i.test(value);
6290
6325
  } else if (![...Object.keys(proxy), "flag"].includes(key)) {
@@ -6670,9 +6705,7 @@ function Loon_WireGuard() {
6670
6705
  }
6671
6706
  function Surge_Direct() {
6672
6707
  const name = "Surge Direct Parser";
6673
- const test = (line) => {
6674
- return /^.*=\s*direct/.test(line.split(",")[0]);
6675
- };
6708
+ const test = (line) => /^[^=,]*=\s*direct\s*$/i.test(line.split(",")[0]);
6676
6709
  const parse2 = (line) => getParser().parse(line);
6677
6710
  return { name, test, parse: parse2 };
6678
6711
  }
@@ -6697,12 +6730,7 @@ function Surge_Masque() {
6697
6730
  const test = (line) => {
6698
6731
  return /^.*=\s*masque/.test(line.split(",")[0]);
6699
6732
  };
6700
- const parse2 = (raw) => {
6701
- const { port_hopping, line } = surge_port_hopping(raw);
6702
- const proxy = getParser().parse(line);
6703
- proxy.ports = port_hopping;
6704
- return proxy;
6705
- };
6733
+ const parse2 = (line) => getParser().parse(line);
6706
6734
  return { name, test, parse: parse2 };
6707
6735
  }
6708
6736
  function Surge_H2Connect() {
@@ -6774,56 +6802,8 @@ function Surge_External() {
6774
6802
  return /^.*=\s*external/.test(line.split(",")[0]);
6775
6803
  };
6776
6804
  const parse2 = (line) => {
6777
- let parsed = /^\s*(.*?)\s*?=\s*?external\s*?,\s*(.*?)\s*$/.exec(line);
6778
- let [_8, name2, other] = parsed;
6779
- line = other;
6780
- let exec = /(,|^)\s*?exec\s*?=\s*"(.*?)"\s*?(,|$)/.exec(line)?.[2];
6781
- if (!exec) {
6782
- exec = /(,|^)\s*?exec\s*?=\s*(.*?)\s*?(,|$)/.exec(line)?.[2];
6783
- }
6784
- let localPort = /(,|^)\s*?local-port\s*?=\s*"(.*?)"\s*?(,|$)/.exec(
6785
- line
6786
- )?.[2];
6787
- if (!localPort) {
6788
- localPort = /(,|^)\s*?local-port\s*?=\s*(.*?)\s*?(,|$)/.exec(
6789
- line
6790
- )?.[2];
6791
- }
6792
- const argsRegex = /(,|^)\s*?args\s*?=\s*("(.*?)"|(.*?))(?=\s*?(,|$))/g;
6793
- let argsMatch;
6794
- const args = [];
6795
- while ((argsMatch = argsRegex.exec(line)) !== null) {
6796
- if (argsMatch[3] != null) {
6797
- args.push(argsMatch[3]);
6798
- } else if (argsMatch[4] != null) {
6799
- args.push(argsMatch[4]);
6800
- }
6801
- }
6802
- const addressesRegex = /(,|^)\s*?addresses\s*?=\s*("(.*?)"|(.*?))(?=\s*?(,|$))/g;
6803
- let addressesMatch;
6804
- const addresses = [];
6805
- while ((addressesMatch = addressesRegex.exec(line)) !== null) {
6806
- let ip;
6807
- if (addressesMatch[3] != null) {
6808
- ip = addressesMatch[3];
6809
- } else if (addressesMatch[4] != null) {
6810
- ip = addressesMatch[4];
6811
- }
6812
- if (ip != null) {
6813
- ip = `${ip}`.trim().replace(/^\[/, "").replace(/\]$/, "");
6814
- }
6815
- if (isIP(ip)) {
6816
- addresses.push(ip);
6817
- }
6818
- }
6819
- const proxy = {
6820
- type: "external",
6821
- name: name2,
6822
- exec,
6823
- "local-port": localPort,
6824
- args,
6825
- addresses
6826
- };
6805
+ const proxy = getParser().parse(line);
6806
+ proxy.addresses = proxy.addresses.map((ip) => ip.trim().replace(/^\[/, "").replace(/\]$/, "")).filter(isIP);
6827
6807
  return proxy;
6828
6808
  };
6829
6809
  return { name, test, parse: parse2 };
@@ -6844,12 +6824,7 @@ function Surge_Tuic() {
6844
6824
  const test = (line) => {
6845
6825
  return /^.*=\s*tuic(-v5)?/.test(line.split(",")[0]);
6846
6826
  };
6847
- const parse2 = (raw) => {
6848
- const { port_hopping, line } = surge_port_hopping(raw);
6849
- const proxy = getParser().parse(line);
6850
- proxy["ports"] = port_hopping;
6851
- return proxy;
6852
- };
6827
+ const parse2 = (line) => getParser().parse(line);
6853
6828
  return { name, test, parse: parse2 };
6854
6829
  }
6855
6830
  function Surge_WireGuard() {
@@ -6865,12 +6840,7 @@ function Surge_Hysteria2() {
6865
6840
  const test = (line) => {
6866
6841
  return /^.*=\s*hysteria2/.test(line.split(",")[0]);
6867
6842
  };
6868
- const parse2 = (raw) => {
6869
- const { port_hopping, line } = surge_port_hopping(raw);
6870
- const proxy = getParser().parse(line);
6871
- proxy["ports"] = port_hopping;
6872
- return proxy;
6873
- };
6843
+ const parse2 = (line) => getParser().parse(line);
6874
6844
  return { name, test, parse: parse2 };
6875
6845
  }
6876
6846
  function isIP(ip) {
@@ -7296,6 +7266,34 @@ function parseWireGuardInterfaceAddress(value, family) {
7296
7266
  cidr: parseWireGuardCIDR(cidrRaw, max)
7297
7267
  };
7298
7268
  }
7269
+ function normalizeWireGuardInterfaceAddress(proxy, config) {
7270
+ const { addressKey, cidrKey, family, defaultCIDR } = config;
7271
+ const parsed = parseWireGuardInterfaceAddress(proxy[addressKey], family);
7272
+ if (!parsed) {
7273
+ if (proxy[addressKey] == null || `${proxy[addressKey]}`.trim().length === 0) {
7274
+ delete proxy[cidrKey];
7275
+ }
7276
+ return;
7277
+ }
7278
+ proxy[addressKey] = parsed.address;
7279
+ const normalizedCIDR = parseWireGuardCIDR(proxy[cidrKey], defaultCIDR);
7280
+ proxy[cidrKey] = normalizedCIDR ?? parsed.cidr ?? defaultCIDR;
7281
+ }
7282
+ function normalizeWireGuardInterface(proxy = {}) {
7283
+ normalizeWireGuardInterfaceAddress(proxy, {
7284
+ addressKey: "ip",
7285
+ cidrKey: "ip-cidr",
7286
+ family: "ipv4",
7287
+ defaultCIDR: 32
7288
+ });
7289
+ normalizeWireGuardInterfaceAddress(proxy, {
7290
+ addressKey: "ipv6",
7291
+ cidrKey: "ipv6-cidr",
7292
+ family: "ipv6",
7293
+ defaultCIDR: 128
7294
+ });
7295
+ return proxy;
7296
+ }
7299
7297
  function getWireGuardAddressWithCIDR(proxy = {}, family = "ipv4") {
7300
7298
  const config = family === "ipv6" ? { addressKey: "ipv6", cidrKey: "ipv6-cidr", defaultCIDR: 128 } : { addressKey: "ip", cidrKey: "ip-cidr", defaultCIDR: 32 };
7301
7299
  const parsed = parseWireGuardInterfaceAddress(
@@ -7366,6 +7364,53 @@ function quoteSurgeValue(value) {
7366
7364
  function hasNonBlankValue(value) {
7367
7365
  return value != null && `${value}`.trim().length > 0;
7368
7366
  }
7367
+ function appendSurgePolicyParameters(result, proxy) {
7368
+ const isDirect = proxy.type === "direct";
7369
+ const supportsInterface = !["wireguard", "wireguard-surge"].includes(
7370
+ proxy.type
7371
+ );
7372
+ const ipVersion = proxy["ip-version"];
7373
+ const tos = stripSurgeQuotes(proxy.tos);
7374
+ if (tos != null && (!/^(?:\d+|0x[\da-f]+)$/i.test(tos) || Number(tos) > 255)) {
7375
+ throw new Error("Surge tos must be an integer between 0 and 255");
7376
+ }
7377
+ const parameters = {
7378
+ "ip-version": ipVersions[ipVersion] ?? ipVersion,
7379
+ "no-error-alert": isDirect ? void 0 : proxy["no-error-alert"],
7380
+ tfo: proxy.tfo ?? proxy["fast-open"],
7381
+ "test-url": proxy["test-url"],
7382
+ "test-timeout": proxy["test-timeout"],
7383
+ "test-udp": proxy["test-udp"],
7384
+ hybrid: proxy.hybrid,
7385
+ tos,
7386
+ "allow-other-interface": supportsInterface ? proxy["allow-other-interface"] : void 0,
7387
+ interface: supportsInterface ? proxy.interface ?? proxy["interface-name"] : void 0,
7388
+ "dns-follow-interface": supportsInterface ? proxy["dns-follow-interface"] : void 0,
7389
+ "block-quic": proxy["block-quic"],
7390
+ ecn: isDirect ? void 0 : proxy.ecn,
7391
+ "underlying-proxy": isDirect ? void 0 : proxy["underlying-proxy"]
7392
+ };
7393
+ for (const [key, value] of Object.entries(parameters)) {
7394
+ if (value == null) continue;
7395
+ const formatted = typeof value === "string" && value.includes(",") ? quoteSurgeValue(value) : value;
7396
+ result.append(`,${key}=${formatted}`);
7397
+ }
7398
+ }
7399
+ function appendPortHopping(result, proxy) {
7400
+ if (hasNonBlankValue(proxy.ports)) {
7401
+ if (hasNonBlankValue(stripSurgeQuotes(proxy["underlying-proxy"]))) {
7402
+ throw new Error(
7403
+ "Surge port-hopping cannot be combined with underlying-proxy"
7404
+ );
7405
+ }
7406
+ result.append(
7407
+ `,port-hopping="${String(proxy.ports).replace(/,/g, ";")}"`
7408
+ );
7409
+ }
7410
+ if (hasNonBlankValue(proxy["hop-interval"])) {
7411
+ result.append(`,port-hopping-interval=${proxy["hop-interval"]}`);
7412
+ }
7413
+ }
7369
7414
  function formatSurgeAlpn(alpn) {
7370
7415
  const values = Array.isArray(alpn) ? alpn : stripSurgeQuotes(`${alpn || ""}`).split(",");
7371
7416
  return values.filter((item) => item != null).map((item) => String(stripSurgeQuotes(item)).trim()).filter((item) => item !== "").join(",");
@@ -7517,7 +7562,7 @@ function Surge_Producer() {
7517
7562
  case "tuic":
7518
7563
  return tuic(proxy);
7519
7564
  case "wireguard-surge":
7520
- return wireguard_surge(proxy);
7565
+ return wireguardPolicy(proxy);
7521
7566
  case "hysteria2":
7522
7567
  return hysteria2(proxy);
7523
7568
  case "ssh":
@@ -7584,12 +7629,6 @@ function shadowsocks(proxy) {
7584
7629
  }
7585
7630
  result.append(`,encrypt-method=${proxy.cipher}`);
7586
7631
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
7587
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
7588
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
7589
- result.appendIfPresent(
7590
- `,no-error-alert=${proxy["no-error-alert"]}`,
7591
- "no-error-alert"
7592
- );
7593
7632
  if (isPresent2(proxy, "plugin")) {
7594
7633
  if (proxy.plugin === "obfs") {
7595
7634
  result.append(`,obfs=${proxy["plugin-opts"].mode}`);
@@ -7605,110 +7644,29 @@ function shadowsocks(proxy) {
7605
7644
  throw unsupported(`plugin ${proxy.plugin} is not supported`);
7606
7645
  }
7607
7646
  }
7608
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
7609
7647
  result.appendIfPresent(`,udp-relay=${proxy.udp}`, "udp");
7610
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
7611
- result.appendIfPresent(
7612
- `,test-timeout=${proxy["test-timeout"]}`,
7613
- "test-timeout"
7614
- );
7615
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
7616
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
7617
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
7618
- result.appendIfPresent(
7619
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
7620
- "allow-other-interface"
7621
- );
7622
- result.appendIfPresent(
7623
- `,interface=${proxy["interface-name"]}`,
7624
- "interface-name"
7625
- );
7626
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
7627
7648
  appendShadowTLS(result, proxy, true);
7628
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
7629
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7630
- result.appendIfPresent(
7631
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
7632
- "underlying-proxy"
7633
- );
7649
+ appendSurgePolicyParameters(result, proxy);
7634
7650
  return result.toString();
7635
7651
  }
7636
7652
  function trojan(proxy) {
7637
7653
  const result = new Result(proxy);
7638
7654
  result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`);
7639
7655
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
7640
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
7641
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
7642
- result.appendIfPresent(
7643
- `,no-error-alert=${proxy["no-error-alert"]}`,
7644
- "no-error-alert"
7645
- );
7646
7656
  handleTransport(result, proxy);
7647
7657
  result.appendIfPresent(`,tls=${proxy.tls}`, "tls");
7648
7658
  appendTlsProxyParams(result, proxy);
7649
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
7650
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
7651
- result.appendIfPresent(
7652
- `,test-timeout=${proxy["test-timeout"]}`,
7653
- "test-timeout"
7654
- );
7655
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
7656
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
7657
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
7658
- result.appendIfPresent(
7659
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
7660
- "allow-other-interface"
7661
- );
7662
- result.appendIfPresent(
7663
- `,interface=${proxy["interface-name"]}`,
7664
- "interface-name"
7665
- );
7666
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
7667
7659
  appendShadowTLS(result, proxy);
7668
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
7669
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7670
- result.appendIfPresent(
7671
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
7672
- "underlying-proxy"
7673
- );
7660
+ appendSurgePolicyParameters(result, proxy);
7674
7661
  return result.toString();
7675
7662
  }
7676
7663
  function anytls(proxy) {
7677
7664
  const result = new Result(proxy);
7678
7665
  result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`);
7679
7666
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
7680
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
7681
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
7682
- result.appendIfPresent(
7683
- `,no-error-alert=${proxy["no-error-alert"]}`,
7684
- "no-error-alert"
7685
- );
7686
7667
  appendTlsProxyParams(result, proxy);
7687
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
7688
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
7689
- result.appendIfPresent(
7690
- `,test-timeout=${proxy["test-timeout"]}`,
7691
- "test-timeout"
7692
- );
7693
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
7694
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
7695
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
7696
- result.appendIfPresent(
7697
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
7698
- "allow-other-interface"
7699
- );
7700
- result.appendIfPresent(
7701
- `,interface=${proxy["interface-name"]}`,
7702
- "interface-name"
7703
- );
7704
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
7705
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
7706
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7707
- result.appendIfPresent(
7708
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
7709
- "underlying-proxy"
7710
- );
7711
7668
  result.appendIfPresent(`,reuse=${proxy["reuse"]}`, "reuse");
7669
+ appendSurgePolicyParameters(result, proxy);
7712
7670
  return result.toString();
7713
7671
  }
7714
7672
  function trusttunnel(proxy) {
@@ -7732,38 +7690,9 @@ function trusttunnel(proxy) {
7732
7690
  "max-streams"
7733
7691
  );
7734
7692
  if (proxy.network === "h3") result.append(",h3=true");
7735
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
7736
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
7737
- result.appendIfPresent(
7738
- `,no-error-alert=${proxy["no-error-alert"]}`,
7739
- "no-error-alert"
7740
- );
7741
7693
  appendTlsProxyParams(result, proxy);
7742
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
7743
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
7744
- result.appendIfPresent(
7745
- `,test-timeout=${proxy["test-timeout"]}`,
7746
- "test-timeout"
7747
- );
7748
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
7749
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
7750
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
7751
- result.appendIfPresent(
7752
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
7753
- "allow-other-interface"
7754
- );
7755
- result.appendIfPresent(
7756
- `,interface=${proxy["interface-name"]}`,
7757
- "interface-name"
7758
- );
7759
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
7760
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
7761
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7762
- result.appendIfPresent(
7763
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
7764
- "underlying-proxy"
7765
- );
7766
7694
  result.appendIfPresent(`,reuse=${proxy["reuse"]}`, "reuse");
7695
+ appendSurgePolicyParameters(result, proxy);
7767
7696
  return result.toString();
7768
7697
  }
7769
7698
  function masque_surge(proxy) {
@@ -7771,49 +7700,9 @@ function masque_surge(proxy) {
7771
7700
  result.append(`${proxy.name}=masque,${proxy.server},${proxy.port}`);
7772
7701
  result.appendIfPresent(`,username="${proxy.username}"`, "username");
7773
7702
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
7774
- if (hasNonBlankValue(proxy.ports)) {
7775
- result.append(
7776
- `,port-hopping="${String(proxy.ports).replace(/,/g, ";")}"`
7777
- );
7778
- }
7779
- if (hasNonBlankValue(proxy["hop-interval"])) {
7780
- result.append(`,port-hopping-interval=${proxy["hop-interval"]}`);
7781
- }
7782
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
7783
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
7784
- result.appendIfPresent(
7785
- `,no-error-alert=${proxy["no-error-alert"]}`,
7786
- "no-error-alert"
7787
- );
7703
+ appendPortHopping(result, proxy);
7788
7704
  appendTlsProxyParams(result, proxy);
7789
- if (isPresent2(proxy, "tfo")) {
7790
- result.append(`,tfo=${proxy.tfo}`);
7791
- } else if (isPresent2(proxy, "fast-open")) {
7792
- result.append(`,tfo=${proxy["fast-open"]}`);
7793
- }
7794
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
7795
- result.appendIfPresent(
7796
- `,test-timeout=${proxy["test-timeout"]}`,
7797
- "test-timeout"
7798
- );
7799
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
7800
- result.appendIfPresent(`,hybrid=${proxy.hybrid}`, "hybrid");
7801
- result.appendIfPresent(`,tos=${proxy.tos}`, "tos");
7802
- result.appendIfPresent(
7803
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
7804
- "allow-other-interface"
7805
- );
7806
- result.appendIfPresent(
7807
- `,interface=${proxy["interface-name"]}`,
7808
- "interface-name"
7809
- );
7810
- result.appendIfPresent(`,interface=${proxy.interface}`, "interface");
7811
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
7812
- result.appendIfPresent(
7813
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
7814
- "underlying-proxy"
7815
- );
7816
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7705
+ appendSurgePolicyParameters(result, proxy);
7817
7706
  return result.toString();
7818
7707
  }
7819
7708
  function h2Connect(proxy) {
@@ -7827,41 +7716,10 @@ function h2Connect(proxy) {
7827
7716
  `,max-streams=${proxy["max-streams"]}`,
7828
7717
  "max-streams"
7829
7718
  );
7830
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
7831
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
7832
- result.appendIfPresent(
7833
- `,no-error-alert=${proxy["no-error-alert"]}`,
7834
- "no-error-alert"
7835
- );
7836
7719
  appendTlsProxyParams(result, proxy);
7837
- if (proxy.tfo) {
7838
- app_default.info(`Option tfo is not supported by Surge, thus omitted`);
7839
- }
7840
7720
  result.appendIfPresent(`,udp-relay=${proxy.udp}`, "udp");
7841
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
7842
- result.appendIfPresent(
7843
- `,test-timeout=${proxy["test-timeout"]}`,
7844
- "test-timeout"
7845
- );
7846
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
7847
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
7848
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
7849
- result.appendIfPresent(
7850
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
7851
- "allow-other-interface"
7852
- );
7853
- result.appendIfPresent(
7854
- `,interface=${proxy["interface-name"]}`,
7855
- "interface-name"
7856
- );
7857
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
7858
7721
  appendShadowTLS(result, proxy);
7859
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
7860
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7861
- result.appendIfPresent(
7862
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
7863
- "underlying-proxy"
7864
- );
7722
+ appendSurgePolicyParameters(result, proxy);
7865
7723
  return result.toString();
7866
7724
  }
7867
7725
  function v2ray(proxy, includeUnsupportedProxy) {
@@ -7875,12 +7733,6 @@ function v2ray(proxy, includeUnsupportedProxy) {
7875
7733
  result.append(`,encrypt-method=${encryptMethod}`);
7876
7734
  }
7877
7735
  }
7878
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
7879
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
7880
- result.appendIfPresent(
7881
- `,no-error-alert=${proxy["no-error-alert"]}`,
7882
- "no-error-alert"
7883
- );
7884
7736
  if (isVless) {
7885
7737
  appendVlessParams(result, proxy);
7886
7738
  } else {
@@ -7890,31 +7742,8 @@ function v2ray(proxy, includeUnsupportedProxy) {
7890
7742
  result.appendIfPresent(`,tls=${proxy.tls}`, "tls");
7891
7743
  appendTlsProxyParams(result, proxy, Boolean(proxy.tls));
7892
7744
  }
7893
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
7894
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
7895
- result.appendIfPresent(
7896
- `,test-timeout=${proxy["test-timeout"]}`,
7897
- "test-timeout"
7898
- );
7899
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
7900
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
7901
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
7902
- result.appendIfPresent(
7903
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
7904
- "allow-other-interface"
7905
- );
7906
- result.appendIfPresent(
7907
- `,interface=${proxy["interface-name"]}`,
7908
- "interface-name"
7909
- );
7910
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
7911
7745
  appendShadowTLS(result, proxy);
7912
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
7913
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7914
- result.appendIfPresent(
7915
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
7916
- "underlying-proxy"
7917
- );
7746
+ appendSurgePolicyParameters(result, proxy);
7918
7747
  return result.toString();
7919
7748
  }
7920
7749
  function appendVlessParams(result, proxy) {
@@ -8007,36 +7836,7 @@ function ssh(proxy) {
8007
7836
  `,server-fingerprint="${proxy["server-fingerprint"]}"`,
8008
7837
  "server-fingerprint"
8009
7838
  );
8010
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8011
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8012
- result.appendIfPresent(
8013
- `,no-error-alert=${proxy["no-error-alert"]}`,
8014
- "no-error-alert"
8015
- );
8016
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
8017
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8018
- result.appendIfPresent(
8019
- `,test-timeout=${proxy["test-timeout"]}`,
8020
- "test-timeout"
8021
- );
8022
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8023
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8024
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8025
- result.appendIfPresent(
8026
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8027
- "allow-other-interface"
8028
- );
8029
- result.appendIfPresent(
8030
- `,interface=${proxy["interface-name"]}`,
8031
- "interface-name"
8032
- );
8033
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8034
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8035
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
8036
- result.appendIfPresent(
8037
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8038
- "underlying-proxy"
8039
- );
7839
+ appendSurgePolicyParameters(result, proxy);
8040
7840
  return result.toString();
8041
7841
  }
8042
7842
  function http(proxy) {
@@ -8046,73 +7846,15 @@ function http(proxy) {
8046
7846
  result.appendIfPresent(`,username="${proxy.username}"`, "username");
8047
7847
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
8048
7848
  appendHeaders(result, proxy);
8049
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8050
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8051
- result.appendIfPresent(
8052
- `,no-error-alert=${proxy["no-error-alert"]}`,
8053
- "no-error-alert"
8054
- );
8055
7849
  appendTlsProxyParams(result, proxy, Boolean(proxy.tls));
8056
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
8057
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8058
- result.appendIfPresent(
8059
- `,test-timeout=${proxy["test-timeout"]}`,
8060
- "test-timeout"
8061
- );
8062
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8063
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8064
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8065
- result.appendIfPresent(
8066
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8067
- "allow-other-interface"
8068
- );
8069
- result.appendIfPresent(
8070
- `,interface=${proxy["interface-name"]}`,
8071
- "interface-name"
8072
- );
8073
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8074
7850
  appendShadowTLS(result, proxy);
8075
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8076
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
8077
- result.appendIfPresent(
8078
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8079
- "underlying-proxy"
8080
- );
7851
+ appendSurgePolicyParameters(result, proxy);
8081
7852
  return result.toString();
8082
7853
  }
8083
7854
  function direct(proxy) {
8084
7855
  const result = new Result(proxy);
8085
- const type = "direct";
8086
- result.append(`${proxy.name}=${type}`);
8087
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8088
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8089
- result.appendIfPresent(
8090
- `,no-error-alert=${proxy["no-error-alert"]}`,
8091
- "no-error-alert"
8092
- );
8093
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
8094
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8095
- result.appendIfPresent(
8096
- `,test-timeout=${proxy["test-timeout"]}`,
8097
- "test-timeout"
8098
- );
8099
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8100
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8101
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8102
- result.appendIfPresent(
8103
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8104
- "allow-other-interface"
8105
- );
8106
- result.appendIfPresent(
8107
- `,interface=${proxy["interface-name"]}`,
8108
- "interface-name"
8109
- );
8110
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8111
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8112
- result.appendIfPresent(
8113
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8114
- "underlying-proxy"
8115
- );
7856
+ result.append(`${proxy.name}=direct`);
7857
+ appendSurgePolicyParameters(result, proxy);
8116
7858
  return result.toString();
8117
7859
  }
8118
7860
  function socks5(proxy) {
@@ -8121,41 +7863,10 @@ function socks5(proxy) {
8121
7863
  result.append(`${proxy.name}=${type},${proxy.server},${proxy.port}`);
8122
7864
  result.appendIfPresent(`,username="${proxy.username}"`, "username");
8123
7865
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
8124
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8125
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8126
- result.appendIfPresent(
8127
- `,no-error-alert=${proxy["no-error-alert"]}`,
8128
- "no-error-alert"
8129
- );
8130
7866
  appendTlsProxyParams(result, proxy, Boolean(proxy.tls));
8131
- if (proxy.tfo) {
8132
- app_default.info(`Option tfo is not supported by Surge, thus omitted`);
8133
- }
8134
7867
  result.appendIfPresent(`,udp-relay=${proxy.udp}`, "udp");
8135
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8136
- result.appendIfPresent(
8137
- `,test-timeout=${proxy["test-timeout"]}`,
8138
- "test-timeout"
8139
- );
8140
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8141
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8142
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8143
- result.appendIfPresent(
8144
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8145
- "allow-other-interface"
8146
- );
8147
- result.appendIfPresent(
8148
- `,interface=${proxy["interface-name"]}`,
8149
- "interface-name"
8150
- );
8151
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8152
7868
  appendShadowTLS(result, proxy);
8153
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8154
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
8155
- result.appendIfPresent(
8156
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8157
- "underlying-proxy"
8158
- );
7869
+ appendSurgePolicyParameters(result, proxy);
8159
7870
  return result.toString();
8160
7871
  }
8161
7872
  function appendHeaders(result, proxy) {
@@ -8187,12 +7898,6 @@ function snell(proxy) {
8187
7898
  if (Number(proxy.version) === 6) {
8188
7899
  result.appendIfPresent(`,mode=${proxy.mode}`, "mode");
8189
7900
  }
8190
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8191
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8192
- result.appendIfPresent(
8193
- `,no-error-alert=${proxy["no-error-alert"]}`,
8194
- "no-error-alert"
8195
- );
8196
7901
  result.appendIfPresent(
8197
7902
  `,obfs=${proxy["obfs-opts"]?.mode}`,
8198
7903
  "obfs-opts.mode"
@@ -8205,32 +7910,9 @@ function snell(proxy) {
8205
7910
  `,obfs-uri=${proxy["obfs-opts"]?.path}`,
8206
7911
  "obfs-opts.path"
8207
7912
  );
8208
- result.appendIfPresent(`,tfo=${proxy.tfo}`, "tfo");
8209
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8210
- result.appendIfPresent(
8211
- `,test-timeout=${proxy["test-timeout"]}`,
8212
- "test-timeout"
8213
- );
8214
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8215
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8216
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8217
- result.appendIfPresent(
8218
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8219
- "allow-other-interface"
8220
- );
8221
- result.appendIfPresent(
8222
- `,interface=${proxy["interface-name"]}`,
8223
- "interface-name"
8224
- );
8225
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8226
7913
  appendShadowTLS(result, proxy);
8227
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8228
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
8229
- result.appendIfPresent(
8230
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8231
- "underlying-proxy"
8232
- );
8233
7914
  result.appendIfPresent(`,reuse=${proxy["reuse"]}`, "reuse");
7915
+ appendSurgePolicyParameters(result, proxy);
8234
7916
  return result.toString();
8235
7917
  }
8236
7918
  function tuic(proxy) {
@@ -8243,176 +7925,116 @@ function tuic(proxy) {
8243
7925
  result.appendIfPresent(`,uuid=${proxy.uuid}`, "uuid");
8244
7926
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
8245
7927
  result.appendIfPresent(`,token=${proxy.token}`, "token");
8246
- if (hasNonBlankValue(proxy.ports)) {
8247
- result.append(
8248
- `,port-hopping="${String(proxy.ports).replace(/,/g, ";")}"`
8249
- );
8250
- }
8251
- if (hasNonBlankValue(proxy["hop-interval"])) {
8252
- result.append(`,port-hopping-interval=${proxy["hop-interval"]}`);
8253
- }
8254
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8255
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8256
- result.appendIfPresent(
8257
- `,no-error-alert=${proxy["no-error-alert"]}`,
8258
- "no-error-alert"
8259
- );
7928
+ appendPortHopping(result, proxy);
8260
7929
  appendTlsProxyParams(result, proxy);
8261
- if (isPresent2(proxy, "tfo")) {
8262
- result.append(`,tfo=${proxy["tfo"]}`);
8263
- } else if (isPresent2(proxy, "fast-open")) {
8264
- result.append(`,tfo=${proxy["fast-open"]}`);
8265
- }
8266
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8267
- result.appendIfPresent(
8268
- `,test-timeout=${proxy["test-timeout"]}`,
8269
- "test-timeout"
8270
- );
8271
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8272
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8273
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8274
- result.appendIfPresent(
8275
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8276
- "allow-other-interface"
8277
- );
8278
- result.appendIfPresent(
8279
- `,interface=${proxy["interface-name"]}`,
8280
- "interface-name"
8281
- );
8282
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8283
7930
  appendShadowTLS(result, proxy);
8284
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8285
- result.appendIfPresent(
8286
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8287
- "underlying-proxy"
8288
- );
8289
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
7931
+ appendSurgePolicyParameters(result, proxy);
8290
7932
  return result.toString();
8291
7933
  }
8292
7934
  function wireguard(proxy) {
8293
- if (Array.isArray(proxy.peers) && proxy.peers.length > 0) {
8294
- proxy.server = proxy.peers[0].server;
8295
- proxy.port = proxy.peers[0].port;
8296
- proxy.ip = proxy.peers[0].ip;
8297
- proxy.ipv6 = proxy.peers[0].ipv6;
8298
- proxy["public-key"] = proxy.peers[0]["public-key"];
8299
- proxy["preshared-key"] = proxy.peers[0]["pre-shared-key"];
8300
- proxy["allowed-ips"] = proxy.peers[0]["allowed-ips"];
8301
- proxy.reserved = proxy.peers[0].reserved;
7935
+ const peers = Array.isArray(proxy.peers) && proxy.peers.length > 0 ? proxy.peers : [proxy];
7936
+ const { ip, ipv6 } = normalizeWireGuardInterface({
7937
+ ip: proxy["self-ip"] ?? proxy.ip ?? peers[0].ip,
7938
+ ipv6: proxy["self-ip-v6"] ?? proxy.ipv6 ?? peers[0].ipv6
7939
+ });
7940
+ const keyField = isNotBlank(proxy["private-key-keystore-name"]) ? "private-key-keystore-name" : "private-key";
7941
+ const keyValue = proxy[keyField];
7942
+ if (!hasNonBlankValue(keyValue) || !ip && !ipv6) {
7943
+ throw new Error(
7944
+ "Surge WireGuard requires a local address and either private-key or private-key-keystore-name"
7945
+ );
8302
7946
  }
7947
+ const listenPort = proxy["listen-port"];
7948
+ if (listenPort != null && (!/^\d+$/.test(`${listenPort}`.trim()) || listenPort < 1 || listenPort > 65535)) {
7949
+ throw new Error(
7950
+ "Surge WireGuard listen-port must be between 1 and 65535"
7951
+ );
7952
+ }
7953
+ const sectionName = getIfNotBlank(proxy["section-name"], proxy.name).trim();
7954
+ const policy = wireguardPolicy({
7955
+ ...proxy,
7956
+ "section-name": sectionName,
7957
+ "underlying-proxy": listenPort == null ? proxy["underlying-proxy"] : void 0
7958
+ });
8303
7959
  const result = new Result(proxy);
8304
7960
  result.append(`# > WireGuard Proxy ${proxy.name}
8305
- # ${proxy.name}=wireguard`);
8306
- proxy["section-name"] = getIfNotBlank(proxy["section-name"], proxy.name);
8307
- result.appendIfPresent(
8308
- `,section-name=${proxy["section-name"]}`,
8309
- "section-name"
8310
- );
8311
- result.appendIfPresent(
8312
- `,no-error-alert=${proxy["no-error-alert"]}`,
8313
- "no-error-alert"
8314
- );
8315
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8316
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8317
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8318
- result.appendIfPresent(
8319
- `,test-timeout=${proxy["test-timeout"]}`,
8320
- "test-timeout"
8321
- );
8322
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8323
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8324
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8325
- result.appendIfPresent(
8326
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8327
- "allow-other-interface"
8328
- );
8329
- result.appendIfPresent(
8330
- `,interface=${proxy["interface-name"]}`,
8331
- "interface-name"
8332
- );
8333
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8334
- appendShadowTLS(result, proxy);
8335
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8336
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
8337
- result.appendIfPresent(
8338
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8339
- "underlying-proxy"
8340
- );
8341
- result.append(`
7961
+ # ${policy}
8342
7962
  # > WireGuard Section ${proxy.name}
8343
- [WireGuard ${proxy["section-name"]}]
8344
- private-key = ${proxy["private-key"]}`);
7963
+ [WireGuard ${sectionName}]
7964
+ ${keyField} = ${keyValue}`);
7965
+ if (ip) result.append(`
7966
+ self-ip = ${ip}`);
7967
+ if (ipv6) result.append(`
7968
+ self-ip-v6 = ${ipv6}`);
7969
+ const dns = proxy["dns-server"] ?? proxy.dns;
7970
+ const dnsServers = Array.isArray(dns) ? dns.join(", ") : dns;
7971
+ if (isNotBlank(dnsServers)) result.append(`
7972
+ dns-server = ${dnsServers}`);
8345
7973
  result.appendIfPresent(`
8346
- self-ip = ${proxy.ip}`, "ip");
8347
- result.appendIfPresent(`
8348
- self-ip-v6 = ${proxy.ipv6}`, "ipv6");
8349
- if (proxy.dns) {
8350
- if (Array.isArray(proxy.dns)) {
8351
- proxy.dns = proxy.dns.join(", ");
8352
- }
7974
+ mtu = ${proxy.mtu}`, "mtu");
7975
+ if (isPresent2(proxy, "prefer-ipv6")) {
8353
7976
  result.append(`
8354
- dns-server = ${proxy.dns}`);
7977
+ prefer-ipv6 = ${proxy["prefer-ipv6"]}`);
7978
+ } else if (["ipv6-prefer", "prefer-v6"].includes(proxy["ip-version"])) {
7979
+ result.append(`
7980
+ prefer-ipv6 = true`);
7981
+ }
7982
+ const allowPeerAccess = proxy["allow-peer-access"] ?? proxy["accept-incoming"];
7983
+ if (listenPort == null && allowPeerAccess != null) {
7984
+ result.append(`
7985
+ allow-peer-access = ${allowPeerAccess}`);
8355
7986
  }
8356
7987
  result.appendIfPresent(`
8357
- mtu = ${proxy.mtu}`, "mtu");
8358
- if (ip_version === "prefer-v6") {
7988
+ listen-port = ${listenPort}`, "listen-port");
7989
+ const singlePeerDefaults = peers.length === 1 ? proxy : {};
7990
+ const defaultAllowedIps = peers.length === 1 && listenPort == null && (allowPeerAccess == null || /^false$/i.test(`${allowPeerAccess}`.trim())) ? [ip && "0.0.0.0/0", ipv6 && "::/0"].filter(Boolean).join(",") : void 0;
7991
+ for (const peer of peers) {
7992
+ const routes = peer["allowed-ips"] ?? singlePeerDefaults["allowed-ips"];
7993
+ const allowedIps = stripSurgeQuotes(
7994
+ Array.isArray(routes) ? routes.join(",") : routes
7995
+ ) || defaultAllowedIps;
7996
+ if (!hasNonBlankValue(peer["public-key"]) || !allowedIps) {
7997
+ throw new Error(
7998
+ "Surge WireGuard peers require public-key and allowed-ips"
7999
+ );
8000
+ }
8001
+ let endpoint = peer.endpoint;
8002
+ if (!isNotBlank(endpoint) && isNotBlank(peer.server) && hasNonBlankValue(peer.port)) {
8003
+ const server = peer.server.trim();
8004
+ endpoint = `${isIPv6(server) ? `[${server}]` : server}:${peer.port}`;
8005
+ }
8006
+ if (!isNotBlank(endpoint) && listenPort == null) {
8007
+ throw new Error(
8008
+ "Surge WireGuard peers require endpoint without listen-port"
8009
+ );
8010
+ }
8011
+ let clientId = stripSurgeQuotes(peer["client-id"] ?? peer.reserved);
8012
+ if (Array.isArray(clientId)) {
8013
+ clientId = clientId.join("/");
8014
+ } else if (/^[\da-f]{6}$/i.test(clientId)) {
8015
+ clientId = `0x${clientId}`;
8016
+ }
8017
+ const fields = {
8018
+ "public-key": peer["public-key"],
8019
+ "allowed-ips": quoteSurgeValue(allowedIps),
8020
+ endpoint,
8021
+ keepalive: peer["persistent-keepalive"] ?? peer.keepalive ?? peer["persistent-keepalive-interval"] ?? proxy["persistent-keepalive"] ?? proxy.keepalive,
8022
+ "client-id": clientId,
8023
+ "preshared-key": peer["preshared-key"] ?? peer["pre-shared-key"] ?? singlePeerDefaults["preshared-key"] ?? singlePeerDefaults["pre-shared-key"]
8024
+ };
8025
+ const options = Object.entries(fields).filter(([, value]) => hasNonBlankValue(value)).map(([key, value]) => `${key} = ${value}`).join(", ");
8359
8026
  result.append(`
8360
- prefer-ipv6 = true`);
8027
+ peer = (${options})`);
8361
8028
  }
8362
- const allowedIps = Array.isArray(proxy["allowed-ips"]) ? proxy["allowed-ips"].join(",") : proxy["allowed-ips"];
8363
- let reserved = Array.isArray(proxy.reserved) ? proxy.reserved.join("/") : proxy.reserved;
8364
- let presharedKey = proxy["preshared-key"] ?? proxy["pre-shared-key"];
8365
- const peer = {
8366
- "public-key": proxy["public-key"],
8367
- "allowed-ips": allowedIps ? `"${allowedIps}"` : void 0,
8368
- endpoint: `${proxy.server}:${proxy.port}`,
8369
- keepalive: proxy["persistent-keepalive"] || proxy.keepalive,
8370
- "client-id": reserved,
8371
- "preshared-key": presharedKey
8372
- };
8373
- result.append(
8374
- `
8375
- peer = (${Object.keys(peer).filter((k) => peer[k] != null).map((k) => `${k} = ${peer[k]}`).join(", ")})`
8376
- );
8377
8029
  return result.toString();
8378
8030
  }
8379
- function wireguard_surge(proxy) {
8031
+ function wireguardPolicy(proxy) {
8032
+ const sectionName = getIfNotBlank(proxy["section-name"])?.trim();
8033
+ if (!sectionName) throw new Error("Surge WireGuard requires section-name");
8034
+ const sectionNameValue = sectionName.includes(",") ? quoteSurgeValue(sectionName) : sectionName;
8380
8035
  const result = new Result(proxy);
8381
- result.append(`${proxy.name}=wireguard`);
8382
- result.appendIfPresent(
8383
- `,section-name=${proxy["section-name"]}`,
8384
- "section-name"
8385
- );
8386
- result.appendIfPresent(
8387
- `,no-error-alert=${proxy["no-error-alert"]}`,
8388
- "no-error-alert"
8389
- );
8390
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8391
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8392
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8393
- result.appendIfPresent(
8394
- `,test-timeout=${proxy["test-timeout"]}`,
8395
- "test-timeout"
8396
- );
8397
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8398
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8399
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8400
- result.appendIfPresent(
8401
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8402
- "allow-other-interface"
8403
- );
8404
- result.appendIfPresent(
8405
- `,interface=${proxy["interface-name"]}`,
8406
- "interface-name"
8407
- );
8408
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8409
- appendShadowTLS(result, proxy);
8410
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8411
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
8412
- result.appendIfPresent(
8413
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8414
- "underlying-proxy"
8415
- );
8036
+ result.append(`${proxy.name}=wireguard,section-name=${sectionNameValue}`);
8037
+ appendSurgePolicyParameters(result, proxy);
8416
8038
  return result.toString();
8417
8039
  }
8418
8040
  function hysteria2(proxy) {
@@ -8426,57 +8048,17 @@ function hysteria2(proxy) {
8426
8048
  const result = new Result(proxy);
8427
8049
  result.append(`${proxy.name}=hysteria2,${proxy.server},${proxy.port}`);
8428
8050
  result.appendIfPresent(`,password="${proxy.password}"`, "password");
8429
- if (hasNonBlankValue(proxy.ports)) {
8430
- result.append(
8431
- `,port-hopping="${String(proxy.ports).replace(/,/g, ";")}"`
8432
- );
8433
- }
8434
- if (hasNonBlankValue(proxy["hop-interval"])) {
8435
- result.append(`,port-hopping-interval=${proxy["hop-interval"]}`);
8436
- }
8051
+ appendPortHopping(result, proxy);
8437
8052
  if (proxy["obfs-password"] && obfsPasswordField) {
8438
8053
  result.append(`,${obfsPasswordField}="${proxy["obfs-password"]}"`);
8439
8054
  }
8440
- const ip_version = ipVersions[proxy["ip-version"]] || proxy["ip-version"];
8441
- result.appendIfPresent(`,ip-version=${ip_version}`, "ip-version");
8442
- result.appendIfPresent(
8443
- `,no-error-alert=${proxy["no-error-alert"]}`,
8444
- "no-error-alert"
8445
- );
8446
8055
  appendTlsProxyParams(result, proxy);
8447
- if (isPresent2(proxy, "tfo")) {
8448
- result.append(`,tfo=${proxy["tfo"]}`);
8449
- } else if (isPresent2(proxy, "fast-open")) {
8450
- result.append(`,tfo=${proxy["fast-open"]}`);
8451
- }
8452
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8453
- result.appendIfPresent(
8454
- `,test-timeout=${proxy["test-timeout"]}`,
8455
- "test-timeout"
8456
- );
8457
- result.appendIfPresent(`,test-udp=${proxy["test-udp"]}`, "test-udp");
8458
- result.appendIfPresent(`,hybrid=${proxy["hybrid"]}`, "hybrid");
8459
- result.appendIfPresent(`,tos=${proxy["tos"]}`, "tos");
8460
- result.appendIfPresent(
8461
- `,allow-other-interface=${proxy["allow-other-interface"]}`,
8462
- "allow-other-interface"
8463
- );
8464
- result.appendIfPresent(
8465
- `,interface=${proxy["interface-name"]}`,
8466
- "interface-name"
8467
- );
8468
- result.appendIfPresent(`,interface=${proxy["interface"]}`, "interface");
8469
8056
  appendShadowTLS(result, proxy);
8470
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8471
- result.appendIfPresent(
8472
- `,underlying-proxy=${proxy["underlying-proxy"]}`,
8473
- "underlying-proxy"
8474
- );
8475
8057
  result.appendIfPresent(
8476
8058
  `,download-bandwidth=${`${proxy["down"]}`.match(/\d+/)?.[0] || 0}`,
8477
8059
  "down"
8478
8060
  );
8479
- result.appendIfPresent(`,ecn=${proxy.ecn}`, "ecn");
8061
+ appendSurgePolicyParameters(result, proxy);
8480
8062
  return result.toString();
8481
8063
  }
8482
8064
  function handleTransport(result, proxy, includeUnsupportedProxy) {
@@ -8940,18 +8522,8 @@ function external(proxy) {
8940
8522
  result.append(`,addresses=${addresses}`);
8941
8523
  });
8942
8524
  }
8943
- result.appendIfPresent(
8944
- `,no-error-alert=${proxy["no-error-alert"]}`,
8945
- "no-error-alert"
8946
- );
8947
8525
  result.appendIfPresent(`,udp-relay=${proxy.udp}`, "udp");
8948
- if (isPresent2(proxy, "tfo")) {
8949
- result.append(`,tfo=${proxy["tfo"]}`);
8950
- } else if (isPresent2(proxy, "fast-open")) {
8951
- result.append(`,tfo=${proxy["fast-open"]}`);
8952
- }
8953
- result.appendIfPresent(`,test-url=${proxy["test-url"]}`, "test-url");
8954
- result.appendIfPresent(`,block-quic=${proxy["block-quic"]}`, "block-quic");
8526
+ appendSurgePolicyParameters(result, proxy);
8955
8527
  return result.toString();
8956
8528
  }
8957
8529
  function mihomo(proxy, type, opts) {