Skip to main content

eggress_pproxy_compat/
uri.rs

1use crate::error::CompatError;
2
3/// Parsed pproxy-style URI.
4#[derive(Debug, Clone)]
5pub struct PproxyUri {
6    /// Protocol scheme (e.g. "socks5", "http", "socks4", "trojan", "bind", "listen", "backward").
7    pub scheme: String,
8    /// Optional username.
9    pub username: Option<String>,
10    /// Optional password.
11    pub password: Option<String>,
12    /// Host (empty string means bind to all interfaces).
13    pub host: String,
14    /// Port number.
15    pub port: u16,
16    /// Whether TLS is requested (+tls suffix).
17    pub tls: bool,
18    /// Whether SSL modifier was used (+ssl suffix, treated as unsupported variant of +tls).
19    pub ssl: bool,
20    /// Whether this is a reverse/inbound URI (+in suffix).
21    pub inbound: bool,
22    /// Count of `+in` tokens parsed from the scheme (backward connection count).
23    pub backward_num: u32,
24    /// Optional rule parameter from query string.
25    pub rule: Option<String>,
26    /// Optional rules_file parameter from query string (pproxy URI-attached rule file).
27    pub rules_file: Option<String>,
28    /// Canonical pproxy rule suffix when the query is not `rule=`/`rules_file=`.
29    pub rule_suffix: Option<String>,
30    /// Optional path (used for unix:// scheme).
31    pub path: Option<String>,
32    /// Protocol tokens in the original `scheme`, excluding transport modifiers.
33    pub protocol_chain: Vec<String>,
34    /// Non-protocol scheme modifiers, in source order (`tls`, `ssl`, `in`, ...).
35    pub transport_modifiers: Vec<String>,
36    /// pproxy's optional outbound source binding (`/@localbind`).
37    pub local_bind: Option<String>,
38    /// Fixed destination used by tunnel-style protocols.
39    pub fixed_target: Option<String>,
40    /// Comma-delimited plugin metadata, retained in source order.
41    pub plugins: Vec<PproxyPluginSpec>,
42    /// Fragment authentication, kept separately from URL userinfo.
43    pub auth_fragment: Option<String>,
44    /// The raw URI, retained for diagnostics.
45    pub raw: String,
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct PproxyPluginSpec {
50    pub name: String,
51    pub options: Option<String>,
52}
53
54impl PproxyUri {
55    /// Returns true if this is a reverse proxy listener URI (bind/listen/backward/rebind scheme).
56    pub fn is_reverse_listener(&self) -> bool {
57        matches!(
58            self.scheme.as_str(),
59            "bind" | "listen" | "backward" | "rebind"
60        )
61    }
62
63    /// Returns true if this is a backward/upstream URI with the `+in` modifier
64    /// (e.g., `socks5+in://...`).
65    pub fn is_backward(&self) -> bool {
66        self.inbound
67    }
68
69    /// Returns the number of `+in` tokens parsed from the scheme (the backward
70    /// connection count). A single `+in` yields 1; multiple `+in+in` yields 2, etc.
71    /// Returns 0 if no `+in` modifier is present.
72    pub fn backward_num(&self) -> u32 {
73        self.backward_num
74    }
75
76    /// Redacted display — credentials shown as `****:****`, Unix paths shown as `unix://****`.
77    pub fn redacted_display(&self) -> String {
78        if self.scheme == "unix" {
79            if let Some(ref p) = self.path {
80                let redacted_path = redact_unix_path(p);
81                return format!("unix://{}", redacted_path);
82            }
83            return "unix://****".to_string();
84        }
85
86        let cred_str = if self.username.is_some() {
87            "****:****@"
88        } else {
89            ""
90        };
91        let rule_str = match &self.rule {
92            Some(r) => format!("?rule={}", r),
93            None => String::new(),
94        };
95        let rules_file_str = match &self.rules_file {
96            Some(rf) => format!("?rules_file={}", rf),
97            None => String::new(),
98        };
99        let suffix = self
100            .rule_suffix
101            .as_deref()
102            .map(|r| format!("?{r}"))
103            .unwrap_or_default();
104        let bind = self
105            .local_bind
106            .as_deref()
107            .map(|b| format!("/@{}", b))
108            .unwrap_or_default();
109        let plugins = if self.plugins.is_empty() {
110            String::new()
111        } else {
112            format!(
113                ",{}",
114                self.plugins
115                    .iter()
116                    .map(|p| p.name.as_str())
117                    .collect::<Vec<_>>()
118                    .join(",")
119            )
120        };
121        let target = self
122            .fixed_target
123            .as_deref()
124            .map(|t| format!("{{{t}}}"))
125            .unwrap_or_else(|| self.endpoint_display());
126        format!(
127            "{}://{}{}{}{}{}{}{}",
128            self.scheme_with_tls(),
129            cred_str,
130            target,
131            rule_str,
132            rules_file_str,
133            suffix,
134            bind,
135            plugins,
136        )
137    }
138
139    pub(crate) fn scheme_with_tls(&self) -> String {
140        let mut parts = if self.protocol_chain.is_empty() {
141            vec![self.scheme.clone()]
142        } else {
143            self.protocol_chain.clone()
144        };
145        // `wss` already carries the TLS scheme semantics. Keep its stable
146        // redacted display as `wss://...` while retaining `self.tls` for
147        // translation and listener transport setup.
148        if self.tls && !parts.iter().any(|p| p == "tls") && !parts.iter().any(|p| p == "wss") {
149            parts.push("tls".to_string());
150        }
151        if self.ssl && !parts.iter().any(|p| p == "ssl") {
152            parts.push("ssl".to_string());
153        }
154        if self.inbound {
155            for _ in 0..self.backward_num.max(1) {
156                parts.push("in".to_string());
157            }
158        }
159        parts.join("+")
160    }
161
162    pub(crate) fn endpoint_display(&self) -> String {
163        format!("{}:{}", format_host_for_uri(&self.host), self.port)
164    }
165
166    pub(crate) fn bind_display(&self) -> String {
167        if self.host.is_empty() {
168            format!("0.0.0.0:{}", self.port)
169        } else {
170            self.endpoint_display()
171        }
172    }
173}
174
175/// Redact a Unix socket path for display, preserving only the filename.
176fn redact_unix_path(path: &str) -> String {
177    match path.rfind('/') {
178        Some(pos) => {
179            let dir = &path[..=pos];
180            format!("{}****", dir)
181        }
182        None => "****".to_string(),
183    }
184}
185
186fn format_host_for_uri(host: &str) -> String {
187    if host.is_empty() {
188        String::new()
189    } else if host.contains(':') {
190        format!("[{}]", host)
191    } else {
192        host.to_string()
193    }
194}
195
196/// Parse a single pproxy-style URI into our typed representation.
197///
198/// Supports:
199/// - `scheme://host:port`
200/// - `scheme://user:pass@host:port`
201/// - `scheme+tls://host:port`
202/// - `scheme://host:port?rule=regex`
203/// - `unix:///path/to/socket`
204/// - `redir://:12345`
205/// - `redir://127.0.0.1:12345`
206pub fn parse_pproxy_uri(uri: &str) -> Result<PproxyUri, CompatError> {
207    // The Python compatibility helpers historically accepted a listener URI
208    // followed by a legacy `;` or `__` remote suffix. The typed single-URI
209    // parser describes the listener portion; callers that need every hop use
210    // `parse_pproxy_chain`.
211    let parse_uri = if uri.contains("__") {
212        split_chain_hops(uri).into_iter().next().unwrap_or(uri)
213    } else {
214        uri.split_once(';').map_or(uri, |(head, _)| head)
215    };
216    let (without_fragment, auth_fragment) = split_top_level(parse_uri, '#');
217    let (before_query, query) = split_top_level(without_fragment, '?');
218
219    // Extract scheme
220    let (scheme_part, after_scheme) = if let Some(colon_pos) = before_query.find("://") {
221        let scheme = &before_query[..colon_pos];
222        let rest = &before_query[colon_pos + 3..];
223        (scheme.to_string(), rest)
224    } else {
225        return Err(CompatError::InvalidUri {
226            message: format!("missing scheme in URI: {}", uri),
227        });
228    };
229
230    // Parse protocol tokens and transport modifiers. Keeping both lists avoids
231    // treating a combined listener as one protocol during translation.
232    let mut tls = false;
233    let mut ssl = false;
234    let mut inbound = false;
235    let mut backward_num: u32 = 0;
236    let mut protocol_chain = Vec::new();
237    let mut transport_modifiers = Vec::new();
238    let mut fixed_target = None;
239    for token in split_scheme_tokens(&scheme_part)? {
240        let (token, token_target) = parse_protocol_token(token)?;
241        if let Some(target) = token_target {
242            if !matches!(token, "tunnel" | "raw" | "ws" | "wss" | "h2") {
243                return Err(CompatError::InvalidUri {
244                    message: format!("fixed target is not supported on '{token}'"),
245                });
246            }
247            if fixed_target.replace(target).is_some() {
248                return Err(CompatError::InvalidUri {
249                    message: "URI contains more than one fixed-target protocol token".to_string(),
250                });
251            }
252        }
253        match token {
254            "tls" => {
255                tls = true;
256                transport_modifiers.push(token.to_string());
257            }
258            "ssl" | "secure" => {
259                ssl = true;
260                tls = true;
261                transport_modifiers.push(token.to_string());
262            }
263            "in" => {
264                inbound = true;
265                backward_num += 1;
266                transport_modifiers.push(token.to_string());
267            }
268            "" => {
269                return Err(CompatError::InvalidUri {
270                    message: "empty protocol or modifier in scheme".to_string(),
271                })
272            }
273            token => protocol_chain.push(token.to_string()),
274        }
275    }
276    if protocol_chain.is_empty() {
277        return Err(CompatError::InvalidUri {
278            message: "URI scheme has no protocol".to_string(),
279        });
280    }
281    let scheme = protocol_chain.join("+");
282
283    if protocol_chain.iter().any(|token| token == "wss") {
284        tls = true;
285    }
286
287    // Validate known schemes
288    for protocol in &protocol_chain {
289        match protocol.as_str() {
290            "http" | "https" | "socks4" | "socks4a" | "socks5" | "trojan" | "ss"
291            | "shadowsocks" | "ssr" | "direct" | "ssh" | "unix" | "redir" | "h2" | "ws" | "wss"
292            | "raw" | "tunnel" | "bind" | "listen" | "backward" | "rebind" | "httponly"
293            | "echo" | "quic" | "h3" => {}
294            other => {
295                return Err(CompatError::UnsupportedProtocol(other.to_string()));
296            }
297        }
298    }
299
300    // Handle unix:// scheme — path-based, not host:port
301    if scheme == "unix" {
302        let path = if after_scheme.starts_with('/') {
303            after_scheme.to_string()
304        } else if after_scheme.is_empty() {
305            return Err(CompatError::InvalidUri {
306                message: "unix:// URI requires a path (e.g. unix:///tmp/socket)".to_string(),
307            });
308        } else {
309            // Treat bare content as a relative path
310            format!("/{}", after_scheme)
311        };
312        let (rule, rules_file, rule_suffix) = query
313            .map(extract_query_params)
314            .unwrap_or((None, None, None));
315        return Ok(PproxyUri {
316            scheme,
317            username: None,
318            password: None,
319            host: String::new(),
320            port: 0,
321            tls,
322            ssl,
323            inbound,
324            backward_num,
325            rule,
326            rules_file,
327            rule_suffix,
328            path: Some(path),
329            protocol_chain,
330            transport_modifiers,
331            local_bind: None,
332            fixed_target: None,
333            plugins: Vec::new(),
334            auth_fragment: auth_fragment.map(str::to_string),
335            raw: uri.to_string(),
336        });
337    }
338
339    let (endpoint_part, path_part) = split_top_level(after_scheme, '/');
340    let (credentials, endpoint_str) =
341        if let Some(at_pos) = find_last_at_outside_brackets(endpoint_part) {
342            let (user, pass) = parse_userinfo(&endpoint_part[..at_pos])?;
343            (Some((user, pass)), &endpoint_part[at_pos + 1..])
344        } else {
345            (None, endpoint_part)
346        };
347    let endpoint_fixed_target = if endpoint_str.starts_with('{') {
348        if !endpoint_str.ends_with('}') || endpoint_str.len() <= 2 {
349            return Err(CompatError::InvalidUri {
350                message: "fixed target braces are malformed or empty".to_string(),
351            });
352        }
353        Some(endpoint_str[1..endpoint_str.len() - 1].to_string())
354    } else if endpoint_str.contains('{') || endpoint_str.contains('}') {
355        return Err(CompatError::InvalidUri {
356            message: "fixed target braces are malformed".to_string(),
357        });
358    } else {
359        None
360    };
361    if let Some(ref target) = endpoint_fixed_target {
362        if fixed_target.is_some() {
363            return Err(CompatError::InvalidUri {
364                message: "fixed target is specified both in the protocol and endpoint".to_string(),
365            });
366        }
367        fixed_target = Some(target.clone());
368    }
369    if let Some(target) = fixed_target.as_deref() {
370        let (target_host, _, target_port_specified) = parse_endpoint(target)?;
371        if target_host.is_empty() || !target_port_specified {
372            return Err(CompatError::InvalidUri {
373                message: "fixed target must contain a non-empty host and port".to_string(),
374            });
375        }
376    }
377    // A canonical token target and the URI endpoint have independent roles:
378    // `tunnel{target}://listener` and `ws{target}://listener` bind the
379    // listener endpoint while fixing the relay destination. The legacy
380    // `raw://{target}` form uses the endpoint extension itself as the target.
381    let endpoint_for_parse = endpoint_fixed_target.as_deref().unwrap_or(endpoint_str);
382    let (host, mut port, port_specified) = parse_endpoint(endpoint_for_parse)?;
383    if !port_specified && !host.is_empty() {
384        if let Some(default) = default_port_for_scheme(&scheme) {
385            port = default;
386        }
387    }
388
389    let (local_bind, plugins) = parse_path_metadata(path_part);
390    for plugin in &plugins {
391        if !matches!(
392            plugin.name.as_str(),
393            "plain"
394                | "origin"
395                | "http_simple"
396                | "tls1.2_ticket_auth"
397                | "verify_simple"
398                | "verify_deflate"
399        ) {
400            return Err(CompatError::InvalidUri {
401                message: format!(
402                    "unknown pproxy plugin '{}'; existing plugins: plain, origin, http_simple, tls1.2_ticket_auth, verify_simple, verify_deflate",
403                    plugin.name
404                ),
405            });
406        }
407    }
408    let (rule, rules_file, rule_suffix) = query
409        .map(extract_query_params)
410        .unwrap_or((None, None, None));
411    let (fragment_user, fragment_password) = auth_fragment
412        .filter(|a| !a.is_empty())
413        .map(parse_userinfo)
414        .transpose()?
415        .map_or((None, None), |(u, p)| (Some(u), Some(p)));
416    let credentials = credentials.or_else(|| fragment_user.zip(fragment_password));
417
418    Ok(PproxyUri {
419        scheme,
420        username: credentials.as_ref().map(|c| c.0.clone()),
421        password: credentials.as_ref().map(|c| c.1.clone()),
422        host,
423        port,
424        tls,
425        ssl,
426        inbound,
427        backward_num,
428        rule,
429        rules_file,
430        path: None,
431        rule_suffix,
432        protocol_chain,
433        transport_modifiers,
434        local_bind,
435        fixed_target,
436        plugins,
437        auth_fragment: auth_fragment.map(str::to_string),
438        raw: uri.to_string(),
439    })
440}
441
442fn split_top_level(input: &str, delimiter: char) -> (&str, Option<&str>) {
443    let mut bracket = 0u32;
444    let mut brace = 0u32;
445    for (idx, ch) in input.char_indices() {
446        match ch {
447            '[' => bracket += 1,
448            ']' => bracket = bracket.saturating_sub(1),
449            '{' => brace += 1,
450            '}' => brace = brace.saturating_sub(1),
451            _ => {}
452        }
453        if ch == delimiter && bracket == 0 && brace == 0 {
454            return (&input[..idx], Some(&input[idx + 1..]));
455        }
456    }
457    (input, None)
458}
459
460/// Split a protocol expression without treating a brace-delimited tunnel
461/// target as a protocol separator.
462fn split_scheme_tokens(scheme: &str) -> Result<Vec<&str>, CompatError> {
463    let mut tokens = Vec::new();
464    let mut start = 0;
465    let mut brace_depth = 0u32;
466    for (idx, ch) in scheme.char_indices() {
467        match ch {
468            '{' => {
469                if brace_depth != 0 {
470                    return Err(CompatError::InvalidUri {
471                        message: "nested fixed-target braces are not valid".to_string(),
472                    });
473                }
474                brace_depth = 1;
475            }
476            '}' => {
477                if brace_depth == 0 {
478                    return Err(CompatError::InvalidUri {
479                        message: "unmatched fixed-target brace".to_string(),
480                    });
481                }
482                brace_depth = 0;
483            }
484            '+' if brace_depth == 0 => {
485                tokens.push(&scheme[start..idx]);
486                start = idx + 1;
487            }
488            _ => {}
489        }
490    }
491    if brace_depth != 0 {
492        return Err(CompatError::InvalidUri {
493            message: "unterminated fixed-target brace".to_string(),
494        });
495    }
496    tokens.push(&scheme[start..]);
497    Ok(tokens)
498}
499
500fn parse_protocol_token(token: &str) -> Result<(&str, Option<String>), CompatError> {
501    let Some(open) = token.find('{') else {
502        if token.contains('}') {
503            return Err(CompatError::InvalidUri {
504                message: "unmatched fixed-target brace".to_string(),
505            });
506        }
507        return Ok((token, None));
508    };
509    if open == 0 || !token.ends_with('}') {
510        return Err(CompatError::InvalidUri {
511            message: "malformed fixed-target protocol token".to_string(),
512        });
513    }
514    let target = &token[open + 1..token.len() - 1];
515    if target.is_empty() || target.contains('{') || target.contains('}') {
516        return Err(CompatError::InvalidUri {
517            message: "fixed-target protocol token has an empty or malformed target".to_string(),
518        });
519    }
520    Ok((&token[..open], Some(target.to_string())))
521}
522
523fn parse_path_metadata(path: Option<&str>) -> (Option<String>, Vec<PproxyPluginSpec>) {
524    let Some(path) = path else {
525        return (None, Vec::new());
526    };
527    let (bind, plugin_text) = if let Some(rest) = path.strip_prefix("@") {
528        (Some(rest), None)
529    } else if let Some(pos) = path.find("/@") {
530        (Some(&path[pos + 2..]), None)
531    } else {
532        (None, Some(path.trim_start_matches('/')))
533    };
534    let (bind, plugin_text) = if let Some(bind) = bind {
535        let (b, p) = bind
536            .split_once(',')
537            .map_or((bind, None), |(b, p)| (b, Some(p)));
538        (Some(b.to_string()), p)
539    } else {
540        (bind.map(str::to_string), plugin_text)
541    };
542    let plugins = plugin_text
543        .unwrap_or_default()
544        .split(',')
545        .filter(|s| !s.is_empty())
546        .map(|spec| {
547            let (name, options) = spec
548                .split_once('=')
549                .map_or((spec, None), |(n, o)| (n, Some(o.to_string())));
550            PproxyPluginSpec {
551                name: name.to_string(),
552                options,
553            }
554        })
555        .collect();
556    (bind, plugins)
557}
558
559/// Find the position of the LAST unbracketed `@` in `s`. The userinfo
560/// separator is the last `@` after the scheme, not the first; a raw
561/// password containing `@` must not be truncated by the parser.
562fn find_last_at_outside_brackets(s: &str) -> Option<usize> {
563    let mut last_at: Option<usize> = None;
564    let mut bracket_depth = 0u32;
565    for (i, c) in s.char_indices() {
566        match c {
567            '[' => bracket_depth += 1,
568            ']' => bracket_depth = bracket_depth.saturating_sub(1),
569            '@' if bracket_depth == 0 => last_at = Some(i),
570            _ => {}
571        }
572    }
573    last_at
574}
575
576fn parse_userinfo(userinfo: &str) -> Result<(String, String), CompatError> {
577    match userinfo.find(':') {
578        Some(colon_pos) => {
579            let user = userinfo[..colon_pos].to_string();
580            let pass = userinfo[colon_pos + 1..].to_string();
581            Ok((user, pass))
582        }
583        None => {
584            // No colon: treat as password-only (e.g. Trojan: trojan://password@host:port)
585            Ok((String::new(), userinfo.to_string()))
586        }
587    }
588}
589
590fn parse_endpoint(endpoint: &str) -> Result<(String, u16, bool), CompatError> {
591    if endpoint.is_empty() {
592        return Ok((String::new(), 0, false));
593    }
594
595    // Handle bracketed IPv6: [::1]:8080
596    if endpoint.starts_with('[') {
597        let close = endpoint.find(']').ok_or_else(|| CompatError::InvalidUri {
598            message: "unterminated IPv6 bracket".to_string(),
599        })?;
600        let host = &endpoint[1..close];
601        let after = &endpoint[close + 1..];
602        if !after.starts_with(':') {
603            return Err(CompatError::InvalidUri {
604                message: "expected ':' after IPv6 bracket".to_string(),
605            });
606        }
607        let port = after[1..]
608            .parse::<u16>()
609            .map_err(|e| CompatError::InvalidUri {
610                message: format!("invalid port: {}", e),
611            })?;
612        return Ok((host.to_string(), port, true));
613    }
614
615    // Regular host:port
616    let colon_pos = match endpoint.rfind(':') {
617        Some(pos) => pos,
618        None => {
619            return Ok((endpoint.to_string(), 0, false));
620        }
621    };
622    let host = &endpoint[..colon_pos];
623    let port_str = &endpoint[colon_pos + 1..];
624    let port = port_str
625        .parse::<u16>()
626        .map_err(|e| CompatError::InvalidUri {
627            message: format!("invalid port '{}': {}", port_str, e),
628        })?;
629
630    Ok((host.to_string(), port, true))
631}
632
633fn default_port_for_scheme(scheme: &str) -> Option<u16> {
634    match scheme {
635        // pproxy's proxy_by_uri() uses 8080 for every non-SSH endpoint when
636        // the port is omitted. Keep conventional native defaults out of this
637        // compatibility parser.
638        "ssh" => Some(22),
639        "unix" | "direct" => None,
640        _ => Some(8080),
641    }
642}
643
644fn extract_query_params(query: &str) -> (Option<String>, Option<String>, Option<String>) {
645    let mut rule = None;
646    let mut rules_file = None;
647    for param in query.split('&') {
648        if let Some(eq_pos) = param.find('=') {
649            let key = &param[..eq_pos];
650            let value = &param[eq_pos + 1..];
651            if !value.is_empty() {
652                match key {
653                    "rule" => rule = Some(value.to_string()),
654                    "rules_file" => rules_file = Some(value.to_string()),
655                    _ => {}
656                }
657            }
658        }
659    }
660    let suffix = if rule.is_none() && rules_file.is_none() && !query.is_empty() {
661        Some(query.to_string())
662    } else {
663        None
664    };
665    (rule, rules_file, suffix)
666}
667
668/// A parsed pproxy chain (one or more hops separated by `__`).
669#[derive(Debug, Clone)]
670pub struct PproxyChain {
671    /// The raw input string.
672    pub raw: String,
673    /// Parsed hops in order (left = first hop, right = the last hop).
674    pub hops: Vec<PproxyUri>,
675}
676
677impl PproxyChain {
678    /// Redacted display showing all hops separated by `__`.
679    pub fn redacted_display(&self) -> String {
680        self.hops
681            .iter()
682            .map(|h| h.redacted_display())
683            .collect::<Vec<_>>()
684            .join("__")
685    }
686}
687
688/// Parse a pproxy chain URI (one or more hops separated by `__`).
689///
690/// Single-hop URIs without `__` are valid chains with one hop.
691/// Returns an error for:
692/// - Leading, trailing, or doubled `__` separators
693/// - Empty hop segments
694/// - Semicolon or comma separators (not supported in pproxy)
695pub fn parse_pproxy_chain(uri: &str) -> Result<PproxyChain, CompatError> {
696    // Semicolons are never pproxy chain separators. Commas belong to plugin
697    // metadata and must remain inside their hop.
698    if uri.contains(';') {
699        return Err(CompatError::InvalidUri {
700            message: format!(
701                "semicolon and comma are not chain separators in pproxy; use '__' (double underscore) to separate hops: {}",
702                uri
703            ),
704        });
705    }
706
707    // Check for leading/trailing __
708    if uri.starts_with("__") || uri.ends_with("__") {
709        return Err(CompatError::InvalidUri {
710            message: format!("chain URI has leading or trailing '__' separator: {}", uri),
711        });
712    }
713
714    // Check for doubled ____
715    if uri.contains("____") {
716        return Err(CompatError::InvalidUri {
717            message: format!("chain URI has doubled '____' separator: {}", uri),
718        });
719    }
720
721    let mut hops = Vec::new();
722    for segment in split_chain_hops(uri) {
723        if segment.is_empty() {
724            return Err(CompatError::InvalidUri {
725                message: format!("chain URI has empty hop segment: {}", uri),
726            });
727        }
728        let hop = parse_pproxy_uri(segment)?;
729        hops.push(hop);
730    }
731
732    Ok(PproxyChain {
733        raw: uri.to_string(),
734        hops,
735    })
736}
737
738fn split_chain_hops(uri: &str) -> Vec<&str> {
739    let mut result = Vec::new();
740    let mut start = 0;
741    let mut bracket = 0u32;
742    let mut brace = 0u32;
743    let bytes = uri.as_bytes();
744    let mut i = 0;
745    while i < bytes.len() {
746        match bytes[i] as char {
747            '[' => bracket += 1,
748            ']' => bracket = bracket.saturating_sub(1),
749            '{' => brace += 1,
750            '}' => brace = brace.saturating_sub(1),
751            '_' if i + 1 < bytes.len() && bytes[i + 1] == b'_' && bracket == 0 && brace == 0 => {
752                result.push(&uri[start..i]);
753                i += 1;
754                start = i + 1;
755            }
756            _ => {}
757        }
758        i += 1;
759    }
760    result.push(&uri[start..]);
761    result
762}
763
764/// Check if any hop in a chain uses an unsupported protocol for chaining.
765///
766/// Returns a list of (hop_index, protocol_name) for unsupported hops.
767pub fn validate_chain_hops(chain: &PproxyChain) -> Vec<(usize, String)> {
768    let mut unsupported = Vec::new();
769    for (idx, hop) in chain.hops.iter().enumerate() {
770        match hop.scheme.as_str() {
771            "ssh" if cfg!(feature = "ssh") => {}
772            "ssh" | "redir" | "direct" => {
773                unsupported.push((idx, hop.scheme.clone()));
774            }
775            _ => {} // http, https, socks4, socks4a, socks5, trojan, ss, ssr, shadowsocks are supported
776        }
777    }
778    unsupported
779}
780
781/// Check if a Shadowsocks method name is a known legacy stream cipher.
782///
783/// Legacy stream ciphers lack authentication and are not supported by eggress.
784/// This function is used for diagnostic purposes in the pproxy compat layer.
785pub fn is_legacy_ss_method(method: &str) -> bool {
786    let method = method.to_ascii_lowercase();
787    let method = method.strip_suffix('!').unwrap_or(&method);
788    let method = method.strip_suffix("-py").unwrap_or(method);
789    matches!(
790        method,
791        "table"
792            | "aes-128-cfb1"
793            | "aes-192-cfb1"
794            | "aes-256-cfb1"
795            | "aes-128-cfb8"
796            | "aes-192-cfb8"
797            | "aes-256-cfb8"
798            | "aes-128-ctr"
799            | "aes-192-ctr"
800            | "aes-256-ctr"
801            | "aes-128-cfb"
802            | "aes-192-cfb"
803            | "aes-256-cfb"
804            | "aes-128-ofb"
805            | "aes-192-ofb"
806            | "aes-256-ofb"
807            | "rc4"
808            | "rc4-md5"
809            | "bf-cfb"
810            | "cast5-cfb"
811            | "des-cfb"
812            | "camellia-128-cfb"
813            | "camellia-192-cfb"
814            | "camellia-256-cfb"
815            | "idea-cfb"
816            | "rc2-cfb"
817            | "seed-cfb"
818            | "chacha20-ietf"
819            | "chacha20"
820            | "xchacha20"
821            | "xchacha20-ietf"
822            | "salsa20"
823            | "xsalsa20"
824    )
825}
826
827#[cfg(test)]
828mod tests {
829    use super::*;
830
831    #[test]
832    fn test_simple_socks5() {
833        let uri = parse_pproxy_uri("socks5://127.0.0.1:1080").unwrap();
834        assert_eq!(uri.scheme, "socks5");
835        assert_eq!(uri.host, "127.0.0.1");
836        assert_eq!(uri.port, 1080);
837        assert!(uri.username.is_none());
838        assert!(!uri.tls);
839    }
840
841    #[test]
842    fn test_http_with_auth() {
843        let uri = parse_pproxy_uri("http://user:pass@proxy:8080").unwrap();
844        assert_eq!(uri.scheme, "http");
845        assert_eq!(uri.username.as_deref(), Some("user"));
846        assert_eq!(uri.password.as_deref(), Some("pass"));
847        assert_eq!(uri.host, "proxy");
848        assert_eq!(uri.port, 8080);
849    }
850
851    #[test]
852    fn test_socks4() {
853        let uri = parse_pproxy_uri("socks4://0.0.0.0:1080").unwrap();
854        assert_eq!(uri.scheme, "socks4");
855        assert_eq!(uri.host, "0.0.0.0");
856        assert_eq!(uri.port, 1080);
857    }
858
859    #[test]
860    fn test_tls_suffix() {
861        let uri = parse_pproxy_uri("socks5+tls://proxy:1080").unwrap();
862        assert!(uri.tls);
863        assert_eq!(uri.scheme, "socks5");
864    }
865
866    #[test]
867    fn test_with_rule() {
868        let uri = parse_pproxy_uri("socks5://127.0.0.1:1080?rule=.*\\.com").unwrap();
869        assert_eq!(uri.rule.as_deref(), Some(".*\\.com"));
870    }
871
872    #[test]
873    fn test_trojan() {
874        let uri = parse_pproxy_uri("trojan://password@server:443").unwrap();
875        assert_eq!(uri.scheme, "trojan");
876        assert_eq!(uri.password.as_deref(), Some("password"));
877    }
878
879    #[test]
880    fn test_empty_host() {
881        let uri = parse_pproxy_uri("socks5://:1080").unwrap();
882        assert_eq!(uri.host, "");
883        assert_eq!(uri.port, 1080);
884    }
885
886    #[test]
887    fn test_ipv6() {
888        let uri = parse_pproxy_uri("socks5://[::1]:1080").unwrap();
889        assert_eq!(uri.host, "::1");
890        assert_eq!(uri.port, 1080);
891    }
892
893    #[test]
894    fn test_unsupported_scheme() {
895        let err = parse_pproxy_uri("ftp://host:22").unwrap_err();
896        match err {
897            CompatError::UnsupportedProtocol(p) => assert_eq!(p, "ftp"),
898            _ => panic!("expected UnsupportedProtocol"),
899        }
900    }
901
902    #[test]
903    fn test_missing_scheme() {
904        let err = parse_pproxy_uri("host:8080").unwrap_err();
905        match err {
906            CompatError::InvalidUri { .. } => {}
907            _ => panic!("expected InvalidUri"),
908        }
909    }
910
911    #[test]
912    fn test_redacted_display() {
913        let uri = parse_pproxy_uri("http://user:pass@proxy:8080").unwrap();
914        let display = uri.redacted_display();
915        assert!(display.contains("****:****@"));
916        assert!(!display.contains("pass"));
917    }
918
919    #[test]
920    fn test_redacted_display_no_creds() {
921        let uri = parse_pproxy_uri("socks5://127.0.0.1:1080").unwrap();
922        let display = uri.redacted_display();
923        assert_eq!(display, "socks5://127.0.0.1:1080");
924    }
925
926    #[test]
927    fn test_redacted_display_tls_suffix_in_scheme() {
928        let uri = parse_pproxy_uri("socks5+tls://proxy:1080").unwrap();
929        assert_eq!(uri.redacted_display(), "socks5+tls://proxy:1080");
930    }
931
932    #[test]
933    fn test_redacted_display_explicit_zero_port() {
934        let uri = parse_pproxy_uri("socks5://host:0").unwrap();
935        assert_eq!(uri.redacted_display(), "socks5://host:0");
936    }
937
938    #[test]
939    fn test_endpoint_display_brackets_ipv6() {
940        let uri = parse_pproxy_uri("socks5://[::1]:1080").unwrap();
941        assert_eq!(uri.endpoint_display(), "[::1]:1080");
942    }
943
944    #[test]
945    fn test_unix_socket_path() {
946        let uri = parse_pproxy_uri("unix:///tmp/eggress.sock").unwrap();
947        assert_eq!(uri.scheme, "unix");
948        assert_eq!(uri.path.as_deref(), Some("/tmp/eggress.sock"));
949        assert!(uri.host.is_empty());
950        assert_eq!(uri.port, 0);
951    }
952
953    #[test]
954    fn test_unix_socket_relative_path() {
955        let uri = parse_pproxy_uri("unix://var/run/proxy.sock").unwrap();
956        assert_eq!(uri.scheme, "unix");
957        assert_eq!(uri.path.as_deref(), Some("/var/run/proxy.sock"));
958    }
959
960    #[test]
961    fn test_unix_socket_empty_path_errors() {
962        let err = parse_pproxy_uri("unix://").unwrap_err();
963        match err {
964            CompatError::InvalidUri { message } => {
965                assert!(message.contains("requires a path"));
966            }
967            _ => panic!("expected InvalidUri for empty unix path"),
968        }
969    }
970
971    #[test]
972    fn test_unix_redacted_display() {
973        let uri = parse_pproxy_uri("unix:///tmp/secret.sock").unwrap();
974        let display = uri.redacted_display();
975        assert_eq!(display, "unix:///tmp/****");
976        assert!(!display.contains("secret"));
977    }
978
979    #[test]
980    fn test_unix_redacted_display_nested() {
981        let uri = parse_pproxy_uri("unix:///var/run/myapp/secret.sock").unwrap();
982        let display = uri.redacted_display();
983        assert_eq!(display, "unix:///var/run/myapp/****");
984    }
985
986    #[test]
987    fn test_redir_colon_port() {
988        let uri = parse_pproxy_uri("redir://:12345").unwrap();
989        assert_eq!(uri.scheme, "redir");
990        assert_eq!(uri.host, "");
991        assert_eq!(uri.port, 12345);
992        assert!(uri.path.is_none());
993    }
994
995    #[test]
996    fn test_redir_host_port() {
997        let uri = parse_pproxy_uri("redir://127.0.0.1:12345").unwrap();
998        assert_eq!(uri.scheme, "redir");
999        assert_eq!(uri.host, "127.0.0.1");
1000        assert_eq!(uri.port, 12345);
1001    }
1002
1003    #[test]
1004    fn test_redir_bind_display() {
1005        let uri = parse_pproxy_uri("redir://:12345").unwrap();
1006        assert_eq!(uri.bind_display(), "0.0.0.0:12345");
1007    }
1008
1009    #[test]
1010    fn test_redir_specific_bind_display() {
1011        let uri = parse_pproxy_uri("redir://127.0.0.1:12345").unwrap();
1012        assert_eq!(uri.bind_display(), "127.0.0.1:12345");
1013    }
1014
1015    #[test]
1016    fn test_redir_redacted_display() {
1017        let uri = parse_pproxy_uri("redir://:12345").unwrap();
1018        assert_eq!(uri.redacted_display(), "redir://:12345");
1019    }
1020
1021    #[test]
1022    fn test_bind_uri() {
1023        let uri = parse_pproxy_uri("bind://0.0.0.0:8080").unwrap();
1024        assert_eq!(uri.scheme, "bind");
1025        assert_eq!(uri.host, "0.0.0.0");
1026        assert_eq!(uri.port, 8080);
1027        assert!(uri.is_reverse_listener());
1028        assert!(!uri.inbound);
1029    }
1030
1031    #[test]
1032    fn test_listen_uri() {
1033        let uri = parse_pproxy_uri("listen://127.0.0.1:9090").unwrap();
1034        assert_eq!(uri.scheme, "listen");
1035        assert!(uri.is_reverse_listener());
1036    }
1037
1038    #[test]
1039    fn test_backward_uri() {
1040        let uri = parse_pproxy_uri("backward://0.0.0.0:8080").unwrap();
1041        assert_eq!(uri.scheme, "backward");
1042        assert!(uri.is_reverse_listener());
1043    }
1044
1045    #[test]
1046    fn test_rebind_uri() {
1047        let uri = parse_pproxy_uri("rebind://0.0.0.0:8080").unwrap();
1048        assert_eq!(uri.scheme, "rebind");
1049        assert!(uri.is_reverse_listener());
1050    }
1051
1052    #[test]
1053    fn test_bind_with_auth() {
1054        let uri = parse_pproxy_uri("bind://user:pass@0.0.0.0:8080").unwrap();
1055        assert_eq!(uri.scheme, "bind");
1056        assert_eq!(uri.username.as_deref(), Some("user"));
1057        assert_eq!(uri.password.as_deref(), Some("pass"));
1058        assert!(uri.is_reverse_listener());
1059    }
1060
1061    #[test]
1062    fn test_bind_with_tls() {
1063        let uri = parse_pproxy_uri("bind+tls://0.0.0.0:8443").unwrap();
1064        assert_eq!(uri.scheme, "bind");
1065        assert!(uri.tls);
1066        assert!(uri.is_reverse_listener());
1067    }
1068
1069    #[test]
1070    fn test_bind_with_inbound_modifier() {
1071        let uri = parse_pproxy_uri("socks5+in://0.0.0.0:1080").unwrap();
1072        assert_eq!(uri.scheme, "socks5");
1073        assert!(uri.inbound);
1074    }
1075
1076    #[test]
1077    fn test_bind_redacted_display() {
1078        let uri = parse_pproxy_uri("bind://user:pass@0.0.0.0:8080").unwrap();
1079        let display = uri.redacted_display();
1080        assert!(display.contains("****:****@"));
1081        assert!(!display.contains("pass"));
1082    }
1083
1084    #[test]
1085    fn test_bind_tls_in_redacted_display() {
1086        let uri = parse_pproxy_uri("bind+tls://0.0.0.0:8443").unwrap();
1087        assert_eq!(uri.redacted_display(), "bind+tls://0.0.0.0:8443");
1088    }
1089
1090    #[test]
1091    fn test_not_reverse_schemes() {
1092        let uri = parse_pproxy_uri("socks5://127.0.0.1:1080").unwrap();
1093        assert!(!uri.is_reverse_listener());
1094
1095        let uri = parse_pproxy_uri("http://proxy:8080").unwrap();
1096        assert!(!uri.is_reverse_listener());
1097    }
1098
1099    #[test]
1100    fn test_inbound_modifier() {
1101        let uri = parse_pproxy_uri("socks5+in://acceptor:1080").unwrap();
1102        assert!(uri.is_backward());
1103        assert!(!uri.is_reverse_listener());
1104        assert_eq!(uri.backward_num(), 1);
1105    }
1106
1107    #[test]
1108    fn test_multiple_inbound_tokens() {
1109        let uri = parse_pproxy_uri("socks5+in+in://acceptor:1080").unwrap();
1110        assert!(uri.is_backward());
1111        assert_eq!(uri.backward_num(), 2);
1112    }
1113
1114    #[test]
1115    fn test_backward_num_zero_without_in() {
1116        let uri = parse_pproxy_uri("socks5://proxy:1080").unwrap();
1117        assert!(!uri.is_backward());
1118        assert_eq!(uri.backward_num(), 0);
1119    }
1120
1121    #[test]
1122    fn test_parse_two_hop_chain() {
1123        let chain = parse_pproxy_chain("http://hop1:8080__socks5://hop2:1080").unwrap();
1124        assert_eq!(chain.hops.len(), 2);
1125        assert_eq!(chain.hops[0].scheme, "http");
1126        assert_eq!(chain.hops[0].host, "hop1");
1127        assert_eq!(chain.hops[0].port, 8080);
1128        assert_eq!(chain.hops[1].scheme, "socks5");
1129        assert_eq!(chain.hops[1].host, "hop2");
1130        assert_eq!(chain.hops[1].port, 1080);
1131    }
1132
1133    #[test]
1134    fn test_parse_three_hop_chain() {
1135        let chain = parse_pproxy_chain("http://h1:80__socks5://h2:1080__socks4://h3:1080").unwrap();
1136        assert_eq!(chain.hops.len(), 3);
1137    }
1138
1139    #[test]
1140    fn test_parse_single_hop_chain() {
1141        let chain = parse_pproxy_chain("socks5://proxy:1080").unwrap();
1142        assert_eq!(chain.hops.len(), 1);
1143        assert_eq!(chain.hops[0].scheme, "socks5");
1144    }
1145
1146    #[test]
1147    fn test_parse_chain_with_creds() {
1148        let chain = parse_pproxy_chain("http://user:pass@h1:80__socks5://h2:1080").unwrap();
1149        assert_eq!(chain.hops.len(), 2);
1150        assert_eq!(chain.hops[0].username.as_deref(), Some("user"));
1151        assert_eq!(chain.hops[0].password.as_deref(), Some("pass"));
1152    }
1153
1154    #[test]
1155    fn test_parse_chain_with_tls_modifier() {
1156        let chain = parse_pproxy_chain("socks5+tls://h1:1080__http://h2:80").unwrap();
1157        assert!(chain.hops[0].tls);
1158        assert!(!chain.hops[1].tls);
1159    }
1160
1161    #[test]
1162    fn test_parse_chain_semicolon_rejected() {
1163        let err = parse_pproxy_chain("http://h1:80;socks5://h2:1080").unwrap_err();
1164        match err {
1165            CompatError::InvalidUri { message } => {
1166                assert!(message.contains("semicolon"));
1167            }
1168            _ => panic!("expected InvalidUri for semicolon"),
1169        }
1170    }
1171
1172    #[test]
1173    fn test_parse_chain_plugin_comma_preserved() {
1174        let chain = parse_pproxy_chain("http://h1:80/,verify_simple").unwrap();
1175        assert_eq!(chain.hops[0].plugins[0].name, "verify_simple");
1176    }
1177
1178    #[test]
1179    fn test_parse_chain_leading_separator() {
1180        let err = parse_pproxy_chain("__http://h1:80").unwrap_err();
1181        match err {
1182            CompatError::InvalidUri { message } => {
1183                assert!(message.contains("leading"));
1184            }
1185            _ => panic!("expected InvalidUri for leading separator"),
1186        }
1187    }
1188
1189    #[test]
1190    fn test_parse_chain_trailing_separator() {
1191        let err = parse_pproxy_chain("http://h1:80__").unwrap_err();
1192        match err {
1193            CompatError::InvalidUri { message } => {
1194                assert!(message.contains("trailing"));
1195            }
1196            _ => panic!("expected InvalidUri for trailing separator"),
1197        }
1198    }
1199
1200    #[test]
1201    fn test_parse_chain_empty_segment() {
1202        let err = parse_pproxy_chain("http://h1:80____socks5://h2:1080").unwrap_err();
1203        match err {
1204            CompatError::InvalidUri { message } => {
1205                assert!(message.contains("doubled"));
1206            }
1207            _ => panic!("expected InvalidUri for doubled separator"),
1208        }
1209    }
1210
1211    #[test]
1212    fn test_chain_redacted_display() {
1213        let chain = parse_pproxy_chain("http://user:pass@h1:80__socks5://h2:1080").unwrap();
1214        let display = chain.redacted_display();
1215        assert!(display.contains("****"));
1216        assert!(!display.contains("pass"));
1217        assert!(display.contains("__"));
1218    }
1219
1220    #[test]
1221    fn test_validate_chain_hops_all_supported() {
1222        let chain = parse_pproxy_chain("http://h1:80__socks5://h2:1080").unwrap();
1223        let unsupported = validate_chain_hops(&chain);
1224        assert!(unsupported.is_empty());
1225    }
1226
1227    #[test]
1228    fn test_validate_chain_hops_ssh_unsupported() {
1229        let chain = parse_pproxy_chain("http://h1:80__ssh://h2:22").unwrap();
1230        let unsupported = validate_chain_hops(&chain);
1231        #[cfg(feature = "ssh")]
1232        assert!(unsupported.is_empty());
1233        #[cfg(not(feature = "ssh"))]
1234        {
1235            assert_eq!(unsupported.len(), 1);
1236            assert_eq!(unsupported[0], (1, "ssh".to_string()));
1237        }
1238    }
1239
1240    #[test]
1241    fn test_validate_chain_hops_ssr_supported() {
1242        let chain = parse_pproxy_chain("http://h1:80__ssr://h2:8388").unwrap();
1243        let unsupported = validate_chain_hops(&chain);
1244        assert!(unsupported.is_empty());
1245    }
1246
1247    #[test]
1248    fn test_default_port_socks5() {
1249        let uri = parse_pproxy_uri("socks5://host").unwrap();
1250        assert_eq!(uri.host, "host");
1251        assert_eq!(uri.port, 8080);
1252    }
1253
1254    #[test]
1255    fn test_default_port_http() {
1256        let uri = parse_pproxy_uri("http://host").unwrap();
1257        assert_eq!(uri.host, "host");
1258        assert_eq!(uri.port, 8080);
1259    }
1260
1261    #[test]
1262    fn test_default_port_https() {
1263        let uri = parse_pproxy_uri("https://host").unwrap();
1264        assert_eq!(uri.host, "host");
1265        assert_eq!(uri.port, 8080);
1266    }
1267
1268    #[test]
1269    fn test_default_port_trojan() {
1270        let uri = parse_pproxy_uri("trojan://password@host").unwrap();
1271        assert_eq!(uri.host, "host");
1272        assert_eq!(uri.port, 8080);
1273    }
1274
1275    #[test]
1276    fn test_default_port_shadowsocks() {
1277        let uri = parse_pproxy_uri("ss://method:pass@host").unwrap();
1278        assert_eq!(uri.host, "host");
1279        assert_eq!(uri.port, 8080);
1280    }
1281
1282    #[test]
1283    fn test_explicit_port_overrides_default() {
1284        let uri = parse_pproxy_uri("socks5://host:9090").unwrap();
1285        assert_eq!(uri.host, "host");
1286        assert_eq!(uri.port, 9090);
1287    }
1288
1289    #[test]
1290    fn test_empty_port_with_colon() {
1291        let uri = parse_pproxy_uri("socks5://:1080").unwrap();
1292        assert_eq!(uri.host, "");
1293        assert_eq!(uri.port, 1080);
1294    }
1295
1296    #[test]
1297    fn test_chain_default_ports() {
1298        let chain = parse_pproxy_chain("socks5://h1__http://h2").unwrap();
1299        assert_eq!(chain.hops[0].port, 8080);
1300        assert_eq!(chain.hops[1].port, 8080);
1301    }
1302
1303    #[test]
1304    fn test_explicit_zero_port_preserved_socks5() {
1305        let uri = parse_pproxy_uri("socks5://127.0.0.1:0").unwrap();
1306        assert_eq!(uri.host, "127.0.0.1");
1307        assert_eq!(uri.port, 0);
1308    }
1309
1310    #[test]
1311    fn test_explicit_zero_port_preserved_http() {
1312        let uri = parse_pproxy_uri("http://example.com:0").unwrap();
1313        assert_eq!(uri.host, "example.com");
1314        assert_eq!(uri.port, 0);
1315    }
1316
1317    #[test]
1318    fn test_explicit_zero_port_preserved_https() {
1319        let uri = parse_pproxy_uri("https://example.com:0").unwrap();
1320        assert_eq!(uri.host, "example.com");
1321        assert_eq!(uri.port, 0);
1322    }
1323
1324    #[test]
1325    fn test_explicit_zero_port_preserved_trojan() {
1326        let uri = parse_pproxy_uri("trojan://password@example.com:0").unwrap();
1327        assert_eq!(uri.host, "example.com");
1328        assert_eq!(uri.port, 0);
1329    }
1330
1331    #[test]
1332    fn test_password_containing_at_sign() {
1333        // Regression: raw '@' inside the password must not be treated as
1334        // the userinfo/host separator. The userinfo separator is the LAST
1335        // unbracketed '@' after the scheme.
1336        let uri = parse_pproxy_uri("socks5://admin:s3cret_p@ssw0rd@127.0.0.1:1080").unwrap();
1337        assert_eq!(uri.scheme, "socks5");
1338        assert_eq!(uri.username.as_deref(), Some("admin"));
1339        assert_eq!(uri.password.as_deref(), Some("s3cret_p@ssw0rd"));
1340        assert_eq!(uri.host, "127.0.0.1");
1341        assert_eq!(uri.port, 1080);
1342    }
1343
1344    #[test]
1345    fn test_password_containing_at_sign_redacted_display() {
1346        // Regression: redacted display must not leak any part of the
1347        // password even when the password contains '@'.
1348        let uri = parse_pproxy_uri("socks5://admin:s3cret_p@ssw0rd@127.0.0.1:1080").unwrap();
1349        let display = uri.redacted_display();
1350        assert_eq!(display, "socks5://****:****@127.0.0.1:1080");
1351        assert!(!display.contains("s3cret_p"));
1352        assert!(!display.contains("ssw0rd"));
1353        assert!(!display.contains("admin"));
1354    }
1355
1356    #[test]
1357    fn test_password_containing_at_sign_chain() {
1358        // Regression: last '@' must also be honored inside chain hops.
1359        let chain = parse_pproxy_chain("socks5://user:p@ss@proxy1:1080__http://h2:8080").unwrap();
1360        assert_eq!(chain.hops.len(), 2);
1361        assert_eq!(chain.hops[0].username.as_deref(), Some("user"));
1362        assert_eq!(chain.hops[0].password.as_deref(), Some("p@ss"));
1363        assert_eq!(chain.hops[0].host, "proxy1");
1364        assert_eq!(chain.hops[0].port, 1080);
1365    }
1366
1367    #[test]
1368    fn test_password_containing_at_sign_redir() {
1369        // Regression: redir:// must also use the LAST unbracketed '@'.
1370        let uri = parse_pproxy_uri("redir://admin:s3cret_p@ssw0rd@127.0.0.1:12345").unwrap();
1371        assert_eq!(uri.scheme, "redir");
1372        assert_eq!(uri.username.as_deref(), Some("admin"));
1373        assert_eq!(uri.password.as_deref(), Some("s3cret_p@ssw0rd"));
1374        assert_eq!(uri.host, "127.0.0.1");
1375        assert_eq!(uri.port, 12345);
1376        assert_eq!(uri.redacted_display(), "redir://****:****@127.0.0.1:12345");
1377    }
1378
1379    #[test]
1380    fn test_password_containing_at_sign_shadowsocks() {
1381        // Regression: ss:// userinfo "method:password@host:port" with '@' in
1382        // the password must keep the full password.
1383        let uri = parse_pproxy_uri("ss://aes-256-gcm:p@ssw0rd@proxy:8388").unwrap();
1384        assert_eq!(uri.scheme, "ss");
1385        assert_eq!(uri.username.as_deref(), Some("aes-256-gcm"));
1386        assert_eq!(uri.password.as_deref(), Some("p@ssw0rd"));
1387        assert_eq!(uri.host, "proxy");
1388        assert_eq!(uri.port, 8388);
1389    }
1390
1391    #[test]
1392    fn test_password_containing_at_sign_trojan() {
1393        // Regression: trojan:// accepts password-only creds; '@' in the
1394        // password must be preserved.
1395        let uri = parse_pproxy_uri("trojan://my_p@ssw0rd@server:443").unwrap();
1396        assert_eq!(uri.scheme, "trojan");
1397        assert_eq!(uri.username.as_deref(), Some(""));
1398        assert_eq!(uri.password.as_deref(), Some("my_p@ssw0rd"));
1399        assert_eq!(uri.host, "server");
1400        assert_eq!(uri.port, 443);
1401    }
1402
1403    #[test]
1404    fn test_with_rules_file() {
1405        let uri =
1406            parse_pproxy_uri("socks5://127.0.0.1:1080?rules_file=/path/to/rules.txt").unwrap();
1407        assert_eq!(uri.rules_file.as_deref(), Some("/path/to/rules.txt"));
1408    }
1409
1410    #[test]
1411    fn test_with_rules_file_and_rule() {
1412        let uri =
1413            parse_pproxy_uri("socks5://127.0.0.1:1080?rule=.*\\.com&rules_file=/path/to/rules.txt")
1414                .unwrap();
1415        assert_eq!(uri.rule.as_deref(), Some(".*\\.com"));
1416        assert_eq!(uri.rules_file.as_deref(), Some("/path/to/rules.txt"));
1417    }
1418
1419    #[test]
1420    fn test_combined_listener_tokens_and_modifiers() {
1421        let uri = parse_pproxy_uri("http+socks4+socks5+tls+in+in://:8080").unwrap();
1422        assert_eq!(uri.protocol_chain, ["http", "socks4", "socks5"]);
1423        assert_eq!(uri.backward_num(), 2);
1424        assert!(uri.tls);
1425        assert_eq!(uri.scheme, "http+socks4+socks5");
1426    }
1427
1428    #[test]
1429    fn test_fragment_auth_local_bind_and_plugins() {
1430        let uri =
1431            parse_pproxy_uri("http://proxy:8080/@192.0.2.1,verify_simple,plain#user:pass").unwrap();
1432        assert_eq!(uri.local_bind.as_deref(), Some("192.0.2.1"));
1433        assert_eq!(uri.plugins.len(), 2);
1434        assert_eq!(uri.plugins[0].name, "verify_simple");
1435        assert_eq!(uri.auth_fragment.as_deref(), Some("user:pass"));
1436        assert_eq!(uri.username.as_deref(), Some("user"));
1437        assert!(!uri.redacted_display().contains("secret"));
1438        assert!(!uri.redacted_display().contains("pass"));
1439    }
1440
1441    #[test]
1442    fn test_fixed_target_and_raw_rule_suffix() {
1443        let uri = parse_pproxy_uri("tunnel://{example.com:443}?example\\.com$").unwrap();
1444        assert_eq!(uri.fixed_target.as_deref(), Some("example.com:443"));
1445        assert_eq!(uri.rule_suffix.as_deref(), Some("example\\.com$"));
1446    }
1447
1448    #[test]
1449    fn test_chain_split_does_not_split_fixed_target() {
1450        let chain = parse_pproxy_chain("tunnel://{example.com:443}__socks5://proxy:1080").unwrap();
1451        assert_eq!(chain.hops.len(), 2);
1452        assert_eq!(
1453            chain.hops[0].fixed_target.as_deref(),
1454            Some("example.com:443")
1455        );
1456    }
1457}