Revert device to pre-capture state after capture run
All checks were successful
build / build-test (push) Successful in 1m46s

This commit is contained in:
Jan-Peter von Hunnius 2026-09-15 19:47:20 +02:00
parent 7eb9dc90f9
commit 94127bec46
3 changed files with 40 additions and 14 deletions

View file

@ -599,6 +599,35 @@ fn clear_device_proxy(config: &CaptureConfig) {
let _ = adb_shell(config, "settings delete global http_proxy");
}
fn remove_adb_reverse(config: &CaptureConfig) {
let remove_socket = format!("--remove tcp:{}", config.proxy.port);
let _ = adb(config, &["reverse", remove_socket.as_str()]);
}
fn clear_stale_proxy(config: &CaptureConfig) {
if let Ok(value) = adb_shell(config, "settings get global http_proxy") {
let value = value.trim();
if !value.is_empty() && value != "null" {
clear_device_proxy(config);
println!("cleared stale proxy setting left by a previous run: {value}");
}
}
remove_adb_reverse(config);
}
fn revert_device_state(config: &CaptureConfig) {
clear_device_proxy(config);
remove_adb_reverse(config);
let filename = android_ca_filename(CA_COMMON_NAME);
let _ = adb_shell_root(config, &format!("rm -f /system/etc/security/cacerts/{filename}"));
let _ = adb_shell_root(config, "umount /system/etc/security/cacerts");
let _ = adb_shell_root(config, "rm -rf /data/local/tmp/cacerts-backup");
let _ = adb_shell(config, "rm -f /data/local/tmp/topdrives-ca.pem");
println!(
"device reverted: proxy cleared, CA removed from the trust store, staging files deleted"
);
}
fn setup_adb_reverse(config: &CaptureConfig, port: u16) -> Result<()> {
let socket = format!("tcp:{port}");
match adb(config, &["reverse", socket.as_str(), socket.as_str()]) {
@ -661,6 +690,7 @@ async fn command_run(config: &CaptureConfig) -> Result<()> {
let (cert_pem, key_pem) = load_or_create_ca(&project_root)?;
ensure_device_connected(config)?;
clear_stale_proxy(config);
let sdk_level = android_sdk_level(config)?;
if sdk_level > 28 {
println!("warning: Android {sdk_level} detected, the tmpfs CA injection is only tested up to Android 9");
@ -705,10 +735,7 @@ async fn command_run(config: &CaptureConfig) -> Result<()> {
_ = tokio::signal::ctrl_c() => Err("aborted".into()),
};
clear_device_proxy(config);
let remove_socket = format!("--remove tcp:{}", config.proxy.port);
let _ = adb(config, &["reverse", remove_socket.as_str()]);
println!("device proxy settings restored");
revert_device_state(config);
println!("stopping {GAME_PACKAGE}");
let _ = adb_shell(config, &format!("am force-stop {GAME_PACKAGE}"));
@ -791,13 +818,7 @@ async fn command_replay(config: &CaptureConfig) -> Result<()> {
async fn command_clean(config: &CaptureConfig) -> Result<()> {
ensure_device_connected(config)?;
clear_device_proxy(config);
let remove_socket = format!("--remove tcp:{}", config.proxy.port);
let _ = adb(config, &["reverse", remove_socket.as_str()]);
let filename = android_ca_filename(CA_COMMON_NAME);
let _ = adb_shell_root(config, &format!("rm -f /system/etc/security/cacerts/{filename}"));
let _ = adb_shell_root(config, "umount /system/etc/security/cacerts");
println!("proxy settings cleared and CA removed from the device");
revert_device_state(config);
Ok(())
}
@ -824,7 +845,7 @@ commands:
setup generate the CA and inject it into BlueStacks
run full flow: start BlueStacks if needed, proxy, game launch, garage capture
replay re-issue the stored garage request without the emulator
clean remove proxy settings and the injected CA
clean revert the device: clear the proxy, remove the injected CA and staging files
configuration is read from {CONFIG_FILE} in the working directory"
);