fix: resolve external ip on launch (#16768)

This commit is contained in:
Matthias Seitz
2025-06-12 11:41:48 +02:00
committed by GitHub
parent 6ddc756489
commit a9bbc9be65
2 changed files with 20 additions and 1 deletions

View File

@@ -252,7 +252,12 @@ impl Discv4 {
local_node_record.udp_port = local_addr.port();
trace!(target: "discv4", ?local_addr,"opened UDP socket");
let service = Discv4Service::new(socket, local_addr, local_node_record, secret_key, config);
let mut service =
Discv4Service::new(socket, local_addr, local_node_record, secret_key, config);
// resolve the external address immediately
service.resolve_external_ip();
let discv4 = service.handle();
Ok((discv4, service))
}
@@ -620,6 +625,15 @@ impl Discv4Service {
self.lookup_interval = tokio::time::interval(duration);
}
/// Sets the external Ip to the configured external IP if [`NatResolver::ExternalIp`].
fn resolve_external_ip(&mut self) {
if let Some(r) = &self.resolve_external_ip_interval {
if let Some(external_ip) = r.resolver().as_external_ip() {
self.set_external_ip_addr(external_ip);
}
}
}
/// Sets the given ip address as the node's external IP in the node record announced in
/// discovery
pub fn set_external_ip_addr(&mut self, external_ip: IpAddr) {

View File

@@ -161,6 +161,11 @@ impl ResolveNatInterval {
Self::with_interval(resolver, interval)
}
/// Returns the resolver used by this interval
pub const fn resolver(&self) -> &NatResolver {
&self.resolver
}
/// Completes when the next [`IpAddr`] in the interval has been reached.
pub async fn tick(&mut self) -> Option<IpAddr> {
poll_fn(|cx| self.poll_tick(cx)).await