do not respond to queries by distrusted personas

pull/4/head
Zlatin Balevsky 2019-05-24 22:33:28 +01:00
parent 82b9d5054d
commit f4dab915f4
6 changed files with 29 additions and 11 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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<Destination, PeerConnection> peerConnections = new ConcurrentHashMap()
final Map<Destination, LeafConnection> 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()