diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 6275e765..f804b034 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -159,7 +159,8 @@ public class Core { log.info("initializing connection manager") connectionManager = props.isLeaf() ? - new LeafConnectionManager(eventBus, me, 3, hostCache) : new UltrapeerConnectionManager(eventBus, me, 512, 512, hostCache, trustService) + new LeafConnectionManager(eventBus, me, 3, hostCache, props) : + new UltrapeerConnectionManager(eventBus, me, 512, 512, hostCache, trustService, props) eventBus.register(TrustEvent.class, connectionManager) eventBus.register(ConnectionEvent.class, connectionManager) eventBus.register(DisconnectionEvent.class, connectionManager) diff --git a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy index a54c79b2..ef86693a 100644 --- a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy @@ -6,6 +6,7 @@ import java.util.concurrent.atomic.AtomicBoolean import java.util.logging.Level import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings import com.muwire.core.Persona import com.muwire.core.hostcache.HostCache import com.muwire.core.hostcache.HostDiscoveredEvent @@ -26,7 +27,8 @@ abstract class Connection implements Closeable { final boolean incoming final HostCache hostCache final TrustService trustService - + final MuWireSettings settings + private final AtomicBoolean running = new AtomicBoolean() private final BlockingQueue messages = new LinkedBlockingQueue() private final Thread reader, writer @@ -35,12 +37,14 @@ abstract class Connection implements Closeable { long lastPingSentTime, lastPongReceivedTime - Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, HostCache hostCache, TrustService trustService) { + Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, + HostCache hostCache, TrustService trustService, MuWireSettings settings) { this.eventBus = eventBus this.incoming = incoming this.endpoint = endpoint this.hostCache = hostCache this.trustService = trustService + this.settings = settings this.name = endpoint.destination.toBase32().substring(0,8) @@ -155,11 +159,15 @@ abstract class Connection implements Closeable { search.keywords = null Destination replyTo = new Destination(search.replyTo) - if (trustService.getLevel(replyTo) == TrustLevel.DISTRUSTED) { + TrustLevel trustLevel = trustService.getLevel(replyTo) + if (trustLevel == TrustLevel.DISTRUSTED) { log.info "dropping search from distrusted peer" return } - // TODO: add option to respond only to trusted peers + if (trustLevel == TrustLevel.NEUTRAL && !settings.allowUntrusted()) { + log.info("dropping search from neutral peer") + return + } Persona originator = null if (search.originator != null) { diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy index 1e6225fc..d11e7c7e 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy @@ -1,6 +1,7 @@ package com.muwire.core.connection import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings import com.muwire.core.Persona import com.muwire.core.hostcache.HostCache import com.muwire.core.search.QueryEvent @@ -19,13 +20,15 @@ abstract class ConnectionManager { protected final HostCache hostCache protected final Persona me + protected final MuWireSettings settings ConnectionManager() {} - ConnectionManager(EventBus eventBus, Persona me, HostCache hostCache) { + ConnectionManager(EventBus eventBus, Persona me, HostCache hostCache, MuWireSettings settings) { this.eventBus = eventBus this.me = me this.hostCache = hostCache + this.settings = settings this.timer = new Timer("connections-pinger",true) } diff --git a/core/src/main/groovy/com/muwire/core/connection/LeafConnection.groovy b/core/src/main/groovy/com/muwire/core/connection/LeafConnection.groovy index 0d18d1eb..b9497f76 100644 --- a/core/src/main/groovy/com/muwire/core/connection/LeafConnection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/LeafConnection.groovy @@ -4,6 +4,7 @@ import java.io.InputStream import java.io.OutputStream import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings import com.muwire.core.hostcache.HostCache import com.muwire.core.trust.TrustService @@ -16,8 +17,9 @@ import net.i2p.data.Destination */ class LeafConnection extends Connection { - public LeafConnection(EventBus eventBus, Endpoint endpoint, HostCache hostCache, TrustService trustService) { - super(eventBus, endpoint, true, hostCache, trustService); + public LeafConnection(EventBus eventBus, Endpoint endpoint, HostCache hostCache, + TrustService trustService, MuWireSettings settings) { + super(eventBus, endpoint, true, hostCache, trustService, settings); } @Override diff --git a/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy index 4395e255..fb913491 100644 --- a/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy @@ -3,6 +3,7 @@ package com.muwire.core.connection import java.util.concurrent.ConcurrentHashMap import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings import com.muwire.core.Persona import com.muwire.core.hostcache.HostCache import com.muwire.core.search.QueryEvent @@ -17,8 +18,9 @@ class LeafConnectionManager extends ConnectionManager { final Map connections = new ConcurrentHashMap() - public LeafConnectionManager(EventBus eventBus, Persona me, int maxConnections, HostCache hostCache) { - super(eventBus, me, hostCache) + public LeafConnectionManager(EventBus eventBus, Persona me, int maxConnections, + HostCache hostCache, MuWireSettings settings) { + super(eventBus, me, hostCache, settings) this.maxConnections = maxConnections } diff --git a/core/src/main/groovy/com/muwire/core/connection/PeerConnection.groovy b/core/src/main/groovy/com/muwire/core/connection/PeerConnection.groovy index c787f946..90ce9711 100644 --- a/core/src/main/groovy/com/muwire/core/connection/PeerConnection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/PeerConnection.groovy @@ -4,6 +4,7 @@ import java.io.InputStream import java.io.OutputStream import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings import com.muwire.core.hostcache.HostCache import com.muwire.core.trust.TrustService import com.muwire.core.util.DataUtil @@ -29,8 +30,9 @@ class PeerConnection extends Connection { private final JsonSlurper slurper = new JsonSlurper() public PeerConnection(EventBus eventBus, Endpoint endpoint, - boolean incoming, HostCache hostCache, TrustService trustService) { - super(eventBus, endpoint, incoming, hostCache, trustService) + boolean incoming, HostCache hostCache, TrustService trustService, + MuWireSettings settings) { + super(eventBus, endpoint, incoming, hostCache, trustService, settings) this.dis = new DataInputStream(endpoint.inputStream) this.dos = new DataOutputStream(endpoint.outputStream) } diff --git a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy index d6306560..2d51598e 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy @@ -4,6 +4,7 @@ import java.util.Collection import java.util.concurrent.ConcurrentHashMap import com.muwire.core.EventBus +import com.muwire.core.MuWireSettings import com.muwire.core.Persona import com.muwire.core.hostcache.HostCache import com.muwire.core.search.QueryEvent @@ -17,15 +18,15 @@ class UltrapeerConnectionManager extends ConnectionManager { final int maxPeers, maxLeafs final TrustService trustService - + final Map peerConnections = new ConcurrentHashMap() final Map leafConnections = new ConcurrentHashMap() UltrapeerConnectionManager() {} public UltrapeerConnectionManager(EventBus eventBus, Persona me, int maxPeers, int maxLeafs, - HostCache hostCache, TrustService trustService) { - super(eventBus, me, hostCache) + HostCache hostCache, TrustService trustService, MuWireSettings settings) { + super(eventBus, me, hostCache, settings) this.maxPeers = maxPeers this.maxLeafs = maxLeafs this.trustService = trustService @@ -85,8 +86,8 @@ class UltrapeerConnectionManager extends ConnectionManager { return Connection c = e.leaf ? - new LeafConnection(eventBus, e.endpoint, hostCache, trustService) : - new PeerConnection(eventBus, e.endpoint, e.incoming, hostCache, trustService) + new LeafConnection(eventBus, e.endpoint, hostCache, trustService, settings) : + new PeerConnection(eventBus, e.endpoint, e.incoming, hostCache, trustService, settings) def map = e.leaf ? leafConnections : peerConnections map.put(e.endpoint.destination, c) c.start()