Skip to content
Open
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
5 changes: 3 additions & 2 deletions .github/workflows/rootfs-2-bake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ jobs:
source "$GITHUB_WORKSPACE/tools/rootfs-builder/r2-cp.sh" # s3cp: aws s3 cp with retries
cd dist

# Big artifacts first, then their per-artifact sidecars (.meta4/.sha256/.installed/
# .torrent). Pointers, logs and the queue stay for the publish job.
# Big artifacts first, then their per-artifact sidecars (.meta4/.sha256/.installed).
# No .torrent since K2GO-588. Pointers, logs and the queue stay for the publish job.
shopt -s nullglob
for f in k2go_*.tar.gz; do
echo ">> upload artifact $f"
Expand Down Expand Up @@ -412,6 +412,7 @@ jobs:
if (( n > KEEP )); then
for key in "${keys[@]:0:n-KEEP}"; do
echo ">> prune $key (+ sidecars)"
# .torrent: older bakes published one (before K2GO-588); prune it with its tarball.
for ext in '' .meta4 .sha256 .installed .torrent .minisig; do
aws s3 rm "s3://${BUCKET_NAME}/${key}${ext}" --endpoint-url "$ENDPOINT" || true
done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,8 @@ public void startDownload(Context context, String url, RootfsManifest.Target sig
File downloadDir = new File(SystemStateEvaluator.rootfsBaseDir(context), "downloads");
if (!downloadDir.exists()) downloadDir.mkdirs();

// Secure DHT file within our app (avoids the com.termux crash)
File dhtFile = new File(context.getFilesDir(), "dht.dat");
if (!dhtFile.exists()) {
try { dhtFile.createNewFile(); } catch (Exception e) { Log.w(TAG, "Could not create dht.dat"); }
}
// K2GO-588: no BitTorrent any more; remove the DHT state that older builds kept.
new File(context.getFilesDir(), "dht.dat").delete();

// --- Extract SSL Certificate from assets ---
File caCertFile = new File(context.getFilesDir(), "cacert.pem");
Expand All @@ -213,7 +210,25 @@ public void startDownload(Context context, String url, RootfsManifest.Target sig
// --split mirror count, and reconciling whatever is already on disk so that
// stopping and restarting a download is safe (no wasted re-download, and the
// network profiler never trips over a leftover completed file).
MetalinkFile mf = MetalinkSplit.isMetalinkUrl(url) ? fetchMetalink(url) : null;
// K2GO-588 (ADR-K2GO-554 A4): aria2 gets a local copy of the .meta4 without its
// <metaurl>, so the download is HTTP only. Without that copy aria2 would read the URL
// and turn the torrent metaurl into a BitTorrent session, so a failed read stops here.
File httpOnlyMeta = new File(context.getCacheDir(), "rootfs-http-only.meta4");
MetalinkFile mf = null;
String source = url;
if (MetalinkSplit.isMetalinkUrl(url)) {
mf = fetchMetalink(url, httpOnlyMeta);
if (mf == null) {
if (postLatchedStop(mainHandler, listener)) return;
// The same answer aria2 gives for a network problem (exit 6), so the caller
// offers a retry as before.
mainHandler.post(() -> listener.onError(
org.appdevforall.k2go.download.domain.Aria2Exit.label(6),
org.appdevforall.k2go.download.domain.Aria2Exit.Kind.TRANSIENT));
return;
}
source = "--metalink-file=" + httpOnlyMeta.getAbsolutePath();
}
// resumeOrRepair: an existing (partial or complete-but-corrupt) file is on
// disk. Keep it and let the main aria2 run below salvage it via
// --check-integrity against the .meta4 piece hashes (re-downloading only the
Expand Down Expand Up @@ -274,7 +289,7 @@ public void startDownload(Context context, String url, RootfsManifest.Target sig
// right, only the preamble was wasted.
boolean resuming = resumeOrRepair || hasControlFile(downloadDir);
boolean forceIpv4 = resuming ? false
: Aria2NetworkProfiler.shouldForceIpv4(aria2Bin, downloadDir, dhtFile, url, mainHandler, listener);
: Aria2NetworkProfiler.shouldForceIpv4(aria2Bin, downloadDir, source, mainHandler, listener);
// ----------------------------------------------------

// 3. Build the command dynamically.
Expand All @@ -283,9 +298,9 @@ public void startDownload(Context context, String url, RootfsManifest.Target sig
// "Get more" downloads. Keep both aligned so behaviour matches; if you change flags
// here, mirror them there (and note any intentional divergence). Known divergences:
// the server uses Debian's system CA (no bundled cacert), the container resolver
// (--async-dns=false) instead of ApplyDnsUseCase + the IPv4 profiler, and has no
// DHT/BitTorrent yet; only this app path pre-reconciles/resumes via MetalinkSplit +
// DownloadVerifier before invoking aria2.
// (--async-dns=false) instead of ApplyDnsUseCase + the IPv4 profiler. Neither path
// uses BitTorrent (K2GO-588). Only this app path pre-reconciles/resumes via
// MetalinkSplit + DownloadVerifier before invoking aria2.
java.util.List<String> command = new java.util.ArrayList<>();
command.add(aria2Bin.getAbsolutePath());
command.add("--dir=" + downloadDir.getAbsolutePath());
Expand All @@ -305,10 +320,12 @@ public void startDownload(Context context, String url, RootfsManifest.Target sig
// while downloading. On mismatch aria2 exits non-zero, so onError fires
// and the archive is never extracted/executed.
command.add("--check-integrity=true");
command.add("--enable-dht=true");
command.add("--dht-file-path=" + dhtFile.getAbsolutePath());
command.add("--bt-enable-lpd=true");
command.add("--seed-time=0");
// K2GO-588: HTTP only. The metaurl is gone from the .meta4 above; these keep aria2
// from any peer traffic even so (its default is DHT on).
command.add("--enable-dht=false");
command.add("--enable-dht6=false");
command.add("--bt-enable-lpd=false");
command.add("--enable-peer-exchange=false");

// --- Apply SSL Certificate Validation (D6: always strict; cacert
// presence was already enforced above, so there is no insecure path) ---
Expand Down Expand Up @@ -375,7 +392,7 @@ public void startDownload(Context context, String url, RootfsManifest.Target sig
command.add("--disable-ipv6=true");
}

command.add(url);
command.add(source);

ProcessBuilder pb = new ProcessBuilder(command);

Expand All @@ -384,16 +401,7 @@ public void startDownload(Context context, String url, RootfsManifest.Target sig
// null, so a stop arriving in that window latches the reason and kills nothing.
// Without this the caller's cleanup could delete the download directory and aria2
// would then start, recreate it and write fresh residue behind it.
if (stopRequest == Stop.CANCEL) {
Log.d(TAG, "Download cancelled before aria2 was started.");
mainHandler.post(listener::onCancelled);
return;
}
if (stopRequest == Stop.PAUSE) {
Log.d(TAG, "Download paused before aria2 was started.");
mainHandler.post(listener::onPaused);
return;
}
if (postLatchedStop(mainHandler, listener)) return;

// Redirect errors to the same input stream
pb.redirectErrorStream(true);
Expand Down Expand Up @@ -613,8 +621,23 @@ private void requestStop(Stop reason) {
/**
* Helper method to copy files from the APK assets folder to internal storage.
*/
/** ADFA-5119: a stop latched before aria2 started; reports it and returns true. */
private boolean postLatchedStop(Handler mainHandler, DownloadListener listener) {
if (stopRequest == Stop.CANCEL) {
Log.d(TAG, "Download cancelled before aria2 was started.");
mainHandler.post(listener::onCancelled);
return true;
}
if (stopRequest == Stop.PAUSE) {
Log.d(TAG, "Download paused before aria2 was started.");
mainHandler.post(listener::onPaused);
return true;
}
return false;
}

/** ADFA-4676: fetch + parse the .meta4 into a verifiable MetalinkFile, or null on any error. */
private MetalinkFile fetchMetalink(String url) {
private MetalinkFile fetchMetalink(String url, File httpOnlyCopy) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
Expand All @@ -624,9 +647,22 @@ private MetalinkFile fetchMetalink(String url) {
if (conn.getResponseCode() != 200) {
return null;
}
byte[] raw;
try (InputStream in = conn.getInputStream()) {
return MetalinkFile.parse(in);
java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
byte[] chunk = new byte[16384];
int n;
while ((n = in.read(chunk)) > 0) buf.write(chunk, 0, n);
raw = buf.toByteArray();
}
MetalinkFile mf = MetalinkFile.parse(new java.io.ByteArrayInputStream(raw));
// K2GO-588: the copy that aria2 reads, without the BitTorrent metaurl.
try (java.io.Writer w = new java.io.OutputStreamWriter(
new java.io.FileOutputStream(httpOnlyCopy), java.nio.charset.StandardCharsets.UTF_8)) {
w.write(org.appdevforall.k2go.download.domain.MetalinkHttpOnly.of(
new String(raw, java.nio.charset.StandardCharsets.UTF_8)));
}
return mf;
} catch (Exception e) {
Log.w(TAG, "fetchMetalink fallback (" + e.getMessage() + ")");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public class Aria2NetworkProfiler {
* Runs a real-world speed test by downloading the target file for a few seconds.
* Returns TRUE if IPv4 proves to be more stable and faster.
*/
public static boolean shouldForceIpv4(File aria2Bin, File downloadDir, File dhtFile, String targetUrl, Handler mainHandler, Aria2Manager.DownloadListener listener) {
public static boolean shouldForceIpv4(File aria2Bin, File downloadDir, String source, Handler mainHandler, Aria2Manager.DownloadListener listener) {
Log.i(TAG, "Starting Network Profiling (Time-Boxed Race)...");

// 1. Test IPv4 (Forced)
mainHandler.post(() -> listener.onProgress(0, "Test IPv4", "Wait"));
Log.i(TAG, "=== TEST 1: FORCED IPv4 ROUTE ===");
double speedIpv4 = runTimeBoxedTest(aria2Bin, downloadDir, dhtFile, targetUrl, true);
double speedIpv4 = runTimeBoxedTest(aria2Bin, downloadDir, source, true);

// 2. Test Default (Usually IPv6 preferred)
mainHandler.post(() -> listener.onProgress(0, "Test IPv6", "Wait"));
Log.i(TAG, "=== TEST 2: DEFAULT ROUTE (IPv6 Preferred) ===");
double speedDefault = runTimeBoxedTest(aria2Bin, downloadDir, dhtFile, targetUrl, false);
double speedDefault = runTimeBoxedTest(aria2Bin, downloadDir, source, false);

Log.i(TAG, "=== FINAL PROFILING RESULTS ===");
Log.i(TAG, String.format("Sustained Speed IPv4: %.2f KB/s", speedIpv4));
Expand All @@ -66,7 +66,7 @@ public static boolean shouldForceIpv4(File aria2Bin, File downloadDir, File dhtF
return forceIpv4;
}

private static double runTimeBoxedTest(File aria2Bin, File downloadDir, File dhtFile, String url, boolean forceIpv4) {
private static double runTimeBoxedTest(File aria2Bin, File downloadDir, String source, boolean forceIpv4) {
Process process = null;
double lastRecordedSpeedKB = 0.0;

Expand All @@ -80,8 +80,9 @@ private static double runTimeBoxedTest(File aria2Bin, File downloadDir, File dht
// Match your baseline configuration
command.add("--max-connection-per-server=4");
command.add("--split=4");
command.add("--enable-dht=true");
command.add("--dht-file-path=" + dhtFile.getAbsolutePath());
// K2GO-588: HTTP only, like the real download (Aria2Manager).
command.add("--enable-dht=false");
command.add("--enable-dht6=false");
command.add("--check-certificate=false");

if (forceIpv4) {
Expand All @@ -91,7 +92,7 @@ private static double runTimeBoxedTest(File aria2Bin, File downloadDir, File dht
command.add("--console-log-level=warn");
command.add("--summary-interval=1"); // Need speed output every second
command.add("--download-result=hide");
command.add(url);
command.add(source);

ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* ============================================================================
* Name : MetalinkHttpOnly.java
* Author : AppDevForAll
* Copyright : Copyright (c) 2026 AppDevForAll
* Description : K2GO-588 (ADR-K2GO-554 A4). A Metalink with its <metaurl> elements
* removed, so aria2 downloads over HTTP only, never BitTorrent.
* ============================================================================
*/
package org.appdevforall.k2go.download.domain;

import java.util.regex.Pattern;

/**
* aria2 turns a Metalink that carries a {@code <metaurl mediatype="torrent">} into a BitTorrent
* session, and {@code --follow-torrent=false} does not stop it (measured, ADR-K2GO-554 A4). So the
* app gives aria2 a copy of the Metalink without the metaurl elements. The {@code <url>} mirrors,
* size and hashes stay unchanged.
*/
public final class MetalinkHttpOnly {

// A <metaurl ...>...</metaurl> element or a self-closing <metaurl .../>, with its leading space.
private static final Pattern METAURL = Pattern.compile(
"[ \\t]*<metaurl\\b[^>]*?(?:/>|>.*?</metaurl\\s*>)[ \\t]*\\r?\\n?",
Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

private MetalinkHttpOnly() {}

/** The same Metalink text without any {@code <metaurl>} element. */
public static String of(String metalinkXml) {
if (metalinkXml == null) return null;
return METAURL.matcher(metalinkXml).replaceAll("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.appdevforall.k2go.download.domain;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

public class MetalinkHttpOnlyTest {

private static final String PUBLISHED =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<metalink xmlns=\"urn:ietf:params:xml:ns:metalink\" version=\"4.0\">\n"
+ " <file name=\"k2go_2026.321_basic_c746edd_arm64-v8a.tar.gz\">\n"
+ " <size>1590750919</size>\n"
+ " <hash type=\"sha-256\">28173c20010e4bf54e3b276cb0dd68201e2e473422704bfae85b8ed7e24d3560</hash>\n"
+ " <url priority=\"10\">https://example.org/k2go_2026.321_basic_c746edd_arm64-v8a.tar.gz</url>\n"
+ " <metaurl priority=\"1\" mediatype=\"torrent\" name=\"k2go_2026.321_basic_c746edd_arm64-v8a.tar.gz\">k2go_2026.321_basic_c746edd_arm64-v8a.tar.gz.torrent</metaurl>\n"
+ " </file>\n"
+ "</metalink>\n";

@Test
public void removesTheTorrentMetaurlAndKeepsEverythingElse() {
String out = MetalinkHttpOnly.of(PUBLISHED);
assertFalse(out.contains("metaurl"));
assertFalse(out.contains(".torrent"));
assertEquals(PUBLISHED.replaceAll(" <metaurl[^\\n]*\\n", ""), out);
}

@Test
public void theResultStillParsesWithTheSameMirrorsSizeAndHash() throws Exception {
MetalinkFile before = MetalinkFile.parse(
new ByteArrayInputStream(PUBLISHED.getBytes(StandardCharsets.UTF_8)));
MetalinkFile after = MetalinkFile.parse(new ByteArrayInputStream(
MetalinkHttpOnly.of(PUBLISHED).getBytes(StandardCharsets.UTF_8)));
assertEquals(before.fileName(), after.fileName());
assertEquals(before.sizeBytes(), after.sizeBytes());
assertEquals(before.sha256(), after.sha256());
assertEquals(before.mirrors(), after.mirrors());
}

@Test
public void removesSelfClosingAndMultilineMetaurls() {
String in = "<file>\n<url>https://a/x</url>\n<metaurl mediatype=\"torrent\"/>\n"
+ "<METAURL mediatype=\"torrent\">\n x.torrent\n</METAURL>\n</file>\n";
assertEquals("<file>\n<url>https://a/x</url>\n</file>\n", MetalinkHttpOnly.of(in));
}

@Test
public void aMetalinkWithoutMetaurlIsUnchanged() {
String in = PUBLISHED.replaceAll(" <metaurl[^\\n]*\\n", "");
assertEquals(in, MetalinkHttpOnly.of(in));
assertTrue(MetalinkHttpOnly.of(in).contains("<url priority=\"10\">"));
}

@Test
public void nullStaysNull() {
assertNull(MetalinkHttpOnly.of(null));
}
}
Loading
Loading