Skip to content

Commit

Permalink
fix: use default (google) dns servers if system resolv.conf is invalid (
Browse files Browse the repository at this point in the history
#447)

* fix: use default (google) dns servers if system resolv.conf is invalid

If the system dns conf is missing nameservers, dns queries will never be resolved. Currently, hickory-dns doesn't error in this case so we handle it manually until the fix is released. This happens on osx if wifi is disabled when you start c1 and is never able to resolve itself without this change.

* fix: new clippy lint
  • Loading branch information
dav1do authored Jul 28, 2024
1 parent f490f0a commit d97c226
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ ceramic-store.workspace = true
cid.workspace = true
futures-util.workspace = true
futures.workspace = true
# temporary to address lack of error for invalid resolv.conf (missing nameservers)
# and libp2p not exposing enough types to actually build a dns resolver
hickory-resolver = { version = "0.24.1", default-features = false, features = ["system-config"] }
iroh-bitswap.workspace = true
iroh-rpc-client.workspace = true
iroh-rpc-types.workspace = true
Expand Down
26 changes: 24 additions & 2 deletions p2p/src/swarm.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
use anyhow::Result;
use ceramic_core::{EventId, Interest};
use libp2p::{noise, relay, tcp, tls, yamux, Swarm, SwarmBuilder};
use libp2p::{dns, noise, relay, tcp, tls, yamux, Swarm, SwarmBuilder};
use libp2p_identity::Keypair;
use recon::{libp2p::Recon, Sha256a};
use std::sync::Arc;

use crate::{behaviour::NodeBehaviour, Libp2pConfig, Metrics};

fn get_dns_config() -> (dns::ResolverConfig, dns::ResolverOpts) {
match hickory_resolver::system_conf::read_system_conf() {
Ok((conf, opts)) => {
// Won't be necessary if https://github.com/hickory-dns/hickory-dns/pull/2327 is merged/incoporated into libp2p
let conf = if conf.name_servers().is_empty() {
tracing::warn!(
"No nameservers found in system DNS configuration. Using Google DNS servers."
);
dns::ResolverConfig::default()
} else {
conf
};
(conf, opts)
}
Err(e) => {
tracing::warn!(err=%e, "Failed to load system DNS configuration. Switching to Google DNS servers. This can only be retried on server restart.");
(dns::ResolverConfig::default(), dns::ResolverOpts::default())
}
}
}

pub(crate) async fn build_swarm<I, M, S>(
config: &Libp2pConfig,
keypair: Keypair,
Expand All @@ -19,6 +40,7 @@ where
M: Recon<Key = EventId, Hash = Sha256a>,
S: iroh_bitswap::Store,
{
let (dns_conf, dns_opts) = get_dns_config();
let builder = SwarmBuilder::with_existing_identity(keypair)
.with_tokio()
.with_tcp(
Expand All @@ -29,7 +51,7 @@ where
yamux::Config::default,
)?
.with_quic()
.with_dns()?
.with_dns_config(dns_conf, dns_opts)
.with_websocket(
(tls::Config::new, noise::Config::new),
yamux::Config::default,
Expand Down
2 changes: 1 addition & 1 deletion store/src/sql/access/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl CeramicOneEvent {
// Fetch add happens with an open transaction (on one writer for the db) so we're guaranteed to get a unique value
sqlx::query(EventQuery::mark_ready_to_deliver())
.bind(Self::next_deliverable())
.bind(&key.to_bytes())
.bind(key.to_bytes())
.execute(&mut **conn.inner())
.await?;

Expand Down

0 comments on commit d97c226

Please sign in to comment.