Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
revert if let merge
  • Loading branch information
Sinderella committed Aug 8, 2022
1 parent 7ab4790 commit 4b38d49
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Tests
run: cargo test --verbose

- name: Build
run: cargo build --verbose --release

Expand Down
131 changes: 130 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "dns-rebinder"
authors = ["sinderella"]
version = "0.2.2"
version = "0.2.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -16,3 +16,7 @@ nonzero_ext = "0.3.0"
rand = "0.8.5"
tokio = { version = "1", features = ["full"] }
trust-dns-proto = "0.21.2"

[dev-dependencies]
assert_cmd = "2.0"
predicates = "2.1"
61 changes: 61 additions & 0 deletions src/ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::net::Ipv4Addr;

#[derive(Clone, Copy)]
pub(crate) struct IP {}

impl IP {
pub(crate) fn encode(primary: &Ipv4Addr, secondary: &Ipv4Addr, domain: &str) -> String {
let encoded_primary = hex::encode(&primary.octets());
let encoded_secondary = hex::encode(&secondary.octets());

format!("{}.{}.{}", encoded_primary, encoded_secondary, domain)
}

pub(crate) fn decode(
encoded_domain: &str,
) -> Result<(Ipv4Addr, Ipv4Addr), Box<dyn std::error::Error>> {
if encoded_domain.matches('.').count() < 2 {
panic!("incorrect format, multi-level subdomain is required");
}
let parts: Vec<&str> = encoded_domain.split('.').collect();

let primary = Ipv4Addr::from(u32::from_str_radix(parts[0], 16).unwrap());
let secondary = Ipv4Addr::from(u32::from_str_radix(parts[1], 16).unwrap());

Ok((primary, secondary))
}
}

#[cfg(test)]
mod tests {
use crate::ip::IP;
use std::net::Ipv4Addr;

#[test]
fn test_ipencoding() -> Result<(), Box<dyn std::error::Error>> {
let primary = Ipv4Addr::new(127, 0, 0, 1);
let secondary = Ipv4Addr::new(192, 168, 1, 1);
let domain = "rebnd.icu";

assert_eq!(
IP::encode(&primary, &secondary, domain),
"7f000001.c0a80101.rebnd.icu"
);

Ok(())
}

#[test]
fn test_ipdecoding() -> Result<(), Box<dyn std::error::Error>> {
let encoded_domain = "7f000001.c0a80101.rebnd.icu";

let primary = Ipv4Addr::new(127, 0, 0, 1);
let secondary = Ipv4Addr::new(192, 168, 1, 1);

let (primary_decoded, secondary_decoded) = IP::decode(encoded_domain).unwrap();
assert_eq!(primary, primary_decoded);
assert_eq!(secondary, secondary_decoded);

Ok(())
}
}
33 changes: 10 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use crate::cli::Cli;
use crate::cli::Commands;
use crate::ip::IP;

use clap::Parser;
use env_logger::Builder;
Expand All @@ -31,6 +32,7 @@ use trust_dns_proto::rr::rdata::SOA;
use trust_dns_proto::rr::{Name, RData, Record, RecordType};

mod cli;
mod ip;

type Error = Box<dyn std::error::Error>;
type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -79,26 +81,14 @@ fn process_query(
return Ok(records);
}

let loopback = u32::from_be_bytes(Ipv4Addr::new(127, 0, 0, 1).octets());

let primary = match u32::from_str_radix(parts[0], 16) {
Ok(decoded) => decoded,
Err(_) => return Ok(records),
};
let secondary = match u32::from_str_radix(parts[1], 16) {
Ok(decoded) => decoded,
Err(_) => return Ok(records),
};
let (primary, secondary) = IP::decode(&qname).unwrap();

info!(
"{:?}[{:?}] - parsed targets: primary: {:#?}, secondary: {:#?}",
addr,
header_id,
Ipv4Addr::from(primary),
Ipv4Addr::from(secondary)
addr, header_id, primary, secondary
);

if primary.eq(&secondary) && primary.ne(&loopback) {
if primary.eq(&secondary) {
warn!(
"{:?}[{:?}] - primary and secondary labels are indentical, possibly an abuse",
addr, header_id
Expand All @@ -113,8 +103,8 @@ fn process_query(
query.name().clone(),
1,
RData::A(match is_primary {
true => Ipv4Addr::from(primary),
false => Ipv4Addr::from(secondary),
true => primary,
false => secondary,
}),
));

Expand Down Expand Up @@ -226,13 +216,10 @@ async fn main() -> Result<()> {
"Domain: {}, Primary: {:?}, Secondary: {:?}",
domain, primary, secondary
);
let encoded_primary = hex::encode(&primary.octets());
let encoded_secondary = hex::encode(&secondary.octets());

println!(
"Encoded: {}.{}.{}",
encoded_primary, encoded_secondary, domain
);
let encoded_domain = ip::IP::encode(primary, secondary, &domain);

println!("Encoded: {}", encoded_domain);
return Ok(());
}

Expand Down
Loading

0 comments on commit 4b38d49

Please sign in to comment.