diff --git a/src/main/java/org/qortal/network/Network.java b/src/main/java/org/qortal/network/Network.java index 0ef7d5a0..772a96d5 100644 --- a/src/main/java/org/qortal/network/Network.java +++ b/src/main/java/org/qortal/network/Network.java @@ -150,9 +150,23 @@ public class Network { } // Load all known peers from repository - try (Repository repository = RepositoryManager.getRepository()) { - synchronized (this.allKnownPeers) { - this.allKnownPeers.addAll(repository.getNetworkRepository().getAllPeers()); + synchronized (this.allKnownPeers) { List fixedNetwork = Settings.getInstance().getFixedNetwork(); + if (fixedNetwork != null && !fixedNetwork.isEmpty()) { + Long addedWhen = NTP.getTime(); + String addedBy = "fixedNetwork"; + List peerAddresses = new ArrayList<>(); + for (String address : fixedNetwork) { + PeerAddress peerAddress = PeerAddress.fromString(address); + peerAddresses.add(peerAddress); + } + List peers = peerAddresses.stream() + .map(peerAddress -> new PeerData(peerAddress, addedWhen, addedBy)) + .collect(Collectors.toList()); + this.allKnownPeers.addAll(peers); + } else { + try (Repository repository = RepositoryManager.getRepository()) { + this.allKnownPeers.addAll(repository.getNetworkRepository().getAllPeers()); + } } } @@ -508,14 +522,24 @@ public class Network { if (socketChannel == null) { return; } + PeerAddress address = PeerAddress.fromSocket(socketChannel.socket()); + List fixedNetwork = Settings.getInstance().getFixedNetwork(); + if (fixedNetwork != null && !fixedNetwork.isEmpty() && ipNotInFixedList(address, fixedNetwork)) { + try { + LOGGER.debug("Connection discarded from peer {} as not in the fixed network list", address); + socketChannel.close(); + } catch (IOException e) { + // IGNORE + } + return; + } final Long now = NTP.getTime(); Peer newPeer; try { if (now == null) { - LOGGER.debug("Connection discarded from peer {} due to lack of NTP sync", - PeerAddress.fromSocket(socketChannel.socket())); + LOGGER.debug("Connection discarded from peer {} due to lack of NTP sync", address); socketChannel.close(); return; } @@ -523,12 +547,12 @@ public class Network { synchronized (this.connectedPeers) { if (connectedPeers.size() >= maxPeers) { // We have enough peers - LOGGER.debug("Connection discarded from peer {}", PeerAddress.fromSocket(socketChannel.socket())); + LOGGER.debug("Connection discarded from peer {} because the server is full", address); socketChannel.close(); return; } - LOGGER.debug("Connection accepted from peer {}", PeerAddress.fromSocket(socketChannel.socket())); + LOGGER.debug("Connection accepted from peer {}", address); newPeer = new Peer(socketChannel, channelSelector); this.connectedPeers.add(newPeer); @@ -536,6 +560,7 @@ public class Network { } catch (IOException e) { if (socketChannel.isOpen()) { try { + LOGGER.debug("Connection failed from peer {} while connecting/closing", address); socketChannel.close(); } catch (IOException ce) { // Couldn't close? @@ -547,6 +572,16 @@ public class Network { this.onPeerReady(newPeer); } + private boolean ipNotInFixedList(PeerAddress address, List fixedNetwork) { + for (String ipAddress : fixedNetwork) { + String[] bits = ipAddress.split(":"); + if (bits.length >= 1 && bits.length <= 2 && address.getHost().equals(bits[0])) { + return false; + } + } + return true; + } + private Peer getConnectablePeer(final Long now) throws InterruptedException { // We can't block here so use tryRepository(). We don't NEED to connect a new peer. try (Repository repository = RepositoryManager.tryRepository()) { @@ -1115,6 +1150,10 @@ public class Network { private boolean mergePeers(Repository repository, String addedBy, long addedWhen, List peerAddresses) throws DataException { + List fixedNetwork = Settings.getInstance().getFixedNetwork(); + if (fixedNetwork != null && !fixedNetwork.isEmpty()) { + return false; + } List newPeers; synchronized (this.allKnownPeers) { for (PeerData knownPeerData : this.allKnownPeers) { diff --git a/src/main/java/org/qortal/settings/Settings.java b/src/main/java/org/qortal/settings/Settings.java index 164223c9..1c3a8ce4 100644 --- a/src/main/java/org/qortal/settings/Settings.java +++ b/src/main/java/org/qortal/settings/Settings.java @@ -5,6 +5,7 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; +import java.util.List; import java.util.Locale; import javax.xml.bind.JAXBContext; @@ -147,6 +148,7 @@ public class Settings { private String repositoryPath = "db"; /** Repository connection pool size. Needs to be a bit bigger than maxNetworkThreadPoolSize */ private int repositoryConnectionPoolSize = 100; + private List fixedNetwork; // Auto-update sources private String[] autoUpdateRepos = new String[] { @@ -507,4 +509,7 @@ public class Settings { return this.onlineSignaturesTrimBatchSize; } + public List getFixedNetwork() { + return fixedNetwork; + } }