change wildcard match
This commit is contained in:
@@ -21,7 +21,6 @@ matrix:
|
|||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
- set -e
|
- set -e
|
||||||
- rustup default nightly
|
|
||||||
- rustup component add rustfmt
|
- rustup component add rustfmt
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
|||||||
@@ -76,9 +76,11 @@ proxy 8.8.8.8:53 # Proxy address
|
|||||||
timeout 2000 # Proxy timeout (ms)
|
timeout 2000 # Proxy timeout (ms)
|
||||||
|
|
||||||
# Domain matching
|
# Domain matching
|
||||||
google.com 1.1.1.1
|
example.com 1.1.1.1
|
||||||
^\w+.bing.com$ 2.2.2.2
|
*.example.com 2.2.2.2
|
||||||
github.com ::
|
^\w+\.example\.[a-z]+$ 3.3.3.3
|
||||||
|
|
||||||
|
test.com ::
|
||||||
|
|
||||||
# Import from other file
|
# Import from other file
|
||||||
import /other/hosts
|
import /other/hosts
|
||||||
|
|||||||
@@ -153,10 +153,7 @@ impl Host {
|
|||||||
|
|
||||||
// *.example.com
|
// *.example.com
|
||||||
if Self::is_wildcard(domain) {
|
if Self::is_wildcard(domain) {
|
||||||
let s = format!(
|
let s = format!("^{}$", domain.replace(".", r"\.").replace("*", r"[^.]+"));
|
||||||
"^{}$",
|
|
||||||
domain.replace(".", r"\.").replace("*", r"([a-z]|\d|-)+")
|
|
||||||
);
|
|
||||||
return Ok(Host(MatchMode::Regex(Regex::new(&s)?)));
|
return Ok(Host(MatchMode::Regex(Regex::new(&s)?)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,3 +354,39 @@ impl Config {
|
|||||||
.boxed()
|
.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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user