Skip to content

Commit e362f32

Browse files
authored
Merge pull request #710 from nextcloud/fallback-ipv4
fix: automatically fall back to ipv4 if ipv6 is disabled an no bind address is specified
2 parents 20eec4b + c57a391 commit e362f32

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/config.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use sqlx::any::AnyConnectOptions;
2020
use std::convert::{TryFrom, TryInto};
2121
use std::env::var;
2222
use std::fmt::{Debug, Display, Formatter};
23-
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
23+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener};
2424
use std::path::{Path, PathBuf};
2525
use std::str::FromStr;
2626

@@ -165,6 +165,15 @@ impl Display for Bind {
165165
}
166166
}
167167

168+
fn default_addr() -> IpAddr {
169+
let v6 = IpAddr::V6(Ipv6Addr::UNSPECIFIED);
170+
if TcpListener::bind((v6, 0)).is_ok() {
171+
v6
172+
} else {
173+
IpAddr::V4(Ipv4Addr::UNSPECIFIED)
174+
}
175+
}
176+
168177
impl TryFrom<PartialConfig> for Config {
169178
type Error = Error;
170179

@@ -183,7 +192,7 @@ impl TryFrom<PartialConfig> for Config {
183192
let bind = match config.socket {
184193
Some(socket) => Bind::Unix(socket, socket_permissions),
185194
None => {
186-
let ip = config.bind.unwrap_or(IpAddr::V6(Ipv6Addr::UNSPECIFIED));
195+
let ip = config.bind.unwrap_or_else(default_addr);
187196
let port = config.port.unwrap_or(7867);
188197
Bind::Tcp((ip, port).into())
189198
}
@@ -192,7 +201,7 @@ impl TryFrom<PartialConfig> for Config {
192201
let metrics_bind = match (config.metrics_socket, config.metrics_port) {
193202
(Some(socket), _) => Some(Bind::Unix(socket, socket_permissions)),
194203
(None, Some(port)) => {
195-
let ip = config.bind.unwrap_or(IpAddr::V6(Ipv6Addr::UNSPECIFIED));
204+
let ip = config.bind.unwrap_or_else(default_addr);
196205
Some(Bind::Tcp((ip, port).into()))
197206
}
198207
_ => None,

0 commit comments

Comments
 (0)