Skip to content

Commit

Permalink
feat: implement client-side command
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffcaii committed Dec 6, 2024
1 parent e52e85c commit abbd3a3
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 86 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0-alpha.5"
edition = "2021"
license = "MIT"
readme = "README.md"
authors = ["Jeffsky Tsai <[email protected]>"]
repository = "https://github.com/jjeffcaii/zerodns"
homepage = "https://github.com/jjeffcaii/zerodns"
description = "A DNS server in Rust, which is inspired from chinadns/dnsmasq."
Expand Down Expand Up @@ -33,9 +34,9 @@ bytes = "1.5.0"
tokio = { version = "1.36.0", features = ["full"] }
tokio-util = { version = "0.7.10", features = ["full"] }
anyhow = "1.0.75"
thiserror = "1.0.64"
thiserror = "2.0.4"
cfg-if = "1.0.0"
clap = { version = "4.4.4", features = ["derive"] }
clap = { version = "4.5.22", features = ["derive", "cargo"] }
once_cell = "1.18.0"
futures = "0.3.28"
parking_lot = "0.12.1"
Expand All @@ -57,14 +58,16 @@ strum_macros = "0.26.1"
deadpool = "0.10.0"
socket2 = "0.5.5"
mlua = { version = "0.9.9", features = ["luajit", "vendored", "serialize", "async", "macros", "send", "parking_lot"] }
garde = { version = "0.20.0", features = ["serde", "derive", "pattern", "regex"] }
garde = { version = "0.20.0", features = ["serde", "derive", "regex"] }
rustls = "0.23.16"
webpki-roots = "0.26.6"
tokio-rustls = "0.26.0"
httparse = "1.9.5"
http = "1.1.0"
urlencoding = "2.1.3"
#urlencoding = "2.1.3"
string_cache = "0.8"
rand = "0.8.5"
resolv-conf = "0.7.0"

[dev-dependencies]
hex = "0.4.3"
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ a DNS server in Rust, which is inspired from chinadns/dnsmasq.

## Quick Start

### Client-Side

ZeroDNS provides similar functionality to dig, but supports more DNS protocols. Here are some examples:

```shell
$ # Simple resolve, will read dns server from /etc/resolv.conf
$ zerodns resolve www.youtube.com
$ # Use short output, similar with 'dig +short ...'
$ zerodns resolve --short www.youtube.com
$ # Resolve over google TCP
$ zerodns resolve -s tcp://8.8.8.8 www.youtube.com
$ # Resolve over google DoT
$ zerodns resolve -s dot://dns.google www.youtube.com
$ # Resolve over cloudflare DoH
$ zerodns resolve -s doh://1.1.1.1 www.youtube.com
$ # Resolve MX records
$ zerodns resolve -t mx gmail.com
```

### Server-Side

> Notice: ensure you have [just](https://github.com/casey/just) installed on your machine!
run an example:
Expand All @@ -29,7 +50,7 @@ $ just r
$ dig @127.0.0.1 -p5454 www.youtube.com
```

## Configuration
#### Configuration

Here's an example configuration file:

Expand Down Expand Up @@ -118,3 +139,7 @@ filters = ["chinadns"]
##### RULES END #####

```

### Client API

// TODO
5 changes: 5 additions & 0 deletions justfile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/usr/bin/env just --justfile

alias r := run
alias i := install
alias l := lint

release:
@cargo build --release

install:
@cargo install --path .

lint:
@cargo clippy

Expand Down
8 changes: 4 additions & 4 deletions src/client/doh.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Client;
use crate::misc::http::{SimpleHttp1Codec, CRLF};
use crate::protocol::Message;
use crate::protocol::{Message, DEFAULT_HTTP_PORT, DEFAULT_TLS_PORT};
use futures::StreamExt;
use once_cell::sync::Lazy;
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -94,7 +94,7 @@ impl DoHClient {
pub const DEFAULT_PATH: &'static str = "/dns-query";

pub fn builder<'a>(addr: SocketAddr) -> DoHClientBuilder<'a> {
let https = addr.port() == 443;
let https = addr.port() == DEFAULT_TLS_PORT;
DoHClientBuilder {
https,
addr,
Expand Down Expand Up @@ -186,12 +186,12 @@ impl DoHClient {
impl Display for DoHClient {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.https {
if self.addr.port() == 443 {
if self.addr.port() == DEFAULT_TLS_PORT {
write!(f, "doh+https://{}", self.addr.ip())?;
} else {
write!(f, "doh+https://{}", self.addr)?;
}
} else if self.addr.port() == 80 {
} else if self.addr.port() == DEFAULT_HTTP_PORT {
write!(f, "doh+http://{}", self.addr.ip())?;
} else {
write!(f, "doh+http://{}", self.addr)?;
Expand Down
4 changes: 1 addition & 3 deletions src/client/dot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Client;
use crate::misc::tls;
use crate::protocol::{Codec, Message};
use crate::protocol::{Codec, Message, DEFAULT_DOT_PORT};
use crate::Result;

use futures::{SinkExt, StreamExt};
Expand All @@ -13,8 +13,6 @@ use tokio::net::TcpStream;
use tokio_rustls::client::TlsStream;
use tokio_util::codec::{FramedRead, FramedWrite};

const DEFAULT_DOT_PORT: u16 = 853;

static GOOGLE: Lazy<DoTClient> = Lazy::new(|| {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)), DEFAULT_DOT_PORT);
DoTClient::builder(addr)
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Rule {
pub filters: Vec<String>,
}

pub fn read_from_toml(pt: PathBuf) -> anyhow::Result<Config> {
pub fn read_from_toml(pt: &PathBuf) -> anyhow::Result<Config> {
let b = std::fs::read(pt)?;
let s = String::from_utf8(b)?;
let c: Config = toml::from_str(&s)?;
Expand All @@ -46,7 +46,7 @@ mod tests {
#[test]
fn test_read_from_toml() {
let pt = PathBuf::from("config.toml");
let c = read_from_toml(pt);
let c = read_from_toml(&pt);

assert!(c.is_ok_and(|c| {
!c.server.listen.is_empty() && !c.rules.is_empty() && !c.filters.is_empty()
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ pub(crate) use error::Error;
pub type Result<T> = anyhow::Result<T>;

pub use builtin::{setup, setup_logger};

pub use misc::resolvconf::read as read_resolvconf;

pub const DEFAULT_RESOLV_CONF_PATH: &str = "/etc/resolv.conf";
pub const DEFAULT_UDP_PORT: u16 = 53;
Loading

0 comments on commit abbd3a3

Please sign in to comment.