Skip to content

Commit 8accff2

Browse files
authored
Merge pull request #14 from Dj-Codeman/port-option-support
Adding support for changing port
2 parents 8e908f6 + 9c8b2e4 commit 8accff2

12 files changed

Lines changed: 101 additions & 73 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exclude = [
1111
]
1212
homepage = "https://dns.lookup.dog/"
1313
license = "MIT"
14-
version = "0.2.1-beta"
14+
version = "0.2.2-beta"
1515

1616

1717
[[bin]]
@@ -60,7 +60,7 @@ log = "0.4"
6060

6161
# Git workflows as well as my sanity will fail without this
6262
[dependencies.openssl-sys]
63-
version = "0.9"
63+
version = "0.9.99"
6464
features = ["vendored"]
6565

6666
# windows default nameserver determination

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ It has colourful output, understands normal command-line argument syntax, suppor
2727
doge example.net MX ...looking up MX records instead
2828
doge example.net MX @1.1.1.1 ...using a specific nameserver instead
2929
doge example.net MX @1.1.1.1 -T ...using TCP rather than UDP
30+
doge exapple.net MX @1.1.1.1 -p 53 ...using a nonstandart port
3031
doge -q example.net -t MX -n 1.1.1.1 -T As above, but using explicit arguments
3132

3233
---
@@ -39,6 +40,7 @@ It has colourful output, understands normal command-line argument syntax, suppor
3940
-q, --query=HOST Host name or domain name to query
4041
-t, --type=TYPE Type of the DNS record being queried (A, MX, NS...)
4142
-n, --nameserver=ADDR Address of the nameserver to send packets to
43+
-p, --port=PORT Port options for sending queries on nonstandart ports
4244
--class=CLASS Network class of the DNS record being queried (IN, CH, HS)
4345

4446
### Sending options

dns-transport/src/auto.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ use super::{Transport, Error, UdpTransport, TcpTransport};
1111
/// This is the default behaviour for many DNS clients.
1212
pub struct AutoTransport {
1313
addr: String,
14+
custom_port: u16
1415
}
1516

