mirror of https://github.com/zlatinb/muwire
fix connecting to only trusted contacts
parent
5a9181f22d
commit
3c1fbf1953
|
@ -41,6 +41,8 @@ class H2HostCache extends HostCache {
|
|||
|
||||
@Override
|
||||
protected synchronized void hostDiscovered(Destination d, boolean fromHostcache) {
|
||||
if (!allowHost(d))
|
||||
return
|
||||
// overwrite MC with optimistic values
|
||||
if (fromHostcache) {
|
||||
sql.execute("delete from HOST_ATTEMPTS where DESTINATION=${d.toBase64()}")
|
||||
|
@ -199,7 +201,7 @@ class H2HostCache extends HostCache {
|
|||
Iterator<Destination> verifyIter = toVerify.iterator()
|
||||
while((rv.size() < n) && (verifyIter.hasNext())) {
|
||||
def d = verifyIter.next()
|
||||
if (filter.test(d))
|
||||
if (filter.test(d) && allowHost(d))
|
||||
rv.add(d)
|
||||
verifyIter.remove()
|
||||
}
|
||||
|
@ -210,7 +212,7 @@ class H2HostCache extends HostCache {
|
|||
return rv
|
||||
|
||||
List<Destination> canTry = new ArrayList<>(allHosts)
|
||||
canTry.retainAll { !profiles.get(it).isHopeless() && filter.test(it)}
|
||||
canTry.retainAll { !profiles.get(it).isHopeless() && filter.test(it) && allowHost(it)}
|
||||
Set<Destination> wouldFail = new HashSet<>()
|
||||
while(rv.size() < n && wouldFail.size() < canTry.size()) {
|
||||
Destination d = canTry.get((int)(Math.random() * canTry.size()))
|
||||
|
@ -234,7 +236,7 @@ class H2HostCache extends HostCache {
|
|||
@Override
|
||||
public synchronized List<Destination> getGoodHosts(int n) {
|
||||
List<Destination> rv = new ArrayList<>(allHosts)
|
||||
rv.retainAll { profiles.get(it).shouldAdvertise() }
|
||||
rv.retainAll { profiles.get(it).shouldAdvertise() && allowHost(it)}
|
||||
if (rv.size() <= n)
|
||||
return rv
|
||||
Collections.shuffle(rv)
|
||||
|
@ -326,6 +328,8 @@ class H2HostCache extends HostCache {
|
|||
log.info("loading hosts from db")
|
||||
sql.eachRow("select distinct DESTINATION from HOST_ATTEMPTS") {
|
||||
Destination dest = new Destination(it.DESTINATION)
|
||||
if (!allowHost(dest))
|
||||
return
|
||||
if (uniqueHosts.add(dest)) {
|
||||
def fromDB = sql.firstRow("select * from HOST_PROFILES where DESTINATION=${dest.toBase64()}")
|
||||
def profile = new HostMCProfile()
|
||||
|
|
|
@ -27,10 +27,10 @@ abstract class HostCache extends Service {
|
|||
this.myself = myself
|
||||
}
|
||||
|
||||
protected final boolean allowHost(Host host) {
|
||||
if (host.destination == myself)
|
||||
protected final boolean allowHost(Destination host) {
|
||||
if (host == myself)
|
||||
return false
|
||||
TrustLevel trust = trustService.getLevel(host.destination)
|
||||
TrustLevel trust = trustService.getLevel(host)
|
||||
switch(trust) {
|
||||
case TrustLevel.DISTRUSTED :
|
||||
return false
|
||||
|
|
|
@ -55,7 +55,7 @@ class SimpleHostCache extends HostCache {
|
|||
}
|
||||
Host host = new Host(destination, settings.hostClearInterval, settings.hostHopelessInterval,
|
||||
settings.hostRejectInterval, settings.hostHopelessPurgeInterval)
|
||||
if (allowHost(host)) {
|
||||
if (allowHost(host.destination)) {
|
||||
hosts.put(destination, host)
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ class SimpleHostCache extends HostCache {
|
|||
|
||||
synchronized(hosts) {
|
||||
rv = new ArrayList<>(hosts.keySet())
|
||||
rv.retainAll {allowHost(hosts[it])}
|
||||
rv.retainAll {allowHost(it)}
|
||||
final long now = System.currentTimeMillis()
|
||||
rv.removeAll {
|
||||
def h = hosts[it];
|
||||
|
@ -108,7 +108,7 @@ class SimpleHostCache extends HostCache {
|
|||
rv = new ArrayList<>(hosts.keySet())
|
||||
rv.retainAll {
|
||||
Host host = hosts[it]
|
||||
allowHost(host) && host.hasSucceeded()
|
||||
allowHost(it) && host.hasSucceeded()
|
||||
}
|
||||
}
|
||||
if (rv.size() <= n)
|
||||
|
@ -162,7 +162,7 @@ class SimpleHostCache extends HostCache {
|
|||
host.lastSuccessfulAttempt = entry.lastSuccessfulAttempt
|
||||
if (entry.lastRejection != null)
|
||||
host.lastRejection = entry.lastRejection
|
||||
if (allowHost(host))
|
||||
if (allowHost(dest))
|
||||
hosts.put(dest, host)
|
||||
}
|
||||
}
|
||||
|
@ -180,7 +180,7 @@ class SimpleHostCache extends HostCache {
|
|||
storage.delete()
|
||||
storage.withPrintWriter { writer ->
|
||||
copy.each { dest, host ->
|
||||
if (allowHost(host) && !host.isHopeless(now)) {
|
||||
if (allowHost(dest) && !host.isHopeless(now)) {
|
||||
def map = [:]
|
||||
map.destination = dest.toBase64()
|
||||
map.failures = host.failures
|
||||
|
|
Loading…
Reference in New Issue