From b3cd781d2a8660e32a872f8f0a932354afe4145c Mon Sep 17 00:00:00 2001 From: wyhaya Date: Sat, 23 Nov 2019 11:44:44 +0800 Subject: [PATCH] change wildcard match --- .travis.yml | 1 - README.md | 14 ++++++++------ src/config.rs | 41 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index c5c8582..5490032 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,6 @@ matrix: before_install: - set -e - - rustup default nightly - rustup component add rustfmt script: diff --git a/README.md b/README.md index a034096..a112ee3 100644 --- a/README.md +++ b/README.md @@ -71,14 +71,16 @@ You can specify standard domains, or utilize [regular expressions](https://ruste You can update the config file at any time, updns will listen for file changes ```ini -bind 0.0.0.0:53 # Binding address -proxy 8.8.8.8:53 # Proxy address -timeout 2000 # Proxy timeout (ms) +bind 0.0.0.0:53 # Binding address +proxy 8.8.8.8:53 # Proxy address +timeout 2000 # Proxy timeout (ms) # Domain matching -google.com 1.1.1.1 -^\w+.bing.com$ 2.2.2.2 -github.com :: +example.com 1.1.1.1 +*.example.com 2.2.2.2 +^\w+\.example\.[a-z]+$ 3.3.3.3 + +test.com :: # Import from other file import /other/hosts diff --git a/src/config.rs b/src/config.rs index e3c8912..5655cfe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -153,10 +153,7 @@ impl Host { // *.example.com if Self::is_wildcard(domain) { - let s = format!( - "^{}$", - domain.replace(".", r"\.").replace("*", r"([a-z]|\d|-)+") - ); + let s = format!("^{}$", domain.replace(".", r"\.").replace("*", r"[^.]+")); return Ok(Host(MatchMode::Regex(Regex::new(&s)?))); } @@ -357,3 +354,39 @@ impl Config { .boxed() } } + +#[cfg(test)] +mod test_host { + use super::*; + + #[test] + fn test_create() {} + + #[test] + fn test_test() { + let host = Host::new("example.com").unwrap(); + assert!(host.is_match("example.com")); + assert!(!host.is_match("-example.com")); + assert!(!host.is_match("example.com.cn")); + } + + #[test] + fn test_wildcard() { + let host = Host::new("*.example.com").unwrap(); + assert!(host.is_match("test.example.com")); + assert!(!host.is_match("test.example.test")); + assert!(!host.is_match("test.test.com")); + + let host = Host::new("*.example.*").unwrap(); + assert!(host.is_match("test.example.test")); + assert!(!host.is_match("example.com")); + assert!(!host.is_match("test.test.test")); + } + + #[test] + fn test_regex() { + let host = Host::new("^example.com$").unwrap(); + assert!(host.is_match("example.com")); + assert!(!host.is_match("test.example.com")); + } +}