Skip to content

Commit 68d315e

Browse files
committed
expose dns in command line options
1 parent 072f89d commit 68d315e

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

bin/sslocal.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ fn main() {
6464
(@arg NO_DELAY: --("no-delay") !takes_value "Set TCP_NODELAY option for socket")
6565
(@arg NOFILE: -n --nofile +takes_value "Set RLIMIT_NOFILE with both soft and hard limit (only for *nix systems)")
6666
(@arg ACL: --acl +takes_value "Path to ACL (Access Control List)")
67+
(@arg DNS: --dns +takes_value "DNS nameservers, formatted like [(tcp|udp)://]host[:port][,host[:port]]..., or unix:///path/to/dns, or predefined keys like \"google\", \"cloudflare\"")
6768

6869
(@arg UDP_TIMEOUT: --("udp-timeout") +takes_value {validator::validate_u64} "Timeout seconds for UDP relay")
6970
(@arg UDP_MAX_ASSOCIATIONS: --("udp-max-associations") +takes_value {validator::validate_u64} "Maximum associations to be kept simultaneously for UDP relay")
@@ -362,6 +363,10 @@ fn main() {
362363
config.acl = Some(acl);
363364
}
364365

366+
if let Some(dns) = matches.value_of("DNS") {
367+
config.set_dns_formatted(dns).expect("dns");
368+
}
369+
365370
if matches.is_present("IPV6_FIRST") {
366371
config.ipv6_first = true;
367372
}

bin/ssmanager.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn main() {
5858

5959
(@arg NOFILE: -n --nofile +takes_value "Set RLIMIT_NOFILE with both soft and hard limit (only for *nix systems)")
6060
(@arg ACL: --acl +takes_value "Path to ACL (Access Control List)")
61+
(@arg DNS: --dns +takes_value "DNS nameservers, formatted like [(tcp|udp)://]host[:port][,host[:port]]..., or unix:///path/to/dns, or predefined keys like \"google\", \"cloudflare\"")
6162

6263
(@arg INBOUND_SEND_BUFFER_SIZE: --("inbound-send-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_SNDBUF option")
6364
(@arg INBOUND_RECV_BUFFER_SIZE: --("inbound-recv-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_RCVBUF option")
@@ -201,6 +202,10 @@ fn main() {
201202
config.acl = Some(acl);
202203
}
203204

205+
if let Some(dns) = matches.value_of("DNS") {
206+
config.set_dns_formatted(dns).expect("dns");
207+
}
208+
204209
if matches.is_present("IPV6_FIRST") {
205210
config.ipv6_first = true;
206211
}

bin/ssserver.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ fn main() {
5959
(@arg NO_DELAY: --("no-delay") !takes_value "Set TCP_NODELAY option for socket")
6060
(@arg NOFILE: -n --nofile +takes_value "Set RLIMIT_NOFILE with both soft and hard limit (only for *nix systems)")
6161
(@arg ACL: --acl +takes_value "Path to ACL (Access Control List)")
62+
(@arg DNS: --dns +takes_value "DNS nameservers, formatted like [(tcp|udp)://]host[:port][,host[:port]]..., or unix:///path/to/dns, or predefined keys like \"google\", \"cloudflare\"")
6263

6364
(@arg UDP_TIMEOUT: --("udp-timeout") +takes_value {validator::validate_u64} "Timeout seconds for UDP relay")
6465
(@arg UDP_MAX_ASSOCIATIONS: --("udp-max-associations") +takes_value {validator::validate_u64} "Maximum associations to be kept simultaneously for UDP relay")
@@ -215,6 +216,10 @@ fn main() {
215216
config.acl = Some(acl);
216217
}
217218

219+
if let Some(dns) = matches.value_of("DNS") {
220+
config.set_dns_formatted(dns).expect("dns");
221+
}
222+
218223
if matches.is_present("IPV6_FIRST") {
219224
config.ipv6_first = true;
220225
}

crates/shadowsocks-service/src/config.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,29 +1244,12 @@ impl Config {
12441244

12451245
// DNS
12461246
{
1247-
nconfig.dns = match config.dns {
1248-
Some(SSDnsConfig::Simple(ds)) => match &ds[..] {
1249-
#[cfg(feature = "trust-dns")]
1250-
"google" => DnsConfig::TrustDns(ResolverConfig::google()),
1251-
1252-
#[cfg(feature = "trust-dns")]
1253-
"cloudflare" => DnsConfig::TrustDns(ResolverConfig::cloudflare()),
1254-
#[cfg(all(feature = "trust-dns", feature = "dns-over-tls"))]
1255-
"cloudflare_tls" => DnsConfig::TrustDns(ResolverConfig::cloudflare_tls()),
1256-
#[cfg(all(feature = "trust-dns", feature = "dns-over-https"))]
1257-
"cloudflare_https" => DnsConfig::TrustDns(ResolverConfig::cloudflare_https()),
1258-
1259-
#[cfg(feature = "trust-dns")]
1260-
"quad9" => DnsConfig::TrustDns(ResolverConfig::quad9()),
1261-
#[cfg(all(feature = "trust-dns", feature = "dns-over-tls"))]
1262-
"quad9_tls" => DnsConfig::TrustDns(ResolverConfig::quad9_tls()),
1263-
1264-
nameservers => Config::parse_dns_nameservers(nameservers)?,
1265-
},
1247+
match config.dns {
1248+
Some(SSDnsConfig::Simple(ds)) => nconfig.set_dns_formatted(&ds)?,
12661249
#[cfg(feature = "trust-dns")]
1267-
Some(SSDnsConfig::TrustDns(c)) => DnsConfig::TrustDns(c),
1268-
None => DnsConfig::System,
1269-
};
1250+
Some(SSDnsConfig::TrustDns(c)) => nconfig.dns = DnsConfig::TrustDns(c),
1251+
None => nconfig.dns = DnsConfig::System,
1252+
}
12701253
}
12711254

12721255
// TCP nodelay
@@ -1291,6 +1274,33 @@ impl Config {
12911274
Ok(nconfig)
12921275
}
12931276

1277+
/// Set DNS configuration in string format
1278+
///
1279+
/// 1. `[(unix|tcp|udp)://]host[:port][,host[:port]]...`
1280+
/// 2. Pre-defined. Like `google`, `cloudflare`
1281+
pub fn set_dns_formatted(&mut self, dns: &str) -> Result<(), Error> {
1282+
self.dns = match dns {
1283+
#[cfg(feature = "trust-dns")]
1284+
"google" => DnsConfig::TrustDns(ResolverConfig::google()),
1285+
1286+
#[cfg(feature = "trust-dns")]
1287+
"cloudflare" => DnsConfig::TrustDns(ResolverConfig::cloudflare()),
1288+
#[cfg(all(feature = "trust-dns", feature = "dns-over-tls"))]
1289+
"cloudflare_tls" => DnsConfig::TrustDns(ResolverConfig::cloudflare_tls()),
1290+
#[cfg(all(feature = "trust-dns", feature = "dns-over-https"))]
1291+
"cloudflare_https" => DnsConfig::TrustDns(ResolverConfig::cloudflare_https()),
1292+
1293+
#[cfg(feature = "trust-dns")]
1294+
"quad9" => DnsConfig::TrustDns(ResolverConfig::quad9()),
1295+
#[cfg(all(feature = "trust-dns", feature = "dns-over-tls"))]
1296+
"quad9_tls" => DnsConfig::TrustDns(ResolverConfig::quad9_tls()),
1297+
1298+
nameservers => Config::parse_dns_nameservers(nameservers)?,
1299+
};
1300+
1301+
Ok(())
1302+
}
1303+
12941304
#[cfg(any(feature = "trust-dns", feature = "local-dns"))]
12951305
fn parse_dns_nameservers(nameservers: &str) -> Result<DnsConfig, Error> {
12961306
#[cfg(all(unix, feature = "local-dns"))]
@@ -1351,7 +1361,7 @@ impl Config {
13511361
} else {
13521362
let e = Error::new(
13531363
ErrorKind::Invalid,
1354-
"invalid `dns` value, can only be host[:port][,host[:port]]...",
1364+
"invalid `dns` value, can only be [(tcp|udp)://]host[:port][,host[:port]]..., or unix:///path/to/dns, or predefined keys like \"google\", \"cloudflare\"",
13551365
None,
13561366
);
13571367
return Err(e);

0 commit comments

Comments
 (0)