Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo-apk: Automate adb reverse port forwarding through Cargo.toml metadata #348

Merged
merged 5 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions cargo-apk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ port = "8080"
path = "/rust-windowing/android-ndk-rs/tree/master/cargo-apk"
path_prefix = "/rust-windowing/"
mime_type = "image/jpeg"

# Set up reverse port forwarding through `adb reverse`, meaning that if the
# Android device connects to `https://localhost:1338` it will be routed to
# the localhost on the host device instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't strictly for http, I'd write Android device connects to `localhost` on port `1338` ....

For demonstration purposes, can we use a different source and destination purposes? e.g. it will be routed to the host on port `XYZ whatever you choose` instead.

MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
[package.metadata.android.reverse_port_fwd]
"tcp:1338" = "tcp:1338"
```

If a manifest attribute is not supported by `cargo apk` feel free to create a PR that adds the missing attribute.
2 changes: 2 additions & 0 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl<'a> ApkBuilder<'a> {
resources,
manifest,
disable_aapt_compression: is_debug_profile,
reverse_port_fwd: self.manifest.reverse_port_fwd.clone(),
};
let mut apk = config.create_apk()?;

Expand Down Expand Up @@ -245,6 +246,7 @@ impl<'a> ApkBuilder<'a> {

pub fn run(&self, artifact: &Artifact) -> Result<(), Error> {
let apk = self.build(artifact)?;
apk.reverse_port_forwarding(self.device_serial.as_deref())?;
apk.install(self.device_serial.as_deref())?;
let pid = apk.start(self.device_serial.as_deref())?;

Expand Down
5 changes: 5 additions & 0 deletions cargo-apk/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) struct Manifest {
pub(crate) runtime_libs: Option<PathBuf>,
/// Maps profiles to keystores
pub(crate) signing: HashMap<String, Signing>,
pub(crate) reverse_port_fwd: HashMap<String, String>,
}

impl Manifest {
Expand All @@ -38,6 +39,7 @@ impl Manifest {
resources: metadata.resources,
runtime_libs: metadata.runtime_libs,
signing: metadata.signing,
reverse_port_fwd: metadata.reverse_port_fwd,
})
}
}
Expand Down Expand Up @@ -71,6 +73,9 @@ struct AndroidMetadata {
/// Maps profiles to keystores
#[serde(default)]
signing: HashMap<String, Signing>,
/// Set up reverse port forwarding before the application launches
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
#[serde(default)]
reverse_port_fwd: HashMap<String, String>,
Jasper-Bekkers marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Clone, Debug, Default, Deserialize)]
Expand Down
23 changes: 23 additions & 0 deletions ndk-build/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::NdkError;
use crate::manifest::AndroidManifest;
use crate::ndk::{Key, Ndk};
use crate::target::Target;
use std::collections::HashMap;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fs;
Expand All @@ -16,6 +17,7 @@ pub struct ApkConfig {
pub resources: Option<PathBuf>,
pub manifest: AndroidManifest,
pub disable_aapt_compression: bool,
pub reverse_port_fwd: HashMap<String, String>,
}

impl ApkConfig {
Expand Down Expand Up @@ -178,6 +180,7 @@ pub struct Apk {
path: PathBuf,
package_name: String,
ndk: Ndk,
reverse_port_fwd: HashMap<String, String>,
}

impl Apk {
Expand All @@ -187,9 +190,29 @@ impl Apk {
path: config.apk(),
package_name: config.manifest.package.clone(),
ndk,
reverse_port_fwd: config.reverse_port_fwd.clone(),
}
}

pub fn reverse_port_forwarding(&self, device_serial: Option<&str>) -> Result<(), NdkError> {
for (from, to) in &self.reverse_port_fwd {
println!("Reverse port forwarding {} {}", from, to);
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
let mut adb = self.ndk.platform_tool(bin!("adb"))?;

if let Some(device_serial) = device_serial {
adb.arg("-s").arg(device_serial);
}
Jasper-Bekkers marked this conversation as resolved.
Show resolved Hide resolved

adb.arg("reverse").arg(from).arg(to);

if !adb.status()?.success() {
return Err(NdkError::CmdFailed(adb));
}
}

Ok(())
}

pub fn install(&self, device_serial: Option<&str>) -> Result<(), NdkError> {
let mut adb = self.ndk.adb(device_serial)?;

Expand Down