soul-browser/sources/warp-runtime/com/mycompany/app/warp/WarpTrace.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

125 lines
4.4 KiB
Java

package com.mycompany.app.warp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/** Fetches Cloudflare {@code /cdn-cgi/trace} (optionally via the local WARP HTTP proxy). */
final class WarpTrace {
private static final String TRACE_URL = "https://www.cloudflare.com/cdn-cgi/trace";
private static final int TIMEOUT_MS = 12000;
static final class Info {
final String ip;
final String colo;
final String loc;
final String warp;
final String rawError;
Info(String ip, String colo, String loc, String warp, String rawError) {
this.ip = ip;
this.colo = colo;
this.loc = loc;
this.warp = warp;
this.rawError = rawError;
}
String summary() {
if (rawError != null) {
return rawError;
}
StringBuilder sb = new StringBuilder();
if (ip != null && !ip.isEmpty()) {
sb.append(ip);
}
if (colo != null && !colo.isEmpty()) {
if (sb.length() > 0) {
sb.append(" · ");
}
sb.append(colo);
}
if (loc != null && !loc.isEmpty()) {
if (sb.length() > 0) {
sb.append(" · ");
}
sb.append(loc);
}
if (warp != null && !warp.isEmpty()) {
if (sb.length() > 0) {
sb.append(" · ");
}
sb.append("warp=").append(warp);
}
return sb.length() > 0 ? sb.toString() : "";
}
}
private WarpTrace() {}
/**
* @param proxyListen host:port for HTTP proxy, or null for a direct request
*/
static Info fetch(String proxyListen) {
HttpURLConnection conn = null;
try {
URL url = new URL(TRACE_URL);
if (proxyListen != null && !proxyListen.isEmpty()) {
int colon = proxyListen.lastIndexOf(':');
String host = colon > 0 ? proxyListen.substring(0, colon) : "127.0.0.1";
int port =
colon > 0
? Integer.parseInt(proxyListen.substring(colon + 1))
: PrefWarp.DEFAULT_PORT;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
conn = (HttpURLConnection) url.openConnection(proxy);
} else {
conn = (HttpURLConnection) url.openConnection();
}
conn.setConnectTimeout(TIMEOUT_MS);
conn.setReadTimeout(TIMEOUT_MS);
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
code >= 400 ? conn.getErrorStream() : conn.getInputStream(),
StandardCharsets.UTF_8));
StringBuilder body = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
body.append(line).append('\n');
}
reader.close();
if (code != 200) {
return new Info(null, null, null, null, "HTTP " + code);
}
Map<String, String> map = parse(body.toString());
return new Info(map.get("ip"), map.get("colo"), map.get("loc"), map.get("warp"), null);
} catch (Exception e) {
String msg = e.getMessage();
return new Info(null, null, null, null, msg != null ? msg : e.getClass().getSimpleName());
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private static Map<String, String> parse(String body) {
HashMap<String, String> map = new HashMap<>();
for (String line : body.split("\n")) {
int eq = line.indexOf('=');
if (eq > 0) {
map.put(line.substring(0, eq).trim(), line.substring(eq + 1).trim());
}
}
return map;
}
}