Skip to content

Commit e04254e

Browse files
committed
feat: add more feature of lua filter and support opt
1 parent 194eca2 commit e04254e

13 files changed

+939
-150
lines changed

.github/workflows/docker-publish.yml

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@ name: Docker
77

88
on:
99
push:
10-
branches: [ "master" ]
10+
branches:
11+
- master
1112
# Publish semver tags as releases.
12-
tags: [ '*' ]
13+
tags:
14+
- 'v*'
15+
# - 'v[0-9]+.[0-9]+.[0-9]+'
1316
pull_request:
14-
branches: [ "master" ]
17+
branches:
18+
- master
1519

1620
env:
1721
# Use docker.io for Docker Hub if empty
1822
REGISTRY: ghcr.io
1923
# github.repository as <account>/<repo>
2024
IMAGE_NAME: ${{ github.repository }}
2125

22-
2326
jobs:
2427
build:
2528

2629
runs-on: ubuntu-latest
30+
2731
permissions:
2832
contents: read
2933
packages: write
@@ -43,10 +47,14 @@ jobs:
4347
with:
4448
cosign-release: 'v2.4.1'
4549

50+
# Setup QEMU
51+
- name: Setup QEMU
52+
uses: docker/setup-qemu-action@v3
53+
4654
# Set up BuildKit Docker container builder to be able to build
4755
# multi-platform images and export cache
4856
# https://github.com/docker/setup-buildx-action
49-
- name: Set up Docker Buildx
57+
- name: Setup Docker Buildx
5058
uses: docker/[email protected]
5159

5260
# Login against a Docker registry except on PR
@@ -73,6 +81,7 @@ jobs:
7381
id: build-and-push
7482
uses: docker/[email protected]
7583
with:
84+
platforms: linux/amd64,linux/arm64
7685
context: .
7786
push: ${{ github.event_name != 'pull_request' }}
7887
tags: ${{ steps.meta.outputs.tags }}

.github/workflows/lint.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ name: Lint
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches:
6+
- master
67
pull_request:
7-
branches: [ "master" ]
8+
branches:
9+
- master
810

911
env:
1012
CARGO_TERM_COLOR: always

.github/workflows/rust-publish.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
name: Crates Publish
2+
13
on:
24
push:
35
# Pattern matched against refs/tags
46
tags:
5-
- '*' # Push events to every tag not containing /
7+
- 'v*' # Push events to every tag not containing /
68
workflow_dispatch:
79

8-
name: Publish
9-
1010
jobs:
1111
publish:
1212
name: Publish

.github/workflows/rust.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ name: Rust
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches:
6+
- master
67
pull_request:
7-
branches: [ "master" ]
8+
branches:
9+
- master
810

911
env:
1012
CARGO_TERM_COLOR: always

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "zerodns"
3-
version = "0.1.0-alpha.9"
3+
version = "0.1.0-alpha.10"
44
edition = "2021"
55
license = "MIT"
66
readme = "README.md"
@@ -59,7 +59,7 @@ strum_macros = "0.26"
5959
deadpool = "0.12"
6060
socket2 = "0.5"
6161
mlua = { version = "0.10", features = ["luajit", "vendored", "serialize", "async", "macros", "send", "anyhow"] }
62-
garde = { version = "0.21", features = ["serde", "derive", "regex"] }
62+
garde = { version = "0.22", features = ["serde", "derive", "regex"] }
6363
rustls = "0.23"
6464
webpki-roots = "0.26"
6565
tokio-rustls = "0.26"

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# ZeroDNS
44

