-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
71 lines (52 loc) · 1.7 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#[macro_use]
extern crate lazy_static;
extern crate libc;
#[macro_use]
extern crate libnss;
use std::net::IpAddr;
use libnss::host::{AddressFamily, Host, HostHooks};
use libnss::interop::Response;
use crate::loggger::log;
use crate::provider::Resolver;
mod provider;
mod error;
mod client;
mod loggger;
struct DoHHost;
libnss_host_hooks!(doh, DoHHost);
const LIB_NAME: &'static str = "nss_doh";
impl HostHooks for DoHHost {
fn get_all_entries() -> Response<Vec<Host>> {
Response::Success(vec![])
}
fn get_host_by_name(name: &str, family: AddressFamily) -> Response<Host> {
let requested_domain = name.trim().to_lowercase();
log(format!("Requesting IP for domain name:{}", requested_domain));
let result = Resolver::resolve(&requested_domain, family.into());
match result {
Ok(host) => Response::Success(host),
Err(err) => err.into()
}
}
fn get_host_by_addr(_addr: IpAddr) -> Response<Host> {
Response::NotFound
}
}
#[cfg(test)]
mod tests {
use libnss::host::{AddressFamily, HostHooks};
use crate::DoHHost;
#[test]
fn it_works() {
let google_domain6 = DoHHost::get_host_by_name("google.com", AddressFamily::IPv6);
match google_domain6 {
libnss::interop::Response::Success(_) => assert_eq!(1, 1, "Resulted in a IP"),
_ => assert_eq!(1, 2, "Not resulted in a IP")
};
let google_domain = DoHHost::get_host_by_name("google.com", AddressFamily::IPv4);
match google_domain {
libnss::interop::Response::Success(_) => assert_eq!(1, 1, "Resulted in a IP"),
_ => assert_eq!(1, 1, "Not resulted in a IP")
};
}
}