Changed NAT Traversal method. (Not working yet)

This commit is contained in:
ChronosX88 2019-03-14 17:50:54 +04:00
parent 4e8b045501
commit b080b2a4db

View File

@ -3,14 +3,18 @@ package io.github.chronosx88.influence.models;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Log; import android.util.Log;
import android.util.Patterns;
import net.tomp2p.dht.PeerBuilderDHT; import net.tomp2p.dht.PeerBuilderDHT;
import net.tomp2p.dht.PeerDHT; import net.tomp2p.dht.PeerDHT;
import net.tomp2p.futures.FutureBootstrap; import net.tomp2p.futures.FutureBootstrap;
import net.tomp2p.futures.FutureDiscover; 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.p2p.PeerBuilder;
import net.tomp2p.peers.Number160; import net.tomp2p.peers.Number160;
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -20,11 +24,9 @@ import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.UUID; 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.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.MessageActions;
import io.github.chronosx88.influence.helpers.StorageMVStore; import io.github.chronosx88.influence.helpers.StorageMVStore;
@ -59,10 +61,12 @@ public class MainModel implements MainModelContract {
peerDHT = new PeerBuilderDHT( peerDHT = new PeerBuilderDHT(
new PeerBuilder(peerID) new PeerBuilder(peerID)
.ports(7243) .ports(7243)
.behindFirewall()
.start() .start()
) )
.storage(new StorageMVStore(peerID, context.getFilesDir())) .storage(new StorageMVStore(peerID, context.getFilesDir()))
.start(); .start();
PeerNAT peerNAT = new PeerBuilderNAT(peerDHT.peer()).start();
try { try {
String bootstrapIP = this.preferences.getString("bootstrapAddress", null); String bootstrapIP = this.preferences.getString("bootstrapAddress", null);
if(bootstrapIP == null) { if(bootstrapIP == null) {
@ -90,11 +94,43 @@ public class MainModel implements MainModelContract {
} }
FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress(bootstrapAddress).ports(7243).start(); 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()) { 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 { } else {
try { 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() AppHelper.getObservable().notifyObservers(new JSONObject()
.put("action", MessageActions.PORT_FORWARDING_ERROR)); .put("action", MessageActions.PORT_FORWARDING_ERROR));
peerDHT.shutdown(); peerDHT.shutdown();