Skip to content

Commit

Permalink
Merge pull request bisq-network#2464 from freimair/seedNodeRepo_refac…
Browse files Browse the repository at this point in the history
…toring

Seed node repo refactoring
  • Loading branch information
ManfredKarrer authored Feb 27, 2019
2 parents 76e406a + 3711529 commit 2473067
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 589 deletions.
2 changes: 0 additions & 2 deletions core/src/main/java/bisq/core/CoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import bisq.core.dao.DaoModule;
import bisq.core.filter.FilterModule;
import bisq.core.network.p2p.seed.DefaultSeedNodeRepository;
import bisq.core.network.p2p.seed.SeedNodeAddressLookup;
import bisq.core.notifications.MobileMessageEncryption;
import bisq.core.notifications.MobileModel;
import bisq.core.notifications.MobileNotificationService;
Expand Down Expand Up @@ -99,7 +98,6 @@ protected void configure() {
bind(CorruptedDatabaseFilesHandler.class).in(Singleton.class);
bind(AvoidStandbyModeService.class).in(Singleton.class);

bind(SeedNodeAddressLookup.class).in(Singleton.class);
bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);

File storageDir = new File(environment.getRequiredProperty(Storage.STORAGE_DIR));
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/bisq/core/app/BisqEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -345,7 +346,7 @@ public BisqEnvironment(PropertySource commandLineProperties) {
bannedPriceRelayNodes = !bannedPriceRelayNodesAsString.isEmpty() ? Arrays.asList(StringUtils.deleteWhitespace(bannedPriceRelayNodesAsString).split(",")) : null;

final String bannedSeedNodesAsString = getProperty(FilterManager.BANNED_SEED_NODES, "");
bannedSeedNodes = !bannedSeedNodesAsString.isEmpty() ? Arrays.asList(StringUtils.deleteWhitespace(bannedSeedNodesAsString).split(",")) : null;
bannedSeedNodes = !bannedSeedNodesAsString.isEmpty() ? Arrays.asList(StringUtils.deleteWhitespace(bannedSeedNodesAsString).split(",")) : new ArrayList<>();

final String bannedBtcNodesAsString = getProperty(FilterManager.BANNED_BTC_NODES, "");
bannedBtcNodes = !bannedBtcNodesAsString.isEmpty() ? Arrays.asList(StringUtils.deleteWhitespace(bannedBtcNodesAsString).split(",")) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import bisq.core.dao.DaoModule;
import bisq.core.filter.FilterModule;
import bisq.core.network.p2p.seed.DefaultSeedNodeRepository;
import bisq.core.network.p2p.seed.SeedNodeAddressLookup;
import bisq.core.offer.OfferModule;
import bisq.core.proto.network.CoreNetworkProtoResolver;
import bisq.core.proto.persistable.CorePersistenceProtoResolver;
Expand Down Expand Up @@ -77,7 +76,6 @@ protected void configure() {
bind(BridgeAddressProvider.class).to(Preferences.class).in(Singleton.class);
bind(TorSetup.class).in(Singleton.class);

bind(SeedNodeAddressLookup.class).in(Singleton.class);
bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);

File storageDir = new File(environment.getRequiredProperty(Storage.STORAGE_DIR));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,87 @@

package bisq.core.network.p2p.seed;

import bisq.core.app.BisqEnvironment;

import bisq.network.NetworkOptionKeys;
import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.seed.SeedNodeRepository;

import javax.inject.Inject;
import javax.inject.Named;

import java.util.Set;
import java.util.stream.Stream;
import java.net.URL;

public class DefaultSeedNodeRepository implements SeedNodeRepository {
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.annotation.Nullable;

private final Set<NodeAddress> seedNodeAddresses;
private final Set<NodeAddress> torSeedNodeAddresses;
private final Set<NodeAddress> localhostSeedNodeAddresses;
public class DefaultSeedNodeRepository implements SeedNodeRepository {
private static final Pattern pattern = Pattern.compile("^([a-z0-9]+\\.onion:\\d+)");
private static final String ENDING = ".seednodes";
private static final Collection<NodeAddress> cache = new HashSet<>();
private final BisqEnvironment bisqEnvironment;
private final String seedNodes;

@Inject
public DefaultSeedNodeRepository(SeedNodeAddressLookup lookup) {
this.seedNodeAddresses = lookup.resolveNodeAddresses();
this.torSeedNodeAddresses = DefaultSeedNodeAddresses.DEFAULT_TOR_SEED_NODE_ADDRESSES;
this.localhostSeedNodeAddresses = DefaultSeedNodeAddresses.DEFAULT_LOCALHOST_SEED_NODE_ADDRESSES;
public DefaultSeedNodeRepository(BisqEnvironment environment,
@Nullable @Named(NetworkOptionKeys.SEED_NODES_KEY) String seedNodes) {
bisqEnvironment = environment;
this.seedNodes = seedNodes;
}

@Override
public Set<NodeAddress> getSeedNodeAddresses() {
return seedNodeAddresses;
}
private void reload() {

@Override
public String getOperator(NodeAddress nodeAddress) {
switch (nodeAddress.getFullAddress()) {
case "5quyxpxheyvzmb2d.onion:8000":
return "@miker";
case "ef5qnzx6znifo3df.onion:8000":
return "@manfredkarrer";
case "s67qglwhkgkyvr74.onion:8000":
return "@emzy";
case "jhgcy2won7xnslrb.onion:8000":
return "@manfredkarrer";
case "3f3cu2yw7u457ztq.onion:8000":
return "@manfredkarrer";
case "723ljisnynbtdohi.onion:8000":
return "@manfredkarrer";
case "rm7b56wbrcczpjvl.onion:8000":
return "@manfredkarrer";
case "fl3mmribyxgrv63c.onion:8000":
return "@manfredkarrer";
default:
return "Undefined";
// see if there are any seed nodes configured manually
if(seedNodes != null && !seedNodes.isEmpty()) {
cache.clear();
Arrays.stream(seedNodes.split(",")).forEach(s -> cache.add(new NodeAddress(s)));

return;
}

// else, we fetch the seed nodes from our resources
try {
// read appropriate file
final URL file = DefaultSeedNodeRepository.class.getClassLoader().getResource(BisqEnvironment.getBaseCurrencyNetwork().name().toLowerCase() + ENDING);
final BufferedReader seedNodeFile = new BufferedReader(new FileReader(file.getFile()));

// only clear if we have a fresh data source (otherwise, an exception would prevent us from getting here)
cache.clear();

// refill the cache
seedNodeFile.lines().forEach(s -> {
final Matcher matcher = pattern.matcher(s);
if(matcher.find())
cache.add(new NodeAddress(matcher.group(1)));
});

// filter
cache.removeAll(bisqEnvironment.getBannedSeedNodes().stream().map(s -> new NodeAddress(s)).collect(Collectors.toSet()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public Collection<NodeAddress> getSeedNodeAddresses() {
if(cache.isEmpty())
reload();

return cache;
}

@Override
public boolean isSeedNode(NodeAddress nodeAddress) {
return Stream.concat(localhostSeedNodeAddresses.stream(), torSeedNodeAddresses.stream())
.anyMatch(e -> e.equals(nodeAddress));
if(cache.isEmpty())
reload();
return cache.contains(nodeAddress);
}
}

This file was deleted.

Loading

0 comments on commit 2473067

Please sign in to comment.