Skip to content

Commit e92c605

Browse files
committed
Add H3 downstream option
1 parent 8479809 commit e92c605

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ dns_hostname = "dns.example.com"
104104
# optional (default = 3000)
105105
timeout_ms = 3000
106106
107+
[[downstream]]
108+
protocol = "h3"
109+
listen = "127.0.0.1"
110+
port = 8443
111+
certificate = "dns.example.com.crt"
112+
key = "dns.example.com.key"
113+
dns_hostname = "dns.example.com"
114+
# optional (default = 3000)
115+
timeout_ms = 3000
116+
107117
# optional
108118
[upstream.options]
109119
# optional (default = false)

example-config.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ dns_hostname = "dns.example.com"
5656
# optional (default = 3000)
5757
timeout_ms = 3000
5858

59+
[[downstream]]
60+
protocol = "h3"
61+
listen = "127.0.0.1"
62+
port = 8443
63+
certificate = "dns.example.com.crt"
64+
key = "dns.example.com.key"
65+
dns_hostname = "dns.example.com"
66+
# optional (default = 3000)
67+
timeout_ms = 3000
68+
5969
# optional
6070
[upstream.options]
6171
# optional (default = false)

src/main.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ fn load_cert_and_key(
246246
Ok((certificates, key))
247247
}
248248

249+
impl TlsConfig {
250+
#[inline(always)]
251+
fn load_cert_and_key(&self) -> anyhow::Result<(Vec<Certificate>, PrivateKey)> {
252+
load_cert_and_key(&self.certificate, &self.key)
253+
}
254+
}
255+
256+
impl HttpsAndQuicConfig {
257+
#[inline(always)]
258+
fn load_cert_and_key(&self) -> anyhow::Result<(Vec<Certificate>, PrivateKey)> {
259+
load_cert_and_key(&self.certificate, &self.key)
260+
}
261+
}
262+
249263
/// Load a text file from url and cache it.
250264
/// If restore_from_cache is true, only the cache is used.
251265
/// The first return value is the file content.
@@ -354,9 +368,9 @@ async fn async_main(config: Config) {
354368
server.register_socket(udp_socket);
355369
},
356370
DownstreamConfig::Tls(downstream) => {
357-
let cert_and_key =
358-
load_cert_and_key(&downstream.certificate, &downstream.key)
359-
.expect("failed to load certificate or private key");
371+
let cert_and_key = downstream
372+
.load_cert_and_key()
373+
.expect("failed to load certificate or private key");
360374
let socket_addr = format!("{}:{}", downstream.listen, downstream.port);
361375
let tcp_listener = TcpListener::bind(&socket_addr)
362376
.await
@@ -371,9 +385,9 @@ async fn async_main(config: Config) {
371385
.expect("failed to register tls downstream");
372386
},
373387
DownstreamConfig::Https(downstream) => {
374-
let cert_and_key =
375-
load_cert_and_key(&downstream.certificate, &downstream.key)
376-
.expect("failed to load certificate or private key");
388+
let cert_and_key = downstream
389+
.load_cert_and_key()
390+
.expect("failed to load certificate or private key");
377391
let socket_addr = format!("{}:{}", downstream.listen, downstream.port);
378392
let tcp_listener = TcpListener::bind(&socket_addr)
379393
.await
@@ -386,16 +400,16 @@ async fn async_main(config: Config) {
386400
cert_and_key,
387401
downstream.dns_hostname
388402
)
389-
.expect("failed to register tls downstream");
403+
.expect("failed to register https downstream");
390404
},
391405
DownstreamConfig::Quic(downstream) => {
392-
let cert_and_key =
393-
load_cert_and_key(&downstream.certificate, &downstream.key)
394-
.expect("failed to load certificate or private key");
406+
let cert_and_key = downstream
407+
.load_cert_and_key()
408+
.expect("failed to load certificate or private key");
395409
let socket_addr = format!("{}:{}", downstream.listen, downstream.port);
396410
let udp_socket = UdpSocket::bind(&socket_addr)
397411
.await
398-
.with_context(|| format!("failed to bind tcp socket {socket_addr}"))
412+
.with_context(|| format!("failed to bind udp socket {socket_addr}"))
399413
.unwrap_or_else(|err| panic!("{err:?}"));
400414
server
401415
.register_quic_listener(
@@ -404,7 +418,25 @@ async fn async_main(config: Config) {
404418
cert_and_key,
405419
downstream.dns_hostname
406420
)
407-
.expect("failed to register tls downstream");
421+
.expect("failed to register quic downstream");
422+
},
423+
DownstreamConfig::H3(downstream) => {
424+
let cert_and_key = downstream
425+
.load_cert_and_key()
426+
.expect("failed to load certificate or private key");
427+
let socket_addr = format!("{}:{}", downstream.listen, downstream.port);
428+
let udp_socket = UdpSocket::bind(&socket_addr)
429+
.await
430+
.with_context(|| format!("failed to bind udp socket {socket_addr}"))
431+
.unwrap_or_else(|err| panic!("{err:?}"));
432+
server
433+
.register_h3_listener(
434+
udp_socket,
435+
Duration::from_millis(downstream.timeout_ms),
436+
cert_and_key,
437+
downstream.dns_hostname
438+
)
439+
.expect("failed to register h3 downstream");
408440
}
409441
}
410442
}
@@ -460,7 +492,8 @@ enum DownstreamConfig {
460492
Udp(UdpConfig),
461493
Tls(TlsConfig),
462494
Https(HttpsAndQuicConfig),
463-
Quic(HttpsAndQuicConfig)
495+
Quic(HttpsAndQuicConfig),
496+
H3(HttpsAndQuicConfig)
464497
}
465498

466499
fn default_timeout() -> u64 {

0 commit comments

Comments
 (0)