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
20 changes: 20 additions & 0 deletions controller/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@

<activity android:name=".feedback.crash.presentation.CrashReportActivity" />
<activity android:name=".QrActivity" />
<!-- K2GO-584: lets the system grant the Clone cable USB permission ("Always open"). -->
<activity
android:name=".sync.cable.presentation.CableUsbActivity"
android:exported="true"
android:excludeFromRecents="true"
android:noHistory="true"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/cable_usb_accessory" />
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/cable_usb_device" />
</activity>
<!-- ADFA-5339: transparent confirm gate for the notification's "Cancel update" action. Not
exported — only the app's own notification PendingIntent starts it. -->
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public void onCreate() {
org.appdevforall.k2go.apptheme.data.AppThemeController.restore(this);
// ADFA-4640: wire up persistence for the server log (survives app restarts).
org.appdevforall.k2go.LogRepository.get().init(this);
// K2GO-584 (privacy): a share log left by a process that died while sharing.
org.appdevforall.k2go.RsyncManager.deleteDaemonLog(this);
// ADFA-5343 (Phase 1): establish the app-scoped server-lifecycle reconciler.
// ADFA-5343 (Phase 4a): start its own background tick so it can bring the box up / re-drive a flap
// with NO foreground Activity. The tick stands down while an Activity is foregrounded (the Activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
public class RsyncManager implements TransportEngine {

private static final String TAG = "IIAB-RsyncManager";
/** K2GO-584: the share daemon log, in the cache dir (DaemonLog reads it). */
public static final String DAEMON_LOG = "rsyncd.log";
private volatile File daemonLog; // deleted by stop(): it names the receiver's address
private volatile Process rsyncProcess;
private volatile String rsyncBinPath; // ADFA-4539: so stop() can find our per-connection children in /proc
private volatile boolean isCancelled = false; // S10: read/written across threads
Expand Down Expand Up @@ -71,6 +74,9 @@ public boolean startServer(Context context, ShareConfig config, String pass, Str
File configFile = new File(cacheDir, "rsyncd.conf");
File pidFile = new File(cacheDir, "rsyncd.pid");
File lockFile = new File(cacheDir, "rsyncd.lock");
File logFile = new File(cacheDir, DAEMON_LOG);
logFile.delete(); // K2GO-584: the log holds only this share's sessions
daemonLog = logFile;

if (pidFile.exists()) {
pidFile.delete();
Expand All @@ -88,7 +94,7 @@ public boolean startServer(Context context, ShareConfig config, String pass, Str
String configContent = RsyncConfig.buildDaemonConf(
pidFile.getAbsolutePath(), lockFile.getAbsolutePath(), config.rsyncPort,
config.moduleName, dirToShare, config.user, secretsFile.getAbsolutePath(),
excludes.getAbsolutePath());
excludes.getAbsolutePath(), logFile.getAbsolutePath());

writeTextToFile(configFile, configContent);

Expand Down Expand Up @@ -364,6 +370,19 @@ public void stop() {
// librsync.so path), which covers the connection children on both the share
// (daemon) and receive (client) sides.
killOurRsyncProcesses();
deleteDaemonLog();
}

/** K2GO-584 (privacy): the share log does not outlive the share. */
private void deleteDaemonLog() {
File f = daemonLog;
daemonLog = null;
if (f != null && f.exists() && !f.delete()) Log.w(TAG, "could not delete the share log");
}

/** K2GO-584 (privacy): Turn off removes a share log left by a process that died while sharing. */
public static void deleteDaemonLog(Context context) {
new File(context.getCacheDir(), DAEMON_LOG).delete();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ private void onTransferState(SyncTransferState st) {
if (!isAdded() || st == null) return;
Log.i("IIAB-Clone", "recv state=" + st.phase + " title=" + st.title + " msg=" + st.message);
if (st.seq > lastSeq) {
if (st.phase == SyncTransferState.Phase.SUCCESS) { lastSeq = st.seq; showReceiveTerminal(true, st.message); }
if (st.phase == SyncTransferState.Phase.SUCCESS) { lastSeq = st.seq; syncVm.pairingDone(); showReceiveTerminal(true, st.message); }
else if (st.phase == SyncTransferState.Phase.FAILED || st.phase == SyncTransferState.Phase.ABORTED) { lastSeq = st.seq; showReceiveTerminal(false, st.message); }
}
// ADFA-5155: on the success exit we WANT renderReceive — it draws step 2 with the exit status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ public void turnOffK2Go() {
// confirm it before exit (finishAndExit waits on this). Fires at once when no hotspot is up.
LocalHotspotManager.get().stop(() -> hotspotTornDown = true);
org.appdevforall.k2go.networkpolicy.presentation.MeteredNetworkObserver.clearNotice(this); // K2GO-580
org.appdevforall.k2go.RsyncManager.deleteDaemonLog(this); // K2GO-584: no share log after Turn off
// ADFA-4834: minimal shutdown feedback — a status line + the service currently stopping,
// shown over the exit animation and kept until the environment is really stopped, so we
// never bounce back to the Library mid-shutdown. Works with or without the Lottie.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.appdevforall.k2go.sync.cable.domain.CableFrame;
import org.appdevforall.k2go.sync.cable.domain.FrameDecoder;
import org.appdevforall.k2go.sync.cable.domain.PeerAlive;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -35,11 +36,14 @@ public final class CableMux implements Closeable {
private static final int PAYLOAD = 32 * 1024;

public interface Listener {
/** The first frame from the peer arrived: a K2Go mux runs on the other side. Called once. */
void onPeer();

/** The peer sent its pairing data (the QR payload). */
void onHello(String payload);

/** The cable or the peer is gone. Called once. */
void onLinkClosed();
/** The cable or the peer is gone. Called once, with this mux. */
void onLinkClosed(CableMux mux);

/** The sender stopped sharing (BYE). */
void onBye();
Expand All @@ -64,6 +68,10 @@ public interface Opener {
private final AtomicLong bytesIn = new AtomicLong();
private final AtomicLong bytesOut = new AtomicLong();
private volatile boolean closed;
private volatile long lastRx; // last bytes from the cable, or the end of local work
private volatile boolean peerPings; // the peer runs the keepalive (an older K2Go does not)
private volatile boolean busy; // the read thread writes to a local socket
private static final long PING_MS = 2_000;

public CableMux(UsbLink link, Opener opener, Listener listener) {
this.link = link;
Expand All @@ -77,6 +85,9 @@ public void start() throws IOException {
Thread t = new Thread(this::readLoop, "cable-read");
t.setDaemon(true);
t.start();
Thread k = new Thread(this::keepAlive, "cable-ping");
k.setDaemon(true);
k.start();
}

public void sendHello(String payload) throws IOException {
Expand Down Expand Up @@ -123,6 +134,10 @@ public void sendDone() {
}
}

public boolean isClosed() {
return closed;
}

/** Payload bytes moved in both directions, for the speed on the cable row. */
public long bytesMoved() {
return bytesIn.get() + bytesOut.get();
Expand All @@ -131,9 +146,34 @@ public long bytesMoved() {
private void readLoop() {
FrameDecoder decoder = new FrameDecoder();
byte[] b = new byte[UsbLink.MAX_READ];
boolean peerSeen = false;
try {
for (int r; !closed && (r = link.read(b)) > 0; ) {
for (CableFrame f : decoder.feed(b, 0, r)) handle(f);
lastRx = System.currentTimeMillis();
for (CableFrame f : decoder.feed(b, 0, r)) {
if (!peerSeen) {
peerSeen = true;
listener.onPeer();
// Our start RESET may have hit a peer that was not reading yet: answer once,
// so the peer also sees a frame from us. PING: a late RESET would close
// channels that are open by then.
new Thread(() -> {
try {
send(CableFrame.control(CableFrame.Type.PING, 0));
} catch (IOException ignored) {
// the link is closing
}
}, "cable-ack").start();
}
// A slow local socket (slow storage) stops our reads: that is not a dead peer.
busy = true;
try {
handle(f);
} finally {
busy = false;
lastRx = System.currentTimeMillis();
}
}
}
} catch (IOException e) {
Log.w(TAG, "cable read ended: " + e.getMessage());
Expand Down Expand Up @@ -186,6 +226,36 @@ private void handle(CableFrame f) throws IOException {
case DONE:
listener.onDone();
break;
case PING:
peerPings = true; // the read itself proves the peer is alive
break;
}
}

/**
* A peer that dies (app killed, phone off) does not always end our blocked USB read, so the
* link would look alive forever. Each side sends PING every PING_MS; PeerAlive decides when
* silence closes the link.
*/
private void keepAlive() {
try {
while (!closed) {
Thread.sleep(PING_MS);
if (closed) return;
if (PeerAlive.isDead(peerPings, busy, System.currentTimeMillis() - lastRx)) {
Log.w(TAG, "nothing from the peer for " + PeerAlive.DEAD_MS + " ms: link closed");
close();
return;
}
try {
send(CableFrame.control(CableFrame.Type.PING, 0));
} catch (IOException e) {
close();
return;
}
}
} catch (InterruptedException ignored) {
// closing
}
}

Expand Down Expand Up @@ -248,6 +318,6 @@ public void close() {
} catch (IOException ignored) {
// nothing left to release
}
listener.onLinkClosed();
listener.onLinkClosed(this);
}
}
Loading
Loading