5-
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/jjeffcaii/zerodns/rust.yml)
5+
t![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/jjeffcaii/zerodns/rust.yml)
66
[![Codecov](https://img.shields.io/codecov/c/github/jjeffcaii/zerodns)](https://app.codecov.io/gh/jjeffcaii/zerodns)
77
[![Crates.io Version](https://img.shields.io/crates/v/zerodns)](https://crates.io/crates/zerodns)
88
[![Crates.io Total Downloads](https://img.shields.io/crates/d/zerodns)](https://crates.io/crates/zerodns)
@@ -90,14 +90,18 @@ props = { trusted = ["tcp://208.67.222.222:443", "tcp://208.67.220.220:443"], mi
9090
kind = "lua"
9191
props.script = """
9292
-- The filter entrance:
93+
94+
local resolver = Resolver('223.5.5.5','223.6.6.6')
95+
96+
9397
function handle(ctx)
9498
-- log something...
9599
for i,v in ipairs(ctx.request:questions()) do
96100
logger:info('---- question#'..i..': '..v.name)
97101
end
98102
99103
-- resolve addr from 223.5.5.5
100-
local resp = resolve(ctx.request,'223.5.5.5')
104+
local resp = resolver:resolve(ctx.request)
101105
102106
-- log something...
103107
for i,v in ipairs(resp:answers()) do

src/bootstrap.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,11 @@ pub async fn run(c: Config, closer: Arc<Notify>) -> anyhow::Result<()> {
2828
rb.build()
2929
};
3030

31-
let cs = match &c.server.cache_size {
32-
None => None,
33-
Some(size) => {
34-
if *size == 0 {
35-
None
36-
} else {
37-
Some(Arc::new(
38-
MemoryLoadingCache::builder().capacity(*size).build(),
39-
))
40-
}
41-
}
31+
let cs = match &c.global.cache_size {
32+
Some(size) if *size > 0 => Some(Arc::new(
33+
MemoryLoadingCache::builder().capacity(*size).build(),
34+
)),
35+
_ => None,
4236
};
4337

4438
let udp_server = {

src/cmds/resolve.rs

+46-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clap::ArgMatches;
44
use std::net::{IpAddr, SocketAddr};
55
use std::time::Duration;
66
use zerodns::client::request;
7-
use zerodns::protocol::{Class, Flags, Kind, Message, DNS};
7+
use zerodns::protocol::{AdditionalRR, Class, Flags, Kind, Message, DNS};
88

99
pub(crate) async fn execute(sm: &ArgMatches) -> Result<()> {
1010
// --timeout 5
@@ -62,7 +62,13 @@ pub(crate) async fn execute(sm: &ArgMatches) -> Result<()> {
6262
timeout = Duration::from_secs(n.parse::<u64>()?);
6363
}
6464

65-
let flags = Flags::builder().request().recursive_query(true).build();
65+
let noedns = sm.get_one::<bool>("noedns").cloned().unwrap_or(false);
66+
67+
let flags = Flags::builder()
68+
.request()
69+
.edns(!noedns)
70+
.recursive_query(true)
71+
.build();
6672
let req = {
6773
let mut bu = Message::builder()
6874
.id({
@@ -76,6 +82,10 @@ pub(crate) async fn execute(sm: &ArgMatches) -> Result<()> {
7682
bu = bu.question(&domain, next, class);
7783
}
7884

85+
if !noedns {
86+
bu = bu.additional_pseudo(4096, 0, 0, 0, None::<&[u8]>);
87+
}
88+
7989
bu.build()?
8090
};
8191

@@ -124,7 +134,22 @@ fn print_resolve_result(
124134

125135
println!();
126136
println!(";; OPT PSEUDOSECTION:");
127-
println!("; EDNS: version: 0, flags:; udp: 512");
137+
for next in res
138+
.additionals()
139+
.filter(|it| matches!(it, AdditionalRR::PseudoRR(_)))
140+
{
141+
if let AdditionalRR::PseudoRR(pseude) = next {
142+
println!(
143+
"; EDNS: version: {}, flags: {:#x}; udp: {}",
144+
pseude.version(),
145+
pseude.extended_rcode(),
146+
pseude.udp_payload_size()
147+
);
148+
// TODO: print pseude data pairs
149+
}
150+
}
151+
152+
println!();
128153
println!(";; QUESTION SECTION:");
129154
for question in req.questions() {
130155
println!(
@@ -149,6 +174,24 @@ fn print_resolve_result(
149174
);
150175
}
151176

177+
println!();
178+
println!(";; ADDITIONAL SECTION:");
179+
for next in res
180+
.additionals()
181+
.filter(|it| matches!(it, AdditionalRR::RR(_)))
182+
{
183+
if let AdditionalRR::RR(rr) = next {
184+
println!(
185+
"{}.\t{}\t{}\t{}\t{}",
186+
rr.name(),
187+
rr.time_to_live(),
188+
rr.class(),
189+
rr.kind(),
190+
rr.rdata()?
191+
);
192+
}
193+
}
194+
152195
println!();
153196
println!(";; Query time: {} msec", cost.num_milliseconds());
154197
println!(";; SERVER: {}", &dns);

src/cmds/run.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clap::ArgMatches;
33
use std::net::{IpAddr, SocketAddr};
44
use std::path::PathBuf;
55
use std::sync::Arc;
6+
use std::time::Duration;
67
use tokio::sync::Notify;
78
use zerodns::client::SystemClient;
89

@@ -73,7 +74,15 @@ pub(crate) async fn execute(sm: &ArgMatches) -> Result<()> {
7374

7475
closer.notify_waiters();
7576

76-
stopped.notified().await;
77+
const SHUTDOWN_TIMEOUT: u64 = 15;
78+
if let Err(_e) =
79+
tokio::time::timeout(Duration::from_secs(SHUTDOWN_TIMEOUT), stopped.notified()).await
80+
{
81+
warn!(
82+
"force stop process because graceful shutdown is timeout after {}s",
83+
SHUTDOWN_TIMEOUT
84+
);
85+
}
7786

7887
Ok(())
7988
}

src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ pub struct GlobalConfig {
2727
pub nameservers: Vec<String>,
2828
pub resolv_file: Option<String>,
2929
pub hosts_file: Option<String>,
30+
pub cache_size: Option<usize>,
3031
}
3132

3233
#[derive(Debug, Clone, Serialize, Deserialize)]
3334
pub struct ServerConfig {
3435
pub listen: String,
35-
pub cache_size: Option<usize>,
3636
}
3737

3838
#[derive(Debug, Clone, Serialize, Deserialize)]

0 commit comments

Comments
 (0)