mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-22 15:22:18 +00:00
Now connecting sna bootstrapping behind NAT
This commit is contained in:
parent
b080b2a4db
commit
127bccd272
@ -5,4 +5,6 @@ public class MessageActions {
|
|||||||
public static final int NETWORK_ERROR = 0x1;
|
public static final int NETWORK_ERROR = 0x1;
|
||||||
public static final int BOOTSTRAP_SUCCESS = 0x2;
|
public static final int BOOTSTRAP_SUCCESS = 0x2;
|
||||||
public static final int PORT_FORWARDING_ERROR = 0x3;
|
public static final int PORT_FORWARDING_ERROR = 0x3;
|
||||||
|
public static final int RELAY_CONNECTION_ERROR = 0x4;
|
||||||
|
public static final int BOOTSTRAP_ERROR = 0x5;
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import net.tomp2p.nat.PeerBuilderNAT;
|
|||||||
import net.tomp2p.nat.PeerNAT;
|
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.peers.PeerAddress;
|
||||||
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
|
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
@ -23,6 +24,8 @@ import java.io.IOException;
|
|||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import io.github.chronosx88.influence.contracts.MainModelContract;
|
import io.github.chronosx88.influence.contracts.MainModelContract;
|
||||||
@ -31,10 +34,14 @@ import io.github.chronosx88.influence.helpers.MessageActions;
|
|||||||
import io.github.chronosx88.influence.helpers.StorageMVStore;
|
import io.github.chronosx88.influence.helpers.StorageMVStore;
|
||||||
|
|
||||||
public class MainModel implements MainModelContract {
|
public class MainModel implements MainModelContract {
|
||||||
|
private static final String LOG_TAG = "MainModel";
|
||||||
|
|
||||||
private SharedPreferences preferences;
|
private SharedPreferences preferences;
|
||||||
private Number160 peerID;
|
private Number160 peerID;
|
||||||
private PeerDHT peerDHT;
|
private PeerDHT peerDHT;
|
||||||
private Context context;
|
private Context context;
|
||||||
|
private InetAddress bootstrapAddress = null;
|
||||||
|
private PeerAddress bootstrapPeerAddress = null;
|
||||||
|
|
||||||
public MainModel() {
|
public MainModel() {
|
||||||
this.context = AppHelper.getContext();
|
this.context = AppHelper.getContext();
|
||||||
@ -56,17 +63,13 @@ public class MainModel implements MainModelContract {
|
|||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
InetAddress bootstrapAddress = null;
|
|
||||||
|
|
||||||
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) {
|
||||||
@ -93,69 +96,89 @@ public class MainModel implements MainModelContract {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress(bootstrapAddress).ports(7243).start();
|
if(!discoverExternalAddress()) {
|
||||||
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.peerAddress());
|
|
||||||
} else {
|
|
||||||
Log.d("MainModel", "# Error when discovering: " + futureDiscover.failedReason());
|
|
||||||
try {
|
try {
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||||
.put("action", MessageActions.PORT_FORWARDING_ERROR));
|
.put("action", MessageActions.PORT_FORWARDING_ERROR));
|
||||||
peerDHT.shutdown();
|
} catch (JSONException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!setupConnectionToRelay()) {
|
||||||
|
try {
|
||||||
|
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||||
|
.put("action", MessageActions.RELAY_CONNECTION_ERROR));
|
||||||
return;
|
return;
|
||||||
} catch (JSONException ex) {
|
} catch (JSONException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(futureNAT.isSuccess()) {
|
if(!bootstrapPeer()) {
|
||||||
Log.d("MainModel", "# Success NAT discover! Your IP: " + futureNAT.peerAddress());
|
|
||||||
} else {
|
|
||||||
try {
|
try {
|
||||||
Log.d("MainModel", "# Error when discovering: " + futureNAT.failedReason());
|
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||||
.put("action", MessageActions.PORT_FORWARDING_ERROR));
|
.put("action", MessageActions.BOOTSTRAP_ERROR));
|
||||||
peerDHT.shutdown();
|
|
||||||
return;
|
return;
|
||||||
} catch (JSONException ex) {
|
} catch (JSONException ex) {
|
||||||
ex.printStackTrace();
|
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();
|
|
||||||
return;
|
|
||||||
} catch (JSONException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FutureBootstrap futureBootstrap = peerDHT.peer().bootstrap().inetAddress(bootstrapAddress).ports(7243).start();
|
|
||||||
futureBootstrap.awaitUninterruptibly();
|
|
||||||
if(futureBootstrap.isSuccess()) {
|
|
||||||
try {
|
try {
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||||
.put("action", MessageActions.BOOTSTRAP_SUCCESS));
|
.put("action", MessageActions.BOOTSTRAP_SUCCESS));
|
||||||
} catch (JSONException ex) {
|
} catch (JSONException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean bootstrapPeer() {
|
||||||
|
FutureBootstrap futureBootstrap = peerDHT.peer().bootstrap().inetAddress(bootstrapAddress).ports(7243).start();
|
||||||
|
futureBootstrap.awaitUninterruptibly();
|
||||||
|
if(futureBootstrap.isSuccess()) {
|
||||||
|
Log.i("MainModel", "# Successfully bootstrapped to " + bootstrapAddress.toString());
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
Log.e("MainModel", "# Cannot bootstrap to " + bootstrapAddress.toString() + ". Reason: " + futureBootstrap.failedReason());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean discoverExternalAddress() {
|
||||||
|
FutureDiscover futureDiscover = peerDHT
|
||||||
|
.peer()
|
||||||
|
.discover()
|
||||||
|
.inetAddress(bootstrapAddress)
|
||||||
|
.ports(7243)
|
||||||
|
.start();
|
||||||
|
futureDiscover.awaitUninterruptibly();
|
||||||
|
bootstrapPeerAddress = futureDiscover.reporter();
|
||||||
|
if(futureDiscover.isSuccess()) {
|
||||||
|
Log.i(LOG_TAG, "# Success discover! Your external IP: " + futureDiscover.peerAddress().toString());
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
Log.e(LOG_TAG, "# Failed to discover my external IP. Reason: " + futureDiscover.failedReason());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean setupConnectionToRelay() {
|
||||||
|
PeerNAT peerNat = new PeerBuilderNAT(peerDHT.peer()).start();
|
||||||
|
FutureRelayNAT futureRelayNAT = peerNat.startRelay(new TCPRelayClientConfig(), bootstrapPeerAddress).awaitUninterruptibly();
|
||||||
|
if (futureRelayNAT.isSuccess()) {
|
||||||
|
Log.i(LOG_TAG, "# Successfully connected to relay node.");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
Log.e(LOG_TAG, "# Cannot connect to relay node. Reason: " + futureRelayNAT.failedReason());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shutdownPeer() {
|
public void shutdownPeer() {
|
||||||
peerDHT.shutdown();
|
peerDHT.shutdown();
|
||||||
|
@ -114,11 +114,24 @@ public class MainActivity extends AppCompatActivity implements Observer, MainVie
|
|||||||
}
|
}
|
||||||
case MessageActions.PORT_FORWARDING_ERROR: {
|
case MessageActions.PORT_FORWARDING_ERROR: {
|
||||||
runOnUiThread(() -> {
|
runOnUiThread(() -> {
|
||||||
progressDialog.dismiss();
|
|
||||||
Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show();
|
Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show();
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case MessageActions.BOOTSTRAP_ERROR: {
|
||||||
|
runOnUiThread(() -> {
|
||||||
|
progressDialog.dismiss();
|
||||||
|
Toast.makeText(this, "Не удалось подключиться к бутстрап-ноде.", Toast.LENGTH_LONG).show();
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case MessageActions.RELAY_CONNECTION_ERROR: {
|
||||||
|
runOnUiThread(() -> {
|
||||||
|
progressDialog.dismiss();
|
||||||
|
Toast.makeText(this, "Не удалось подключиться к relay-ноде.", Toast.LENGTH_LONG).show();
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
Reference in New Issue
Block a user