Skip to content

Commit 12d05f2

Browse files
committed
integrate dbus
1 parent f45f504 commit 12d05f2

File tree

24 files changed

+1147
-372
lines changed

24 files changed

+1147
-372
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
33
"libnss",
4-
"doh"
5-
]
4+
"doh",
5+
"doh-daemon",
6+
"doh-common"]

doh-common/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "doh-common"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
name = "doh_common"
9+
10+
[dependencies]
11+
libnss = { path = "../libnss" }
12+
log = "0.4.17"
13+
serde_json = "1.0"
14+
sqlite = "0.37.0"
15+
zbus = "5.9.0"
16+
17+
zvariant = "5.6.0"
18+
serde = { version = "1", features = ["derive"] }
19+
reqwest = { version = "0.12.23", features = ["gzip","json", "brotli"] }
20+
url = "2.5.4"

doh-common/src/error.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use std::error::Error as Err;
2+
use std::fmt::{Display, Formatter};
3+
use std::time::SystemTimeError;
4+
5+
use log::error;
6+
use url::ParseError;
7+
8+
use libnss::host::Host;
9+
use libnss::interop::Response;
10+
11+
#[derive(Debug)]
12+
pub enum Error {
13+
DNSErrorReply,
14+
EmptyDNSReply,
15+
UpstreamError,
16+
DatabaseError,
17+
}
18+
19+
impl Display for Error {
20+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21+
match self {
22+
Error::UpstreamError => write!(f, "UpstreamError"),
23+
Error::DNSErrorReply => write!(f, "DNSErrorReply"),
24+
Error::EmptyDNSReply => write!(f, "EmptyDNSReply"),
25+
Error::DatabaseError => write!(f, "DatabaseError")
26+
}
27+
}
28+
}
29+
30+
impl Err for Error {}
31+
32+
impl From<std::io::Error> for Error {
33+
fn from(error: std::io::Error) -> Self {
34+
error!("reading response body error: {:?}", error);
35+
Error::UpstreamError
36+
}
37+
}
38+
39+
impl From<serde_json::Error> for Error {
40+
fn from(error: serde_json::Error) -> Self {
41+
error!("error deserialization JSON: {} {:?}", error, error);
42+
Error::UpstreamError
43+
}
44+
}
45+
46+
impl From<sqlite::Error> for Error {
47+
fn from(err: sqlite::Error) -> Self {
48+
error!("database error: {}", err);
49+
Error::DatabaseError
50+
}
51+
}
52+
53+
impl From<SystemTimeError> for Error {
54+
fn from(error: SystemTimeError) -> Self {
55+
error!("error getting system time: {}", error);
56+
Error::DatabaseError
57+
}
58+
}
59+
60+
impl Into<Response<Host>> for Error {
61+
fn into(self) -> Response<Host> {
62+
match self {
63+
Error::EmptyDNSReply => Response::NotFound,
64+
Error::UpstreamError => Response::TryAgain,
65+
_ => Response::Unavail
66+
}
67+
}
68+
}
69+
70+
impl From<crate::error::Error> for zbus::fdo::Error {
71+
fn from(value: Error) -> Self {
72+
zbus::fdo::Error::Failed(value.to_string())
73+
}
74+
}
75+
76+
77+
impl From<url::ParseError> for Error {
78+
fn from(error: ParseError) -> Self {
79+
error!("error parsing url: {} {:?}", error, error);
80+
Error::UpstreamError
81+
}
82+
}
83+
84+
impl From<reqwest::Error> for Error {
85+
fn from(error: reqwest::Error) -> Self {
86+
error!("error while making upstream request: {} {:?}", error, error);
87+
Error::UpstreamError
88+
}
89+
}

doh-common/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pub mod error;
2+
pub mod loggger;
3+
4+
use serde::{Serialize};
5+
use zvariant::Type;
6+
7+
8+
#[derive(Serialize, Type)]
9+
pub struct AuditDnsQueryPage {
10+
current_page: u64,
11+
results: Vec<AuditDnsQuery>,
12+
}
13+
14+
impl AuditDnsQueryPage {
15+
pub fn new(current_page: u64, results: Vec<AuditDnsQuery>) -> Self {
16+
Self { current_page, results }
17+
}
18+
}
19+
20+
#[derive(Serialize, Type)]
21+
pub struct AuditDnsQuery {
22+
process_name: String,
23+
host: String,
24+
create: u64,
25+
}
26+
27+
impl AuditDnsQuery {
28+
pub fn new(process_name: String, host: String, create: u64) -> Self {
29+
Self { process_name, host, create }
30+
}
31+
}

