diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index e795da0e..5abd5309 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -120,8 +120,8 @@ public class Core { eventBus = new EventBus() log.info("initializing trust service") - File goodTrust = new File(home, "trust.good") - File badTrust = new File(home, "trust.bad") + File goodTrust = new File(home, "trusted") + File badTrust = new File(home, "distrusted") trustService = new TrustService(goodTrust, badTrust, 5000) eventBus.register(TrustEvent.class, trustService) diff --git a/core/src/main/groovy/com/muwire/core/Persona.groovy b/core/src/main/groovy/com/muwire/core/Persona.groovy index 5b38d24b..5b7ff7ef 100644 --- a/core/src/main/groovy/com/muwire/core/Persona.groovy +++ b/core/src/main/groovy/com/muwire/core/Persona.groovy @@ -2,6 +2,7 @@ package com.muwire.core import net.i2p.crypto.DSAEngine import net.i2p.crypto.SigType +import net.i2p.data.Base64 import net.i2p.data.Destination import net.i2p.data.Signature import net.i2p.data.SigningPublicKey @@ -14,6 +15,7 @@ public class Persona { private final Destination destination private final byte[] sig private volatile String humanReadableName + private volatile String base64 private volatile byte[] payload public Persona(InputStream personaStream) throws IOException, InvalidSignatureException { @@ -59,6 +61,15 @@ public class Persona { humanReadableName } + public String toBase64() { + if (base64 == null) { + def baos = new ByteArrayOutputStream() + write(baos) + base64 = Base64.encode(baos.toByteArray()) + } + base64 + } + @Override public int hashCode() { name.hashCode() ^ destination.hashCode() 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 ae9816cf..1e6225fc 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy @@ -40,7 +40,7 @@ abstract class ConnectionManager { void onTrustEvent(TrustEvent e) { if (e.level == TrustLevel.DISTRUSTED) - drop(e.destination) + drop(e.persona.destination) } abstract void drop(Destination d) diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy index bb9e1554..d5118a0f 100644 --- a/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy @@ -1,11 +1,10 @@ package com.muwire.core.trust import com.muwire.core.Event - -import net.i2p.data.Destination +import com.muwire.core.Persona class TrustEvent extends Event { - Destination destination + Persona persona TrustLevel level } diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy index 6a4dd5a5..0f7c4b70 100644 --- a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy +++ b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy @@ -1,7 +1,11 @@ package com.muwire.core.trust +import java.util.concurrent.ConcurrentHashMap + +import com.muwire.core.Persona import com.muwire.core.Service +import net.i2p.data.Base64 import net.i2p.data.Destination import net.i2p.util.ConcurrentHashSet @@ -10,8 +14,8 @@ class TrustService extends Service { final File persistGood, persistBad final long persistInterval - final Set good = new ConcurrentHashSet<>() - final Set bad = new ConcurrentHashSet<>() + final Map good = new ConcurrentHashMap<>() + final Map bad = new ConcurrentHashMap<>() final Timer timer @@ -35,12 +39,16 @@ class TrustService extends Service { void load() { if (persistGood.exists()) { persistGood.eachLine { - good.add(new Destination(it)) + byte [] decoded = Base64.decode(it) + Persona persona = new Persona(new ByteArrayInputStream(decoded)) + good.put(persona.destination, persona) } } if (persistBad.exists()) { persistBad.eachLine { - bad.add(new Destination(it)) + byte [] decoded = Base64.decode(it) + Persona persona = new Persona(new ByteArrayInputStream(decoded)) + bad.put(persona.destination, persona) } } timer.schedule({persist()} as TimerTask, persistInterval, persistInterval) @@ -50,22 +58,22 @@ class TrustService extends Service { private void persist() { persistGood.delete() persistGood.withPrintWriter { writer -> - good.each { - writer.println it.toBase64() + good.each {k,v -> + writer.println v.toBase64() } } persistBad.delete() persistBad.withPrintWriter { writer -> - bad.each { - writer.println it.toBase64() + bad.each { k,v -> + writer.println v.toBase64() } } } TrustLevel getLevel(Destination dest) { - if (good.contains(dest)) + if (good.containsKey(dest)) return TrustLevel.TRUSTED - else if (bad.contains(dest)) + else if (bad.containsKey(dest)) return TrustLevel.DISTRUSTED TrustLevel.NEUTRAL } @@ -73,16 +81,16 @@ class TrustService extends Service { void onTrustEvent(TrustEvent e) { switch(e.level) { case TrustLevel.TRUSTED: - bad.remove(e.destination) - good.add(e.destination) + bad.remove(e.persona.destination) + good.put(e.persona.destination, e.persona) break case TrustLevel.DISTRUSTED: - good.remove(e.destination) - bad.add(e.destination) + good.remove(e.persona.destination) + bad.put(e.persona.destination, e.persona) break case TrustLevel.NEUTRAL: - good.remove(e.destination) - bad.remove(e.destination) + good.remove(e.persona.destination) + bad.remove(e.persona.destination) break } } diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index ae46d427..e10b5ebb 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -79,7 +79,7 @@ class MainFrameController { def result = selectedResult() if (result == null) return // TODO disable button - core.eventBus.publish( new TrustEvent(destination : result.sender.destination, level : TrustLevel.TRUSTED)) + core.eventBus.publish( new TrustEvent(persona : result.sender, level : TrustLevel.TRUSTED)) } @ControllerAction @@ -87,7 +87,7 @@ class MainFrameController { def result = selectedResult() if (result == null) return // TODO disable button - core.eventBus.publish( new TrustEvent(destination : result.sender.destination, level : TrustLevel.DISTRUSTED)) + core.eventBus.publish( new TrustEvent(persona : result.sender, level : TrustLevel.DISTRUSTED)) } @ControllerAction