From b080b2a4db14b6f3aaeca47d6ea00cc939c9ebaa Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 14 Mar 2019 17:50:54 +0400 Subject: [PATCH] Changed NAT Traversal method. (Not working yet) --- .../influence/models/MainModel.java | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java b/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java index e5134bb..da0847f 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java @@ -3,14 +3,18 @@ package io.github.chronosx88.influence.models; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; -import android.util.Patterns; import net.tomp2p.dht.PeerBuilderDHT; import net.tomp2p.dht.PeerDHT; import net.tomp2p.futures.FutureBootstrap; import net.tomp2p.futures.FutureDiscover; +import net.tomp2p.nat.FutureNAT; +import net.tomp2p.nat.FutureRelayNAT; +import net.tomp2p.nat.PeerBuilderNAT; +import net.tomp2p.nat.PeerNAT; import net.tomp2p.p2p.PeerBuilder; import net.tomp2p.peers.Number160; +import net.tomp2p.relay.tcp.TCPRelayClientConfig; import org.json.JSONException; import org.json.JSONObject; @@ -20,11 +24,9 @@ import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; -import java.util.regex.Pattern; -import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.contracts.MainModelContract; -import io.github.chronosx88.influence.contracts.MainViewContract; +import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.helpers.MessageActions; import io.github.chronosx88.influence.helpers.StorageMVStore; @@ -59,10 +61,12 @@ public class MainModel implements MainModelContract { peerDHT = new PeerBuilderDHT( new PeerBuilder(peerID) .ports(7243) + .behindFirewall() .start() ) .storage(new StorageMVStore(peerID, context.getFilesDir())) .start(); + PeerNAT peerNAT = new PeerBuilderNAT(peerDHT.peer()).start(); try { String bootstrapIP = this.preferences.getString("bootstrapAddress", null); if(bootstrapIP == null) { @@ -90,11 +94,43 @@ public class MainModel implements MainModelContract { } FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress(bootstrapAddress).ports(7243).start(); - futureDiscover.awaitUninterruptibly(); + FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover); + FutureRelayNAT futureRelayNAT = peerNAT.startRelay(new TCPRelayClientConfig(), futureDiscover, futureNAT); + futureRelayNAT.awaitUninterruptibly(); + if(futureDiscover.isSuccess()) { - Log.d("MainModel", "Success discover! Your IP: " + futureDiscover.externalAddress().toString()); + Log.d("MainModel", "# Success discover! Your IP: " + futureDiscover.peerAddress()); + } else { + Log.d("MainModel", "# Error when discovering: " + futureDiscover.failedReason()); + try { + AppHelper.getObservable().notifyObservers(new JSONObject() + .put("action", MessageActions.PORT_FORWARDING_ERROR)); + peerDHT.shutdown(); + return; + } catch (JSONException ex) { + ex.printStackTrace(); + } + } + + if(futureNAT.isSuccess()) { + Log.d("MainModel", "# Success NAT discover! Your IP: " + futureNAT.peerAddress()); } else { try { + Log.d("MainModel", "# Error when discovering: " + futureNAT.failedReason()); + AppHelper.getObservable().notifyObservers(new JSONObject() + .put("action", MessageActions.PORT_FORWARDING_ERROR)); + peerDHT.shutdown(); + return; + } catch (JSONException ex) { + ex.printStackTrace(); + } + } + + if(futureRelayNAT.isSuccess()) { + Log.d("MainModel", "# Success discover with relay!"); + } else { + try { + Log.d("MainModel", "# Error when discovering: " + futureRelayNAT.failedReason()); AppHelper.getObservable().notifyObservers(new JSONObject() .put("action", MessageActions.PORT_FORWARDING_ERROR)); peerDHT.shutdown();