fix connecting to only trusted contacts

auto-update
Zlatin Balevsky 2021-10-10 07:51:10 +01:00
parent 5a9181f22d
commit 3c1fbf1953
No known key found for this signature in database
GPG Key ID: A72832072D525E41
3 changed files with 15 additions and 11 deletions

View File

@ -41,6 +41,8 @@ class H2HostCache extends HostCache {
@Override @Override
protected synchronized void hostDiscovered(Destination d, boolean fromHostcache) { protected synchronized void hostDiscovered(Destination d, boolean fromHostcache) {
if (!allowHost(d))
return
// overwrite MC with optimistic values // overwrite MC with optimistic values
if (fromHostcache) { if (fromHostcache) {
sql.execute("delete from HOST_ATTEMPTS where DESTINATION=${d.toBase64()}") sql.execute("delete from HOST_ATTEMPTS where DESTINATION=${d.toBase64()}")
@ -199,7 +201,7 @@ class H2HostCache extends HostCache {
Iterator<Destination> verifyIter = toVerify.iterator() Iterator<Destination> verifyIter = toVerify.iterator()
while((rv.size() < n) && (verifyIter.hasNext())) { while((rv.size() < n) && (verifyIter.hasNext())) {
def d = verifyIter.next() def d = verifyIter.next()
if (filter.test(d)) if (filter.test(d) && allowHost(d))
rv.add(d) rv.add(d)
verifyIter.remove() verifyIter.remove()
} }
@ -210,7 +212,7 @@ class H2HostCache extends HostCache {
return rv return rv
List<Destination> canTry = new ArrayList<>(allHosts) 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<>() Set<Destination> wouldFail = new HashSet<>()
while(rv.size() < n && wouldFail.size() < canTry.size()) { while(rv.size() < n && wouldFail.size() < canTry.size()) {
Destination d = canTry.get((int)(Math.random() * canTry.size())) Destination d = canTry.get((int)(Math.random() * canTry.size()))
@ -234,7 +236,7 @@ class H2HostCache extends HostCache {
@Override @Override
public synchronized List<Destination> getGoodHosts(int n) { public synchronized List<Destination> getGoodHosts(int n) {
List<Destination> rv = new ArrayList<>(allHosts) List<Destination> rv = new ArrayList<>(allHosts)
rv.retainAll { profiles.get(it).shouldAdvertise() } rv.retainAll { profiles.get(it).shouldAdvertise() && allowHost(it)}
if (rv.size() <= n) if (rv.size() <= n)
return rv return rv
Collections.shuffle(rv) Collections.shuffle(rv)
@ -326,6 +328,8 @@ class H2HostCache extends HostCache {
log.info("loading hosts from db") log.info("loading hosts from db")
sql.eachRow("select distinct DESTINATION from HOST_ATTEMPTS") { sql.eachRow("select distinct DESTINATION from HOST_ATTEMPTS") {
Destination dest = new Destination(it.DESTINATION) Destination dest = new Destination(it.DESTINATION)
if (!allowHost(dest))
return
if (uniqueHosts.add(dest)) { if (uniqueHosts.add(dest)) {
def fromDB = sql.firstRow("select * from HOST_PROFILES where DESTINATION=${dest.toBase64()}") def fromDB = sql.firstRow("select * from HOST_PROFILES where DESTINATION=${dest.toBase64()}")
def profile = new HostMCProfile() def profile = new HostMCProfile()

View File

@ -27,10 +27,10 @@ abstract class HostCache extends Service {
this.myself = myself this.myself = myself
} }
protected final boolean allowHost(Host host) { protected final boolean allowHost(Destination host) {
if (host.destination == myself) if (host == myself)
return false return false
TrustLevel trust = trustService.getLevel(host.destination) TrustLevel trust = trustService.getLevel(host)
switch(trust) { switch(trust) {
case TrustLevel.DISTRUSTED : case TrustLevel.DISTRUSTED :
return false return false

View File

@ -55,7 +55,7 @@ class SimpleHostCache extends HostCache {
} }
Host host = new Host(destination, settings.hostClearInterval, settings.hostHopelessInterval, Host host = new Host(destination, settings.hostClearInterval, settings.hostHopelessInterval,
settings.hostRejectInterval, settings.hostHopelessPurgeInterval) settings.hostRejectInterval, settings.hostHopelessPurgeInterval)
if (allowHost(host)) { if (allowHost(host.destination)) {
hosts.put(destination, host) hosts.put(destination, host)
} }
} }
@ -89,7 +89,7 @@ class SimpleHostCache extends HostCache {
synchronized(hosts) { synchronized(hosts) {
rv = new ArrayList<>(hosts.keySet()) rv = new ArrayList<>(hosts.keySet())
rv.retainAll {allowHost(hosts[it])} rv.retainAll {allowHost(it)}
final long now = System.currentTimeMillis() final long now = System.currentTimeMillis()
rv.removeAll { rv.removeAll {
def h = hosts[it]; def h = hosts[it];
@ -108,7 +108,7 @@ class SimpleHostCache extends HostCache {
rv = new ArrayList<>(hosts.keySet()) rv = new ArrayList<>(hosts.keySet())
rv.retainAll { rv.retainAll {
Host host = hosts[it] Host host = hosts[it]
allowHost(host) && host.hasSucceeded() allowHost(it) && host.hasSucceeded()
} }
} }
if (rv.size() <= n) if (rv.size() <= n)
@ -162,7 +162,7 @@ class SimpleHostCache extends HostCache {
host.lastSuccessfulAttempt = entry.lastSuccessfulAttempt host.lastSuccessfulAttempt = entry.lastSuccessfulAttempt
if (entry.lastRejection != null) if (entry.lastRejection != null)
host.lastRejection = entry.lastRejection host.lastRejection = entry.lastRejection
if (allowHost(host)) if (allowHost(dest))
hosts.put(dest, host) hosts.put(dest, host)
} }
} }
@ -180,7 +180,7 @@ class SimpleHostCache extends HostCache {
storage.delete() storage.delete()
storage.withPrintWriter { writer -> storage.withPrintWriter { writer ->
copy.each { dest, host -> copy.each { dest, host ->
if (allowHost(host) && !host.isHopeless(now)) { if (allowHost(dest) && !host.isHopeless(now)) {
def map = [:] def map = [:]
map.destination = dest.toBase64() map.destination = dest.toBase64()
map.failures = host.failures map.failures = host.failures