soul-browser/sources/warp-runtime/com/mycompany/app/warp/WarpAppProxy.java
KaKi87 d1ec2a89ea
Add Cloudflare WARP (#58)
* Add unofficial Cloudflare WARP for WebView via amz proxy.

Route browsing through a local HTTP CONNECT proxy and ProxyController, mutually exclusive with the DNS VPN. Downloads and torrents stay direct.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix soulamz DNS on Android so WARP registration can succeed.

Pure-Go resolution defaults to localhost:53 without resolv.conf; bootstrap public DNS and build both ABIs with NDK cgo.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Load Android CA roots in soulamz and warm WebView before proxy override.

Fixes TLS verification on device and ProxyController startup races. Document that local VPNs like AdGuard can block WARP endpoint probes.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Prefer non-443 MASQUE endpoints so WARP CONNECT succeeds.

amz auto-select often picks *:443, which fails TLS/CONNECT against current Cloudflare MASQUE; probe preferred ports and only print READY after warp=on.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Prefer non-443 MASQUE endpoints so WARP CONNECT succeeds.

amz auto-selects *:443 with SNI warp.cloudflare.com, which fails against Cloudflare's masque cert and yields CONNECT 502; try known-good ports and probe warp=on before READY.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Ship rebuilt soulamz with MASQUE :4443 preference and rebuild-on-change.

CI was packaging stale bundled libsoulamz.so; rebuild when main.go is newer and pin working non-443 WARP endpoints.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Make WARP settings use the shared SettingActivity UI.

Match DNS and other settings pages with header chrome and list rows; add compile-only stubs so the warp dex can still build separately.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix WARP settings crash from wrong R$string package.

Use literal public.xml string IDs so SettingWarp does not reference
com.mycompany.app.soulbrowser.R (app R is net.kaki87.soul2[.testing]).

Co-authored-by: Cursor <cursoragent@cursor.com>

* Show WARP busy spinner and refresh IP from Cloudflare trace.

Replace the settings switch with a ProgressBar while starting/stopping, and show ip/colo/loc under the toggle refreshed on connect and disconnect.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix WARP toggle crash writing final SettingItem.t.

Mark the row busy via mutable SettingItem.s only; t is final in the app dex.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix WARP spinner crash from wrong ViewHolder.z field type.

Locate the switch via findViewById instead of accessing ViewHolder.z (MySwitchView in app dex).

Co-authored-by: Cursor <cursoragent@cursor.com>

* Avoid off-flash when disabling WARP.

Keep the switch visually on under the spinner while stopping; the list adapter toggles eagerly before our listener runs.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Route all Soul HTTP through WARP and reword VPN guidance.

Install a process-wide ProxySelector plus chokepoint openConnection patches; update warp_info to ask users to whitelist Soul in other VPNs.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-17 02:07:27 +02:00

148 lines
5.2 KiB
Java

package com.mycompany.app.warp;
import android.util.Log;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.List;
/**
* Routes JVM HTTP(S) (HttpURLConnection, OkHttp default selector, etc.) through the
* local soulamz CONNECT proxy while WARP is up. WebView still uses
* {@link WebViewProxyHelper} separately.
*/
public final class WarpAppProxy {
private static final String TAG = "WarpAppProxy";
private static final Object LOCK = new Object();
private static volatile Proxy activeProxy = Proxy.NO_PROXY;
private static volatile ProxySelector previousSelector;
private static volatile boolean installed;
private WarpAppProxy() {}
/** Install process-wide proxy routing for {@code host:port}. */
public static void install(String hostPort) {
if (hostPort == null || hostPort.isEmpty()) {
uninstall();
return;
}
int colon = hostPort.lastIndexOf(':');
String host = colon > 0 ? hostPort.substring(0, colon) : "127.0.0.1";
int port;
try {
port =
colon > 0
? Integer.parseInt(hostPort.substring(colon + 1))
: PrefWarp.DEFAULT_PORT;
} catch (NumberFormatException e) {
Log.e(TAG, "bad listen address " + hostPort, e);
return;
}
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
synchronized (LOCK) {
activeProxy = proxy;
if (!installed) {
previousSelector = ProxySelector.getDefault();
installed = true;
}
ProxySelector.setDefault(new Selector(proxy, previousSelector));
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", Integer.toString(port));
System.setProperty("http.nonProxyHosts", "localhost|127.*|[::1]|*.localhost");
Log.i(TAG, "installed app HTTP proxy " + hostPort);
}
}
/** Remove process-wide proxy routing. */
public static void uninstall() {
synchronized (LOCK) {
activeProxy = Proxy.NO_PROXY;
if (installed) {
ProxySelector.setDefault(previousSelector);
previousSelector = null;
installed = false;
}
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
System.clearProperty("https.proxyHost");
System.clearProperty("https.proxyPort");
System.clearProperty("http.nonProxyHosts");
Log.i(TAG, "uninstalled app HTTP proxy");
}
}
/** Explicit open used by smali patches of Soul HTTP chokepoints. */
public static URLConnection open(URL url) throws IOException {
Proxy proxy = activeProxy;
if (proxy == null || proxy == Proxy.NO_PROXY || shouldBypass(url)) {
return url.openConnection();
}
return url.openConnection(proxy);
}
private static boolean shouldBypass(URL url) {
if (url == null) {
return true;
}
String host = url.getHost();
if (host == null || host.isEmpty()) {
return true;
}
String h = host.toLowerCase();
return "localhost".equals(h)
|| "127.0.0.1".equals(h)
|| "[::1]".equals(h)
|| h.endsWith(".localhost");
}
private static final class Selector extends ProxySelector {
private final Proxy proxy;
private final ProxySelector fallback;
Selector(Proxy proxy, ProxySelector fallback) {
this.proxy = proxy;
this.fallback = fallback;
}
@Override
public List<Proxy> select(URI uri) {
if (uri == null) {
return Collections.singletonList(Proxy.NO_PROXY);
}
String scheme = uri.getScheme();
String host = uri.getHost();
if (host != null) {
String h = host.toLowerCase();
if ("localhost".equals(h)
|| "127.0.0.1".equals(h)
|| "[::1]".equals(h)
|| h.endsWith(".localhost")) {
return Collections.singletonList(Proxy.NO_PROXY);
}
}
if ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme)) {
return Collections.singletonList(proxy);
}
if (fallback != null) {
return fallback.select(uri);
}
return Collections.singletonList(Proxy.NO_PROXY);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
if (fallback != null) {
fallback.connectFailed(uri, sa, ioe);
}
}
}
}