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
72
73
74
75
76
use byteorder::{LittleEndian, WriteBytesExt};
use crate::{lp_encode, vlp_encode, InformalProperty, WithInformalProperty};
use std::io;
#[derive(Default, Debug)]
pub struct DoHBuilder {
informal_properties: u64,
addrs: Vec<String>,
hashes: Vec<Vec<u8>>,
hostname: String,
path: String,
bootstrap_ips: Vec<String>,
}
impl DoHBuilder {
pub fn new(hostname: String, path: String) -> Self {
DoHBuilder {
informal_properties: 0,
addrs: vec![],
hostname,
path,
hashes: vec![],
bootstrap_ips: vec![],
}
}
pub fn with_address(mut self, addr: String) -> Self {
self.addrs.push(addr);
self
}
pub fn with_hash(mut self, hash: Vec<u8>) -> Self {
self.hashes.push(hash);
self
}
pub fn with_bootstrap_ip(mut self, ip: String) -> Self {
self.bootstrap_ips.push(ip);
self
}
pub fn serialize(self) -> io::Result<String> {
let mut bin = vec![];
bin.push(0x02);
bin.write_u64::<LittleEndian>(self.informal_properties)?;
let addrs_bin: Vec<_> = self
.addrs
.iter()
.map(|addr| addr.as_bytes().to_vec())
.collect();
vlp_encode(&mut bin, &addrs_bin)?;
vlp_encode(&mut bin, &self.hashes)?;
lp_encode(&mut bin, &self.hostname.as_bytes())?;
lp_encode(&mut bin, &self.path.as_bytes())?;
if !self.bootstrap_ips.is_empty() {
let bootstrap_ips_bin: Vec<_> = self
.bootstrap_ips
.iter()
.map(|ip| ip.as_bytes().to_vec())
.collect();
vlp_encode(&mut bin, &bootstrap_ips_bin)?;
}
let serialized = base64::encode_config(
&bin,
base64::Config::new(base64::CharacterSet::UrlSafe, false),
);
Ok(format!("sdns://{}", serialized))
}
}
impl WithInformalProperty for DoHBuilder {
fn with_informal_property(mut self, informal_property: InformalProperty) -> Self {
self.informal_properties |= u64::from(informal_property);
self
}
}