Browse Source

Added "bindAddressFallback" setting, which defaults to "0.0.0.0".

Should fix problems on systems unable to use IPv6 wildcard (::) for listening, and avoids having to manually specify "bindAddress": "0.0.0.0" in settings.json.
q-apps
CalDescent 1 year ago
parent
commit
358e67b050
  1. 5
      src/main/java/org/qortal/api/ApiService.java
  2. 5
      src/main/java/org/qortal/api/DomainMapService.java
  3. 5
      src/main/java/org/qortal/api/GatewayService.java
  4. 30
      src/main/java/org/qortal/network/Network.java
  5. 5
      src/main/java/org/qortal/settings/Settings.java

5
src/main/java/org/qortal/api/ApiService.java

@ -41,6 +41,7 @@ import org.glassfish.jersey.servlet.ServletContainer;
import org.qortal.api.resource.AnnotationPostProcessor; import org.qortal.api.resource.AnnotationPostProcessor;
import org.qortal.api.resource.ApiDefinition; import org.qortal.api.resource.ApiDefinition;
import org.qortal.api.websocket.*; import org.qortal.api.websocket.*;
import org.qortal.network.Network;
import org.qortal.settings.Settings; import org.qortal.settings.Settings;
public class ApiService { public class ApiService {
@ -123,13 +124,13 @@ public class ApiService {
ServerConnector portUnifiedConnector = new ServerConnector(this.server, ServerConnector portUnifiedConnector = new ServerConnector(this.server,
new DetectorConnectionFactory(sslConnectionFactory), new DetectorConnectionFactory(sslConnectionFactory),
httpConnectionFactory); httpConnectionFactory);
portUnifiedConnector.setHost(Settings.getInstance().getBindAddress()); portUnifiedConnector.setHost(Network.getInstance().getBindAddress());
portUnifiedConnector.setPort(Settings.getInstance().getApiPort()); portUnifiedConnector.setPort(Settings.getInstance().getApiPort());
this.server.addConnector(portUnifiedConnector); this.server.addConnector(portUnifiedConnector);
} else { } else {
// Non-SSL // Non-SSL
InetAddress bindAddr = InetAddress.getByName(Settings.getInstance().getBindAddress()); InetAddress bindAddr = InetAddress.getByName(Network.getInstance().getBindAddress());
InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getApiPort()); InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getApiPort());
this.server = new Server(endpoint); this.server = new Server(endpoint);
} }

5
src/main/java/org/qortal/api/DomainMapService.java

@ -16,6 +16,7 @@ import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer; import org.glassfish.jersey.servlet.ServletContainer;
import org.qortal.api.resource.AnnotationPostProcessor; import org.qortal.api.resource.AnnotationPostProcessor;
import org.qortal.api.resource.ApiDefinition; import org.qortal.api.resource.ApiDefinition;
import org.qortal.network.Network;
import org.qortal.settings.Settings; import org.qortal.settings.Settings;
import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.KeyManagerFactory;
@ -99,13 +100,13 @@ public class DomainMapService {
ServerConnector portUnifiedConnector = new ServerConnector(this.server, ServerConnector portUnifiedConnector = new ServerConnector(this.server,
new DetectorConnectionFactory(sslConnectionFactory), new DetectorConnectionFactory(sslConnectionFactory),
httpConnectionFactory); httpConnectionFactory);
portUnifiedConnector.setHost(Settings.getInstance().getBindAddress()); portUnifiedConnector.setHost(Network.getInstance().getBindAddress());
portUnifiedConnector.setPort(Settings.getInstance().getDomainMapPort()); portUnifiedConnector.setPort(Settings.getInstance().getDomainMapPort());
this.server.addConnector(portUnifiedConnector); this.server.addConnector(portUnifiedConnector);
} else { } else {
// Non-SSL // Non-SSL
InetAddress bindAddr = InetAddress.getByName(Settings.getInstance().getBindAddress()); InetAddress bindAddr = InetAddress.getByName(Network.getInstance().getBindAddress());
InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getDomainMapPort()); InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getDomainMapPort());
this.server = new Server(endpoint); this.server = new Server(endpoint);
} }

5
src/main/java/org/qortal/api/GatewayService.java

@ -15,6 +15,7 @@ import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer; import org.glassfish.jersey.servlet.ServletContainer;
import org.qortal.api.resource.AnnotationPostProcessor; import org.qortal.api.resource.AnnotationPostProcessor;
import org.qortal.api.resource.ApiDefinition; import org.qortal.api.resource.ApiDefinition;
import org.qortal.network.Network;
import org.qortal.settings.Settings; import org.qortal.settings.Settings;
import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.KeyManagerFactory;
@ -98,13 +99,13 @@ public class GatewayService {
ServerConnector portUnifiedConnector = new ServerConnector(this.server, ServerConnector portUnifiedConnector = new ServerConnector(this.server,
new DetectorConnectionFactory(sslConnectionFactory), new DetectorConnectionFactory(sslConnectionFactory),
httpConnectionFactory); httpConnectionFactory);
portUnifiedConnector.setHost(Settings.getInstance().getBindAddress()); portUnifiedConnector.setHost(Network.getInstance().getBindAddress());
portUnifiedConnector.setPort(Settings.getInstance().getGatewayPort()); portUnifiedConnector.setPort(Settings.getInstance().getGatewayPort());
this.server.addConnector(portUnifiedConnector); this.server.addConnector(portUnifiedConnector);
} else { } else {
// Non-SSL // Non-SSL
InetAddress bindAddr = InetAddress.getByName(Settings.getInstance().getBindAddress()); InetAddress bindAddr = InetAddress.getByName(Network.getInstance().getBindAddress());
InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getGatewayPort()); InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getGatewayPort());
this.server = new Server(endpoint); this.server = new Server(endpoint);
} }

