diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 9d79faff..c7f4f239 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -137,7 +137,7 @@ class Core { log.info("initializing connection manager") ConnectionManager connectionManager = props.isLeaf() ? - new LeafConnectionManager(eventBus,3, hostCache) : new UltrapeerConnectionManager(eventBus, 512, 512, hostCache) + new LeafConnectionManager(eventBus,3, hostCache) : new UltrapeerConnectionManager(eventBus, 512, 512, hostCache, trustService) 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 e990e8de..1f0b0155 100644 --- a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy @@ -10,6 +10,8 @@ import com.muwire.core.hostcache.HostCache import com.muwire.core.hostcache.HostDiscoveredEvent import com.muwire.core.search.QueryEvent import com.muwire.core.search.SearchEvent +import com.muwire.core.trust.TrustLevel +import com.muwire.core.trust.TrustService import groovy.util.logging.Log import net.i2p.data.Destination @@ -21,6 +23,7 @@ abstract class Connection implements Closeable { final Endpoint endpoint final boolean incoming final HostCache hostCache + final TrustService trustService private final AtomicBoolean running = new AtomicBoolean() private final BlockingQueue messages = new LinkedBlockingQueue() @@ -30,11 +33,12 @@ abstract class Connection implements Closeable { long lastPingSentTime, lastPongReceivedTime - Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, HostCache hostCache) { + Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, HostCache hostCache, TrustService trustService) { this.eventBus = eventBus this.incoming = incoming this.endpoint = endpoint this.hostCache = hostCache + this.trustService = trustService this.name = endpoint.destination.toBase32().substring(0,8) @@ -135,7 +139,14 @@ abstract class Connection implements Closeable { UUID uuid = UUID.fromString(search.uuid) if (search.infohash != null) search.keywords = null + Destination replyTo = new Destination(search.replyTo) + if (trustService.getLevel(replyTo) == TrustLevel.DISTRUSTED) { + log.info "dropping search from distrusted peer" + return + } + // TODO: add option to respond only to trusted peers + SearchEvent searchEvent = new SearchEvent(searchTerms : search.keywords, searchHash : search.infohash, uuid : uuid) 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 f349df24..0d18d1eb 100644 --- a/core/src/main/groovy/com/muwire/core/connection/LeafConnection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/LeafConnection.groovy @@ -5,6 +5,7 @@ import java.io.OutputStream import com.muwire.core.EventBus import com.muwire.core.hostcache.HostCache +import com.muwire.core.trust.TrustService import net.i2p.data.Destination @@ -15,8 +16,8 @@ import net.i2p.data.Destination */ class LeafConnection extends Connection { - public LeafConnection(EventBus eventBus, Endpoint endpoint, HostCache hostCache) { - super(eventBus, endpoint, true, hostCache); + public LeafConnection(EventBus eventBus, Endpoint endpoint, HostCache hostCache, TrustService trustService) { + super(eventBus, endpoint, true, hostCache, trustService); } @Override 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 df97a84d..c787f946 100644 --- a/core/src/main/groovy/com/muwire/core/connection/PeerConnection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/PeerConnection.groovy @@ -5,6 +5,7 @@ import java.io.OutputStream import com.muwire.core.EventBus import com.muwire.core.hostcache.HostCache +import com.muwire.core.trust.TrustService import com.muwire.core.util.DataUtil import groovy.json.JsonOutput @@ -28,8 +29,8 @@ class PeerConnection extends Connection { private final JsonSlurper slurper = new JsonSlurper() public PeerConnection(EventBus eventBus, Endpoint endpoint, - boolean incoming, HostCache hostCache) { - super(eventBus, endpoint, incoming, hostCache) + boolean incoming, HostCache hostCache, TrustService trustService) { + super(eventBus, endpoint, incoming, hostCache, trustService) this.dis = new DataInputStream(endpoint.inputStream) this.dos = new DataOutputStream(endpoint.outputStream) } diff --git a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnection.groovy b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnection.groovy index 5dc063f0..9a45525f 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnection.groovy @@ -5,6 +5,7 @@ import java.io.OutputStream import com.muwire.core.EventBus import com.muwire.core.hostcache.HostCache +import com.muwire.core.trust.TrustService import net.i2p.data.Destination @@ -16,8 +17,8 @@ import net.i2p.data.Destination */ class UltrapeerConnection extends Connection { - public UltrapeerConnection(EventBus eventBus, Endpoint endpoint, HostCache hostCache) { - super(eventBus, endpoint, false, hostCache) + public UltrapeerConnection(EventBus eventBus, Endpoint endpoint, HostCache hostCache, TrustService trustService) { + super(eventBus, endpoint, false, hostCache, trustService) } @Override 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 64542ee4..9acb0d13 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy @@ -6,6 +6,7 @@ import java.util.concurrent.ConcurrentHashMap import com.muwire.core.EventBus import com.muwire.core.hostcache.HostCache import com.muwire.core.search.QueryEvent +import com.muwire.core.trust.TrustService import groovy.util.logging.Log import net.i2p.data.Destination @@ -14,16 +15,19 @@ import net.i2p.data.Destination 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, int maxPeers, int maxLeafs, HostCache hostCache) { + public UltrapeerConnectionManager(EventBus eventBus, int maxPeers, int maxLeafs, + HostCache hostCache, TrustService trustService) { super(eventBus, hostCache) this.maxPeers = maxPeers this.maxLeafs = maxLeafs + this.trustService = trustService } @Override public void drop(Destination d) { @@ -67,8 +71,8 @@ class UltrapeerConnectionManager extends ConnectionManager { return Connection c = e.leaf ? - new LeafConnection(eventBus, e.endpoint, hostCache) : - new PeerConnection(eventBus, e.endpoint, e.incoming, hostCache) + new LeafConnection(eventBus, e.endpoint, hostCache, trustService) : + new PeerConnection(eventBus, e.endpoint, e.incoming, hostCache, trustService) def map = e.leaf ? leafConnections : peerConnections map.put(e.endpoint.destination, c) c.start()