Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
152836fe54 | ||
|
|
507796887b | ||
|
|
2f08e5e16d | ||
|
|
6a2d652f60 | ||
|
|
b7ae0b1155 | ||
|
|
ec0ae681ac | ||
|
|
61ba07ecf0 | ||
|
|
c835f20263 | ||
|
|
b51965f5af | ||
|
|
66b0e4c71c |
2
.github/workflows/rust-quality.yml
vendored
2
.github/workflows/rust-quality.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- run: rustup component add clippy
|
- run: rustup component add clippy
|
||||||
- name: Run clippy
|
- name: Run clippy
|
||||||
run : cargo clippy
|
run : cargo clippy --all-features
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
name: "fmt"
|
name: "fmt"
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ authors = ["Corentin LIAUD"]
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
homepage = "https://github.com/cocool97/adb_client"
|
homepage = "https://github.com/cocool97/adb_client"
|
||||||
keywords = ["adb", "android", "tcp", "usb"]
|
keywords = ["adb", "android", "tcp", "usb"]
|
||||||
license-file = "LICENSE"
|
license = "MIT"
|
||||||
repository = "https://github.com/cocool97/adb_client"
|
repository = "https://github.com/cocool97/adb_client"
|
||||||
version = "2.0.2"
|
version = "2.0.5"
|
||||||
|
|
||||||
# To build locally when working on a new release
|
# To build locally when working on a new release
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) [year] [fullname]
|
Copyright (c) 2023-2024 Corentin LIAUD
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
@@ -11,6 +11,9 @@
|
|||||||
<a href="https://deps.rs/repo/github/cocool97/adb_client">
|
<a href="https://deps.rs/repo/github/cocool97/adb_client">
|
||||||
<img alt="dependency status" src="https://deps.rs/repo/github/cocool97/adb_client/status.svg"/>
|
<img alt="dependency status" src="https://deps.rs/repo/github/cocool97/adb_client/status.svg"/>
|
||||||
</a>
|
</a>
|
||||||
|
<a href="https://opensource.org/licenses/MIT">
|
||||||
|
<img alt="dependency status" src="https://img.shields.io/badge/License-MIT-yellow.svg"/>
|
||||||
|
</a>
|
||||||
</p>
|
</p>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ authors.workspace = true
|
|||||||
description = "Rust ADB (Android Debug Bridge) CLI"
|
description = "Rust ADB (Android Debug Bridge) CLI"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
keywords.workspace = true
|
keywords.workspace = true
|
||||||
license-file.workspace = true
|
license.workspace = true
|
||||||
name = "adb_cli"
|
name = "adb_cli"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
adb_client = { version = "2.0.1" }
|
adb_client = { version = "2.0.5" }
|
||||||
anyhow = { version = "1.0.89" }
|
anyhow = { version = "1.0.89" }
|
||||||
clap = { version = "4.5.18", features = ["derive"] }
|
clap = { version = "4.5.18", features = ["derive"] }
|
||||||
env_logger = { version = "0.11.5" }
|
env_logger = { version = "0.11.5" }
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::models::RebootTypeCommand;
|
use crate::models::RebootTypeCommand;
|
||||||
|
|
||||||
@@ -16,6 +17,15 @@ pub enum LocalCommand {
|
|||||||
Stat { path: String },
|
Stat { path: String },
|
||||||
/// Spawn an interactive shell or run a list of commands on the device
|
/// Spawn an interactive shell or run a list of commands on the device
|
||||||
Shell { commands: Vec<String> },
|
Shell { commands: Vec<String> },
|
||||||
|
/// Run an activity on device specified by the intent
|
||||||
|
Run {
|
||||||
|
/// The package whose activity is to be invoked
|
||||||
|
#[clap(short = 'p', long = "package")]
|
||||||
|
package: String,
|
||||||
|
/// The activity to be invoked itself, Usually it is MainActivity
|
||||||
|
#[clap(short = 'a', long = "activity")]
|
||||||
|
activity: String,
|
||||||
|
},
|
||||||
/// Reboot the device
|
/// Reboot the device
|
||||||
Reboot {
|
Reboot {
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
@@ -28,4 +38,9 @@ pub enum LocalCommand {
|
|||||||
/// Path to output file (created if not exists)
|
/// Path to output file (created if not exists)
|
||||||
path: Option<String>,
|
path: Option<String>,
|
||||||
},
|
},
|
||||||
|
/// Install an APK on device
|
||||||
|
Install {
|
||||||
|
/// Path to APK file. Extension must be ".apk"
|
||||||
|
path: PathBuf,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,23 @@ pub enum UsbCommands {
|
|||||||
Push { filename: String, path: String },
|
Push { filename: String, path: String },
|
||||||
/// Stat a file on device
|
/// Stat a file on device
|
||||||
Stat { path: String },
|
Stat { path: String },
|
||||||
|
/// Run an activity on device specified by the intent
|
||||||
|
Run {
|
||||||
|
/// The package whose activity is to be invoked
|
||||||
|
#[clap(short = 'p', long = "package")]
|
||||||
|
package: String,
|
||||||
|
/// The activity to be invoked itself, Usually it is MainActivity
|
||||||
|
#[clap(short = 'a', long = "activity")]
|
||||||
|
activity: String,
|
||||||
|
},
|
||||||
/// Reboot the device
|
/// Reboot the device
|
||||||
Reboot {
|
Reboot {
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
reboot_type: RebootTypeCommand,
|
reboot_type: RebootTypeCommand,
|
||||||
},
|
},
|
||||||
|
/// Install an APK on device
|
||||||
|
Install {
|
||||||
|
/// Path to APK file. Extension must be ".apk"
|
||||||
|
path: PathBuf,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,10 @@ fn main() -> Result<()> {
|
|||||||
device.shell_command(commands, std::io::stdout())?;
|
device.shell_command(commands, std::io::stdout())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
LocalCommand::Run { package, activity } => {
|
||||||
|
let output = device.run_activity(&package, &activity)?;
|
||||||
|
std::io::stdout().write_all(&output)?;
|
||||||
|
}
|
||||||
LocalCommand::HostFeatures => {
|
LocalCommand::HostFeatures => {
|
||||||
let features = device
|
let features = device
|
||||||
.host_features()?
|
.host_features()?
|
||||||
@@ -91,6 +95,10 @@ fn main() -> Result<()> {
|
|||||||
};
|
};
|
||||||
device.get_logs(writer)?;
|
device.get_logs(writer)?;
|
||||||
}
|
}
|
||||||
|
LocalCommand::Install { path } => {
|
||||||
|
log::info!("Starting installation of APK {}...", path.display());
|
||||||
|
device.install(path)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::Host(host) => {
|
Command::Host(host) => {
|
||||||
@@ -215,6 +223,14 @@ fn main() -> Result<()> {
|
|||||||
device.push(&mut input, &path)?;
|
device.push(&mut input, &path)?;
|
||||||
log::info!("Uploaded {filename} to {path}");
|
log::info!("Uploaded {filename} to {path}");
|
||||||
}
|
}
|
||||||
|
UsbCommands::Run { package, activity } => {
|
||||||
|
let output = device.run_activity(&package, &activity)?;
|
||||||
|
std::io::stdout().write_all(&output)?;
|
||||||
|
}
|
||||||
|
UsbCommands::Install { path } => {
|
||||||
|
log::info!("Starting installation of APK {}...", path.display());
|
||||||
|
device.install(path)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,26 +3,28 @@ authors.workspace = true
|
|||||||
description = "Rust ADB (Android Debug Bridge) client library"
|
description = "Rust ADB (Android Debug Bridge) client library"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
keywords.workspace = true
|
keywords.workspace = true
|
||||||
license-file.workspace = true
|
license.workspace = true
|
||||||
name = "adb_client"
|
name = "adb_client"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.22.1"
|
base64 = { version = "0.22.1" }
|
||||||
bincode = "1.3.3"
|
bincode = { version = "1.3.3" }
|
||||||
byteorder = { version = "1.5.0" }
|
byteorder = { version = "1.5.0" }
|
||||||
chrono = { version = "0.4.38" }
|
chrono = { version = "0.4.38" }
|
||||||
homedir = { version = "0.3.4" }
|
homedir = { version = "0.3.4" }
|
||||||
image = { version = "0.25.4" }
|
image = { version = "0.25.4" }
|
||||||
lazy_static = { version = "1.5.0" }
|
lazy_static = { version = "1.5.0" }
|
||||||
log = { version = "0.4.22" }
|
log = { version = "0.4.22" }
|
||||||
num-bigint = { version = "0.6", package = "num-bigint-dig" }
|
num-bigint = { version = "0.8.4", package = "num-bigint-dig" }
|
||||||
rand = { version = "0.7.0" }
|
num-traits = { version = "0.2.19" }
|
||||||
|
rand = { version = "0.8.5" }
|
||||||
regex = { version = "1.11.0", features = ["perf", "std", "unicode"] }
|
regex = { version = "1.11.0", features = ["perf", "std", "unicode"] }
|
||||||
rsa = { version = "0.3.0" }
|
rsa = { version = "0.9.6" }
|
||||||
rusb = { version = "0.9.4", features = ["vendored"] }
|
rusb = { version = "0.9.4", features = ["vendored"] }
|
||||||
serde = { version = "1.0.210", features = ["derive"] }
|
serde = { version = "1.0.210", features = ["derive"] }
|
||||||
serde_repr = "0.1.19"
|
serde_repr = { version = "0.1.19" }
|
||||||
thiserror = { version = "1.0.64" }
|
sha1 = { version = "0.10.6", features = ["oid"] }
|
||||||
|
thiserror = { version = "2.0.1" }
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::models::AdbStatResponse;
|
use crate::models::AdbStatResponse;
|
||||||
use crate::{RebootType, Result};
|
use crate::{RebootType, Result};
|
||||||
@@ -28,4 +29,18 @@ pub trait ADBDeviceExt {
|
|||||||
|
|
||||||
/// Reboots the device using given reboot type
|
/// Reboots the device using given reboot type
|
||||||
fn reboot(&mut self, reboot_type: RebootType) -> Result<()>;
|
fn reboot(&mut self, reboot_type: RebootType) -> Result<()>;
|
||||||
|
|
||||||
|
/// Run `activity` from `package` on device. Return the command output.
|
||||||
|
fn run_activity(&mut self, package: &str, activity: &str) -> Result<Vec<u8>> {
|
||||||
|
let mut output = Vec::new();
|
||||||
|
self.shell_command(
|
||||||
|
["am", "start", &format!("{package}/{package}.{activity}")],
|
||||||
|
&mut output,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Install an APK pointed to by `apk_path` on device.
|
||||||
|
fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,4 +87,10 @@ pub enum RustADBError {
|
|||||||
/// Cannot convert given data from slice
|
/// Cannot convert given data from slice
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TryFromSliceError(#[from] std::array::TryFromSliceError),
|
TryFromSliceError(#[from] std::array::TryFromSliceError),
|
||||||
|
/// Given path does not represent an APK
|
||||||
|
#[error("wrong file extension: {0}")]
|
||||||
|
WrongFileExtension(String),
|
||||||
|
/// An error occurred with PKCS8 data
|
||||||
|
#[error("error with pkcs8: {0}")]
|
||||||
|
RsaPkcs8Error(#[from] rsa::pkcs8::Error),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ mod utils;
|
|||||||
|
|
||||||
pub use adb_device_ext::ADBDeviceExt;
|
pub use adb_device_ext::ADBDeviceExt;
|
||||||
pub use error::{Result, RustADBError};
|
pub use error::{Result, RustADBError};
|
||||||
pub use models::{AdbVersion, DeviceLong, DeviceShort, DeviceState, RebootType};
|
pub use models::{AdbStatResponse, AdbVersion, DeviceLong, DeviceShort, DeviceState, RebootType};
|
||||||
pub use server::*;
|
pub use server::*;
|
||||||
pub use transports::*;
|
pub use transports::*;
|
||||||
pub use usb::ADBUSBDevice;
|
pub use usb::ADBUSBDevice;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ pub(crate) enum AdbServerCommand {
|
|||||||
Pair(SocketAddrV4, String),
|
Pair(SocketAddrV4, String),
|
||||||
TransportAny,
|
TransportAny,
|
||||||
TransportSerial(String),
|
TransportSerial(String),
|
||||||
|
Install(u64),
|
||||||
// Local commands
|
// Local commands
|
||||||
ShellCommand(String),
|
ShellCommand(String),
|
||||||
Shell,
|
Shell,
|
||||||
@@ -61,6 +62,7 @@ impl Display for AdbServerCommand {
|
|||||||
AdbServerCommand::Reverse(remote, local) => {
|
AdbServerCommand::Reverse(remote, local) => {
|
||||||
write!(f, "reverse:forward:{remote};{local}")
|
write!(f, "reverse:forward:{remote};{local}")
|
||||||
}
|
}
|
||||||
|
AdbServerCommand::Install(size) => write!(f, "exec:cmd package 'install' -S {size}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,14 @@ use std::{
|
|||||||
use byteorder::LittleEndian;
|
use byteorder::LittleEndian;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// Represents a `stat` response
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct AdbStatResponse {
|
pub struct AdbStatResponse {
|
||||||
|
/// File permissions
|
||||||
pub file_perm: u32,
|
pub file_perm: u32,
|
||||||
|
/// File size, in bytes
|
||||||
pub file_size: u32,
|
pub file_size: u32,
|
||||||
|
/// File modification time
|
||||||
pub mod_time: u32,
|
pub mod_time: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use std::io::{Read, Write};
|
use std::{
|
||||||
|
io::{Read, Write},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
constants::BUFFER_SIZE,
|
constants::BUFFER_SIZE,
|
||||||
@@ -121,4 +124,8 @@ impl ADBDeviceExt for ADBServerDevice {
|
|||||||
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
|
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
|
||||||
self.push(stream, path)
|
self.push(stream, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()> {
|
||||||
|
self.install(apk_path)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
use std::{io::Read, iter::Map, path::Path, slice::ChunksExact};
|
use std::{
|
||||||
|
io::{Read, Seek, Write},
|
||||||
|
iter::Map,
|
||||||
|
path::Path,
|
||||||
|
slice::ChunksExact,
|
||||||
|
};
|
||||||
|
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use image::{ImageBuffer, Rgba};
|
use image::{ImageBuffer, ImageFormat, Rgba};
|
||||||
|
|
||||||
use crate::{models::AdbServerCommand, utils, ADBServerDevice, Result, RustADBError};
|
use crate::{models::AdbServerCommand, utils, ADBServerDevice, Result, RustADBError};
|
||||||
|
|
||||||
@@ -98,13 +103,21 @@ impl TryFrom<[u8; std::mem::size_of::<Self>()]> for FrameBufferInfoV2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ADBServerDevice {
|
impl ADBServerDevice {
|
||||||
/// Dump framebuffer of this device
|
/// Dump framebuffer of this device into given ['path']
|
||||||
/// Big help from source code (https://android.googlesource.com/platform/system/adb/+/refs/heads/main/framebuffer_service.cpp)
|
/// Big help from source code (https://android.googlesource.com/platform/system/adb/+/refs/heads/main/framebuffer_service.cpp)
|
||||||
pub fn framebuffer<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
|
pub fn framebuffer<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
|
||||||
let img = self.framebuffer_inner()?;
|
let img = self.framebuffer_inner()?;
|
||||||
Ok(img.save(path.as_ref())?)
|
Ok(img.save(path.as_ref())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Dump framebuffer of this device and return corresponding bytes.
|
||||||
|
///
|
||||||
|
/// Output data format is currently only `PNG`.
|
||||||
|
pub fn framebuffer_bytes<W: Write + Seek>(&mut self, mut writer: W) -> Result<()> {
|
||||||
|
let img = self.framebuffer_inner()?;
|
||||||
|
Ok(img.write_to(&mut writer, ImageFormat::Png)?)
|
||||||
|
}
|
||||||
|
|
||||||
/// Inner method requesting framebuffer from Android device
|
/// Inner method requesting framebuffer from Android device
|
||||||
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
|
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
|
||||||
let serial: String = self.identifier.clone();
|
let serial: String = self.identifier.clone();
|
||||||
|
|||||||
41
adb_client/src/server/device_commands/install.rs
Normal file
41
adb_client/src/server/device_commands/install.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use std::{fs::File, io::Read, path::Path};
|
||||||
|
|
||||||
|
use crate::{models::AdbServerCommand, utils::check_extension_is_apk, ADBServerDevice, Result};
|
||||||
|
|
||||||
|
impl ADBServerDevice {
|
||||||
|
/// Install an APK on device
|
||||||
|
pub fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()> {
|
||||||
|
let mut apk_file = File::open(&apk_path)?;
|
||||||
|
|
||||||
|
check_extension_is_apk(&apk_path)?;
|
||||||
|
|
||||||
|
let file_size = apk_file.metadata()?.len();
|
||||||
|
|
||||||
|
let serial: String = self.identifier.clone();
|
||||||
|
self.connect()?
|
||||||
|
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
|
||||||
|
|
||||||
|
self.get_transport_mut()
|
||||||
|
.send_adb_request(AdbServerCommand::Install(file_size))?;
|
||||||
|
|
||||||
|
let mut raw_connection = self.get_transport_mut().get_raw_connection()?;
|
||||||
|
|
||||||
|
std::io::copy(&mut apk_file, &mut raw_connection)?;
|
||||||
|
|
||||||
|
let mut data = [0; 1024];
|
||||||
|
let read_amount = self.get_transport().get_raw_connection()?.read(&mut data)?;
|
||||||
|
|
||||||
|
match &data[0..read_amount] {
|
||||||
|
b"Success\n" => {
|
||||||
|
log::info!(
|
||||||
|
"APK file {} successfully installed",
|
||||||
|
apk_path.as_ref().display()
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||||
|
d.to_vec(),
|
||||||
|
)?)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
mod forward;
|
mod forward;
|
||||||
mod framebuffer;
|
mod framebuffer;
|
||||||
mod host_features;
|
mod host_features;
|
||||||
|
mod install;
|
||||||
mod list;
|
mod list;
|
||||||
mod logcat;
|
mod logcat;
|
||||||
mod reboot;
|
mod reboot;
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ impl<R: Read> Read for ADBRecvCommandReader<R> {
|
|||||||
match &header[..] {
|
match &header[..] {
|
||||||
b"DATA" => {
|
b"DATA" => {
|
||||||
let length = self.inner.read_u32::<LittleEndian>()? as usize;
|
let length = self.inner.read_u32::<LittleEndian>()? as usize;
|
||||||
let effective_read = self.inner.read(buf)?;
|
let effective_read = self.inner.read(&mut buf[0..length])?;
|
||||||
self.remaining_data_bytes_to_read = length - effective_read;
|
self.remaining_data_bytes_to_read = length - effective_read;
|
||||||
|
|
||||||
Ok(effective_read)
|
Ok(effective_read)
|
||||||
|
|||||||
@@ -1,93 +1,114 @@
|
|||||||
use crate::{Result, RustADBError};
|
use crate::{Result, RustADBError};
|
||||||
use base64::{engine::general_purpose::STANDARD, Engine};
|
use base64::{engine::general_purpose::STANDARD, Engine};
|
||||||
use byteorder::{LittleEndian, WriteBytesExt};
|
use num_bigint::{BigUint, ModInverse};
|
||||||
use num_bigint::traits::ModInverse;
|
use num_traits::cast::ToPrimitive;
|
||||||
use num_bigint::BigUint;
|
use num_traits::FromPrimitive;
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use rsa::{Hash, PaddingScheme, PublicKeyParts, RSAPrivateKey};
|
use rsa::pkcs8::DecodePrivateKey;
|
||||||
use std::convert::TryInto;
|
use rsa::traits::PublicKeyParts;
|
||||||
|
use rsa::{Pkcs1v15Sign, RsaPrivateKey};
|
||||||
|
|
||||||
// From project: https://github.com/hajifkd/webadb
|
const ADB_PRIVATE_KEY_SIZE: usize = 2048;
|
||||||
#[derive(Debug)]
|
const ANDROID_PUBKEY_MODULUS_SIZE_WORDS: u32 = 64;
|
||||||
pub struct ADBRsaKey {
|
|
||||||
private_key: RSAPrivateKey,
|
#[repr(C)]
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
/// Internal ADB representation of a public key
|
||||||
|
struct ADBRsaInternalPublicKey {
|
||||||
|
pub modulus_size_words: u32,
|
||||||
|
pub n0inv: u32,
|
||||||
|
pub modulus: BigUint,
|
||||||
|
pub rr: Vec<u8>,
|
||||||
|
pub exponent: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ADBRsaKey {
|
impl ADBRsaInternalPublicKey {
|
||||||
pub fn random_with_size(size: usize) -> Result<Self> {
|
pub fn new(exponent: &BigUint, modulus: &BigUint) -> Result<Self> {
|
||||||
let mut rng = OsRng;
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
private_key: RSAPrivateKey::new(&mut rng, size)?,
|
modulus_size_words: ANDROID_PUBKEY_MODULUS_SIZE_WORDS,
|
||||||
|
exponent: exponent.to_u32().ok_or(RustADBError::ConversionError)?,
|
||||||
|
modulus: modulus.clone(),
|
||||||
|
..Default::default()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_pkcs8(pkcs8_content: &str) -> Result<Self> {
|
pub fn into_bytes(mut self) -> Vec<u8> {
|
||||||
let der_encoded = pkcs8_content
|
let mut bytes: Vec<u8> = Vec::new();
|
||||||
.lines()
|
bytes.append(&mut self.modulus_size_words.to_le_bytes().to_vec());
|
||||||
.filter(|line| !line.starts_with("-") && !line.is_empty())
|
bytes.append(&mut self.n0inv.to_le_bytes().to_vec());
|
||||||
.fold(String::new(), |mut data, line| {
|
bytes.append(&mut self.modulus.to_bytes_le());
|
||||||
data.push_str(line);
|
bytes.append(&mut self.rr);
|
||||||
data
|
bytes.append(&mut self.exponent.to_le_bytes().to_vec());
|
||||||
});
|
|
||||||
let der_bytes = STANDARD.decode(&der_encoded)?;
|
|
||||||
let private_key = RSAPrivateKey::from_pkcs8(&der_bytes)?;
|
|
||||||
|
|
||||||
Ok(ADBRsaKey { private_key })
|
bytes
|
||||||
}
|
|
||||||
|
|
||||||
pub fn encoded_public_key(&self) -> Result<String> {
|
|
||||||
// see https://android.googlesource.com/platform/system/core/+/android-4.4_r1/adb/adb_auth_host.c
|
|
||||||
// L63 RSA_to_RSAPublicKey
|
|
||||||
const RSANUMBYTES: u32 = 256;
|
|
||||||
const RSANUMWORDS: u32 = 64;
|
|
||||||
let user: String = format!("adb_client@{}", env!("CARGO_PKG_VERSION"));
|
|
||||||
|
|
||||||
let mut result = vec![];
|
|
||||||
result.write_u32::<LittleEndian>(RSANUMWORDS)?;
|
|
||||||
let r32 = set_bit(32)?;
|
|
||||||
let n = self.private_key.n();
|
|
||||||
let r = set_bit((32 * RSANUMWORDS) as _)?;
|
|
||||||
// Well, let rr = set_bit((64 * RSANUMWORDS) as _) % n is also fine, since r \sim n.
|
|
||||||
let rr = r.modpow(&BigUint::from(2u32), n);
|
|
||||||
let rem = n % &r32;
|
|
||||||
let n0inv = rem.mod_inverse(&r32);
|
|
||||||
if let Some(n0inv) = n0inv {
|
|
||||||
let n0inv = n0inv.to_biguint().ok_or(RustADBError::ConversionError)?;
|
|
||||||
let n0inv_p: u32 = 1 + !u32::from_le_bytes((&n0inv.to_bytes_le()[..4]).try_into()?);
|
|
||||||
result.write_u32::<LittleEndian>(n0inv_p)?;
|
|
||||||
} else {
|
|
||||||
return Err(RustADBError::ConversionError);
|
|
||||||
}
|
|
||||||
|
|
||||||
write_biguint(&mut result, n, RSANUMBYTES as _)?;
|
|
||||||
write_biguint(&mut result, &rr, RSANUMBYTES as _)?;
|
|
||||||
write_biguint(&mut result, self.private_key.e(), 4)?;
|
|
||||||
|
|
||||||
let mut encoded = STANDARD.encode(&result);
|
|
||||||
encoded.push(' ');
|
|
||||||
encoded.push_str(&user);
|
|
||||||
Ok(encoded)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sign(&self, msg: impl AsRef<[u8]>) -> Result<Vec<u8>> {
|
|
||||||
Ok(self.private_key.sign(
|
|
||||||
PaddingScheme::new_pkcs1v15_sign(Some(Hash::SHA1)),
|
|
||||||
msg.as_ref(),
|
|
||||||
)?)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_biguint(writer: &mut Vec<u8>, data: &BigUint, n_bytes: usize) -> Result<()> {
|
#[derive(Debug)]
|
||||||
for &v in data
|
pub struct ADBRsaKey {
|
||||||
.to_bytes_le()
|
private_key: RsaPrivateKey,
|
||||||
.iter()
|
}
|
||||||
.chain(std::iter::repeat(&0))
|
|
||||||
.take(n_bytes)
|
impl ADBRsaKey {
|
||||||
{
|
pub fn new_random() -> Result<Self> {
|
||||||
writer.write_u8(v)?;
|
Ok(Self {
|
||||||
|
private_key: RsaPrivateKey::new(&mut OsRng, ADB_PRIVATE_KEY_SIZE)?,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
pub fn new_from_pkcs8(pkcs8_content: &str) -> Result<Self> {
|
||||||
|
Ok(ADBRsaKey {
|
||||||
|
private_key: RsaPrivateKey::from_pkcs8_pem(pkcs8_content)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn android_pubkey_encode(&self) -> Result<String> {
|
||||||
|
// Helped from project: https://github.com/hajifkd/webadb
|
||||||
|
// Source code: https://android.googlesource.com/platform/system/core/+/refs/heads/main/libcrypto_utils/android_pubkey.cpp
|
||||||
|
// Useful function `android_pubkey_encode()`
|
||||||
|
let mut adb_rsa_pubkey =
|
||||||
|
ADBRsaInternalPublicKey::new(self.private_key.e(), self.private_key.n())?;
|
||||||
|
|
||||||
|
// r32 = 2 ^ 32
|
||||||
|
let r32 = BigUint::from_u64(1 << 32).ok_or(RustADBError::ConversionError)?;
|
||||||
|
|
||||||
|
// r = 2 ^ rsa_size = 2 ^ 2048
|
||||||
|
let r = set_bit(ADB_PRIVATE_KEY_SIZE)?;
|
||||||
|
|
||||||
|
// rr = r ^ 2 mod N
|
||||||
|
let rr = r.modpow(&BigUint::from(2u32), &adb_rsa_pubkey.modulus);
|
||||||
|
adb_rsa_pubkey.rr = rr.to_bytes_le();
|
||||||
|
|
||||||
|
// rem = N[0]
|
||||||
|
let rem = &adb_rsa_pubkey.modulus % &r32;
|
||||||
|
|
||||||
|
// n0inv = -1 / rem mod r32
|
||||||
|
let n0inv = rem
|
||||||
|
.mod_inverse(&r32)
|
||||||
|
.and_then(|v| v.to_biguint())
|
||||||
|
.ok_or(RustADBError::ConversionError)?;
|
||||||
|
|
||||||
|
// BN_sub(n0inv, r32, n0inv)
|
||||||
|
adb_rsa_pubkey.n0inv = (r32 - n0inv)
|
||||||
|
.to_u32()
|
||||||
|
.ok_or(RustADBError::ConversionError)?;
|
||||||
|
|
||||||
|
Ok(self.encode_public_key(adb_rsa_pubkey.into_bytes()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_public_key(&self, pub_key: Vec<u8>) -> String {
|
||||||
|
let mut encoded = STANDARD.encode(pub_key);
|
||||||
|
encoded.push(' ');
|
||||||
|
encoded.push_str(&format!("adb_client@{}", env!("CARGO_PKG_VERSION")));
|
||||||
|
|
||||||
|
encoded
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sign(&self, msg: impl AsRef<[u8]>) -> Result<Vec<u8>> {
|
||||||
|
Ok(self
|
||||||
|
.private_key
|
||||||
|
.sign(Pkcs1v15Sign::new::<sha1::Sha1>(), msg.as_ref())?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_bit(n: usize) -> Result<BigUint> {
|
fn set_bit(n: usize) -> Result<BigUint> {
|
||||||
@@ -133,9 +154,9 @@ CNkECiepaGyquQaffwR1CAi8dH6biJjlTQWQPFcCLA0hvernWo3eaSfiL7fHyym+
|
|||||||
ile69MHFENUePSpuRSiF3Z02
|
ile69MHFENUePSpuRSiF3Z02
|
||||||
-----END PRIVATE KEY-----";
|
-----END PRIVATE KEY-----";
|
||||||
let priv_key =
|
let priv_key =
|
||||||
ADBRsaKey::from_pkcs8(DEFAULT_PRIV_KEY).expect("cannot create rsa key from data");
|
ADBRsaKey::new_from_pkcs8(DEFAULT_PRIV_KEY).expect("cannot create rsa key from data");
|
||||||
let pub_key = priv_key
|
let pub_key = priv_key
|
||||||
.encoded_public_key()
|
.android_pubkey_encode()
|
||||||
.expect("cannot encode public key");
|
.expect("cannot encode public key");
|
||||||
let pub_key_adb = "\
|
let pub_key_adb = "\
|
||||||
QAAAAFH/pU9PVrHRgEjMGnpvOr2QzKYCavSE1fcSwvpS1uPn9GTmuyZr7c9up\
|
QAAAAFH/pU9PVrHRgEjMGnpvOr2QzKYCavSE1fcSwvpS1uPn9GTmuyZr7c9up\
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ pub struct ADBUSBDevice {
|
|||||||
fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Option<ADBRsaKey>> {
|
fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Option<ADBRsaKey>> {
|
||||||
read_to_string(private_key_path.as_ref())
|
read_to_string(private_key_path.as_ref())
|
||||||
.map_err(RustADBError::from)
|
.map_err(RustADBError::from)
|
||||||
.map(|pk| match ADBRsaKey::from_pkcs8(&pk) {
|
.map(|pk| match ADBRsaKey::new_from_pkcs8(&pk) {
|
||||||
Ok(pk) => Some(pk),
|
Ok(pk) => Some(pk),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Error while create RSA private key: {e}");
|
log::error!("Error while create RSA private key: {e}");
|
||||||
@@ -57,7 +57,7 @@ fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match (found_devices.get(0), found_devices.get(1)) {
|
match (found_devices.first(), found_devices.get(1)) {
|
||||||
(None, _) => Ok(None),
|
(None, _) => Ok(None),
|
||||||
(Some(identifiers), None) => Ok(Some(*identifiers)),
|
(Some(identifiers), None) => Ok(Some(*identifiers)),
|
||||||
(Some((vid1, pid1)), Some((vid2, pid2))) => Err(RustADBError::DeviceNotFound(format!(
|
(Some((vid1, pid1)), Some((vid2, pid2))) => Err(RustADBError::DeviceNotFound(format!(
|
||||||
@@ -121,7 +121,7 @@ impl ADBUSBDevice {
|
|||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let private_key = match read_adb_private_key(private_key_path)? {
|
let private_key = match read_adb_private_key(private_key_path)? {
|
||||||
Some(pk) => pk,
|
Some(pk) => pk,
|
||||||
None => ADBRsaKey::random_with_size(2048)?,
|
None => ADBRsaKey::new_random()?,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut s = Self {
|
let mut s = Self {
|
||||||
@@ -202,7 +202,7 @@ impl ADBUSBDevice {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut pubkey = self.private_key.encoded_public_key()?.into_bytes();
|
let mut pubkey = self.private_key.android_pubkey_encode()?.into_bytes();
|
||||||
pubkey.push(b'\0');
|
pubkey.push(b'\0');
|
||||||
|
|
||||||
let message = ADBUsbMessage::new(USBCommand::Auth, AUTH_RSAPUBLICKEY, 0, pubkey);
|
let message = ADBUsbMessage::new(USBCommand::Auth, AUTH_RSAPUBLICKEY, 0, pubkey);
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
models::AdbStatResponse,
|
models::AdbStatResponse,
|
||||||
usb::{ADBUsbMessage, USBCommand, USBSubcommand},
|
usb::{ADBUsbMessage, USBCommand, USBSubcommand},
|
||||||
|
utils::check_extension_is_apk,
|
||||||
ADBDeviceExt, ADBUSBDevice, RebootType, Result, RustADBError,
|
ADBDeviceExt, ADBUSBDevice, RebootType, Result, RustADBError,
|
||||||
};
|
};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::io::{Read, Write};
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{Read, Write},
|
||||||
|
};
|
||||||
|
|
||||||
use super::USBShellWriter;
|
use super::{USBShellWriter, USBWriter};
|
||||||
|
|
||||||
impl ADBDeviceExt for ADBUSBDevice {
|
impl ADBDeviceExt for ADBUSBDevice {
|
||||||
fn shell_command<S: ToString, W: Write>(
|
fn shell_command<S: ToString, W: Write>(
|
||||||
@@ -194,4 +198,48 @@ impl ADBDeviceExt for ADBUSBDevice {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn install<P: AsRef<std::path::Path>>(&mut self, apk_path: P) -> Result<()> {
|
||||||
|
let mut apk_file = File::open(&apk_path)?;
|
||||||
|
|
||||||
|
check_extension_is_apk(&apk_path)?;
|
||||||
|
|
||||||
|
let file_size = apk_file.metadata()?.len();
|
||||||
|
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let local_id = rng.gen();
|
||||||
|
|
||||||
|
let message = ADBUsbMessage::new(
|
||||||
|
USBCommand::Open,
|
||||||
|
local_id,
|
||||||
|
0,
|
||||||
|
format!("exec:cmd package 'install' -S {}\0", file_size)
|
||||||
|
.as_bytes()
|
||||||
|
.to_vec(),
|
||||||
|
);
|
||||||
|
self.transport.write_message(message)?;
|
||||||
|
|
||||||
|
let response = self.transport.read_message()?;
|
||||||
|
let remote_id = response.header().arg0();
|
||||||
|
|
||||||
|
let mut writer = USBWriter::new(self.transport.clone(), local_id, remote_id);
|
||||||
|
|
||||||
|
std::io::copy(&mut apk_file, &mut writer)?;
|
||||||
|
|
||||||
|
let final_status = self.transport.read_message()?;
|
||||||
|
|
||||||
|
match final_status.into_payload().as_slice() {
|
||||||
|
b"Success\n" => {
|
||||||
|
log::info!(
|
||||||
|
"APK file {} successfully installed",
|
||||||
|
apk_path.as_ref().display()
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||||
|
d.to_vec(),
|
||||||
|
)?)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ mod adb_usb_device;
|
|||||||
mod adb_usb_device_commands;
|
mod adb_usb_device_commands;
|
||||||
mod adb_usb_message;
|
mod adb_usb_message;
|
||||||
mod usb_commands;
|
mod usb_commands;
|
||||||
mod usb_shell;
|
mod usb_shell_writer;
|
||||||
|
mod usb_writer;
|
||||||
|
|
||||||
pub use adb_rsa_key::ADBRsaKey;
|
pub use adb_rsa_key::ADBRsaKey;
|
||||||
pub use adb_usb_device::ADBUSBDevice;
|
pub use adb_usb_device::ADBUSBDevice;
|
||||||
pub use adb_usb_message::{ADBUsbMessage, ADBUsbMessageHeader};
|
pub use adb_usb_message::{ADBUsbMessage, ADBUsbMessageHeader};
|
||||||
pub use usb_commands::{USBCommand, USBSubcommand};
|
pub use usb_commands::{USBCommand, USBSubcommand};
|
||||||
pub use usb_shell::USBShellWriter;
|
pub use usb_shell_writer::USBShellWriter;
|
||||||
|
pub use usb_writer::USBWriter;
|
||||||
|
|||||||
53
adb_client/src/usb/usb_writer.rs
Normal file
53
adb_client/src/usb/usb_writer.rs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
use std::io::{ErrorKind, Write};
|
||||||
|
|
||||||
|
use crate::USBTransport;
|
||||||
|
|
||||||
|
use super::{ADBUsbMessage, USBCommand};
|
||||||
|
|
||||||
|
/// Wraps a `Writer` to hide underlying ADB protocol write logic.
|
||||||
|
///
|
||||||
|
/// Read received responses to check that message has been received.
|
||||||
|
pub struct USBWriter {
|
||||||
|
transport: USBTransport,
|
||||||
|
local_id: u32,
|
||||||
|
remote_id: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl USBWriter {
|
||||||
|
pub fn new(transport: USBTransport, local_id: u32, remote_id: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
transport,
|
||||||
|
local_id,
|
||||||
|
remote_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for USBWriter {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
|
let message = ADBUsbMessage::new(
|
||||||
|
USBCommand::Write,
|
||||||
|
self.local_id,
|
||||||
|
self.remote_id,
|
||||||
|
buf.to_vec(),
|
||||||
|
);
|
||||||
|
self.transport
|
||||||
|
.write_message(message)
|
||||||
|
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
|
||||||
|
|
||||||
|
match self.transport.read_message() {
|
||||||
|
Ok(response) => match response.header().command() {
|
||||||
|
USBCommand::Okay => Ok(buf.len()),
|
||||||
|
c => Err(std::io::Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("wrong response received: {c}"),
|
||||||
|
)),
|
||||||
|
},
|
||||||
|
Err(e) => Err(std::io::Error::new(ErrorKind::Other, e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::{ffi::OsStr, path::Path};
|
||||||
|
|
||||||
use crate::{Result, RustADBError};
|
use crate::{Result, RustADBError};
|
||||||
|
|
||||||
pub fn u32_from_le(value: &[u8]) -> Result<u32> {
|
pub fn u32_from_le(value: &[u8]) -> Result<u32> {
|
||||||
@@ -7,3 +9,16 @@ pub fn u32_from_le(value: &[u8]) -> Result<u32> {
|
|||||||
.map_err(|_| RustADBError::ConversionError)?,
|
.map_err(|_| RustADBError::ConversionError)?,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn check_extension_is_apk<P: AsRef<Path>>(path: P) -> Result<()> {
|
||||||
|
if let Some(extension) = path.as_ref().extension() {
|
||||||
|
if ![OsStr::new("apk")].contains(&extension) {
|
||||||
|
return Err(RustADBError::WrongFileExtension(format!(
|
||||||
|
"{} is not an APK file",
|
||||||
|
extension.to_string_lossy()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user