1617
impl AutoTransport {
1718

1819
/// Creates a new automatic transport that connects to the given host.
19-
pub fn new(addr: String) -> Self {
20-
Self { addr }
20+
pub fn new(addr: String, port: Option<u16>) -> Self {
21+
let custom_port: u16 = match port {
22+
Some(port) => port,
23+
None => 53,
24+
};
25+
Self { addr, custom_port }
2126
}
2227
}
2328

2429

2530
impl Transport for AutoTransport {
2631
fn send(&self, request: &Request) -> Result<Response, Error> {
27-
let udp_transport = UdpTransport::new(self.addr.clone());
32+
let udp_transport = UdpTransport::new(self.addr.clone(), Some(self.custom_port.clone()));
2833
let udp_response = udp_transport.send(&request)?;
2934

3035
if ! udp_response.flags.truncated {
@@ -33,7 +38,7 @@ impl Transport for AutoTransport {
3338

3439
debug!("Truncated flag set, so switching to TCP");
3540

36-
let tcp_transport = TcpTransport::new(self.addr.clone());
41+
let tcp_transport = TcpTransport::new(self.addr.clone(), Some(self.custom_port.clone()));
3742
let tcp_response = tcp_transport.send(&request)?;
3843
Ok(tcp_response)
3944
}

dns-transport/src/https.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ use super::tls_stream;
1414
/// encrypted with TLS, using TCP.
1515
pub struct HttpsTransport {
1616
url: String,
17+
custom_port: u16
1718
}
1819

1920
impl HttpsTransport {
2021

2122
/// Creates a new HTTPS transport that connects to the given URL.
22-
pub fn new(url: String) -> Self {
23-
Self { url }
23+
pub fn new(url: String, port: Option<u16>) -> Self {
24+
let custom_port: u16 = match port {
25+
Some(port) => port,
26+
None => 443,
27+
};
28+
Self { url, custom_port }
2429
}
2530
}
2631

@@ -42,7 +47,7 @@ impl Transport for HttpsTransport {
4247
let (domain, path) = self.split_domain().expect("Invalid HTTPS nameserver");
4348

4449
info!("Opening TLS socket to {:?}", domain);
45-
let mut stream = Self::stream(&domain, 443)?;
50+
let mut stream = Self::stream(&domain, *&self.custom_port)?;
4651

4752
debug!("Connected");
4853

@@ -123,5 +128,5 @@ impl HttpsTransport {
123128
}
124129

125130
/// The User-Agent header sent with HTTPS requests.
126-
static USER_AGENT: &str = concat!("dog/", env!("CARGO_PKG_VERSION"));
131+
static USER_AGENT: &str = concat!("doge/", env!("CARGO_PKG_VERSION"));
127132

dns-transport/src/tcp.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ use super::{Transport, Error};
1818
/// TCP, Implementation Requirements (March 2016)
1919
pub struct TcpTransport {
2020
addr: String,
21+
custom_port: u16
2122
}
2223

2324
impl TcpTransport {
2425

2526
/// Creates a new TCP transport that connects to the given host.
26-
pub fn new(addr: String) -> Self {
27-
Self { addr }
27+
pub fn new(addr: String, port: Option<u16>) -> Self {
28+
let custom_port: u16 = match port {
29+
Some(port) => port,
30+
None => 53,
31+
};
32+
Self { addr, custom_port }
2833
}
2934
}
3035

@@ -33,12 +38,8 @@ impl Transport for TcpTransport {
3338
fn send(&self, request: &Request) -> Result<Response, Error> {
3439
info!("Opening TCP stream");
3540
let mut stream =
36-
if self.addr.contains(':') {
37-
TcpStream::connect(&*self.addr)?
38-
}
39-
else {
40-
TcpStream::connect((&*self.addr, 53))?
41-
};
41+
TcpStream::connect((&*self.addr, self.custom_port))?;
42+
4243
debug!("Opened");
4344

4445
// The message is prepended with the length when sent over TCP,

dns-transport/src/tls.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ use super::tls_stream::TlsStream;
1414
/// encrypted TLS connection.
1515
pub struct TlsTransport {
1616
addr: String,
17+
custom_port: u16
1718
}
1819

1920
impl TlsTransport {
2021

2122
/// Creates a new TLS transport that connects to the given host.
22-
pub fn new(addr: String) -> Self {
23-
Self { addr }
23+
pub fn new(addr: String, port: Option<u16>) -> Self {
24+
let custom_port: u16 = match port {
25+
Some(p) => p,
26+
None => 853,
27+
};
28+
Self { addr, custom_port }
2429
}
2530
}
2631

@@ -30,22 +35,14 @@ impl Transport for TlsTransport {
3035

3136
#[cfg(feature = "with_tls")]
3237
fn send(&self, request: &Request) -> Result<Response, Error> {
38+
use native_tls::TlsStream;
39+
3340
info!("Opening TLS socket");
3441

3542
let domain = self.sni_domain();
3643
info!("Connecting using domain {:?}", domain);
37-
let mut stream =
38-
if self.addr.contains(':') {
39-
let mut parts = self.addr.split(":");
40-
let domain = parts.nth(0).unwrap();
41-
let port = parts.last().unwrap().parse::<u16>().expect("Invalid port number");
42-
43-
Self::stream(domain, port)?
44-
}
45-
else {
46-
Self::stream(&*self.addr, 853)?
47-
};
48-
44+
// comminicate that the port must EXPLICATLY BE SEPERATE
45+
let mut stream: TlsStream<TcpStream> = Self::stream(&self.addr, *&self.custom_port)?;
4946

5047
debug!("Connected");
5148

dns-transport/src/udp.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ use super::{Transport, Error};
1414
/// Implementation and Specification (November 1987)
1515
pub struct UdpTransport {
1616
addr: String,
17+
custom_port: u16
1718
}
1819

1920
impl UdpTransport {
2021

2122
/// Creates a new UDP transport that connects to the given host.
22-
pub fn new(addr: String) -> Self {
23-
Self { addr }
23+
pub fn new(addr: String, port: Option<u16>) -> Self {
24+
let custom_port: u16 = match port {
25+
Some(p) => p,
26+
None => 53,
27+
};
28+
// info!("Running on nonstandart port");
29+
Self { addr, custom_port }
2430
}
2531
}
2632

@@ -30,13 +36,8 @@ impl Transport for UdpTransport {
3036
info!("Opening UDP socket");
3137
// TODO: This will need to be changed for IPv6 support.
3238
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
39+
socket.connect( (&*self.addr, self.custom_port))?;
3340

34-
if self.addr.contains(':') {
35-
socket.connect(&*self.addr)?;
36-
}
37-
else {
38-
socket.connect((&*self.addr, 53))?;
39-
}
4041
debug!("Opened");
4142

4243
let bytes_to_send = request.to_bytes().expect("failed to serialise request");

src/connect.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ pub enum TransportType {
1212
/// UDP is used by default. If the request packet would be too large, send
1313
/// a TCP packet instead; if a UDP _response_ packet is truncated, try
1414
/// again with TCP.
15-
Automatic,
15+
/// Takes an 'Option<u16>' for diffrent ports None uses the protocol default port
16+
Automatic(Option<u16>),
1617

1718
/// Send packets over UDP only.
1819
/// If the request packet is too large or the response packet is
1920
/// truncated, fail with an error.
20-
UDP,
21+
/// Takes an 'Option<u16>' for diffrent ports None uses the protocol default port
22+
UDP(Option<u16>),
2123

2224
/// Send packets over TCP only.
23-
TCP,
25+
/// Takes an 'Option<u16>' for diffrent ports None uses the protocol default port
26+
TCP(Option<u16>),
2427

2528
/// Send encrypted DNS-over-TLS packets.
26-
TLS,
29+
/// Takes an 'Option<u16>' for diffrent ports None uses the protocol default port
30+
TLS(Option<u16>),
2731

2832
/// Send encrypted DNS-over-HTTPS packets.
29-
HTTPS,
33+
/// Takes an 'Option<u16>' for diffrent ports None uses the protocol default port
34+
HTTPS(Option<u16>),
3035
}
3136

3237
impl TransportType {
@@ -36,11 +41,11 @@ impl TransportType {
3641
/// stringified address for the others.
3742
pub fn make_transport(self, param: String) -> Box<dyn Transport> {
3843
match self {
39-
Self::Automatic => Box::new(AutoTransport::new(param)),
40-
Self::UDP => Box::new(UdpTransport::new(param)),
41-
Self::TCP => Box::new(TcpTransport::new(param)),
42-
Self::TLS => Box::new(TlsTransport::new(param)),
43-
Self::HTTPS => Box::new(HttpsTransport::new(param)),
44+
Self::Automatic(p) => Box::new(AutoTransport::new(param, p)),
45+
Self::UDP(p) => Box::new(UdpTransport::new(param, p)),
46+
Self::TCP(p) => Box::new(TcpTransport::new(param, p)),
47+
Self::TLS(p) => Box::new(TlsTransport::new(param, p)),
48+
Self::HTTPS(p) => Box::new(HttpsTransport::new(param, p)),
4449
}
4550
}
4651
}

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ fn disabled_feature_check(options: &Options) {
182182
use crate::connect::TransportType;
183183

184184
#[cfg(all(not(feature = "with_tls"), not(feature = "with_rustls_tls")))]
185-
if options.requests.inputs.transport_types.contains(&TransportType::TLS) {
186-
eprintln!("dog: Cannot use '--tls': This version of dog has been compiled without TLS support");
185+
if options.requests.inputs.transport_types.contains(&TransportType::TLS(None)) {
186+
eprintln!("doge: Cannot use '--tls': This version of dog has been compiled without TLS support");
187187
exit(exits::OPTIONS_ERROR);
188188
}
189189

190190
#[cfg(all(not(feature = "with_https"), not(feature = "with_rustls_https")))]
191-
if options.requests.inputs.transport_types.contains(&TransportType::HTTPS) {
192-
eprintln!("dog: Cannot use '--https': This version of dog has been compiled without HTTPS support");
191+
if options.requests.inputs.transport_types.contains(&TransportType::HTTPS(None)) {
192+
eprintln!("doge: Cannot use '--https': This version of dog has been compiled without HTTPS support");
193193
exit(exits::OPTIONS_ERROR);
194194
}
195195
}

0 commit comments

Comments
 (0)