Resolve endpoint addresses

This commit is contained in:
Andrey Golovizin 2023-04-15 12:45:30 +02:00
parent 79e88ae9ee
commit 707d55557e
2 changed files with 17 additions and 2 deletions

View file

@ -1,4 +1,5 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};
use anyhow::anyhow;
use log::debug;
use serde::{Deserialize, Serialize};
@ -30,6 +31,20 @@ pub(crate) struct Location {
pub pubkey: String,
}
impl Location {
pub(crate) fn get_endpoint(&self) -> anyhow::Result<SocketAddr> {
let endpoint_addr_with_port: String;
let endpoint_addr = if self.pool.contains(':') {
&self.pool
} else {
endpoint_addr_with_port = format!("{}:51820", self.pool);
&endpoint_addr_with_port
};
let mut endpoints = endpoint_addr.to_socket_addrs()?;
endpoints.next().ok_or_else(|| anyhow!("no valid endpoint address"))
}
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct Addresses {
pub status: String,

View file

@ -146,7 +146,7 @@ fn write_config(
writeln!(output, "[Peer]")?;
writeln!(output, "PublicKey = {}", &location.pubkey)?;
writeln!(output, "Endpoint = {}:51820", &location.pool)?;
writeln!(output, "Endpoint = {}", &location.get_endpoint()?)?;
let allowed_ips: &[&str] = if config_opts.no_ipv6 {
&["0.0.0.0/0"]