Resolve endpoint addresses
This commit is contained in:
parent
79e88ae9ee
commit
707d55557e
2 changed files with 17 additions and 2 deletions
17
src/api.rs
17
src/api.rs
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue