Skip to content

Commit

Permalink
🎨 Rename client_subnet to subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish committed Jun 8, 2024
1 parent 462adee commit bed9862
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/config/domain_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct DomainRule {
pub rr_ttl_min: Option<u64>,
pub rr_ttl_max: Option<u64>,

pub client_subnet: Option<ClientSubnet>,
pub subnet: Option<ClientSubnet>,
}

impl std::ops::AddAssign for DomainRule {
Expand Down
38 changes: 33 additions & 5 deletions src/config/nameserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ pub struct NameServerInfo {
pub server: DnsUrl,

/// set server to group, use with nameserver /domain/group.
/// ```
/// example:
/// -g name
/// -group name
/// ```
pub group: Vec<String>,

/// filter result with blacklist ip
Expand All @@ -64,12 +69,27 @@ pub struct NameServerInfo {
pub check_edns: bool,

/// exclude this server from default group.
/// ```
/// example:
/// -e
/// -exclude-default-group
/// ```
pub exclude_default_group: bool,

/// use proxy to connect to server.
/// ```
/// example:
/// -p name
/// -proxy name
/// ```
pub proxy: Option<String>,

/// set as bootstrap dns server
/// ```
/// example:
/// -b
/// -bootstrap-dns
/// ```
pub bootstrap_dns: bool,

/// nameserver group for resolving.
Expand All @@ -79,16 +99,24 @@ pub struct NameServerInfo {
///
/// ```
/// example:
/// edns-client-subnet [ip/subnet]
/// edns-client-subnet 192.168.1.1/24
/// edns-client-subnet 8::8/56
/// -subnet [ip/subnet]
/// -subnet 192.168.1.1/24
/// -subnet 8::8/56
/// ```
#[serde(with = "serde_opt_str")]
pub edns_client_subnet: Option<IpNet>,
pub subnet: Option<IpNet>,

/// The value for the SO_MARK option on socket.
/// ```
/// example:
/// -set-mark mark
/// ```
pub so_mark: Option<u32>,

/// ```
/// example:
/// -interface lo
/// ```
#[serde(skip_serializing_if = "Option::is_none")]
pub interface: Option<String>,
}
Expand All @@ -107,7 +135,7 @@ impl From<DnsUrl> for NameServerInfo {
interface: None,
so_mark: None,
resolve_group: None,
edns_client_subnet: None,
subnet: None,
}
}
}
8 changes: 4 additions & 4 deletions src/config/parser/domain_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl NomParser for DomainRule {
},
),
map(
options::parse_value(tag_no_case("client-subnet"), NomParser::parse),
options::parse_value(tag_no_case("subnet"), NomParser::parse),
|v| {
rule.client_subnet = Some(From::<IpNet>::from(v));
rule.subnet = Some(From::<IpNet>::from(v));
},
),
map(options::parse_no_value(tag_no_case("no-cache")), |v| {
Expand All @@ -69,12 +69,12 @@ mod tests {
#[test]
fn test_parse() {
assert_eq!(
DomainRule::parse("--speed-check-mode=ping --client-subnet 192.168.0.0/16"),
DomainRule::parse("--speed-check-mode=ping -subnet 192.168.0.0/16"),
Ok((
"",
DomainRule {
speed_check_mode: Some(vec![SpeedCheckMode::Ping].into()),
client_subnet: Some("192.168.0.0/16".parse().unwrap()),
subnet: Some("192.168.0.0/16".parse().unwrap()),
..Default::default()
}
))
Expand Down
18 changes: 9 additions & 9 deletions src/config/parser/nameserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl NomParser for NameServerInfo {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: None,
};
Expand Down Expand Up @@ -116,7 +116,7 @@ impl NomParser for NameServerInfo {
nameserver.interface = v.map(|p| p.to_string());
}
"subnet" => match v {
Some(s) => nameserver.edns_client_subnet = s.parse().ok(),
Some(s) => nameserver.subnet = s.parse().ok(),
None => {
log::warn!("expect suedns client subnetbnet")
}
Expand Down Expand Up @@ -167,7 +167,7 @@ mod tests {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: Some("Net".to_string()),
}
Expand All @@ -188,7 +188,7 @@ mod tests {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: None,
}
Expand Down Expand Up @@ -222,7 +222,7 @@ mod tests {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: None,
}
Expand All @@ -243,7 +243,7 @@ mod tests {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: None,
}
Expand All @@ -264,7 +264,7 @@ mod tests {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: None,
}
Expand All @@ -290,7 +290,7 @@ mod tests {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: None,
}
Expand All @@ -316,7 +316,7 @@ mod tests {
proxy: None,
bootstrap_dns: Default::default(),
resolve_group: None,
edns_client_subnet: None,
subnet: None,
so_mark: None,
interface: None,
}
Expand Down
4 changes: 1 addition & 3 deletions src/dns_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,7 @@ impl NameServerFactory {
info.blacklist_ip,
info.whitelist_ip,
info.check_edns,
info.edns_client_subnet
.map(|x| x.into())
.or(default_client_subnet),
info.subnet.map(|x| x.into()).or(default_client_subnet),
resolver.options().clone(),
);

Expand Down
5 changes: 1 addition & 4 deletions src/dns_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,10 +1022,7 @@ mod tests {

assert_eq!(server.server.proto(), &Protocol::Https);
assert_eq!(server.server.to_string(), "https://223.5.5.5/dns-query");
assert_eq!(
server.edns_client_subnet,
Some("192.168.0.0/16".parse().unwrap())
);
assert_eq!(server.subnet, Some("192.168.0.0/16".parse().unwrap()));
assert!(server.exclude_default_group);
assert!(server.bootstrap_dns);
}
Expand Down
2 changes: 1 addition & 1 deletion src/dns_mw_ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Middleware<DnsContext, DnsRequest, DnsResponse, DnsError> for NameServerMid
_ => None,
})
})
.or_else(|| ctx.domain_rule.as_ref().and_then(|r| r.client_subnet)),
.or_else(|| ctx.domain_rule.as_ref().and_then(|r| r.subnet)),
};

// skip nameserver rule
Expand Down

0 comments on commit bed9862

Please sign in to comment.