Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json output #32

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
110 changes: 86 additions & 24 deletions Cargo.lock

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

26 changes: 18 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ categories = ["command-line-utilities"]
readme = "README.md"
keywords = ["DNS", "domain"]
license = "BSD-3-Clause"
exclude = [ ".github", ".gitignore" ]
exclude = [".github", ".gitignore"]

[dependencies]
bytes = "1"
clap = { version = "4", features = ["derive", "unstable-doc"] }
chrono = { version = "0.4.38", features = [ "alloc", "clock" ] }
domain = { version = "0.10", features = ["resolv", "unstable-client-transport"]}
bytes = "1"
chrono = { version = "0.4.38", features = ["alloc", "clock", "serde"] }
clap = { version = "4", features = ["derive", "unstable-doc"] }
domain = { version = "0.10", git = "https://github.com/NLnetLabs/domain.git", features = [
"resolv",
"unstable-client-transport",
"serde",
] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = { version = "1.0.135", features = ["preserve_order"] }
tempfile = "3.1.0"
tokio = { version = "1.33", features = ["rt-multi-thread"] }
tokio-rustls = { version = "0.26.0", default-features = false, features = [ "ring", "logging", "tls12" ] }
tokio = { version = "1.33", features = ["rt-multi-thread"] }
tokio-rustls = { version = "0.26.1", default-features = false, features = [
"ring",
"logging",
"tls12",
] }
webpki-roots = "0.26.3"

[package.metadata.deb]
Expand All @@ -30,5 +40,5 @@ aspects of the DNS."""
[package.metadata.generate-rpm]
license = "BSD"
assets = [
{ source = "target/release/dnsi", dest = "/usr/bin/dnsi", mode = "755" },
{ source = "target/release/dnsi", dest = "/usr/bin/dnsi", mode = "755" },
]
40 changes: 30 additions & 10 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ use domain::base::message_builder::MessageBuilder;
use domain::base::name::ToName;
use domain::base::question::Question;
use domain::net::client::protocol::UdpConnect;
use domain::net::client::request::{RequestMessage, SendRequest};
use domain::net::client::request::{
RequestMessage, RequestMessageMulti, SendRequest,
};
use domain::net::client::{dgram, stream};
use domain::resolv::stub::conf;
use serde::{Serialize, Serializer};
use std::fmt;
use std::net::SocketAddr;
use std::sync::Arc;
Expand Down Expand Up @@ -62,7 +65,7 @@ impl Client {
let mut res = res.question();
res.push(question.into()).unwrap();

self.request(RequestMessage::new(res)).await
self.request(RequestMessage::new(res)?).await
}

pub async fn request(
Expand Down Expand Up @@ -132,9 +135,11 @@ impl Client {
) -> Result<Answer, Error> {
let mut stats = Stats::new(server.addr, Protocol::Tcp);
let socket = TcpStream::connect(server.addr).await?;
let (conn, tran) = stream::Connection::with_config(
socket,
Self::stream_config(server),
let (conn, tran) = stream::Connection::<
_,
RequestMessageMulti<Vec<u8>>,
>::with_config(
socket, Self::stream_config(server)
);
tokio::spawn(tran.run());
let message = conn.send_request(request).get_response().await?;
Expand Down Expand Up @@ -170,9 +175,11 @@ impl Client {
})?;
let tls_socket =
tls_connector.connect(server_name, tcp_socket).await?;
let (conn, tran) = stream::Connection::with_config(
tls_socket,
Self::stream_config(server),
let (conn, tran) = stream::Connection::<
_,
RequestMessageMulti<Vec<u8>>,
>::with_config(
tls_socket, Self::stream_config(server)
);
tokio::spawn(tran.run());
let message = conn.send_request(request).get_response().await?;
Expand Down Expand Up @@ -256,14 +263,26 @@ impl AsRef<Message<Bytes>> for Answer {

//------------ Stats ---------------------------------------------------------

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Serialize)]
pub struct Stats {
pub start: DateTime<Local>,
#[serde(serialize_with = "serialize_time_delta")]
pub duration: TimeDelta,
pub server_addr: SocketAddr,
pub server_proto: Protocol,
}

fn serialize_time_delta<S>(
t: &TimeDelta,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let msecs = t.num_milliseconds();
serializer.serialize_i64(msecs)
}

impl Stats {
fn new(server_addr: SocketAddr, server_proto: Protocol) -> Self {
Stats {
Expand All @@ -281,7 +300,8 @@ impl Stats {

//------------ Protocol ------------------------------------------------------

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Protocol {
Udp,
Tcp,
Expand Down
Loading
Loading