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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::consts::DFLT_PORT;
use err_derive::Error;
use quinn::{Certificate, CertificateChain, ParseError, PrivateKey};
use std::fs::read as fs_read;
use std::io;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
#[derive(Debug, Error)]
pub enum CoordConfigError {
#[error(display = "Reading certificate or key file: {:?}", _0)]
ReadingCertOrKey(#[source] io::Error),
#[error(display = "Parsing certificate or key: {:?}", _0)]
ParsingCertOrKey(#[source] ParseError),
}
#[derive(Clone, Debug)]
pub struct CoordConfig {
pub(super) laddr: SocketAddr,
pub(super) keylog: bool,
pub(super) stateless_retry: bool,
pub(super) cert: CertificateChain,
pub(super) key: PrivateKey,
}
impl CoordConfig {
pub fn new(cert_path: PathBuf, key_path: PathBuf) -> Result<Self, CoordConfigError> {
let key = {
let tmp = fs_read(&key_path)?;
if key_path.extension().map_or(false, |x| x == "der") {
PrivateKey::from_der(&tmp)
} else {
PrivateKey::from_pem(&tmp)
}
}?;
let cert = {
let tmp = fs_read(&cert_path)?;
if cert_path.extension().map_or(false, |x| x == "der") {
CertificateChain::from_certs(Certificate::from_der(&tmp))
} else {
CertificateChain::from_pem(&tmp)?
}
};
Ok(Self {
laddr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), DFLT_PORT),
keylog: false,
stateless_retry: false,
cert,
key,
})
}
pub fn set_port(&mut self, port: u16) -> &mut Self {
self.laddr.set_port(port);
self
}
pub fn set_ip(&mut self, ip: IpAddr) -> &mut Self {
self.laddr.set_ip(ip);
self
}
pub fn enable_keylog(&mut self) -> &mut Self {
self.keylog = true;
self
}
pub fn enable_stateless_retry(&mut self) -> &mut Self {
self.stateless_retry = true;
self
}
}