-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: dns short name #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix: dns short name #156
Changes from all commits
0fcd63d
a9a93ab
013cc33
2fe175a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| use std::convert::Infallible; | ||
| use std::net::{IpAddr, Ipv4Addr}; | ||
| use std::path::PathBuf; | ||
| use std::sync::Arc; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
|
|
@@ -38,12 +39,19 @@ use crate::verifier::IntermeshVerifier; | |
|
|
||
| mod dns; | ||
| mod intercept; | ||
| mod resolv_conf; | ||
|
|
||
| use dns::{create_resolver, Dns}; | ||
| use intercept::{ | ||
| bind_dns, bind_tcp, get_original_dst, nftables_clean, nftables_inject, EXTERNAL_PORT, | ||
| PROXY_PORT, | ||
| }; | ||
| use resolv_conf::ResolvConf; | ||
|
|
||
| /// Default location of the host's resolver config. | ||
| const RESOLV_CONF_PATH: &str = "/etc/resolv.conf"; | ||
| /// Filename of the on-disk snapshot, placed alongside the daemon state file. | ||
| const RESOLV_CONF_SNAPSHOT: &str = "resolv.conf.orig"; | ||
|
|
||
| // ============================================================================ | ||
| // Proxy | ||
|
|
@@ -83,8 +91,15 @@ impl Handle { | |
| /// enabled, creates local service for host namespace interception and | ||
| /// spawns HTTP CONNECT listener for incoming mTLS from remote peers. | ||
| pub(crate) async fn run(self, cancel: CancellationToken) -> Result<()> { | ||
| // Clean up any stale rules from a previous run. | ||
| // Clean up any stale state from a previous run. | ||
| nftables_clean(); | ||
| let resolv_conf = make_resolv_conf(&self.state)?; | ||
| // A prior run may have crashed with our managed file still installed. | ||
| // Restore the real upstream config before create_resolver() reads it, | ||
| // otherwise the resolver would forward queries to our own loopback | ||
| // nameserver. Runs regardless of intercept so a stale managed file | ||
| // never outlives the proxy. | ||
| resolv_conf.recover().context("recover resolv.conf")?; | ||
|
|
||
| if !self.state.intercept { | ||
| cancel.cancelled().await; | ||
|
|
@@ -96,7 +111,21 @@ impl Handle { | |
| let tcp_sock = bind_tcp(Ipv4Addr::LOCALHOST, PROXY_PORT).context("bind proxy socket")?; | ||
| let ext_sock = | ||
| bind_tcp(Ipv4Addr::UNSPECIFIED, EXTERNAL_PORT).context("bind external socket")?; | ||
| // Read upstream nameservers from /etc/resolv.conf *before* | ||
| // resolv_conf.install() overwrites the file. Otherwise we'd read back | ||
| // our own `nameserver 127.0.0.1` and forward queries to ourselves. | ||
| let resolver = create_resolver(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI-generated, human-unreviewed, non-binding: The normal-path ordering avoids reading the managed |
||
| // Defer order matters: scopeguard fires LIFO. Register the resolv.conf | ||
| // restore first so nftables_clean (registered second) fires first on | ||
| // shutdown — global system rules drop ASAP, local file restore after. | ||
| resolv_conf | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI-generated, human-unreviewed, non-binding: The restore guard is only registered after |
||
| .install() | ||
| .context("install managed resolv.conf")?; | ||
| defer!({ | ||
| if let Err(e) = resolv_conf.restore() { | ||
| error!("restore resolv.conf failed: {e:#}"); | ||
| } | ||
| }); | ||
| nftables_inject().context("inject nftables rules")?; | ||
| defer!(nftables_clean()); | ||
|
|
||
|
|
@@ -322,6 +351,17 @@ impl Handle { | |
| // Utilities | ||
| // ============================================================================ | ||
|
|
||
| /// Construct a `ResolvConf` for production use, with the snapshot placed in | ||
| /// the same directory as the daemon state file. | ||
| fn make_resolv_conf(state: &State) -> Result<ResolvConf> { | ||
| let snapshot = state | ||
| .state_file | ||
| .parent() | ||
| .context("state file has no parent directory")? | ||
| .join(RESOLV_CONF_SNAPSHOT); | ||
| Ok(ResolvConf::new(PathBuf::from(RESOLV_CONF_PATH), snapshot)) | ||
| } | ||
|
|
||
| /// Parse the target from an HTTP CONNECT request URI authority. | ||
| fn parse_connect_target(uri: &Uri) -> Result<(Name, u16)> { | ||
| let authority = uri.authority().context("missing authority")?.as_str(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AI-generated, human-unreviewed, non-binding:
make_resolv_conf(&self.state)?now runs before the!self.state.interceptearly-return path, so a non-intercepting proxy can fail due to resolv.conf snapshot path setup even though it will never manage/etc/resolv.conf. Moving this below the intercept check keeps the non-intercept mode behavior unchanged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AI-generated -- a later change on this branch added resolv_conf.recover() immediately
after make_resolv_conf and before the intercept gate, so it's no longer dead weight
recover() restores a managed /etc/resolv.conf left by a crashed prior run and intentionally runs
cross-mode: if a previous intercept run crashed (leaving nameserver 127.0.0.1) and the operator
restarts without intercept, moving this below the gate would strand the host's DNS on a dead
127.0.0.1 until a future intercept run. The cited failure only occurs if state_file has no
parent (path /), which would break the daemon everywhere else anyway.