forked from mokeyish/smartdns-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.rs
208 lines (189 loc) · 5.55 KB
/
listener.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use enum_dispatch::enum_dispatch;
use serde::{Deserialize, Serialize};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4};
use crate::third_ext::serde_str;
use super::{ServerOpts, SslConfig};
#[enum_dispatch(NomParser, IListenerConfig)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ListenerConfig {
Udp(UdpListenerConfig),
Tcp(TcpListenerConfig),
Tls(TlsListenerConfig),
Https(HttpsListenerConfig),
Quic(QuicListenerConfig),
}
#[enum_dispatch]
pub trait IListenerConfig {
fn listen(&self) -> ListenerAddress;
fn mut_listen(&mut self) -> &mut ListenerAddress;
fn port(&self) -> u16;
fn device(&self) -> Option<&str>;
fn server_opts(&self) -> &ServerOpts;
fn sock_addr(&self) -> SocketAddr {
match self.listen() {
ListenerAddress::Localhost => {
SocketAddrV4::new(Ipv4Addr::LOCALHOST, self.port()).into()
}
ListenerAddress::All => SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, self.port()).into(),
ListenerAddress::V4(ip) => (ip, self.port()).into(),
ListenerAddress::V6(ip) => (ip, self.port()).into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UdpListenerConfig {
/// listen adress
#[serde(with = "serde_str")]
pub listen: ListenerAddress,
/// listen port
pub port: u16,
/// bind network device.
#[serde(skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
/// ssl config
#[serde(flatten)]
pub opts: ServerOpts,
}
impl Default for UdpListenerConfig {
fn default() -> Self {
Self {
listen: ListenerAddress::V4(Ipv4Addr::UNSPECIFIED),
port: 53,
device: Default::default(),
opts: Default::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TcpListenerConfig {
/// listen adress
#[serde(with = "serde_str")]
pub listen: ListenerAddress,
/// listen port
pub port: u16,
/// bind network device.
#[serde(skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
/// ssl config
#[serde(flatten)]
pub opts: ServerOpts,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TlsListenerConfig {
/// listen adress
#[serde(with = "serde_str")]
pub listen: ListenerAddress,
/// listen port
pub port: u16,
/// bind network device.
#[serde(skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
/// the server options
#[serde(flatten)]
pub opts: ServerOpts,
/// ssl config
#[serde(flatten)]
pub ssl_config: SslConfig,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct HttpsListenerConfig {
/// listen adress
#[serde(with = "serde_str")]
pub listen: ListenerAddress,
/// listen port
pub port: u16,
/// bind network device.
#[serde(skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
/// the server options
#[serde(flatten)]
pub opts: ServerOpts,
/// ssl config
#[serde(flatten)]
pub ssl_config: SslConfig,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct QuicListenerConfig {
/// listen adress
#[serde(with = "serde_str")]
pub listen: ListenerAddress,
/// listen port
pub port: u16,
/// bind network device.
#[serde(skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
/// the server options
#[serde(flatten)]
pub opts: ServerOpts,
/// ssl config
#[serde(flatten)]
pub ssl_config: SslConfig,
}
macro_rules! impl_listener {
($($name:ident),+) => {
$(
impl IListenerConfig for $name {
fn listen(&self) -> ListenerAddress {
self.listen
}
fn mut_listen(&mut self) -> &mut ListenerAddress {
&mut self.listen
}
fn port(&self) -> u16 {
self.port
}
fn device(&self) -> Option<&str> {
self.device.as_deref()
}
fn server_opts(&self) -> &ServerOpts {
&self.opts
}
}
)+
}
}
impl_listener!(
UdpListenerConfig,
TcpListenerConfig,
TlsListenerConfig,
HttpsListenerConfig,
QuicListenerConfig
);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ListenerAddress {
Localhost,
All,
V4(Ipv4Addr),
V6(Ipv6Addr),
}
impl ToString for ListenerAddress {
fn to_string(&self) -> String {
use ListenerAddress::*;
match self {
Localhost => "localhost".to_string(),
All => "*".to_string(),
V4(ip) => ip.to_string(),
V6(ip) => format!("[{ip}]"),
}
}
}
impl From<IpAddr> for ListenerAddress {
fn from(value: IpAddr) -> Self {
match value {
IpAddr::V4(ip) => ListenerAddress::V4(ip),
IpAddr::V6(ip) => ListenerAddress::V6(ip),
}
}
}
impl ListenerAddress {
/// Returns the ip addr of this [`ListenerAddress`].
fn ip_addr(self) -> IpAddr {
match self {
ListenerAddress::Localhost => IpAddr::V4(Ipv4Addr::LOCALHOST),
ListenerAddress::All => IpAddr::V4(Ipv4Addr::UNSPECIFIED),
ListenerAddress::V4(ip) => ip.into(),
ListenerAddress::V6(ip) => ip.into(),
}
}
}