fix server port allocation

This commit is contained in:
Christien Rioux 2023-08-02 15:13:23 -04:00
parent 79bf6fca69
commit 559ac5f162
2 changed files with 21 additions and 17 deletions

View File

@ -164,9 +164,9 @@ impl Network {
/////////////////////////////////////////////////////
fn find_available_udp_port(&self) -> EyreResult<u16> {
fn find_available_udp_port(&self, start_port: u16) -> EyreResult<u16> {
// If the address is empty, iterate ports until we find one we can use.
let mut udp_port = 5150u16;
let mut udp_port = start_port;
loop {
if BAD_PORTS.contains(&udp_port) {
continue;
@ -182,9 +182,9 @@ impl Network {
Ok(udp_port)
}
fn find_available_tcp_port(&self) -> EyreResult<u16> {
fn find_available_tcp_port(&self, start_port: u16) -> EyreResult<u16> {
// If the address is empty, iterate ports until we find one we can use.
let mut tcp_port = 5150u16;
let mut tcp_port = start_port;
loop {
if BAD_PORTS.contains(&tcp_port) {
continue;
@ -203,7 +203,7 @@ impl Network {
async fn allocate_udp_port(&self, listen_address: String) -> EyreResult<(u16, Vec<IpAddr>)> {
if listen_address.is_empty() {
// If listen address is empty, find us a port iteratively
let port = self.find_available_udp_port()?;
let port = self.find_available_udp_port(5150)?;
let ip_addrs = vec![
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
IpAddr::V6(Ipv6Addr::UNSPECIFIED),
@ -218,9 +218,7 @@ impl Network {
bail!("No valid listen address: {}", listen_address);
}
let port = sockaddrs[0].port();
if !self.bind_first_udp_port(port) {
bail!("Could not find free udp port to listen on");
}
Ok((port, sockaddrs.iter().map(|s| s.ip()).collect()))
}
}
@ -228,7 +226,7 @@ impl Network {
async fn allocate_tcp_port(&self, listen_address: String) -> EyreResult<(u16, Vec<IpAddr>)> {
if listen_address.is_empty() {
// If listen address is empty, find us a port iteratively
let port = self.find_available_tcp_port()?;
let port = self.find_available_tcp_port(5150)?;
let ip_addrs = vec![
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
IpAddr::V6(Ipv6Addr::UNSPECIFIED),

View File

@ -120,38 +120,38 @@ core:
application:
https:
enabled: false
listen_address: ':5150'
listen_address: ':443'
path: 'app'
# url: 'https://localhost:5150'
# url: 'https://localhost'
http:
enabled: false
listen_address: ':5150'
listen_address: ':80'
path: 'app'
# url: 'http://localhost:5150'
# url: 'http://localhost'
protocol:
udp:
enabled: true
socket_pool_size: 0
listen_address: ':5150'
listen_address: ''
# public_address: ''
tcp:
connect: true
listen: true
max_connections: 32
listen_address: ':5150'
listen_address: ''
#'public_address: ''
ws:
connect: true
listen: true
max_connections: 16
listen_address: ':5150'
listen_address: ''
path: 'ws'
# url: 'ws://localhost:5150/ws'
wss:
connect: true
listen: false
max_connections: 16
listen_address: ':5150'
listen_address: ''
path: 'ws'
# url: ''
"#,
@ -351,6 +351,12 @@ pub struct NamedSocketAddrs {
impl FromStr for NamedSocketAddrs {
type Err = std::io::Error;
fn from_str(s: &str) -> Result<NamedSocketAddrs, std::io::Error> {
if s.is_empty() {
return Ok(NamedSocketAddrs {
name: String::new(),
addrs: vec![],
});
}
let addr_iter = listen_address_to_socket_addrs(s)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
Ok(NamedSocketAddrs {