Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

add and use public_ip as fallback #295

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 206 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ hashbrown = { version = "0.12.0", features = [
"ahash-compile-time-rng",
] }
dashmap = "5.3"
public-ip = "0.2.2"

[target.'cfg(target_os = "linux")'.dependencies]
e2p-fileflags = { git = "https://github.com/michaellass/e2p-fileflags" }
Expand Down
9 changes: 8 additions & 1 deletion src/sentry/devp2p/disc/v4/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,14 @@ impl Node {
node_endpoint.write().address = Ip(v);
}
Err(e) => {
debug!("Failed to get public IP: {}", e);
debug!("Failed to get public IP via UPNP: {}", e);
debug!("Trying with public_ip DNS/HTTP resolvers");
if let Some(v) = public_ip::addr().await {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way to get and print specific error, not None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, however the crate tries various resolvers to try and get the external IP address on a best effort basis. The error would be resolver specific and in worst case would be a collection of errors of all resolvers tried (connection/networking errors).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would look something like this

                                let mut errors = vec![];
                                let ip = public_ip::resolve(public_ip::ALL, public_ip::Version::Any)
                                    .filter_map(|result| std::future::ready( result.map_err(|e| errors.push(e)).ok() ))
                                    .next()
                                    .await
                                    .map(|(addr, _)| addr);

                                if let Some(v) = ip {
                                    debug!("Discovered public IP: {}", v);
                                    node_endpoint.write().address = Ip(v);
                                } else {
                                    debug!("Failed to get public IP via DNS/HTTP resolvers: {:?}", errors);
                                }

debug!("Discovered public IP: {}", v);
node_endpoint.write().address = Ip(v);
} else {
debug!("Failed to get public IP via DNS/HTTP resolvers");
}
}
}
sleep(UPNP_INTERVAL).await;
Expand Down