Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 11 additions & 76 deletions controller/app/src/main/java/org/iiab/controller/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
import java.util.Map;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.Proxy;
import java.net.InetSocketAddress;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "IIAB-MainActivity";
Expand Down Expand Up @@ -641,21 +639,10 @@ private void initiatePermissionChain() {
}
}

private boolean pingUrl(String urlStr, boolean useProxy) {
private boolean pingUrl(String urlStr) {
try {
URL url = new URL(urlStr);
HttpURLConnection conn;

if (useProxy) {
// We routed the request directly to the app's SOCKS proxy
int socksPort = prefs.getSocksPort(); // generally 1080
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", socksPort));
conn = (HttpURLConnection) url.openConnection(proxy);
} else {
// Normal request (for localhost)
conn = (HttpURLConnection) url.openConnection();
}

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setConnectTimeout(1500);
conn.setReadTimeout(1500);
Expand All @@ -673,50 +660,13 @@ private void runNegotiationSequence() {
});

AppExecutors.get().io().execute(() -> {
boolean boxAlive = false;

// Attempt 1 (0 seconds)
boxAlive = pingUrl("http://box/home", true);

// Attempt 2 (At 2 seconds)
if (!boxAlive) {
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
}
boxAlive = pingUrl("http://box/home", true);
}

// Attempt 3 (At 3 seconds)
if (!boxAlive) {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
boxAlive = pingUrl("http://box/home", true);
}

// We validate if localhost serves as a fallback.
boolean localAlive = pingUrl("http://localhost:8085/home", false);
// Native architecture: content is served locally at localhost:8085.
boolean localAlive = pingUrl("http://localhost:8085/home");

// We evaluate the results
isNegotiating = false;
updateServerAlive(boxAlive || localAlive);

// If VPN is ON but box/proxy is dead, the tunnel is degraded (Orange).
if (prefs.getEnable()) {
isProxyDegraded = !boxAlive;
} else {
isProxyDegraded = false;
}

if (boxAlive) {
currentTargetUrl = "http://box/home";
} else if (localAlive) {
currentTargetUrl = "http://localhost:8085/home";
} else {
currentTargetUrl = null;
}
updateServerAlive(localAlive);
isProxyDegraded = false;
currentTargetUrl = localAlive ? "http://localhost:8085/home" : null;

runOnUiThread(this::updateUIColorsAndVisibility);
});
Expand Down Expand Up @@ -1058,19 +1008,10 @@ private void checkServerStatus() {
if (isNegotiating) return;

AppExecutors.get().io().execute(() -> {
boolean localAlive = pingUrl("http://localhost:8085/home", false);
boolean vpnOn = prefs.getEnable();
boolean boxAlive = false;

if (vpnOn) {
// The passive radar must also use the proxy to test the tunnel.
boxAlive = pingUrl("http://box/home", true);
isProxyDegraded = !boxAlive;
} else {
isProxyDegraded = false;
}
boolean localAlive = pingUrl("http://localhost:8085/home");
isProxyDegraded = false;

updateServerAlive(localAlive || boxAlive);
updateServerAlive(localAlive);

// STATE MACHINE: Has the target state been reached?
if (targetServerState != null && isServerAlive == targetServerState) {
Expand All @@ -1079,13 +1020,7 @@ private void checkServerStatus() {
if (usageFragment != null) runOnUiThread(() -> usageFragment.stopBtnProgress());
}

if (vpnOn && boxAlive) {
currentTargetUrl = "http://box/home";
} else if (localAlive) {
currentTargetUrl = "http://localhost:8085/home";
} else {
currentTargetUrl = null;
}
currentTargetUrl = localAlive ? "http://localhost:8085/home" : null;

runOnUiThread(this::updateUIColorsAndVisibility);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;

import java.util.concurrent.Executor;

import android.graphics.Bitmap;
import android.view.View;
Expand All @@ -27,7 +25,6 @@
import android.os.Handler;
import android.os.Looper;

import org.iiab.controller.portal.data.WebViewProxyConfigurator;
import org.iiab.controller.portal.domain.NavigationPolicy;
import org.iiab.controller.portal.presentation.GestureWebView;
import org.iiab.controller.portal.presentation.PortalViewModel;
Expand Down Expand Up @@ -122,9 +119,6 @@ protected void onCreate(Bundle savedInstanceState) {
resetTimer.run();
});

Preferences prefs = new Preferences(this);
boolean isVpnActive = prefs.getEnable();

// Resolve the target URL once (domain), surviving rotation via the ViewModel.
final String finalTargetUrl = vm.targetUrl(getIntent().getStringExtra("TARGET_URL"));

Expand Down Expand Up @@ -256,27 +250,8 @@ public boolean onConsoleMessage(android.webkit.ConsoleMessage consoleMessage) {
downloadServedApk(uri, contentDisposition, mimetype);
});

// Port and Mirror logic
int tempPort = prefs.getSocksPort();
if (tempPort <= 0) tempPort = 1080;
final int finalProxyPort = tempPort;

// Proxy block (ONLY IF VPN IS ACTIVE)
if (isVpnActive) {
if (WebViewProxyConfigurator.isSupported()) {
Executor executor = ContextCompat.getMainExecutor(this);
WebViewProxyConfigurator.applySocks(finalProxyPort, executor, () -> {
Log.d(TAG, "Proxy configured on port: " + finalProxyPort);
webView.loadUrl(finalTargetUrl); // load only when proxy is ready
});
} else {
Log.w(TAG, "Proxy Override not supported");
webView.loadUrl(finalTargetUrl);
}
} else {
// VPN is OFF. Load localhost directly.
webView.loadUrl(finalTargetUrl);
}
// Native architecture: content is served locally; load it directly.
webView.loadUrl(finalTargetUrl);
}

/**
Expand Down Expand Up @@ -315,14 +290,6 @@ private void downloadServedApk(Uri uri, String contentDisposition, String mimety
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (WebViewProxyConfigurator.isSupported()) {
WebViewProxyConfigurator.clear(() -> Log.d(TAG, "WebView proxy released"));
}
}

@Override
public void onBackPressed() {
if (webView.canGoBack()) {
Expand Down

This file was deleted.