From 1b629a8f0ade329389e9108d0c896c1cce6be291 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Wed, 25 Jul 2018 17:39:21 +0100 Subject: [PATCH] Initialize i2p session first on startup. Prevent own destination from ending up in host cache --- .../main/groovy/com/muwire/core/Core.groovy | 51 ++++++++++--------- .../muwire/core/hostcache/HostCache.groovy | 9 +++- .../core/hostcache/HostCacheTest.groovy | 3 +- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index c6724ae7..82cddd02 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -38,28 +38,6 @@ class Core { props = new MuWireSettings(props) - EventBus eventBus = new EventBus() - - log.info("initializing trust service") - File goodTrust = new File(home, "trust.good") - File badTrust = new File(home, "trust.bad") - TrustService trustService = new TrustService(goodTrust, badTrust, 5000) - eventBus.register(TrustEvent.class, trustService) - trustService.start() - trustService.waitForLoad() - - log.info("initializing host cache") - File hostStorage = new File(home, "hosts.json") - HostCache hostCache = new HostCache(trustService,hostStorage, 30000, props) - eventBus.register(HostDiscoveredEvent.class, hostCache) - eventBus.register(ConnectionEvent.class, hostCache) - hostCache.start() - hostCache.waitForLoad() - - log.info("initializing connection manager") - ConnectionManager connectionManager = props.isLeaf() ? - new LeafConnectionManager(eventBus,3) : new UltrapeerConnectionManager(eventBus, 512, 512) - eventBus.register(TrustEvent.class, connectionManager) log.info("initializing I2P session") def i2pClient = new I2PClientFactory().createClient() @@ -72,15 +50,38 @@ class Core { } def sysProps = System.getProperties().clone() - sysProps["inbound.nickname"] = "MuWire" - I2PSession i2pSession - keyDat.withInputStream { + sysProps["inbound.nickname"] = "MuWire" + I2PSession i2pSession + keyDat.withInputStream { i2pSession = i2pClient.createSession(it, sysProps) } log.info("connecting i2p session") i2pSession.connect() + EventBus eventBus = new EventBus() + + log.info("initializing trust service") + File goodTrust = new File(home, "trust.good") + File badTrust = new File(home, "trust.bad") + TrustService trustService = new TrustService(goodTrust, badTrust, 5000) + eventBus.register(TrustEvent.class, trustService) + trustService.start() + trustService.waitForLoad() + + log.info("initializing host cache") + File hostStorage = new File(home, "hosts.json") + HostCache hostCache = new HostCache(trustService,hostStorage, 30000, props, i2pSession.getMyDestination()) + eventBus.register(HostDiscoveredEvent.class, hostCache) + eventBus.register(ConnectionEvent.class, hostCache) + hostCache.start() + hostCache.waitForLoad() + + log.info("initializing connection manager") + ConnectionManager connectionManager = props.isLeaf() ? + new LeafConnectionManager(eventBus,3) : new UltrapeerConnectionManager(eventBus, 512, 512) + eventBus.register(TrustEvent.class, connectionManager) + log.info("initializing cache client") CacheClient cacheClient = new CacheClient(eventBus,hostCache, connectionManager, i2pSession, props, 10000) cacheClient.start() diff --git a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy index 0c9f0881..1fd9c852 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy @@ -20,13 +20,16 @@ class HostCache extends Service { final int interval final Timer timer final MuWireSettings settings + final Destination myself final Map hosts = new ConcurrentHashMap<>() - public HostCache(TrustService trustService, File storage, int interval, MuWireSettings settings) { + public HostCache(TrustService trustService, File storage, int interval, + MuWireSettings settings, Destination myself) { this.trustService = trustService this.storage = storage this.interval = interval this.settings = settings + this.myself = myself this.timer = new Timer("host-persister",true) } @@ -39,6 +42,8 @@ class HostCache extends Service { } void onHostDiscoveredEvent(HostDiscoveredEvent e) { + if (myself == e.destination) + return if (hosts.containsKey(e.destination)) return Host host = new Host(e.destination) @@ -93,6 +98,8 @@ class HostCache extends Service { private boolean allowHost(Host host) { if (host.isFailed()) return false + if (host.destination == myself) + return false TrustLevel trust = trustService.getLevel(host.destination) switch(trust) { case TrustLevel.DISTRUSTED : diff --git a/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy b/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy index 6274cd30..20c1c954 100644 --- a/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy +++ b/core/src/test/groovy/com/muwire/core/hostcache/HostCacheTest.groovy @@ -15,6 +15,7 @@ import groovy.json.JsonOutput import groovy.json.JsonSlurper import groovy.mock.interceptor.MockFor import groovy.mock.interceptor.StubFor +import net.i2p.data.Destination class HostCacheTest { @@ -50,7 +51,7 @@ class HostCacheTest { private void initMocks() { trust = trustMock.proxyInstance() settings = settingsMock.proxyInstance() - cache = new HostCache(trust, persist, 100, settings) + cache = new HostCache(trust, persist, 100, settings, new Destination()) cache.start() Thread.sleep(150) }