30
src/main/java/org/qortal/network/Network.java

@ -124,6 +124,8 @@ public class Network {
private final List<PeerAddress> selfPeers = new ArrayList<>(); private final List<PeerAddress> selfPeers = new ArrayList<>();
private String bindAddress = null;
private final ExecuteProduceConsume networkEPC; private final ExecuteProduceConsume networkEPC;
private Selector channelSelector; private Selector channelSelector;
private ServerSocketChannel serverChannel; private ServerSocketChannel serverChannel;
@ -159,9 +161,19 @@ public class Network {
// Grab P2P port from settings // Grab P2P port from settings
int listenPort = Settings.getInstance().getListenPort(); int listenPort = Settings.getInstance().getListenPort();
// Grab P2P bind address from settings // Grab P2P bind addresses from settings
List<String> bindAddresses = new ArrayList<>();
if (Settings.getInstance().getBindAddress() != null) {
bindAddresses.add(Settings.getInstance().getBindAddress());
}
if (Settings.getInstance().getBindAddressFallback() != null) {
bindAddresses.add(Settings.getInstance().getBindAddressFallback());
}
for (int i=0; i<bindAddresses.size(); i++) {
try { try {
InetAddress bindAddr = InetAddress.getByName(Settings.getInstance().getBindAddress()); String bindAddress = bindAddresses.get(i);
InetAddress bindAddr = InetAddress.getByName(bindAddress);
InetSocketAddress endpoint = new InetSocketAddress(bindAddr, listenPort); InetSocketAddress endpoint = new InetSocketAddress(bindAddr, listenPort);
channelSelector = Selector.open(); channelSelector = Selector.open();
@ -172,13 +184,21 @@ public class Network {
serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
serverChannel.bind(endpoint, LISTEN_BACKLOG); serverChannel.bind(endpoint, LISTEN_BACKLOG);
serverSelectionKey = serverChannel.register(channelSelector, SelectionKey.OP_ACCEPT); serverSelectionKey = serverChannel.register(channelSelector, SelectionKey.OP_ACCEPT);
this.bindAddress = bindAddress; // Store the selected address, so that it can be used by other parts of the app
break; // We don't want to bind to more than one address
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
LOGGER.error("Can't bind listen socket to address {}", Settings.getInstance().getBindAddress()); LOGGER.error("Can't bind listen socket to address {}", Settings.getInstance().getBindAddress());
if (i == bindAddresses.size()-1) { // Only throw an exception if all addresses have been tried
throw new IOException("Can't bind listen socket to address", e); throw new IOException("Can't bind listen socket to address", e);
}
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Can't create listen socket: {}", e.getMessage()); LOGGER.error("Can't create listen socket: {}", e.getMessage());
if (i == bindAddresses.size()-1) { // Only throw an exception if all addresses have been tried
throw new IOException("Can't create listen socket", e); throw new IOException("Can't create listen socket", e);
} }
}
}
// Load all known peers from repository // Load all known peers from repository
synchronized (this.allKnownPeers) { synchronized (this.allKnownPeers) {
@ -228,6 +248,10 @@ public class Network {
return this.maxPeers; return this.maxPeers;
} }
public String getBindAddress() {
return this.bindAddress;
}
public byte[] getMessageMagic() { public byte[] getMessageMagic() {
return Settings.getInstance().isTestNet() ? TESTNET_MESSAGE_MAGIC : MAINNET_MESSAGE_MAGIC; return Settings.getInstance().isTestNet() ? TESTNET_MESSAGE_MAGIC : MAINNET_MESSAGE_MAGIC;
} }
@ -1556,7 +1580,7 @@ public class Network {
this.isShuttingDown = true; this.isShuttingDown = true;
// Close listen socket to prevent more incoming connections // Close listen socket to prevent more incoming connections
if (this.serverChannel.isOpen()) { if (this.serverChannel != null && this.serverChannel.isOpen()) {
try { try {
this.serverChannel.close(); this.serverChannel.close();
} catch (IOException e) { } catch (IOException e) {

5
src/main/java/org/qortal/settings/Settings.java

@ -61,6 +61,7 @@ public class Settings {
// Common to all networking (API/P2P) // Common to all networking (API/P2P)
private String bindAddress = "::"; // Use IPv6 wildcard to listen on all local addresses private String bindAddress = "::"; // Use IPv6 wildcard to listen on all local addresses
private String bindAddressFallback = "0.0.0.0"; // Some systems are unable to bind using IPv6
// UI servers // UI servers
private int uiPort = 12388; private int uiPort = 12388;
@ -684,6 +685,10 @@ public class Settings {
return this.bindAddress; return this.bindAddress;
} }
public String getBindAddressFallback() {
return this.bindAddressFallback;
}
public boolean isUPnPEnabled() { public boolean isUPnPEnabled() {
return this.uPnPEnabled; return this.uPnPEnabled;
} }

Loading…
Cancel
Save