use anyhow::{Result, ensure};
pub fn script(suffix: &str, port: u16) -> Result<String> {
ensure!(
is_suffix(suffix),
"suffix {suffix:?} must be lowercase letters, digits, hyphens and dots"
);
Ok(format!(
"function FindProxyForURL(url, host) {{\n \
if (dnsDomainIs(host, \".{suffix}\") || host === \"{suffix}\") {{\n \
return \"PROXY 127.0.0.1:{port}\";\n \
}}\n \
return \"DIRECT\";\n\
}}\n"
))
}
pub(crate) fn is_suffix(s: &str) -> bool {
!s.is_empty()
&& !s.starts_with(['-', '.'])
&& !s.ends_with(['-', '.'])
&& s.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-' || b == b'.')
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn routes_the_suffix_and_nothing_else() {
let s = script("ssh-browser", 7391).unwrap();
assert!(s.contains("dnsDomainIs(host, \".ssh-browser\")"));
assert!(s.contains("PROXY 127.0.0.1:7391"));
assert!(s.contains("return \"DIRECT\""));
}
#[test]
fn a_suffix_that_could_break_out_of_the_string_is_refused() {
assert!(script("a\" + evil + \"b", 7391).is_err());
assert!(script("a\nb", 7391).is_err());
assert!(script("", 7391).is_err());
assert!(script("UPPER", 7391).is_err());
}
#[test]
fn a_custom_suffix_works() {
let s = script("internal.example", 9000).unwrap();
assert!(s.contains("dnsDomainIs(host, \".internal.example\")"));
assert!(s.contains("PROXY 127.0.0.1:9000"));
}
}