Skip to content

Commit 63f9dcc

Browse files
committed
✨ Add support for bind-h3
1 parent f6bdf0d commit 63f9dcc

File tree

10 files changed

+430
-207
lines changed

10 files changed

+430
-207
lines changed

Cargo.lock

Lines changed: 65 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ either = { version = "1.12.0", optional = true }
133133

134134
# webapi
135135
axum = { version = "0.8.1" }
136+
axum-h3 = "0.0.3"
137+
h3-util = { version = "0.0.3", default-features = false, features = ["quinn"]}
136138
hyper = { version = "1.1.0", default-features = false }
137139
hyper-util = { version = "0.1.3", features = ["http2"]}
138140
tower = { version = "0.5.2", default-features = false }

src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct App {
2626
cfg: RwLock<Arc<RuntimeConfig>>,
2727
mw_handler: RwLock<Arc<DnsMiddlewareHandler>>,
2828
dns_handle: DnsHandle,
29-
listener_map: Arc<RwLock<HashMap<crate::config::ListenerConfig, ServerHandle>>>,
29+
listener_map: Arc<RwLock<HashMap<crate::config::BindAddrConfig, ServerHandle>>>,
3030
cache: RwLock<Option<Arc<DnsCache>>>,
3131
runtime: Handle,
3232
guard: AppGuard,
@@ -303,7 +303,7 @@ async fn register_listeners(app: &Arc<App>) {
303303

304304
async fn serve(
305305
app: &Arc<App>,
306-
listener: &crate::config::ListenerConfig,
306+
listener: &crate::config::BindAddrConfig,
307307
) -> Result<ServerHandle, crate::Error> {
308308
use crate::server::serve;
309309

Lines changed: 76 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,40 @@ use crate::third_ext::serde_str;
66

77
use super::{ServerOpts, SslConfig};
88

9-
#[enum_dispatch(NomParser, IListenerConfig)]
9+
#[enum_dispatch(NomParser, IBindConfig)]
1010
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
1111
#[serde(tag = "type")]
12-
pub enum ListenerConfig {
13-
Udp(UdpListenerConfig),
14-
Tcp(TcpListenerConfig),
15-
Tls(TlsListenerConfig),
16-
Https(HttpsListenerConfig),
17-
Quic(QuicListenerConfig),
12+
pub enum BindAddrConfig {
13+
Udp(UdpBindAddrConfig),
14+
Tcp(TcpBindAddrConfig),
15+
Tls(TlsBindAddrConfig),
16+
Https(HttpsBindAddrConfig),
17+
H3(H3BindAddrConfig),
18+
Quic(QuicBindAddrConfig),
1819
}
1920

2021
#[enum_dispatch]
21-
pub trait IListenerConfig {
22-
fn listen(&self) -> ListenerAddress;
23-
fn mut_listen(&mut self) -> &mut ListenerAddress;
22+
pub trait IBindConfig {
23+
fn addr(&self) -> BindAddr;
24+
fn mut_addr(&mut self) -> &mut BindAddr;
2425
fn port(&self) -> u16;
2526
fn device(&self) -> Option<&str>;
2627
fn server_opts(&self) -> &ServerOpts;
2728
fn sock_addr(&self) -> SocketAddr {
28-
match self.listen() {
29-
ListenerAddress::Localhost => {
30-
SocketAddrV4::new(Ipv4Addr::LOCALHOST, self.port()).into()
31-
}
32-
ListenerAddress::All => SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, self.port()).into(),
33-
ListenerAddress::V4(ip) => (ip, self.port()).into(),
34-
ListenerAddress::V6(ip) => (ip, self.port()).into(),
29+
match self.addr() {
30+
BindAddr::Localhost => SocketAddrV4::new(Ipv4Addr::LOCALHOST, self.port()).into(),
31+
BindAddr::All => SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, self.port()).into(),
32+
BindAddr::V4(ip) => (ip, self.port()).into(),
33+
BindAddr::V6(ip) => (ip, self.port()).into(),
3534
}
3635
}
3736
}
3837

3938
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
40-
pub struct UdpListenerConfig {
39+
pub struct UdpBindAddrConfig {
4140
/// listen adress
4241
#[serde(with = "serde_str")]
43-
pub listen: ListenerAddress,
42+
pub addr: BindAddr,
4443
/// listen port
4544
pub port: u16,
4645
/// bind network device.
@@ -51,10 +50,10 @@ pub struct UdpListenerConfig {
5150
pub opts: ServerOpts,
5251
}
5352

54-
impl Default for UdpListenerConfig {
53+
impl Default for UdpBindAddrConfig {
5554
fn default() -> Self {
5655
Self {
57-
listen: ListenerAddress::V4(Ipv4Addr::UNSPECIFIED),
56+
addr: BindAddr::V4(Ipv4Addr::UNSPECIFIED),
5857
port: 53,
5958
device: Default::default(),
6059
opts: Default::default(),
@@ -63,11 +62,11 @@ impl Default for UdpListenerConfig {
6362
}
6463

6564
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
66-
pub struct TcpListenerConfig {
67-
/// listen adress
65+
pub struct TcpBindAddrConfig {
66+
/// addr adress
6867
#[serde(with = "serde_str")]
69-
pub listen: ListenerAddress,
70-
/// listen port
68+
pub addr: BindAddr,
69+
/// addr port
7170
pub port: u16,
7271
/// bind network device.
7372
#[serde(skip_serializing_if = "Option::is_none")]
@@ -78,11 +77,11 @@ pub struct TcpListenerConfig {
7877
}
7978

8079
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
81-
pub struct TlsListenerConfig {
82-
/// listen adress
80+
pub struct TlsBindAddrConfig {
81+
/// addr adress
8382
#[serde(with = "serde_str")]
84-
pub listen: ListenerAddress,
85-
/// listen port
83+
pub addr: BindAddr,
84+
/// addr port
8685
pub port: u16,
8786
/// bind network device.
8887
#[serde(skip_serializing_if = "Option::is_none")]
@@ -96,11 +95,11 @@ pub struct TlsListenerConfig {
9695
}
9796

9897
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
99-
pub struct HttpsListenerConfig {
100-
/// listen adress
98+
pub struct HttpsBindAddrConfig {
99+
/// addr adress
101100
#[serde(with = "serde_str")]
102-
pub listen: ListenerAddress,
103-
/// listen port
101+
pub addr: BindAddr,
102+
/// addr port
104103
pub port: u16,
105104
/// bind network device.
106105
#[serde(skip_serializing_if = "Option::is_none")]
@@ -114,11 +113,29 @@ pub struct HttpsListenerConfig {
114113
}
115114

116115
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
117-
pub struct QuicListenerConfig {
118-
/// listen adress
116+
pub struct H3BindAddrConfig {
117+
/// addr adress
119118
#[serde(with = "serde_str")]
120-
pub listen: ListenerAddress,
121-
/// listen port
119+
pub addr: BindAddr,
120+
/// addr port
121+
pub port: u16,
122+
/// bind network device.
123+
#[serde(skip_serializing_if = "Option::is_none")]
124+
pub device: Option<String>,
125+
/// the server options
126+
#[serde(flatten)]
127+
pub opts: ServerOpts,
128+
/// ssl config
129+
#[serde(flatten)]
130+
pub ssl_config: SslConfig,
131+
}
132+
133+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
134+
pub struct QuicBindAddrConfig {
135+
/// addr adress
136+
#[serde(with = "serde_str")]
137+
pub addr: BindAddr,
138+
/// addr port
122139
pub port: u16,
123140
/// bind network device.
124141
#[serde(skip_serializing_if = "Option::is_none")]
@@ -134,12 +151,12 @@ pub struct QuicListenerConfig {
134151
macro_rules! impl_listener {
135152
($($name:ident),+) => {
136153
$(
137-
impl IListenerConfig for $name {
138-
fn listen(&self) -> ListenerAddress {
139-
self.listen
154+
impl IBindConfig for $name {
155+
fn addr(&self) -> BindAddr {
156+
self.addr
140157
}
141-
fn mut_listen(&mut self) -> &mut ListenerAddress {
142-
&mut self.listen
158+
fn mut_addr(&mut self) -> &mut BindAddr {
159+
&mut self.addr
143160
}
144161

145162
fn port(&self) -> u16 {
@@ -158,24 +175,25 @@ macro_rules! impl_listener {
158175
}
159176

160177
impl_listener!(
161-
UdpListenerConfig,
162-
TcpListenerConfig,
163-
TlsListenerConfig,
164-
HttpsListenerConfig,
165-
QuicListenerConfig
178+
UdpBindAddrConfig,
179+
TcpBindAddrConfig,
180+
TlsBindAddrConfig,
181+
HttpsBindAddrConfig,
182+
H3BindAddrConfig,
183+
QuicBindAddrConfig
166184
);
167185

168186
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
169-
pub enum ListenerAddress {
187+
pub enum BindAddr {
170188
Localhost,
171189
All,
172190
V4(Ipv4Addr),
173191
V6(Ipv6Addr),
174192
}
175193

176-
impl std::fmt::Display for ListenerAddress {
194+
impl std::fmt::Display for BindAddr {
177195
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
178-
use ListenerAddress::*;
196+
use BindAddr::*;
179197

180198
match self {
181199
Localhost => write!(f, "localhost"),
@@ -186,23 +204,23 @@ impl std::fmt::Display for ListenerAddress {
186204
}
187205
}
188206

189-
impl From<IpAddr> for ListenerAddress {
207+
impl From<IpAddr> for BindAddr {
190208
fn from(value: IpAddr) -> Self {
191209
match value {
192-
IpAddr::V4(ip) => ListenerAddress::V4(ip),
193-
IpAddr::V6(ip) => ListenerAddress::V6(ip),
210+
IpAddr::V4(ip) => BindAddr::V4(ip),
211+
IpAddr::V6(ip) => BindAddr::V6(ip),
194212
}
195213
}
196214
}
197215

198-
impl ListenerAddress {
216+
impl BindAddr {
199217
/// Returns the ip addr of this [`ListenerAddress`].
200218
fn ip_addr(self) -> IpAddr {
201219
match self {
202-
ListenerAddress::Localhost => IpAddr::V4(Ipv4Addr::LOCALHOST),
203-
ListenerAddress::All => IpAddr::V4(Ipv4Addr::UNSPECIFIED),
204-
ListenerAddress::V4(ip) => ip.into(),
205-
ListenerAddress::V6(ip) => ip.into(),
220+
BindAddr::Localhost => IpAddr::V4(Ipv4Addr::LOCALHOST),
221+
BindAddr::All => IpAddr::V4(Ipv4Addr::UNSPECIFIED),
222+
BindAddr::V4(ip) => ip.into(),
223+
BindAddr::V6(ip) => ip.into(),
206224
}
207225
}
208226
}

0 commit comments

Comments
 (0)