doh-common/src/loggger.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use log::info;
2+
3+
// use syslog::{Facility, Formatter3164};
4+
5+
pub fn log<M: AsRef<str>>(message: M) {
6+
// Log to syslog of it is in debug mode
7+
// if let Ok(_) = std::env::var(format!("{}_DEBUG", LIB_NAME.to_uppercase())) {
8+
info!("{}", message.as_ref())
9+
}

doh-daemon/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "doh-daemon"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
log = "0.4.17"
10+
env_logger = "0.11.8"
11+
zbus = { version = "5.9.0", features = ["tokio"] }
12+
zvariant = "5.6.0"
13+
zvariant_derive = "5.6.0"
14+
tokio = { version = "1", features = ["full"] }
15+
serde_json = "1.0"
16+
serde = { version = "1", features = ["derive"] }
17+
libnss = { path = "../libnss" }
18+
doh-common = {path = "../doh-common"}
19+
punycode = "0.4.1"
20+
sqlite = "0.37.0"
21+
reqwest = { version = "0.12.23", features = ["gzip","json", "brotli"] }
22+
configparser = "3.1.0"

doh-daemon/config.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[sqlite]
2+
connection=test.db
3+
4+
[resolver]
5+
provider=google
6+
ttl=500

doh-daemon/src/client/mod.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use std::net::{IpAddr, SocketAddr};
2+
use log::{debug, trace};
3+
use reqwest::Url;
4+
use serde::de::DeserializeOwned;
5+
6+
7+
8+
pub async fn request<'de, B>(
9+
server_address: IpAddr,
10+
port: u16,
11+
url: &str,
12+
headers: &Vec<(&str, &str)>,
13+
query_params: &Vec<(&str, &str)>) -> Result<B, doh_common::error::Error>
14+
where B: DeserializeOwned {
15+
16+
let address = SocketAddr::new(server_address, port);
17+
18+
let url = Url::parse(url)?;
19+
20+
let client = reqwest::ClientBuilder::new()
21+
.resolve(url.domain().unwrap(), address)
22+
.user_agent("???")
23+
.brotli(true)
24+
.gzip(true)
25+
.build()?;
26+
27+
let mut request_builder = client.get(url);
28+
29+
for (key, value) in headers {
30+
request_builder = request_builder.header(key.to_string(), value.to_string());
31+
}
32+
33+
request_builder = request_builder.query(query_params);
34+
35+
let request = request_builder.build()?;
36+
37+
let response = client.execute(request).await?;
38+
39+
let status = response.status();
40+
41+
if status.is_success() {
42+
43+
//debug!("Response body: {}", body.clone());
44+
45+
let body = response.bytes().await?;
46+
47+
debug!("response body: {}", String::from_utf8_lossy(&body.to_vec()));
48+
49+
let obj : B = serde_json::from_slice(&body)?;
50+
51+
Ok(obj)
52+
} else {
53+
54+
let body = response.bytes().await?;
55+
56+
debug!("response status {:?}: {:?}", status, String::from_utf8_lossy(&body.to_vec()));
57+
58+
Err(doh_common::error::Error::UpstreamError)
59+
60+
}
61+
}
62+
63+
64+
// pub fn request<'de, B>(server_address: IpAddr,
65+
// port: u16,
66+
// url: &str,
67+
// headers: &Vec<(&str, &str)>,
68+
// query_params: &Vec<(&str, &str)>) -> Result<B, doh_common::error::Error>
69+
// where B: DeserializeOwned {
70+
//
71+
// let address = SocketAddr::new(server_address, port);
72+
//
73+
// let agent = ureq::AgentBuilder::new()
74+
// .user_agent("??????????????????????????")
75+
// .resolver(move |addr: &str| match addr {
76+
// _ => Ok(vec![address])
77+
// })
78+
// .build();
79+
//
80+
// let mut request = agent.get(&url);
81+
//
82+
// for (key, value) in headers {
83+
// request = request.set(key, value);
84+
// }
85+
//
86+
// for (key, value) in query_params {
87+
// request = request.query(key, value)
88+
// }
89+
//
90+
// let response = request.call()?;
91+
// let status = response.status();
92+
//
93+
// if status > 199 && status < 300 {
94+
//
95+
// let body = response.into_string()?;
96+
//
97+
// debug!("Response body: {}", body.clone());
98+
//
99+
// let obj: B = serde_json::from_str::<B>(&body)
100+
// .inspect_err(|er| {
101+
//
102+
// error!("Error deserializing: {}", er);
103+
//
104+
// })?;
105+
//
106+
// Ok(obj)
107+
// } else {
108+
//
109+
// let body = response.into_string()?;
110+
//
111+
// debug!("Response status {}: {}", status, body.clone());
112+
//
113+
// Err(doh_common::error::Error::UpstreamError)
114+
// }
115+
// }

0 commit comments

Comments
 (0)