44 Commits

Author SHA1 Message Date
LIAUD Corentin
6a2d652f60 chore: version 2.0.3 2024-11-14 20:56:19 +01:00
cocool97
b7ae0b1155 feat: add install command (tcp + usb) (#56) 2024-11-14 20:48:12 +01:00
MahieDev
ec0ae681ac fix: underflow in recv (#53)
* fix: read length
2024-11-10 12:58:28 +01:00
LIAUD Corentin
61ba07ecf0 fix: minor improvments 2024-11-09 14:00:23 +01:00
Himadri Bhattacharjee
c835f20263 feat: add run_activity method and default impl for ADBDeviceExt 2024-11-09 14:00:23 +01:00
Himadri Bhattacharjee
b51965f5af feat: add adb run command with activitymanager
### Changes
- `LocalCommand` and `USBCommand` now have respective variants for "run"
- Both these impls run the shell command `am start INTENT`
2024-11-09 14:00:23 +01:00
LIAUD Corentin
66b0e4c71c license: remove license-file in favor of license 2024-11-09 13:40:59 +01:00
LIAUD Corentin
cb23fd6155 ci: add publish on crates.io 2024-11-05 08:03:33 +01:00
Rohit Sangwan
d932e93d0b Add Forward and Reverse commands 2024-11-04 08:01:54 +01:00
LIAUD Corentin
e60ae7434b ci: deb + rpm + binary upload on new release 2024-11-02 18:06:17 +01:00
LIAUD Corentin
005d864609 chore: add license file 2024-11-02 18:06:17 +01:00
LIAUD Corentin
4d8e5d9367 chore: version 2.0.1 2024-10-31 16:05:19 +01:00
Himadri Bhattacharjee
cff0e68f46 feat: autodetect an ADB device when no vendor or product ID is specified (#44)
* feat: autodetect an ADB device when no vendor or product ID is specified

### Changes

- Added methods `autodetect` to `autodetect_with_custom_private_key` to
`ADBUSBDevice`
- Added private functions to check if a usb device has the signature of
an ADB device
- Made the `vendor_id` and `product_id` USB arguments optional and
default to aforementioned methods

* fix: improve github actions workflows

---------

Co-authored-by: LIAUD Corentin <corentinliaud26@gmail.com>
2024-10-31 16:00:13 +01:00
LIAUD Corentin
266265ca35 feat: v2.0.0 2024-10-25 18:13:11 +02:00
cocool97
479d2d9ade Support devices over usb (#40)
* feat: device interactions over USB (shell, push, pull, reboot for now)

* feat: parse vid and pid from hex as seen in lsusb output (#32)

* usb: Read RSA keypair from user home or generate them (#34)

* feat read keypair from user home or generate ephemeral ones
---------

Co-authored-by: LIAUD Corentin <corentin.liaud@orange.fr>
Co-authored-by: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com>
2024-10-25 18:09:41 +02:00
LIAUD Corentin
c53f5cc11b feat: update workflow 2024-10-24 12:21:29 +02:00
Jinke
95fe89a12c Fix RegexParsingError when parsing device list (#37) 2024-10-11 15:43:51 +02:00
cocool97
e37b6d47cc feat: add github actions (#39) 2024-10-11 15:33:00 +02:00
cocool97
d2d9ede8ab feat: add logo + rework README.md (#38) 2024-10-10 16:41:16 +02:00
cocool97
ed884b0d27 feat: should make crate usable on Windows (#35)
Co-authored-by: LIAUD Corentin <corentin.liaud@orange.fr>
2024-10-01 19:39:25 +02:00
LIAUD Corentin
2f39c13355 feat: v1.0.7 2024-09-28 21:30:44 +02:00
LIAUD Corentin
488af2b9dd feat: improve performances of recv & send 2024-09-28 21:29:09 +02:00
LIAUD Corentin
83d716d685 release: 1.0.6 2024-09-13 13:56:42 +02:00
Samuel Cavalcanti
101fafe4ec Fix pairing (#30)
* fix: pairing code that starts with 0

* feat: add`no_run` end `std::io::stdout` to pass in examples
---------

Co-authored-by: LIAUD Corentin <corentin.liaud@orange.fr>
2024-09-13 13:49:46 +02:00
LIAUD Corentin
251276c766 feat: add logcat + log crate 2024-09-05 16:54:39 +02:00
LIAUD Corentin
d00290c450 feat: release adb_cli version 1.0.4 2024-08-09 17:25:53 +02:00
LIAUD Corentin
265971bd6d bump: version 1.0.4 2024-08-09 17:20:57 +02:00
LIAUD Corentin
7abfa451d2 feat: add emu commands interface 2024-08-09 15:32:51 +02:00
LIAUD Corentin
fbb65373a8 fix: windows server start 2024-08-02 10:51:44 +02:00
LIAUD Corentin
aa22472952 feat: license; start server if not running 2024-08-02 10:49:22 +02:00
LIAUD Corentin
2919003a9b feat: add framebuffer command 2024-07-19 14:09:10 +02:00
LIAUD Corentin
1a62105565 feat: remove serial param from functions 2024-07-17 21:48:24 +02:00
LIAUD Corentin
1805c60e32 feat: add disconnect + rework with new archi 2024-07-17 20:43:11 +02:00
Valentin MILLET
24a6e49ab8 Remove useless error when connecting device 2024-07-17 20:43:11 +02:00
Valentin MILLET
13b69120f5 Fix regex for displaying devices in long format with IP:PORT 2024-07-17 20:43:11 +02:00
Valentin MILLET
a7b4cf7d80 Add pair and connect command 2024-07-17 20:43:11 +02:00
LIAUD Corentin
1342b4c34a feat: rework interface to add new transports 2024-07-17 20:07:52 +02:00
LIAUD Corentin
8a682f7472 feat: improve TCP connection management 2024-07-15 16:31:57 +02:00
LIAUD Corentin
3d3546106e feat: add AdbTcpConnection::new_with_default method 2024-07-15 15:25:21 +02:00
LIAUD Corentin
c6ffa2ff6b fix: Error message handling when FAIL 2024-07-15 10:33:51 +02:00
LIAUD Corentin
f46c996095 release: v1.0.3 2024-07-13 19:02:52 +02:00
LIAUD Corentin
6fe4905bb7 fix: Error when reusing same connection 2024-07-13 19:01:15 +02:00
LIAUD Corentin
2a551182ec fix: README.md 2024-07-13 19:01:15 +02:00
LIAUD Corentin
3c5efb2dae fix: fix stdin read when using shell function 2024-07-13 19:01:15 +02:00
94 changed files with 4199 additions and 1104 deletions

15
.github/workflows/rust-build.yml vendored Normal file
View File

@@ -0,0 +1,15 @@
name: Rust - Build
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
jobs:
build-release:
name: "build-release"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build project
run: cargo build --release

32
.github/workflows/rust-quality.yml vendored Normal file
View File

@@ -0,0 +1,32 @@
name: Rust - Quality
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
jobs:
clippy:
name: "clippy"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: rustup component add clippy
- name: Run clippy
run : cargo clippy
fmt:
name: "fmt"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run formatter
run : cargo fmt --all --check
tests:
name: "tests"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: cargo test --verbose

50
.github/workflows/rust-release.yml vendored Normal file
View File

@@ -0,0 +1,50 @@
name: Rust - Release creation
on:
release:
types: [created]
jobs:
create-release:
runs-on: ubuntu-latest
steps:
- name: "Checkout repository"
uses: actions/checkout@v4
- name: "Set up Rust"
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: "Install dependencies"
run: |
sudo apt update
sudo apt install -y rpm
cargo install cargo-deb
cargo install cargo-generate-rpm
- name: "build-release"
run: cargo build --release
- name: "Build DEB package"
run: cargo deb -p adb_cli
- name: "Build RPM package"
run: cargo generate-rpm -p adb_cli
- name: "Publish GitHub artefacts"
uses: softprops/action-gh-release@v2
with:
files: |
target/debian/*.deb
target/generate-rpm/*.rpm
target/release/adb_cli
- name: "Publish crates"
run: |
cargo publish -p adb_client --token ${CRATES_IO_TOKEN}
cargo publish -p adb_cli --token ${CRATES_IO_TOKEN}
env:
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}

View File

@@ -1,30 +1,23 @@
[package]
description = "Rust ADB (Android Debug Bridge) client library"
[workspace]
members = ["adb_cli", "adb_client"]
resolver = "2"
[workspace.package]
authors = ["Corentin LIAUD"]
edition = "2021"
keywords = ["adb", "android"]
homepage = "https://github.com/cocool97/adb_client"
keywords = ["adb", "android", "tcp", "usb"]
license = "MIT"
name = "adb_client"
readme = "README.md"
repository = "https://github.com/cocool97/adb_client"
version = "1.0.2"
version = "2.0.3"
[lib]
name = "adb_client"
path = "src/lib.rs"
# To build locally when working on a new release
[patch.crates-io]
adb_client = { path = "./adb_client" }
[[example]]
name = "adb_cli"
path = "examples/adb_cli.rs"
[dependencies]
byteorder = { version = "1.5.0" }
chrono = { version = "0.4.38" }
lazy_static = { version = "1.5.0" }
regex = { version = "1.10.5", features = ["perf", "std", "unicode"] }
termios = { version = "0.3.3" }
thiserror = { version = "1.0.61" }
## Binary-only dependencies
## Marked as optional so that lib users do not depend on them
[dev-dependencies]
clap = { version = "4.5.4", features = ["derive"] }
[profile.release]
codegen-units = 1
debug-assertions = false
lto = "thin"
opt-level = 'z'
strip = true

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,72 +1,43 @@
# adb_client
<p align="center" style="text-align: center">
<img src="assets/logo.png" width="33%">
</p>
Android Debug Bridge (ADB) client implementation in pure Rust !
<p align="center">
<p align="center">Android Debug Bridge (ADB) client implementation in pure Rust !</p>
<p align="center">
<a href="https://crates.io/crates/adb_client">
<img alt="crates.io" src="https://img.shields.io/crates/v/adb_client.svg"/>
</a>
<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"/>
</a>
</p>
</p>
Main features :
Main features of this library:
- Full Rust, no need to use shell commands
- Currently only support server TCP/IP protocol
- Full Rust, don't use `adb *` shell commands to interact with devices
- Supports:
- **TCP/IP** protocol, using ADB server as a proxy (standard behavior when using `adb` CLI)
- **USB** protocol, interacting directly with end devices
- Implements hidden `adb` features, like `framebuffer`
- Highly configurable
- Easy to use !
## Examples
## adb_client
First declare `adb_client` as a dependency by simply adding this to your `Cargo.toml`:
Rust library implementing both ADB protocols and providing a high-level abstraction over many supported commands.
```toml
[dependencies]
adb_client = "*"
```
Improved documentation [here](./adb_client/README.md).
### Launch a command on host device
## adb_cli
```rust
use adb_client::AdbTcpConnection;
use std::net::Ipv4Addr;
Rust binary providing an improved version of official `adb` CLI, wrapping `adb_client` library. Can act as an usage example of the library.
let mut connection = AdbTcpConnection::new(Ipv4Addr::from([127,0,0,1]), 5037).unwrap();
connection.shell_command(None, vec!["df", "-h"]);
```
Improved documentation [here](./adb_cli/README.md).
### Get available ADB devices
## Related publications
```rust
use adb_client::AdbTcpConnection;
use std::net::Ipv4Addr;
- [Diving into ADB protocol internals (1/2)](https://www.synacktiv.com/publications/diving-into-adb-protocol-internals-12)
let mut connection = AdbTcpConnection::new(Ipv4Addr::from([127,0,0,1]), 5037).unwrap();
connection.devices();
```
### Push a file to the device
```rust
use adb_client::AdbTcpConnection;
use std::net::Ipv4Addr;
use std::fs::File;
use std::path::Path;
let mut connection = AdbTcpConnection::new(Ipv4Addr::from([127,0,0,1]), 5037).unwrap();
let mut input = File::open(Path::new(&filename)).unwrap();
connection.send(None, &mut input, &path)?;
```
## Rust binary
This crate also provides a lightweight binary based on the `adb_client` crate. You can install it by running the following command :
```shell
cargo install adb_client --example adb_cli
```
## Missing features
- USB protocol
All pull requests are welcome !
## Documentation
- <https://developer.android.com/studio/command-line/adb>
- <https://github.com/cstyan/adbDocumentation>
Some features may still be missing, all pull requests are welcome !

39
adb_cli/Cargo.toml Normal file
View File

@@ -0,0 +1,39 @@
[package]
authors.workspace = true
description = "Rust ADB (Android Debug Bridge) CLI"
edition.workspace = true
keywords.workspace = true
license.workspace = true
name = "adb_cli"
readme = "README.md"
repository.workspace = true
version.workspace = true
[dependencies]
adb_client = { version = "2.0.1" }
anyhow = { version = "1.0.89" }
clap = { version = "4.5.18", features = ["derive"] }
env_logger = { version = "0.11.5" }
log = { version = "0.4.22" }
[target.'cfg(unix)'.dependencies]
termios = { version = "0.3.3" }
#####################################
# Debian package build instructions #
#####################################
[package.metadata.deb]
assets = [
{ source = "target/release/adb_cli", dest = "usr/bin/", mode = "755" },
]
priority = "optional"
section = "utility"
##################################
# RPM package build instructions #
##################################
[package.metadata.generate-rpm]
assets = [
{ source = "target/release/adb_cli", dest = "/usr/bin/adb_cli", mode = "755" },
]
license = "MIT"

76
adb_cli/README.md Normal file
View File

@@ -0,0 +1,76 @@
# adb_cli
[![MIT licensed](https://img.shields.io/crates/l/adb_cli.svg)](./LICENSE-MIT)
![Crates.io Total Downloads](https://img.shields.io/crates/d/adb_cli)
Rust binary providing an improved version of `adb` CLI.
## Rust binary
This crate provides a lightweight binary based on the `adb_client` crate. You can install it by running the following command :
```shell
cargo install adb_cli
```
Usage is quite simple, and tends to look like `adb`:
- To use ADB server as a proxy:
```bash
user@laptop ~/adb_client (main)> adb_cli --help
Rust ADB (Android Debug Bridge) CLI
Usage: adb_cli [OPTIONS] <COMMAND>
Commands:
host-features List available server features
push Push a file on device
pull Pull a file from device
list List a directory on device
stat Stat a file specified on device
shell Spawn an interactive shell or run a list of commands on the device
reboot Reboot the device
framebuffer Dump framebuffer of device
logcat Get logs of device
version Print current ADB version
kill Ask ADB server to quit immediately
devices List connected devices
track-devices Track new devices showing up
pair Pair device with a given code
connect Connect device over WI-FI
disconnect Disconnect device over WI-FI
sms Send a SMS with given phone number and given content
rotate Rotate device screen from 90°
help Print this message or the help of the given subcommand(s)
Options:
-d, --debug
-a, --address <ADDRESS> [default: 127.0.0.1:5037]
-s, --serial <SERIAL> Serial id of a specific device. Every request will be sent to this device
-h, --help Print help
-V, --version Print version
```
- To interact directly with end devices
```bash
user@laptop ~/adb_client (main)> adb_cli usb --help
Device commands via USB, no server needed
Usage: adb_cli usb [OPTIONS] --vendor-id <VID> --product-id <PID> <COMMAND>
Commands:
shell Spawn an interactive shell or run a list of commands on the device
pull Pull a file from device
push Push a file on device
stat Stat a file on device
reboot Reboot the device
help Print this message or the help of the given subcommand(s)
Options:
-v, --vendor-id <VID> Hexadecimal vendor id of this USB device
-p, --product-id <PID> Hexadecimal product id of this USB device
-k, --private-key <PATH_TO_PRIVATE_KEY> Path to a custom private key to use for authentication
-h, --help Print help
```

View File

@@ -1,3 +1,5 @@
#![cfg(any(target_os = "linux", target_os = "macos"))]
use std::os::unix::prelude::{AsRawFd, RawFd};
use termios::{tcsetattr, Termios, TCSANOW, VMIN, VTIME};
@@ -33,6 +35,8 @@ impl ADBTermios {
impl Drop for ADBTermios {
fn drop(&mut self) {
// Custom drop implementation, restores previous termios structure.
tcsetattr(self.fd, TCSANOW, &self.old_termios).unwrap();
if let Err(e) = tcsetattr(self.fd, TCSANOW, &self.old_termios) {
log::error!("Error while droping ADBTermios: {e}")
}
}
}

View File

@@ -0,0 +1,12 @@
use clap::Parser;
#[derive(Parser, Debug)]
pub enum EmuCommand {
/// Send a SMS with given phone number and given content
Sms {
phone_number: String,
content: String,
},
/// Rotate device screen from 90°
Rotate,
}

View File

@@ -0,0 +1,24 @@
use std::net::SocketAddrV4;
use clap::Parser;
#[derive(Parser, Debug)]
pub enum HostCommand {
/// Print current ADB version.
Version,
/// Ask ADB server to quit immediately.
Kill,
/// List connected devices.
Devices {
#[clap(short = 'l', long = "long")]
long: bool,
},
/// Track new devices showing up.
TrackDevices,
/// Pair device with a given code
Pair { address: SocketAddrV4, code: String },
/// Connect device over WI-FI
Connect { address: SocketAddrV4 },
/// Disconnect device over WI-FI
Disconnect { address: SocketAddrV4 },
}

View File

@@ -0,0 +1,46 @@
use clap::Parser;
use std::path::PathBuf;
use crate::models::RebootTypeCommand;
#[derive(Parser, Debug)]
pub enum LocalCommand {
/// List available server features.
HostFeatures,
/// Push a file on device
Push { filename: String, path: String },
/// Pull a file from device
Pull { path: String, filename: String },
/// List a directory on device
List { path: String },
/// Stat a file specified on device
Stat { path: String },
/// Spawn an interactive shell or run a list of commands on the device
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 {
#[clap(subcommand)]
reboot_type: RebootTypeCommand,
},
/// Dump framebuffer of device
Framebuffer { path: String },
/// Get logs of device
Logcat {
/// Path to output file (created if not exists)
path: Option<String>,
},
/// Install an APK on device
Install {
/// Path to APK file. Extension must be ".apk"
path: PathBuf,
},
}

View File

@@ -0,0 +1,9 @@
mod emu;
mod host;
mod local;
mod usb;
pub use emu::EmuCommand;
pub use host::HostCommand;
pub use local::LocalCommand;
pub use usb::{UsbCommand, UsbCommands};

View File

@@ -0,0 +1,56 @@
use std::num::ParseIntError;
use std::path::PathBuf;
use clap::Parser;
use crate::models::RebootTypeCommand;
fn parse_hex_id(id: &str) -> Result<u16, ParseIntError> {
u16::from_str_radix(id, 16)
}
#[derive(Parser, Debug)]
pub struct UsbCommand {
/// Hexadecimal vendor id of this USB device
#[clap(short = 'v', long = "vendor-id", value_parser=parse_hex_id, value_name="VID")]
pub vendor_id: Option<u16>,
/// Hexadecimal product id of this USB device
#[clap(short = 'p', long = "product-id", value_parser=parse_hex_id, value_name="PID")]
pub product_id: Option<u16>,
/// Path to a custom private key to use for authentication
#[clap(short = 'k', long = "private-key")]
pub path_to_private_key: Option<PathBuf>,
#[clap(subcommand)]
pub commands: UsbCommands,
}
#[derive(Parser, Debug)]
pub enum UsbCommands {
/// Spawn an interactive shell or run a list of commands on the device
Shell { commands: Vec<String> },
/// Pull a file from device
Pull { source: String, destination: String },
/// Push a file on device
Push { filename: String, path: String },
/// Stat a file on device
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 {
#[clap(subcommand)]
reboot_type: RebootTypeCommand,
},
/// Install an APK on device
Install {
/// Path to APK file. Extension must be ".apk"
path: PathBuf,
},
}

239
adb_cli/src/main.rs Normal file
View File

@@ -0,0 +1,239 @@
#![doc = include_str!("../README.md")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
mod adb_termios;
mod commands;
mod models;
use adb_client::{ADBDeviceExt, ADBEmulatorDevice, ADBServer, ADBUSBDevice, DeviceShort};
use anyhow::{anyhow, Result};
use clap::Parser;
use commands::{EmuCommand, HostCommand, LocalCommand, UsbCommands};
use models::{Command, Opts};
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() -> Result<()> {
let opt = Opts::parse();
env_logger::init();
match opt.command {
Command::Local(local) => {
let mut adb_server = ADBServer::new(opt.address);
let mut device = match opt.serial {
Some(serial) => adb_server.get_device_by_name(&serial)?,
None => adb_server.get_device()?,
};
match local {
LocalCommand::Pull { path, filename } => {
let mut output = File::create(Path::new(&filename))?;
device.pull(&path, &mut output)?;
log::info!("Downloaded {path} as {filename}");
}
LocalCommand::Push { filename, path } => {
let mut input = File::open(Path::new(&filename))?;
device.push(&mut input, &path)?;
log::info!("Uploaded {filename} to {path}");
}
LocalCommand::List { path } => {
device.list(path)?;
}
LocalCommand::Stat { path } => {
let stat_response = device.stat(path)?;
println!("{}", stat_response);
}
LocalCommand::Shell { commands } => {
if commands.is_empty() {
// Need to duplicate some code here as ADBTermios [Drop] implementation resets terminal state.
// Using a scope here would call drop() too early..
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut adb_termios = adb_termios::ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
device.shell(std::io::stdin(), std::io::stdout())?;
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
device.shell(std::io::stdin(), std::io::stdout())?;
}
} else {
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 => {
let features = device
.host_features()?
.iter()
.map(|v| v.to_string())
.reduce(|a, b| format!("{a},{b}"))
.ok_or(anyhow!("cannot list features"))?;
log::info!("Available host features: {features}");
}
LocalCommand::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
LocalCommand::Framebuffer { path } => {
device.framebuffer(&path)?;
log::info!("Framebuffer dropped: {path}");
}
LocalCommand::Logcat { path } => {
let writer: Box<dyn Write> = if let Some(path) = path {
let f = File::create(path)?;
Box::new(f)
} else {
Box::new(std::io::stdout())
};
device.get_logs(writer)?;
}
LocalCommand::Install { path } => {
log::info!("Starting installation of APK {}...", path.display());
device.install(path)?;
}
}
}
Command::Host(host) => {
let mut adb_server = ADBServer::new(opt.address);
match host {
HostCommand::Version => {
let version = adb_server.version()?;
log::info!("Android Debug Bridge version {}", version);
log::info!("Package version {}-rust", std::env!("CARGO_PKG_VERSION"));
}
HostCommand::Kill => {
adb_server.kill()?;
}
HostCommand::Devices { long } => {
if long {
log::info!("List of devices attached (extended)");
for device in adb_server.devices_long()? {
log::info!("{}", device);
}
} else {
log::info!("List of devices attached");
for device in adb_server.devices()? {
log::info!("{}", device);
}
}
}
HostCommand::TrackDevices => {
let callback = |device: DeviceShort| {
log::info!("{}", device);
Ok(())
};
log::info!("Live list of devices attached");
adb_server.track_devices(callback)?;
}
HostCommand::Pair { address, code } => {
adb_server.pair(address, code)?;
log::info!("Paired device {address}");
}
HostCommand::Connect { address } => {
adb_server.connect_device(address)?;
log::info!("Connected to {address}");
}
HostCommand::Disconnect { address } => {
adb_server.disconnect_device(address)?;
log::info!("Disconnected {address}");
}
}
}
Command::Emu(emu) => {
let mut emulator = match opt.serial {
Some(serial) => ADBEmulatorDevice::new(serial, None)?,
None => return Err(anyhow!("Serial must be set to use emulators !")),
};
match emu {
EmuCommand::Sms {
phone_number,
content,
} => {
emulator.send_sms(&phone_number, &content)?;
log::info!("SMS sent to {phone_number}");
}
EmuCommand::Rotate => emulator.rotate()?,
}
}
Command::Usb(usb) => {
let mut device = match (usb.vendor_id, usb.product_id) {
(Some(vid), Some(pid)) => match usb.path_to_private_key {
Some(pk) => ADBUSBDevice::new_with_custom_private_key(vid, pid, pk)?,
None => ADBUSBDevice::new(vid, pid)?,
},
(None, None) => match usb.path_to_private_key {
Some(pk) => ADBUSBDevice::autodetect_with_custom_private_key(pk)?,
None => ADBUSBDevice::autodetect()?,
},
_ => {
anyhow::bail!("please either supply values for both the --vendor-id and --product-id flags or none.");
}
};
match usb.commands {
UsbCommands::Shell { commands } => {
if commands.is_empty() {
// Need to duplicate some code here as ADBTermios [Drop] implementation resets terminal state.
// Using a scope here would call drop() too early..
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut adb_termios = adb_termios::ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
device.shell(std::io::stdin(), std::io::stdout())?;
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
device.shell(std::io::stdin(), std::io::stdout())?;
}
} else {
device.shell_command(commands, std::io::stdout())?;
}
}
UsbCommands::Pull {
source,
destination,
} => {
let mut output = File::create(Path::new(&destination))?;
device.pull(&source, &mut output)?;
log::info!("Downloaded {source} as {destination}");
}
UsbCommands::Stat { path } => {
let stat_response = device.stat(&path)?;
println!("{}", stat_response);
}
UsbCommands::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
UsbCommands::Push { filename, path } => {
let mut input = File::open(Path::new(&filename))?;
device.push(&mut input, &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)?;
}
}
}
}
Ok(())
}

View File

@@ -0,0 +1,5 @@
mod opts;
mod reboot_type;
pub use opts::{Command, Opts};
pub use reboot_type::RebootTypeCommand;

View File

@@ -0,0 +1,30 @@
use std::net::SocketAddrV4;
use clap::Parser;
use crate::commands::{EmuCommand, HostCommand, LocalCommand, UsbCommand};
#[derive(Parser, Debug)]
#[clap(about, version, author)]
pub struct Opts {
#[clap(short = 'a', long = "address", default_value = "127.0.0.1:5037")]
pub address: SocketAddrV4,
/// Serial id of a specific device. Every request will be sent to this device.
#[clap(short = 's', long = "serial")]
pub serial: Option<String>,
#[clap(subcommand)]
pub command: Command,
}
#[derive(Parser, Debug)]
pub enum Command {
#[clap(flatten)]
Local(LocalCommand),
#[clap(flatten)]
Host(HostCommand),
/// Emulator specific commands
#[clap(subcommand)]
Emu(EmuCommand),
/// Device commands via USB, no server needed
Usb(UsbCommand),
}

View File

@@ -0,0 +1,23 @@
use adb_client::RebootType;
use clap::Parser;
#[derive(Parser, Debug)]
pub enum RebootTypeCommand {
System,
Bootloader,
Recovery,
Sideload,
SideloadAutoReboot,
}
impl From<RebootTypeCommand> for RebootType {
fn from(value: RebootTypeCommand) -> Self {
match value {
RebootTypeCommand::System => RebootType::System,
RebootTypeCommand::Bootloader => RebootType::Bootloader,
RebootTypeCommand::Recovery => RebootType::Recovery,
RebootTypeCommand::Sideload => RebootType::Sideload,
RebootTypeCommand::SideloadAutoReboot => RebootType::SideloadAutoReboot,
}
}
}

28
adb_client/Cargo.toml Normal file
View File

@@ -0,0 +1,28 @@
[package]
authors.workspace = true
description = "Rust ADB (Android Debug Bridge) client library"
edition.workspace = true
keywords.workspace = true
license.workspace = true
name = "adb_client"
readme = "README.md"
repository.workspace = true
version.workspace = true
[dependencies]
base64 = "0.22.1"
bincode = "1.3.3"
byteorder = { version = "1.5.0" }
chrono = { version = "0.4.38" }
homedir = { version = "0.3.4" }
image = { version = "0.25.4" }
lazy_static = { version = "1.5.0" }
log = { version = "0.4.22" }
num-bigint = { version = "0.6", package = "num-bigint-dig" }
rand = { version = "0.7.0" }
regex = { version = "1.11.0", features = ["perf", "std", "unicode"] }
rsa = { version = "0.3.0" }
rusb = { version = "0.9.4", features = ["vendored"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_repr = "0.1.19"
thiserror = { version = "2.0.1" }

85
adb_client/README.md Normal file
View File

@@ -0,0 +1,85 @@
# adb_client
[![MIT licensed](https://img.shields.io/crates/l/adb_client.svg)](./LICENSE-MIT)
[![Documentation](https://docs.rs/adb_client/badge.svg)](https://docs.rs/adb_client)
![Crates.io Total Downloads](https://img.shields.io/crates/d/adb_client)
Rust library implementing ADB protocol.
## Installation
Add `adb_client` crate as a dependency by simply adding it to your `Cargo.toml`:
```toml
[dependencies]
adb_client = "*"
```
## Examples
### Get available ADB devices
```rust no_run
use adb_client::ADBServer;
use std::net::{SocketAddrV4, Ipv4Addr};
// A custom server address can be provided
let server_ip = Ipv4Addr::new(127, 0, 0, 1);
let server_port = 5037;
let mut server = ADBServer::new(SocketAddrV4::new(server_ip, server_port));
server.devices();
```
### Using ADB server as proxy
#### [TCP] Launch a command on device
```rust no_run
use adb_client::{ADBServer, ADBDeviceExt};
let mut server = ADBServer::default();
let mut device = server.get_device().expect("cannot get device");
device.shell_command(["df", "-h"],std::io::stdout());
```
#### [TCP] Push a file to the device
```rust no_run
use adb_client::ADBServer;
use std::net::Ipv4Addr;
use std::fs::File;
use std::path::Path;
let mut server = ADBServer::default();
let mut device = server.get_device().expect("cannot get device");
let mut input = File::open(Path::new("/tmp/f")).expect("Cannot open file");
device.push(&mut input, "/data/local/tmp");
```
### Interacting directly with device
#### [USB] Launch a command on device
```rust no_run
use adb_client::{ADBUSBDevice, ADBDeviceExt};
let vendor_id = 0x04e8;
let product_id = 0x6860;
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device");
device.shell_command(["df", "-h"],std::io::stdout());
```
#### [USB] Push a file to the device
```rust no_run
use adb_client::{ADBUSBDevice, ADBDeviceExt};
use std::fs::File;
use std::path::Path;
let vendor_id = 0x04e8;
let product_id = 0x6860;
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device");
let mut input = File::open(Path::new("/tmp/f")).expect("Cannot open file");
device.push(&mut input, "/data/local/tmp");
```

View File

@@ -0,0 +1,46 @@
use std::io::{Read, Write};
use std::path::Path;
use crate::models::AdbStatResponse;
use crate::{RebootType, Result};
/// Trait representing all features available on both [`ADBServerDevice`] and [`ADBUSBDevice`]
pub trait ADBDeviceExt {
/// Runs 'command' in a shell on the device, and write its output and error streams into [`output`].
fn shell_command<S: ToString, W: Write>(
&mut self,
command: impl IntoIterator<Item = S>,
output: W,
) -> Result<()>;
/// Starts an interactive shell session on the device.
/// Input data is read from [reader] and write to [writer].
/// [W] has a 'static bound as it is internally used in a thread.
fn shell<R: Read, W: Write + Send + 'static>(&mut self, reader: R, writer: W) -> Result<()>;
/// Display the stat information for a remote file
fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse>;
/// Pull the remote file pointed to by [source] and write its contents into [`output`]
fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()>;
/// Push [stream] to [path] on the device.
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()>;
/// Reboots the device using given reboot type
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<()>;
}

View File

@@ -0,0 +1 @@
pub const BUFFER_SIZE: usize = 65536;

View File

@@ -0,0 +1,2 @@
mod rotate;
mod sms;

View File

@@ -0,0 +1,10 @@
use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result};
impl ADBEmulatorDevice {
/// Send a SMS to this emulator with given content with given phone number
pub fn rotate(&mut self) -> Result<()> {
let transport = self.connect()?;
transport.send_command(ADBEmulatorCommand::Rotate)?;
Ok(())
}
}

View File

@@ -0,0 +1,13 @@
use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result};
impl ADBEmulatorDevice {
/// Send a SMS to this emulator with given content with given phone number
pub fn send_sms(&mut self, phone_number: &str, content: &str) -> Result<()> {
let transport = self.connect()?;
transport.send_command(ADBEmulatorCommand::Sms(
phone_number.to_string(),
content.to_string(),
))?;
Ok(())
}
}

93
adb_client/src/error.rs Normal file
View File

@@ -0,0 +1,93 @@
use thiserror::Error;
/// Custom Result type thrown by this crate.
pub type Result<T> = std::result::Result<T, RustADBError>;
/// Represents all error types that can be thrown by the crate.
#[derive(Error, Debug)]
pub enum RustADBError {
/// Indicates that an error occurred with I/O.
#[error(transparent)]
IOError(#[from] std::io::Error),
/// Indicates that an error occurred when sending ADB request.
#[error("ADB request failed - {0}")]
ADBRequestFailed(String),
/// Indicates that ADB server responded an unknown response type.
#[error("Unknown response type {0}")]
UnknownResponseType(String),
/// Indicates that ADB server responses an unknown device state.
#[error("Unknown device state {0}")]
UnknownDeviceState(String),
/// Indicates that an error occurred during UTF-8 parsing.
#[error(transparent)]
Utf8StrError(#[from] std::str::Utf8Error),
/// Indicates that an error occurred during UTF-8 parsing.
#[error(transparent)]
Utf8StringError(#[from] std::string::FromUtf8Error),
/// Indicates that the provided address is not a correct IP address.
#[error(transparent)]
AddrParseError(#[from] std::net::AddrParseError),
/// Indicates an error with regexps.
#[error(transparent)]
RegexError(#[from] regex::Error),
/// Indicates that parsing regex did not worked.
#[error("Regex parsing error: missing field")]
RegexParsingError,
/// Indicates an error with the integer conversion.
#[error(transparent)]
ParseIntError(#[from] std::num::ParseIntError),
/// Indicates that an error occurred when converting a value.
#[error("Conversion error")]
ConversionError,
/// Remote ADB server does not support shell feature.
#[error("Remote ADB server does not support shell feature")]
ADBShellNotSupported,
/// Desired device has not been found
#[error("Device not found: {0}")]
DeviceNotFound(String),
/// Indicates that the device must be paired before attempting a connection over WI-FI
#[error("Device not paired before attempting to connect")]
ADBDeviceNotPaired,
/// An error occurred when getting device's framebuffer image
#[error(transparent)]
FramebufferImageError(#[from] image::error::ImageError),
/// An error occurred when converting framebuffer content
#[error("Cannot convert framebuffer into image")]
FramebufferConversionError,
/// Unimplemented framebuffer image version
#[error("Unimplemented framebuffer image version: {0}")]
UnimplementedFramebufferImageVersion(u32),
/// An error occurred while getting user's home directory
#[error(transparent)]
HomeError(#[from] homedir::GetHomeError),
/// Cannot get home directory
#[error("Cannot get home directory")]
NoHomeDirectory,
/// Generic USB error
#[error(transparent)]
UsbError(#[from] rusb::Error),
/// USB device not found
#[error("USB Device not found: {0} {1}")]
USBDeviceNotFound(u16, u16),
/// No descriptor found
#[error("No USB descriptor found")]
USBNoDescriptorFound,
/// Integrity of the received message cannot be validated
#[error("Invalid integrity. Expected CRC32 {0}, got {1}")]
InvalidIntegrity(u32, u32),
/// Error while decoding base64 data
#[error(transparent)]
Base64DecodeError(#[from] base64::DecodeError),
/// Error while encoding base64 data
#[error(transparent)]
Base64EncodeError(#[from] base64::EncodeSliceError),
/// An error occurred with RSA engine
#[error(transparent)]
RSAError(#[from] rsa::errors::Error),
/// Cannot convert given data from slice
#[error(transparent)]
TryFromSliceError(#[from] std::array::TryFromSliceError),
/// Given path does not represent an APK
#[error("wrong file extension: {0}")]
WrongFileExtension(String),
}

22
adb_client/src/lib.rs Normal file
View File

@@ -0,0 +1,22 @@
#![crate_type = "lib"]
#![forbid(unsafe_code)]
#![forbid(missing_debug_implementations)]
#![forbid(missing_docs)]
#![doc = include_str!("../README.md")]
mod adb_device_ext;
mod constants;
mod emulator;
mod error;
mod models;
mod server;
mod transports;
mod usb;
mod utils;
pub use adb_device_ext::ADBDeviceExt;
pub use error::{Result, RustADBError};
pub use models::{AdbVersion, DeviceLong, DeviceShort, DeviceState, RebootType};
pub use server::*;
pub use transports::*;
pub use usb::ADBUSBDevice;

View File

@@ -0,0 +1,31 @@
use std::fmt::Display;
pub(crate) enum ADBEmulatorCommand {
Authenticate(String),
Sms(String, String),
Rotate,
}
impl Display for ADBEmulatorCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Need to call `writeln!` because emulator commands are '\n' terminated
match self {
ADBEmulatorCommand::Authenticate(token) => writeln!(f, "auth {token}"),
ADBEmulatorCommand::Sms(phone_number, content) => {
writeln!(f, "sms send {phone_number} {content}")
}
ADBEmulatorCommand::Rotate => writeln!(f, "rotate"),
}
}
}
impl ADBEmulatorCommand {
/// Return the number of lines to skip per command when checking its result
pub(crate) fn skip_response_lines(&self) -> u8 {
match self {
ADBEmulatorCommand::Authenticate(_) => 1,
ADBEmulatorCommand::Sms(_, _) => 0,
ADBEmulatorCommand::Rotate => 0,
}
}
}

View File

@@ -0,0 +1,79 @@
use std::fmt::Display;
use super::RebootType;
use std::net::SocketAddrV4;
pub(crate) enum AdbServerCommand {
// Host commands
Version,
Kill,
Devices,
DevicesLong,
TrackDevices,
HostFeatures,
Connect(SocketAddrV4),
Disconnect(SocketAddrV4),
Pair(SocketAddrV4, String),
TransportAny,
TransportSerial(String),
Install(u64),
// Local commands
ShellCommand(String),
Shell,
FrameBuffer,
Sync,
Reboot(RebootType),
Forward(String, String, String),
Reverse(String, String),
}
impl Display for AdbServerCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AdbServerCommand::Version => write!(f, "host:version"),
AdbServerCommand::Kill => write!(f, "host:kill"),
AdbServerCommand::Devices => write!(f, "host:devices"),
AdbServerCommand::DevicesLong => write!(f, "host:devices-l"),
AdbServerCommand::Sync => write!(f, "sync:"),
AdbServerCommand::TrackDevices => write!(f, "host:track-devices"),
AdbServerCommand::TransportAny => write!(f, "host:transport-any"),
AdbServerCommand::TransportSerial(serial) => write!(f, "host:transport:{serial}"),
AdbServerCommand::ShellCommand(command) => match std::env::var("TERM") {
Ok(term) => write!(f, "shell,TERM={term},raw:{command}"),
Err(_) => write!(f, "shell,raw:{command}"),
},
AdbServerCommand::Shell => match std::env::var("TERM") {
Ok(term) => write!(f, "shell,TERM={term},raw:"),
Err(_) => write!(f, "shell,raw:"),
},
AdbServerCommand::HostFeatures => write!(f, "host:features"),
AdbServerCommand::Reboot(reboot_type) => {
write!(f, "reboot:{reboot_type}")
}
AdbServerCommand::Connect(addr) => write!(f, "host:connect:{}", addr),
AdbServerCommand::Disconnect(addr) => write!(f, "host:disconnect:{}", addr),
AdbServerCommand::Pair(addr, code) => {
write!(f, "host:pair:{code}:{addr}")
}
AdbServerCommand::FrameBuffer => write!(f, "framebuffer:"),
AdbServerCommand::Forward(serial, remote, local) => {
write!(f, "host-serial:{serial}:forward:{local};{remote}")
}
AdbServerCommand::Reverse(remote, local) => {
write!(f, "reverse:forward:{remote};{local}")
}
AdbServerCommand::Install(size) => write!(f, "exec:cmd package 'install' -S {size}"),
}
}
}
#[test]
fn test_pair_command() {
let host = "192.168.0.197:34783";
let code = "091102";
let code_u32 = code.parse::<u32>().expect("cannot parse u32");
let pair = AdbServerCommand::Pair(host.parse().expect("cannot parse host"), code.into());
assert_eq!(pair.to_string(), format!("host:pair:{code}:{host}"));
assert_ne!(pair.to_string(), format!("host:pair:{code_u32}:{host}"))
}

View File

@@ -0,0 +1,43 @@
use byteorder::ByteOrder;
use chrono::{DateTime, Utc};
use std::{
fmt::Display,
time::{Duration, UNIX_EPOCH},
};
use byteorder::LittleEndian;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct AdbStatResponse {
pub file_perm: u32,
pub file_size: u32,
pub mod_time: u32,
}
impl From<[u8; 12]> for AdbStatResponse {
fn from(value: [u8; 12]) -> Self {
Self {
file_perm: LittleEndian::read_u32(&value[0..4]),
file_size: LittleEndian::read_u32(&value[4..8]),
mod_time: LittleEndian::read_u32(&value[8..]),
}
}
}
impl Display for AdbStatResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let d = UNIX_EPOCH + Duration::from_secs(self.mod_time.into());
// Create DateTime from SystemTime
let datetime = DateTime::<Utc>::from(d);
writeln!(f, "File permissions: {}", self.file_perm)?;
writeln!(f, "File size: {} bytes", self.file_size)?;
write!(
f,
"Modification time: {}",
datetime.format("%Y-%m-%d %H:%M:%S.%f %Z")
)?;
Ok(())
}
}

View File

@@ -1,9 +1,14 @@
use lazy_static::lazy_static;
use std::str::FromStr;
use std::{fmt::Display, str};
use crate::{DeviceState, RustADBError};
use regex::bytes::Regex;
use crate::{DeviceState, RustADBError};
lazy_static! {
static ref DEVICES_LONG_REGEX: Regex = Regex::new("^(?P<identifier>\\S+)\\s+(?P<state>\\w+) ((usb:(?P<usb1>.*)|(?P<usb2>\\d-\\d)) )?(product:(?P<product>\\w+) model:(?P<model>\\w+) device:(?P<device>\\w+) )?transport_id:(?P<transport_id>\\d+)$").expect("cannot build devices long regex");
}
/// Represents a new device with more informations helded.
#[derive(Debug)]
@@ -43,11 +48,8 @@ impl Display for DeviceLong {
impl TryFrom<Vec<u8>> for DeviceLong {
type Error = RustADBError;
// TODO: Prevent regex compilation every call to try_from()
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let parse_regex = Regex::new("^(?P<identifier>\\w+)\\s+(?P<state>\\w+) usb:(?P<usb>.*) (product:(?P<product>\\w+) model:(?P<model>\\w+) device:(?P<device>\\w+) )?transport_id:(?P<transport_id>\\d+)$")?;
let groups = parse_regex
let groups = DEVICES_LONG_REGEX
.captures(&value)
.ok_or(RustADBError::RegexParsingError)?;
@@ -66,13 +68,13 @@ impl TryFrom<Vec<u8>> for DeviceLong {
.as_bytes()
.to_vec(),
)?)?,
usb: String::from_utf8(
groups
.name("usb")
.ok_or(RustADBError::RegexParsingError)?
.as_bytes()
.to_vec(),
)?,
usb: match groups.name("usb1") {
None => match groups.name("usb2") {
None => "Unk".to_string(),
Some(usb) => String::from_utf8(usb.as_bytes().to_vec())?,
},
Some(usb) => String::from_utf8(usb.as_bytes().to_vec())?,
},
product: match groups.name("product") {
None => "Unk".to_string(),
Some(product) => String::from_utf8(product.as_bytes().to_vec())?,

View File

@@ -5,31 +5,34 @@ use std::{fmt::Display, str::FromStr};
use crate::{DeviceState, RustADBError};
lazy_static! {
static ref DEVICES_REGEX: Regex = Regex::new("^(\\S+)\t(\\w+)\n?$").unwrap();
static ref DEVICES_REGEX: Regex =
Regex::new("^(\\S+)\t(\\w+)\n?$").expect("Cannot build devices regex");
}
/// Represents a device connected to the ADB server.
#[derive(Debug)]
pub struct Device {
#[derive(Debug, Clone)]
pub struct DeviceShort {
/// Unique device identifier.
pub identifier: String,
/// Connection state of the device.
pub state: DeviceState,
}
impl Display for Device {
impl Display for DeviceShort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}\t{}", self.identifier, self.state)
}
}
impl TryFrom<Vec<u8>> for Device {
impl TryFrom<Vec<u8>> for DeviceShort {
type Error = RustADBError;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
// Optional final '\n' is used to match TrackDevices inputs
let groups = DEVICES_REGEX.captures(&value).unwrap();
Ok(Device {
let groups = DEVICES_REGEX
.captures(&value)
.ok_or(RustADBError::RegexParsingError)?;
Ok(DeviceShort {
identifier: String::from_utf8(
groups
.get(1)

View File

@@ -3,7 +3,7 @@ use std::{fmt::Display, str::FromStr};
use crate::RustADBError;
/// Represents the connection state of the device.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum DeviceState {
/// The device is not connected to adb or is not responding.
Offline,

View File

@@ -1,18 +1,22 @@
mod adb_command;
mod adb_emulator_command;
mod adb_request_status;
mod adb_server_command;
mod adb_stat_response;
mod adb_version;
mod device;
mod device_long;
mod device_short;
mod device_state;
mod host_features;
mod reboot_type;
mod sync_command;
pub use adb_command::AdbCommand;
pub(crate) use adb_emulator_command::ADBEmulatorCommand;
pub use adb_request_status::AdbRequestStatus;
pub(crate) use adb_server_command::AdbServerCommand;
pub use adb_stat_response::AdbStatResponse;
pub use adb_version::AdbVersion;
pub use device::Device;
pub use device_long::DeviceLong;
pub use device_short::DeviceShort;
pub use device_state::DeviceState;
pub use host_features::HostFeatures;
pub use reboot_type::RebootType;

View File

@@ -0,0 +1,81 @@
use std::net::{Ipv4Addr, SocketAddrV4};
use crate::{ADBTransport, Result, RustADBError, TCPEmulatorTransport};
use lazy_static::lazy_static;
use regex::Regex;
use super::ADBServerDevice;
lazy_static! {
pub static ref EMULATOR_REGEX: Regex =
Regex::new("^emulator-(?P<port>\\d+)$").expect("wrong syntax for emulator regex");
}
/// Represents an emulator connected to the ADB server.
#[derive(Debug)]
pub struct ADBEmulatorDevice {
/// Unique device identifier.
pub identifier: String,
/// Internal [TCPEmulatorTransport]
transport: TCPEmulatorTransport,
}
impl ADBEmulatorDevice {
/// Instantiates a new [ADBEmulatorDevice]
pub fn new(identifier: String, ip_address: Option<Ipv4Addr>) -> Result<Self> {
let ip_address = match ip_address {
Some(ip_address) => ip_address,
None => Ipv4Addr::new(127, 0, 0, 1),
};
let groups = EMULATOR_REGEX
.captures(&identifier)
.ok_or(RustADBError::DeviceNotFound(format!(
"Device {} is likely not an emulator",
identifier
)))?;
let port = groups
.name("port")
.ok_or(RustADBError::RegexParsingError)?
.as_str()
.parse::<u16>()?;
let socket_addr = SocketAddrV4::new(ip_address, port);
let transport = TCPEmulatorTransport::new(socket_addr);
Ok(Self {
identifier,
transport,
})
}
pub(crate) fn get_transport_mut(&mut self) -> &mut TCPEmulatorTransport {
&mut self.transport
}
/// Connect to underlying transport
pub(crate) fn connect(&mut self) -> Result<&mut TCPEmulatorTransport> {
self.transport.connect()?;
Ok(self.get_transport_mut())
}
}
impl TryFrom<ADBServerDevice> for ADBEmulatorDevice {
type Error = RustADBError;
fn try_from(value: ADBServerDevice) -> std::result::Result<Self, Self::Error> {
ADBEmulatorDevice::new(
value.identifier.clone(),
Some(*value.get_transport().get_socketaddr().ip()),
)
}
}
impl Drop for ADBEmulatorDevice {
fn drop(&mut self) {
// Best effort here
let _ = self.transport.disconnect();
}
}

View File

@@ -0,0 +1,76 @@
use crate::ADBTransport;
use crate::Result;
use crate::RustADBError;
use crate::TCPServerTransport;
use std::net::SocketAddrV4;
use std::process::Command;
/// Represents an ADB Server
#[derive(Debug, Default)]
pub struct ADBServer {
/// Internal [TcpStream], lazily initialized
pub(crate) transport: Option<TCPServerTransport>,
/// Address to connect to
pub(crate) socket_addr: Option<SocketAddrV4>,
}
impl ADBServer {
/// Instantiates a new [ADBServer]
pub fn new(address: SocketAddrV4) -> Self {
Self {
transport: None,
socket_addr: Some(address),
}
}
/// Returns the current selected transport
pub(crate) fn get_transport(&mut self) -> Result<&mut TCPServerTransport> {
self.transport
.as_mut()
.ok_or(RustADBError::IOError(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"server connection not initialized",
)))
}
/// Connect to underlying transport
pub(crate) fn connect(&mut self) -> Result<&mut TCPServerTransport> {
let mut is_local_ip = false;
let mut transport = if let Some(addr) = &self.socket_addr {
let ip = addr.ip();
if ip.is_loopback() || ip.is_unspecified() {
is_local_ip = true;
}
TCPServerTransport::new(*addr)
} else {
is_local_ip = true;
TCPServerTransport::default()
};
if is_local_ip {
// ADB Server is local, we start it if not already running
let child = Command::new("adb").arg("start-server").spawn();
match child {
Ok(mut child) => {
if let Err(e) = child.wait() {
log::error!("error while starting adb server: {e}")
}
}
Err(e) => log::error!("error while starting adb server: {e}"),
}
}
transport.connect()?;
self.transport = Some(transport);
self.get_transport()
}
}
impl Drop for ADBServer {
fn drop(&mut self) {
if let Some(ref mut transport) = &mut self.transport {
let _ = transport.disconnect();
}
}
}

View File

@@ -0,0 +1,49 @@
use crate::{ADBTransport, Result, TCPServerTransport};
use std::net::SocketAddrV4;
/// Represents a device connected to the ADB server.
#[derive(Debug)]
pub struct ADBServerDevice {
/// Unique device identifier.
pub identifier: String,
/// Internal [TCPServerTransport]
transport: TCPServerTransport,
}
impl ADBServerDevice {
/// Instantiates a new [ADBServerDevice]
pub fn new(identifier: String, socket_addr: Option<SocketAddrV4>) -> Self {
let transport = if let Some(addr) = socket_addr {
TCPServerTransport::new(addr)
} else {
TCPServerTransport::default()
};
Self {
identifier,
transport,
}
}
pub(crate) fn get_transport(&self) -> &TCPServerTransport {
&self.transport
}
pub(crate) fn get_transport_mut(&mut self) -> &mut TCPServerTransport {
&mut self.transport
}
/// Connect to underlying transport
pub(crate) fn connect(&mut self) -> Result<&mut TCPServerTransport> {
self.transport.connect()?;
Ok(self.get_transport_mut())
}
}
impl Drop for ADBServerDevice {
fn drop(&mut self) {
// Best effort here
let _ = self.transport.disconnect();
}
}

View File

@@ -0,0 +1,131 @@
use std::{
io::{Read, Write},
path::Path,
};
use crate::{
constants::BUFFER_SIZE,
models::{AdbServerCommand, AdbStatResponse, HostFeatures},
ADBDeviceExt, ADBServerDevice, Result, RustADBError,
};
impl ADBDeviceExt for ADBServerDevice {
fn shell_command<S: ToString, W: Write>(
&mut self,
command: impl IntoIterator<Item = S>,
mut output: W,
) -> Result<()> {
let supported_features = self.host_features()?;
if !supported_features.contains(&HostFeatures::ShellV2)
&& !supported_features.contains(&HostFeatures::Cmd)
{
return Err(RustADBError::ADBShellNotSupported);
}
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut()
.send_adb_request(AdbServerCommand::ShellCommand(
command
.into_iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" "),
))?;
const BUFFER_SIZE: usize = 4096;
loop {
let mut buffer = [0; BUFFER_SIZE];
match self
.get_transport_mut()
.get_raw_connection()?
.read(&mut buffer)
{
Ok(size) => {
if size == 0 {
return Ok(());
} else {
output.write_all(&buffer[..size])?;
}
}
Err(e) => {
return Err(RustADBError::IOError(e));
}
}
}
}
fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
self.stat(remote_path)
}
fn shell<R: Read, W: Write + Send + 'static>(
&mut self,
mut reader: R,
mut writer: W,
) -> Result<()> {
let supported_features = self.host_features()?;
if !supported_features.contains(&HostFeatures::ShellV2)
&& !supported_features.contains(&HostFeatures::Cmd)
{
return Err(RustADBError::ADBShellNotSupported);
}
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut()
.send_adb_request(AdbServerCommand::Shell)?;
let mut read_stream = self.get_transport_mut().get_raw_connection()?.try_clone()?;
let mut write_stream = read_stream.try_clone()?;
// Reading thread, reads response from adb-server
std::thread::spawn(move || -> Result<()> {
loop {
let mut buffer = [0; BUFFER_SIZE];
match read_stream.read(&mut buffer) {
Ok(0) => {
read_stream.shutdown(std::net::Shutdown::Both)?;
return Ok(());
}
Ok(size) => {
writer.write_all(&buffer[..size])?;
writer.flush()?;
}
Err(e) => {
return Err(RustADBError::IOError(e));
}
}
}
});
// Read from given reader (that could be stdin e.g), and write content to server socket
if let Err(e) = std::io::copy(&mut reader, &mut write_stream) {
match e.kind() {
std::io::ErrorKind::BrokenPipe => return Ok(()),
_ => return Err(RustADBError::IOError(e)),
}
}
Ok(())
}
fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, mut output: W) -> Result<()> {
self.pull(source, &mut output)
}
fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> {
self.reboot(reboot_type)
}
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
self.push(stream, path)
}
fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()> {
self.install(apk_path)
}
}

View File

@@ -0,0 +1,14 @@
use crate::{models::AdbServerCommand, ADBServerDevice, Result};
impl ADBServerDevice {
/// Forward socket connection
pub fn forward(&mut self, remote: String, local: String) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?;
self.get_transport_mut()
.proxy_connection(AdbServerCommand::Forward(serial, remote, local), false)
.map(|_| ())
}
}

View File

@@ -0,0 +1,176 @@
use std::{io::Read, iter::Map, path::Path, slice::ChunksExact};
use byteorder::{LittleEndian, ReadBytesExt};
use image::{ImageBuffer, Rgba};
use crate::{models::AdbServerCommand, utils, ADBServerDevice, Result, RustADBError};
type U32ChunkIter<'a> = Map<ChunksExact<'a, u8>, fn(&[u8]) -> Result<u32>>;
fn read_next(chunks: &mut U32ChunkIter) -> Result<u32> {
chunks
.next()
.ok_or(RustADBError::FramebufferConversionError)?
}
#[derive(Debug)]
struct FrameBufferInfoV1 {
pub _bpp: u32,
pub size: u32,
pub width: u32,
pub height: u32,
pub _red_offset: u32,
pub _red_length: u32,
pub _blue_offset: u32,
pub _blue_length: u32,
pub _green_offset: u32,
pub _green_length: u32,
pub _alpha_offset: u32,
pub _alpha_length: u32,
}
impl TryFrom<[u8; std::mem::size_of::<Self>()]> for FrameBufferInfoV1 {
type Error = RustADBError;
fn try_from(
value: [u8; std::mem::size_of::<Self>()],
) -> std::result::Result<Self, Self::Error> {
let mut chunks: U32ChunkIter = value.chunks_exact(4).map(utils::u32_from_le);
Ok(Self {
_bpp: read_next(&mut chunks)?,
size: read_next(&mut chunks)?,
width: read_next(&mut chunks)?,
height: read_next(&mut chunks)?,
_red_offset: read_next(&mut chunks)?,
_red_length: read_next(&mut chunks)?,
_blue_offset: read_next(&mut chunks)?,
_blue_length: read_next(&mut chunks)?,
_green_offset: read_next(&mut chunks)?,
_green_length: read_next(&mut chunks)?,
_alpha_offset: read_next(&mut chunks)?,
_alpha_length: read_next(&mut chunks)?,
})
}
}
#[derive(Debug)]
struct FrameBufferInfoV2 {
pub _bpp: u32,
pub _color_space: u32,
pub size: u32,
pub width: u32,
pub height: u32,
pub _red_offset: u32,
pub _red_length: u32,
pub _blue_offset: u32,
pub _blue_length: u32,
pub _green_offset: u32,
pub _green_length: u32,
pub _alpha_offset: u32,
pub _alpha_length: u32,
}
impl TryFrom<[u8; std::mem::size_of::<Self>()]> for FrameBufferInfoV2 {
type Error = RustADBError;
fn try_from(
value: [u8; std::mem::size_of::<Self>()],
) -> std::result::Result<Self, Self::Error> {
let mut chunks: U32ChunkIter = value.chunks_exact(4).map(utils::u32_from_le);
Ok(Self {
_bpp: read_next(&mut chunks)?,
_color_space: read_next(&mut chunks)?,
size: read_next(&mut chunks)?,
width: read_next(&mut chunks)?,
height: read_next(&mut chunks)?,
_red_offset: read_next(&mut chunks)?,
_red_length: read_next(&mut chunks)?,
_blue_offset: read_next(&mut chunks)?,
_blue_length: read_next(&mut chunks)?,
_green_offset: read_next(&mut chunks)?,
_green_length: read_next(&mut chunks)?,
_alpha_offset: read_next(&mut chunks)?,
_alpha_length: read_next(&mut chunks)?,
})
}
}
impl ADBServerDevice {
/// Dump framebuffer of this device
/// 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<()> {
let img = self.framebuffer_inner()?;
Ok(img.save(path.as_ref())?)
}
/// Inner method requesting framebuffer from Android device
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
let serial: String = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut()
.send_adb_request(AdbServerCommand::FrameBuffer)?;
let version = self
.get_transport_mut()
.get_raw_connection()?
.read_u32::<LittleEndian>()?;
match version {
// RGBA_8888
1 => {
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV1>()];
self.get_transport_mut()
.get_raw_connection()?
.read_exact(&mut buf)?;
let h: FrameBufferInfoV1 = buf.try_into()?;
let mut data = vec![
0_u8;
h.size
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
self.get_transport_mut()
.get_raw_connection()?
.read_exact(&mut data)?;
Ok(
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(h.width, h.height, data)
.ok_or_else(|| RustADBError::FramebufferConversionError)?,
)
}
// RGBX_8888
2 => {
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV2>()];
self.get_transport_mut()
.get_raw_connection()?
.read_exact(&mut buf)?;
let h: FrameBufferInfoV2 = buf.try_into()?;
let mut data = vec![
0_u8;
h.size
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
self.get_transport_mut()
.get_raw_connection()?
.read_exact(&mut data)?;
Ok(
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(h.width, h.height, data)
.ok_or_else(|| RustADBError::FramebufferConversionError)?,
)
}
v => Err(RustADBError::UnimplementedFramebufferImageVersion(v)),
}
}
}

View File

@@ -0,0 +1,22 @@
use crate::{
models::{AdbServerCommand, HostFeatures},
ADBServerDevice, Result,
};
impl ADBServerDevice {
/// Lists available ADB server features.
pub fn host_features(&mut self) -> Result<Vec<HostFeatures>> {
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
let features = self
.get_transport_mut()
.proxy_connection(AdbServerCommand::HostFeatures, true)?;
Ok(features
.split(|x| x.eq(&b','))
.filter_map(|v| HostFeatures::try_from(v).ok())
.collect())
}
}

View 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(),
)?)),
}
}
}

View File

@@ -1,6 +1,6 @@
use crate::{
models::{AdbCommand, SyncCommand},
AdbTcpConnection, Result,
models::{AdbServerCommand, SyncCommand},
ADBServerDevice, Result,
};
use byteorder::{ByteOrder, LittleEndian};
use std::{
@@ -8,23 +8,20 @@ use std::{
str,
};
impl AdbTcpConnection {
impl ADBServerDevice {
/// Lists files in [path] on the device.
pub fn list<S: ToString, A: AsRef<str>>(&mut self, serial: Option<S>, path: A) -> Result<()> {
self.new_connection()?;
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
pub fn list<A: AsRef<str>>(&mut self, path: A) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode
self.send_adb_request(AdbCommand::Sync)?;
self.get_transport_mut()
.send_adb_request(AdbServerCommand::Sync)?;
// Send a list command
self.send_sync_request(SyncCommand::List)?;
self.get_transport_mut()
.send_sync_request(SyncCommand::List)?;
self.handle_list_command(path)
}
@@ -36,16 +33,21 @@ impl AdbTcpConnection {
LittleEndian::write_u32(&mut len_buf, path.as_ref().len() as u32);
// 4 bytes of command name is already sent by send_sync_request
self.tcp_stream.write_all(&len_buf)?;
self.get_transport_mut()
.get_raw_connection()?
.write_all(&len_buf)?;
// List sends the string of the directory to list, and then the server sends a list of files
self.tcp_stream
// List send the string of the directory to list, and then the server send a list of files
self.get_transport_mut()
.get_raw_connection()?
.write_all(path.as_ref().to_string().as_bytes())?;
// Reads returned status code from ADB server
let mut response = [0_u8; 4];
loop {
self.tcp_stream.read_exact(&mut response)?;
self.get_transport_mut()
.get_raw_connection()?
.read_exact(&mut response)?;
match str::from_utf8(response.as_ref())? {
"DENT" => {
// TODO: Move this to a struct that extract this data, but as the device
@@ -54,18 +56,21 @@ impl AdbTcpConnection {
let mut file_size = [0_u8; 4];
let mut mod_time = [0_u8; 4];
let mut name_len = [0_u8; 4];
self.tcp_stream.read_exact(&mut file_mod)?;
self.tcp_stream.read_exact(&mut file_size)?;
self.tcp_stream.read_exact(&mut mod_time)?;
self.tcp_stream.read_exact(&mut name_len)?;
let mut connection = self.get_transport_mut().get_raw_connection()?;
connection.read_exact(&mut file_mod)?;
connection.read_exact(&mut file_size)?;
connection.read_exact(&mut mod_time)?;
connection.read_exact(&mut name_len)?;
let name_len = LittleEndian::read_u32(&name_len);
let mut name_buf = vec![0_u8; name_len as usize];
self.tcp_stream.read_exact(&mut name_buf)?;
connection.read_exact(&mut name_buf)?;
}
"DONE" => {
return Ok(());
}
x => println!("Unknown response {}", x),
x => log::error!("Got an unknown response {}", x),
}
}
}

View File

@@ -0,0 +1,56 @@
use std::io::{self, Write};
use crate::{ADBDeviceExt, ADBServerDevice, Result};
struct LogFilter<W: Write> {
writer: W,
buffer: Vec<u8>,
}
impl<W: Write> LogFilter<W> {
pub fn new(writer: W) -> Self {
LogFilter {
writer,
buffer: Vec::new(),
}
}
fn should_write(&self, _line: &[u8]) -> bool {
// Can implement checks here to ensure if logs have to be written
true
}
}
impl<W: Write> Write for LogFilter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buffer.extend_from_slice(buf);
let buf_clone = self.buffer.clone();
let mut lines = buf_clone.split_inclusive(|&byte| byte == b'\n').peekable();
while let Some(line) = lines.next() {
if lines.peek().is_some() {
if self.should_write(line) {
self.writer.write_all(line)?;
}
} else {
// This is the last (unfinished) element, we keep it for next round
self.buffer = line.to_vec();
break;
}
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
self.writer.flush()
}
}
impl ADBServerDevice {
/// Get logs from device
pub fn get_logs<W: Write>(&mut self, output: W) -> Result<()> {
self.shell_command(["exec logcat"], LogFilter::new(output))
}
}

View File

@@ -1,11 +1,12 @@
mod devices;
mod forward;
mod framebuffer;
mod host_features;
mod kill;
mod install;
mod list;
mod logcat;
mod reboot;
mod recv;
mod reverse;
mod send;
mod shell;
mod stat;
mod transport;
mod version;

View File

@@ -0,0 +1,17 @@
use crate::{
models::{AdbServerCommand, RebootType},
ADBServerDevice, Result,
};
impl ADBServerDevice {
/// Reboots the device
pub fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut()
.proxy_connection(AdbServerCommand::Reboot(reboot_type), false)
.map(|_| ())
}
}

View File

@@ -0,0 +1,111 @@
use crate::{
constants,
models::{AdbServerCommand, SyncCommand},
ADBServerDevice, Result,
};
use byteorder::{LittleEndian, ReadBytesExt};
use std::io::{BufReader, BufWriter, Read, Write};
/// Internal structure wrapping a [std::io::Read] and hiding underlying protocol logic.
struct ADBRecvCommandReader<R: Read> {
inner: R,
remaining_data_bytes_to_read: usize,
}
impl<R: Read> ADBRecvCommandReader<R> {
pub fn new(inner: R) -> Self {
Self {
inner,
remaining_data_bytes_to_read: 0,
}
}
}
impl<R: Read> Read for ADBRecvCommandReader<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
// In case of a "DATA" header, we may not have enough space in `buf` to fill it with "length" bytes coming from device.
// `remaining_data_bytes_to_read` represents how many bytes are still left to read before receiving another header.
if self.remaining_data_bytes_to_read == 0 {
let mut header = [0_u8; 4];
self.inner.read_exact(&mut header)?;
match &header[..] {
b"DATA" => {
let length = self.inner.read_u32::<LittleEndian>()? as usize;
let effective_read = self.inner.read(&mut buf[0..length])?;
self.remaining_data_bytes_to_read = length - effective_read;
Ok(effective_read)
}
b"DONE" => Ok(0),
b"FAIL" => {
let length = self.inner.read_u32::<LittleEndian>()? as usize;
let mut error_msg = vec![0; length];
self.inner.read_exact(&mut error_msg)?;
Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"ADB request failed: {}",
String::from_utf8_lossy(&error_msg)
),
))
}
_ => Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Unknown response from device {:#?}", header),
)),
}
} else {
// Computing minimum to ensure to stop reading before next header...
let data_to_read = std::cmp::min(self.remaining_data_bytes_to_read, buf.len());
self.inner.read_exact(&mut buf[..data_to_read])?;
self.remaining_data_bytes_to_read -= self.remaining_data_bytes_to_read;
Ok(data_to_read)
}
}
}
impl ADBServerDevice {
/// Receives [path] to [stream] from the device.
pub fn pull<A: AsRef<str>>(&mut self, path: A, stream: &mut dyn Write) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode
self.get_transport_mut()
.send_adb_request(AdbServerCommand::Sync)?;
// Send a recv command
self.get_transport_mut()
.send_sync_request(SyncCommand::Recv)?;
self.handle_recv_command(path, stream)
}
fn handle_recv_command<S: AsRef<str>>(
&mut self,
from: S,
output: &mut dyn Write,
) -> Result<()> {
let mut raw_connection = self.get_transport().get_raw_connection()?;
let from_as_bytes = from.as_ref().as_bytes();
let mut buffer = Vec::with_capacity(4 + from_as_bytes.len());
buffer.extend_from_slice(&(from.as_ref().len() as u32).to_le_bytes());
buffer.extend_from_slice(from_as_bytes);
raw_connection.write_all(&buffer)?;
let reader = ADBRecvCommandReader::new(raw_connection);
std::io::copy(
&mut BufReader::with_capacity(constants::BUFFER_SIZE, reader),
&mut BufWriter::with_capacity(constants::BUFFER_SIZE, output),
)?;
// Connection should've been left in SYNC mode by now
Ok(())
}
}

View File

@@ -0,0 +1,14 @@
use crate::{models::AdbServerCommand, ADBServerDevice, Result};
impl ADBServerDevice {
/// Reverse socket connection
pub fn reverse(&mut self, remote: String, local: String) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut()
.proxy_connection(AdbServerCommand::Reverse(remote, local), false)
.map(|_| ())
}
}

View File

@@ -0,0 +1,121 @@
use crate::{
constants,
models::{AdbRequestStatus, AdbServerCommand, SyncCommand},
ADBServerDevice, Result, RustADBError,
};
use std::{
convert::TryInto,
io::{BufReader, BufWriter, Read, Write},
str::{self, FromStr},
time::SystemTime,
};
/// Internal structure wrapping a [std::io::Write] and hiding underlying protocol logic.
struct ADBSendCommandWriter<W: Write> {
inner: W,
}
impl<W: Write> ADBSendCommandWriter<W> {
pub fn new(inner: W) -> Self {
ADBSendCommandWriter { inner }
}
}
impl<W: Write> Write for ADBSendCommandWriter<W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let chunk_len = buf.len() as u32;
// 8 = "DATA".len() + sizeof(u32)
let mut buffer = Vec::with_capacity(8 + buf.len());
buffer.extend_from_slice(b"DATA");
buffer.extend_from_slice(&chunk_len.to_le_bytes());
buffer.extend_from_slice(buf);
self.inner.write_all(&buffer)?;
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}
impl ADBServerDevice {
/// Send [stream] to [path] on the device.
pub fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
log::info!("Sending data to {}", path.as_ref());
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode
self.get_transport_mut()
.send_adb_request(AdbServerCommand::Sync)?;
// Send a send command
self.get_transport_mut()
.send_sync_request(SyncCommand::Send)?;
self.handle_send_command(stream, path)
}
fn handle_send_command<R: Read, S: AsRef<str>>(&mut self, input: R, to: S) -> Result<()> {
// Append the permission flags to the filename
let to = to.as_ref().to_string() + ",0777";
let mut raw_connection = self.get_transport_mut().get_raw_connection()?;
// The name of the command is already sent by get_transport()?.send_sync_request
let to_as_bytes = to.as_bytes();
let mut buffer = Vec::with_capacity(4 + to_as_bytes.len());
buffer.extend_from_slice(&(to.len() as u32).to_le_bytes());
buffer.extend_from_slice(to_as_bytes);
raw_connection.write_all(&buffer)?;
let writer = ADBSendCommandWriter::new(raw_connection);
std::io::copy(
&mut BufReader::with_capacity(constants::BUFFER_SIZE, input),
&mut BufWriter::with_capacity(constants::BUFFER_SIZE, writer),
)?;
// Copy is finished, we can now notify as finished
// Have to send DONE + file mtime
let last_modified = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n,
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
};
let mut done_buffer = Vec::with_capacity(8);
done_buffer.extend_from_slice(b"DONE");
done_buffer.extend_from_slice(&last_modified.as_secs().to_le_bytes());
raw_connection.write_all(&done_buffer)?;
// We expect 'OKAY' response from this
let mut request_status = [0; 4];
raw_connection.read_exact(&mut request_status)?;
match AdbRequestStatus::from_str(str::from_utf8(&request_status)?)? {
AdbRequestStatus::Fail => {
// We can keep reading to get further details
let length = self.get_transport_mut().get_body_length()?;
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
if length > 0 {
self.get_transport()
.get_raw_connection()?
.read_exact(&mut body)?;
}
Err(RustADBError::ADBRequestFailed(String::from_utf8(body)?))
}
AdbRequestStatus::Okay => Ok(()),
}
}
}

View File

@@ -0,0 +1,60 @@
use std::io::{Read, Write};
use byteorder::{ByteOrder, LittleEndian};
use crate::{
models::{AdbServerCommand, AdbStatResponse, SyncCommand},
ADBServerDevice, Result, RustADBError,
};
impl ADBServerDevice {
fn handle_stat_command<S: AsRef<str>>(&mut self, path: S) -> Result<AdbStatResponse> {
let mut len_buf = [0_u8; 4];
LittleEndian::write_u32(&mut len_buf, path.as_ref().len() as u32);
// 4 bytes of command name is already sent by send_sync_request
self.get_transport_mut()
.get_raw_connection()?
.write_all(&len_buf)?;
self.get_transport_mut()
.get_raw_connection()?
.write_all(path.as_ref().to_string().as_bytes())?;
// Reads returned status code from ADB server
let mut response = [0_u8; 4];
self.get_transport_mut()
.get_raw_connection()?
.read_exact(&mut response)?;
match std::str::from_utf8(response.as_ref())? {
"STAT" => {
let mut data = [0_u8; 12];
self.get_transport_mut()
.get_raw_connection()?
.read_exact(&mut data)?;
Ok(data.into())
}
x => Err(RustADBError::UnknownResponseType(format!(
"Unknown response {}",
x
))),
}
}
/// Stat file given as [path] on the device.
pub fn stat<A: AsRef<str>>(&mut self, path: A) -> Result<AdbStatResponse> {
let serial = self.identifier.clone();
self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode
self.get_transport_mut()
.send_adb_request(AdbServerCommand::Sync)?;
// Send a "Stat" command
self.get_transport_mut()
.send_sync_request(SyncCommand::Stat)?;
self.handle_stat_command(path)
}
}

View File

@@ -1,9 +1,10 @@
use crate::{models::AdbCommand, AdbTcpConnection, Result};
use crate::{models::AdbServerCommand, ADBServerDevice, Result};
impl AdbTcpConnection {
impl ADBServerDevice {
/// Asks ADB server to switch the connection to either the device or emulator connect to/running on the host. Will fail if there is more than one such device/emulator available.
pub fn transport_any(&mut self) -> Result<()> {
self.proxy_connection(AdbCommand::TransportAny, false)
self.connect()?
.proxy_connection(AdbServerCommand::TransportAny, false)
.map(|_| ())
}
}

View File

@@ -0,0 +1,10 @@
mod adb_emulator_device;
mod adb_server;
mod adb_server_device;
mod adb_server_device_commands;
mod device_commands;
mod server_commands;
pub use adb_emulator_device::ADBEmulatorDevice;
pub use adb_server::ADBServer;
pub use adb_server_device::ADBServerDevice;

View File

@@ -0,0 +1,17 @@
use crate::{models::AdbServerCommand, ADBServer, Result, RustADBError};
use std::net::SocketAddrV4;
impl ADBServer {
/// Connect device over tcp with address and port
pub fn connect_device(&mut self, address: SocketAddrV4) -> Result<()> {
let response = self
.connect()?
.proxy_connection(AdbServerCommand::Connect(address), true)?;
match String::from_utf8(response) {
Ok(s) if s.starts_with("connected to") => Ok(()),
Ok(s) => Err(RustADBError::ADBRequestFailed(s)),
Err(e) => Err(e.into()),
}
}
}

View File

@@ -0,0 +1,118 @@
use std::io::Read;
use crate::{
models::{AdbServerCommand, DeviceShort},
ADBEmulatorDevice, ADBServer, ADBServerDevice, DeviceLong, Result, RustADBError,
};
impl ADBServer {
/// Gets a list of connected devices.
pub fn devices(&mut self) -> Result<Vec<DeviceShort>> {
let devices = self
.connect()?
.proxy_connection(AdbServerCommand::Devices, true)?;
let mut vec_devices: Vec<DeviceShort> = vec![];
for device in devices.split(|x| x.eq(&b'\n')) {
if device.is_empty() {
break;
}
vec_devices.push(DeviceShort::try_from(device.to_vec())?);
}
Ok(vec_devices)
}
/// Gets an extended list of connected devices including the device paths in the state.
pub fn devices_long(&mut self) -> Result<Vec<DeviceLong>> {
let devices_long = self
.connect()?
.proxy_connection(AdbServerCommand::DevicesLong, true)?;
let mut vec_devices: Vec<DeviceLong> = vec![];
for device in devices_long.split(|x| x.eq(&b'\n')) {
if device.is_empty() {
break;
}
vec_devices.push(DeviceLong::try_from(device.to_vec())?);
}
Ok(vec_devices)
}
/// Get a device, assuming that only this device is connected.
pub fn get_device(&mut self) -> Result<ADBServerDevice> {
let mut devices = self.devices()?.into_iter();
match devices.next() {
Some(device) => match devices.next() {
Some(_) => Err(RustADBError::DeviceNotFound(
"too many devices connected".to_string(),
)),
None => Ok(ADBServerDevice::new(device.identifier, self.socket_addr)),
},
None => Err(RustADBError::DeviceNotFound(
"no device connected".to_string(),
)),
}
}
/// Get a device matching the given name, if existing.
/// - There is no device connected => Error
/// - There is a single device connected => Ok
/// - There are more than 1 device connected => Error
pub fn get_device_by_name(&mut self, name: &str) -> Result<ADBServerDevice> {
let nb_devices = self
.devices()?
.into_iter()
.filter(|d| d.identifier.as_str() == name)
.collect::<Vec<DeviceShort>>()
.len();
if nb_devices != 1 {
Err(RustADBError::DeviceNotFound(format!(
"could not find device {name}"
)))
} else {
Ok(ADBServerDevice::new(name.to_string(), self.socket_addr))
}
}
/// Tracks new devices showing up.
pub fn track_devices(&mut self, callback: impl Fn(DeviceShort) -> Result<()>) -> Result<()> {
self.connect()?
.send_adb_request(AdbServerCommand::TrackDevices)?;
loop {
let length = self.get_transport()?.get_hex_body_length()?;
if length > 0 {
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
self.get_transport()?
.get_raw_connection()?
.read_exact(&mut body)?;
callback(DeviceShort::try_from(body)?)?;
}
}
}
/// Get an emulator, assuming that only this device is connected.
pub fn get_emulator_device(&mut self) -> Result<ADBEmulatorDevice> {
let device = self.get_device()?;
ADBEmulatorDevice::try_from(device)
}
/// Get an emulator by its name
pub fn get_emulator_device_by_name(&mut self, name: &str) -> Result<ADBEmulatorDevice> {
let device = self.get_device_by_name(name)?;
ADBEmulatorDevice::try_from(device)
}
}

View File

@@ -0,0 +1,17 @@
use crate::{models::AdbServerCommand, ADBServer, Result, RustADBError};
use std::net::SocketAddrV4;
impl ADBServer {
/// Connect device over tcp with address and port
pub fn disconnect_device(&mut self, address: SocketAddrV4) -> Result<()> {
let response = self
.connect()?
.proxy_connection(AdbServerCommand::Disconnect(address), true)?;
match String::from_utf8(response) {
Ok(s) if s.starts_with("disconnected") => Ok(()),
Ok(s) => Err(RustADBError::ADBRequestFailed(s)),
Err(e) => Err(e.into()),
}
}
}

View File

@@ -0,0 +1,10 @@
use crate::{models::AdbServerCommand, ADBServer, Result};
impl ADBServer {
/// Asks the ADB server to quit immediately.
pub fn kill(&mut self) -> Result<()> {
self.connect()?
.proxy_connection(AdbServerCommand::Kill, false)
.map(|_| ())
}
}

View File

@@ -0,0 +1,6 @@
mod connect;
mod devices;
mod disconnect;
mod kill;
mod pair;
mod version;

View File

@@ -0,0 +1,18 @@
use crate::models::AdbServerCommand;
use crate::{ADBServer, Result, RustADBError};
use std::net::SocketAddrV4;
impl ADBServer {
/// Pair device on a specific port with a generated 'code'
pub fn pair(&mut self, address: SocketAddrV4, code: String) -> Result<()> {
let response = self
.connect()?
.proxy_connection(AdbServerCommand::Pair(address, code), true)?;
match String::from_utf8(response) {
Ok(s) if s.starts_with("Successfully paired to") => Ok(()),
Ok(s) => Err(RustADBError::ADBRequestFailed(s)),
Err(e) => Err(e.into()),
}
}
}

View File

@@ -0,0 +1,12 @@
use crate::{models::AdbServerCommand, ADBServer, AdbVersion, Result};
impl ADBServer {
/// Gets server's internal version number.
pub fn version(&mut self) -> Result<AdbVersion> {
let version = self
.connect()?
.proxy_connection(AdbServerCommand::Version, true)?;
AdbVersion::try_from(version)
}
}

View File

@@ -0,0 +1,8 @@
mod tcp_emulator_transport;
mod tcp_server_transport;
mod transport_trait;
mod usb_transport;
pub use tcp_emulator_transport::TCPEmulatorTransport;
pub use tcp_server_transport::TCPServerTransport;
pub use transport_trait::ADBTransport;
pub use usb_transport::USBTransport;

View File

@@ -0,0 +1,127 @@
use std::{
fs::File,
io::{BufRead, BufReader, Read, Write},
net::{SocketAddrV4, TcpStream},
};
use homedir::my_home;
use super::ADBTransport;
use crate::{models::ADBEmulatorCommand, Result, RustADBError};
/// Emulator transport running on top on TCP.
#[derive(Debug)]
pub struct TCPEmulatorTransport {
socket_addr: SocketAddrV4,
tcp_stream: Option<TcpStream>,
}
impl TCPEmulatorTransport {
/// Instantiates a new instance of [TCPEmulatorTransport]
pub fn new(socket_addr: SocketAddrV4) -> Self {
Self {
socket_addr,
tcp_stream: None,
}
}
pub(crate) fn get_raw_connection(&self) -> Result<&TcpStream> {
self.tcp_stream
.as_ref()
.ok_or(RustADBError::IOError(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"not connected",
)))
}
/// Return authentication token stored in $HOME/.emulator_console_auth_token
pub fn get_authentication_token(&mut self) -> Result<String> {
let home = match my_home()? {
Some(home) => home,
None => return Err(RustADBError::NoHomeDirectory),
};
let mut f = File::open(home.join(".emulator_console_auth_token"))?;
let mut token = String::new();
f.read_to_string(&mut token)?;
Ok(token)
}
/// Send an authenticate request to this emulator
pub fn authenticate(&mut self) -> Result<()> {
let token = self.get_authentication_token()?;
self.send_command(ADBEmulatorCommand::Authenticate(token))
}
/// Send an [ADBEmulatorCommand] to this emulator
pub(crate) fn send_command(&mut self, command: ADBEmulatorCommand) -> Result<()> {
let mut connection = self.get_raw_connection()?;
// Send command
connection.write_all(command.to_string().as_bytes())?;
// Check is an error occurred skipping lines depending on command
self.check_error(command.skip_response_lines())?;
Ok(())
}
fn check_error(&mut self, skipping: u8) -> Result<()> {
let mut reader = BufReader::new(self.get_raw_connection()?);
for _ in 0..skipping {
let mut line = String::new();
reader.read_line(&mut line)?;
if line.starts_with("KO:") {
return Err(RustADBError::ADBRequestFailed(line));
}
}
let mut line = String::new();
reader.read_line(&mut line)?;
match line.starts_with("OK") {
true => Ok(()),
false => Err(RustADBError::ADBRequestFailed(line)),
}
}
}
impl ADBTransport for TCPEmulatorTransport {
fn disconnect(&mut self) -> Result<()> {
if let Some(conn) = &mut self.tcp_stream {
conn.shutdown(std::net::Shutdown::Both)?;
log::trace!("Disconnected from {}", conn.peer_addr()?);
}
Ok(())
}
/// Connect to current emulator and authenticate
fn connect(&mut self) -> Result<()> {
if self.tcp_stream.is_none() {
let stream = TcpStream::connect(self.socket_addr)?;
log::trace!("Successfully connected to {}", self.socket_addr);
self.tcp_stream = Some(stream.try_clone()?);
let mut reader = BufReader::new(stream);
// Android Console: Authentication required
// Android Console: type 'auth <auth_token>' to authenticate
// Android Console: you can find your <auth_token> in
// '/home/xxx/.emulator_console_auth_token'
for _ in 0..=4 {
let mut line = String::new();
reader.read_line(&mut line)?;
}
self.authenticate()?;
log::trace!("Authentication successful");
}
Ok(())
}
}

View File

@@ -0,0 +1,164 @@
use std::io::{Read, Write};
use std::net::{Ipv4Addr, SocketAddrV4, TcpStream};
use std::str::FromStr;
use byteorder::{ByteOrder, LittleEndian};
use crate::models::{AdbRequestStatus, SyncCommand};
use crate::{models::AdbServerCommand, ADBTransport};
use crate::{Result, RustADBError};
const DEFAULT_SERVER_IP: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
const DEFAULT_SERVER_PORT: u16 = 5037;
/// Server transport running on top on TCP
#[derive(Debug)]
pub struct TCPServerTransport {
socket_addr: SocketAddrV4,
tcp_stream: Option<TcpStream>,
}
impl Default for TCPServerTransport {
fn default() -> Self {
Self::new(SocketAddrV4::new(DEFAULT_SERVER_IP, DEFAULT_SERVER_PORT))
}
}
impl TCPServerTransport {
/// Instantiates a new instance of [TCPServerTransport]
pub fn new(socket_addr: SocketAddrV4) -> Self {
Self {
socket_addr,
tcp_stream: None,
}
}
/// Get underlying [SocketAddrV4]
pub fn get_socketaddr(&self) -> SocketAddrV4 {
self.socket_addr
}
pub(crate) fn proxy_connection(
&mut self,
adb_command: AdbServerCommand,
with_response: bool,
) -> Result<Vec<u8>> {
self.send_adb_request(adb_command)?;
if with_response {
let length = self.get_hex_body_length()?;
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
if length > 0 {
self.get_raw_connection()?.read_exact(&mut body)?;
}
Ok(body)
} else {
Ok(vec![])
}
}
pub(crate) fn get_raw_connection(&self) -> Result<&TcpStream> {
self.tcp_stream
.as_ref()
.ok_or(RustADBError::IOError(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"not connected",
)))
}
/// Gets the body length from hexadecimal value
pub(crate) fn get_hex_body_length(&mut self) -> Result<u32> {
let length_buffer = self.read_body_length()?;
Ok(u32::from_str_radix(
std::str::from_utf8(&length_buffer)?,
16,
)?)
}
/// Send the given [SyncCommand] to ADB server, and checks that the request has been taken in consideration.
pub(crate) fn send_sync_request(&mut self, command: SyncCommand) -> Result<()> {
// First 4 bytes are the name of the command we want to send
// (e.g. "SEND", "RECV", "STAT", "LIST")
Ok(self
.get_raw_connection()?
.write_all(command.to_string().as_bytes())?)
}
/// Gets the body length from a LittleEndian value
pub(crate) fn get_body_length(&mut self) -> Result<u32> {
let length_buffer = self.read_body_length()?;
Ok(LittleEndian::read_u32(&length_buffer))
}
/// Read 4 bytes representing body length
fn read_body_length(&mut self) -> Result<[u8; 4]> {
let mut length_buffer = [0; 4];
self.get_raw_connection()?.read_exact(&mut length_buffer)?;
Ok(length_buffer)
}
/// Send the given [AdbCommand] to ADB server, and checks that the request has been taken in consideration.
/// If an error occurred, a [RustADBError] is returned with the response error string.
pub(crate) fn send_adb_request(&mut self, command: AdbServerCommand) -> Result<()> {
let adb_command_string = command.to_string();
let adb_request = format!("{:04x}{}", adb_command_string.len(), adb_command_string);
self.get_raw_connection()?
.write_all(adb_request.as_bytes())?;
// Reads returned status code from ADB server
let mut request_status = [0; 4];
self.get_raw_connection()?.read_exact(&mut request_status)?;
match AdbRequestStatus::from_str(std::str::from_utf8(request_status.as_ref())?)? {
AdbRequestStatus::Fail => {
// We can keep reading to get further details
let length = self.get_hex_body_length()?;
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
if length > 0 {
self.get_raw_connection()?.read_exact(&mut body)?;
}
Err(RustADBError::ADBRequestFailed(String::from_utf8(body)?))
}
AdbRequestStatus::Okay => Ok(()),
}
}
}
impl ADBTransport for TCPServerTransport {
fn disconnect(&mut self) -> Result<()> {
if let Some(conn) = &mut self.tcp_stream {
conn.shutdown(std::net::Shutdown::Both)?;
log::trace!("Disconnected from {}", conn.peer_addr()?);
}
Ok(())
}
fn connect(&mut self) -> Result<()> {
if let Some(previous) = &self.tcp_stream {
// Ignoring underlying error, we will recreate a new connection
let _ = previous.shutdown(std::net::Shutdown::Both);
}
let tcp_stream = TcpStream::connect(self.socket_addr)?;
tcp_stream.set_nodelay(true)?;
self.tcp_stream = Some(tcp_stream);
log::trace!("Successfully connected to {}", self.socket_addr);
Ok(())
}
}

View File

@@ -0,0 +1,10 @@
use crate::Result;
/// Trait representing a transport
pub trait ADBTransport {
/// Initializes the connection
fn connect(&mut self) -> Result<()>;
/// Shuts down the connection
fn disconnect(&mut self) -> Result<()>;
}

View File

@@ -0,0 +1,239 @@
use std::{sync::Arc, time::Duration};
use rusb::{
constants::LIBUSB_CLASS_VENDOR_SPEC, DeviceHandle, Direction, GlobalContext, TransferType,
};
use super::ADBTransport;
use crate::{
usb::{ADBUsbMessage, ADBUsbMessageHeader, USBCommand},
Result, RustADBError,
};
#[derive(Debug)]
struct Endpoint {
iface: u8,
address: u8,
}
const MAX_READ_TIMEOUT: Duration = Duration::from_secs(u64::MAX);
const DEFAULT_WRITE_TIMEOUT: Duration = Duration::from_secs(2);
/// Transport running on USB
#[derive(Debug, Clone)]
pub struct USBTransport {
vendor_id: u16,
product_id: u16,
handle: Option<Arc<DeviceHandle<GlobalContext>>>,
}
impl USBTransport {
/// Instantiate a new [USBTransport]
pub fn new(vendor_id: u16, product_id: u16) -> Self {
Self {
handle: None,
vendor_id,
product_id,
}
}
pub(crate) fn get_raw_connection(&self) -> Result<Arc<DeviceHandle<GlobalContext>>> {
self.handle
.as_ref()
.ok_or(RustADBError::IOError(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"not connected",
)))
.cloned()
}
fn configure_endpoint(handle: &DeviceHandle<GlobalContext>, endpoint: &Endpoint) -> Result<()> {
handle.claim_interface(endpoint.iface)?;
Ok(())
}
/// Write data to underlying connection, with default timeout
pub(crate) fn write_message(&mut self, message: ADBUsbMessage) -> Result<()> {
self.write_message_with_timeout(message, DEFAULT_WRITE_TIMEOUT)
}
/// Write data to underlying connection
pub(crate) fn write_message_with_timeout(
&mut self,
message: ADBUsbMessage,
timeout: Duration,
) -> Result<()> {
let endpoint = self.find_writable_endpoint()?;
let handle = self.get_raw_connection()?;
if let Ok(true) = handle.kernel_driver_active(endpoint.iface) {
handle.detach_kernel_driver(endpoint.iface)?;
}
Self::configure_endpoint(&handle, &endpoint)?;
let message_bytes = message.header().as_bytes()?;
let mut total_written = 0;
loop {
total_written +=
handle.write_bulk(endpoint.address, &message_bytes[total_written..], timeout)?;
if total_written == message_bytes.len() {
break;
}
}
let payload = message.into_payload();
let mut total_written = 0;
loop {
total_written +=
handle.write_bulk(endpoint.address, &payload[total_written..], timeout)?;
if total_written == payload.len() {
break;
}
}
Ok(())
}
/// Blocking method to read data from underlying connection.
pub(crate) fn read_message(&mut self) -> Result<ADBUsbMessage> {
self.read_message_with_timeout(MAX_READ_TIMEOUT)
}
/// Read data from underlying connection with given timeout.
pub(crate) fn read_message_with_timeout(&mut self, timeout: Duration) -> Result<ADBUsbMessage> {
let endpoint = self.find_readable_endpoint()?;
let handle = self.get_raw_connection()?;
if let Ok(true) = handle.kernel_driver_active(endpoint.iface) {
handle.detach_kernel_driver(endpoint.iface)?;
}
Self::configure_endpoint(&handle, &endpoint)?;
let mut data = [0; 24];
let mut total_read = 0;
loop {
total_read += handle.read_bulk(endpoint.address, &mut data[total_read..], timeout)?;
if total_read == data.len() {
break;
}
}
let header = ADBUsbMessageHeader::try_from(data)?;
log::trace!("received header {header:?}");
if header.data_length() != 0 {
let mut msg_data = vec![0_u8; header.data_length() as usize];
let mut total_read = 0;
loop {
total_read +=
handle.read_bulk(endpoint.address, &mut msg_data[total_read..], timeout)?;
if total_read == msg_data.capacity() {
break;
}
}
let message = ADBUsbMessage::from_header_and_payload(header, msg_data);
// Check message integrity
if !message.check_message_integrity() {
return Err(RustADBError::InvalidIntegrity(
ADBUsbMessageHeader::compute_crc32(message.payload()),
message.header().data_crc32(),
));
}
return Ok(message);
}
Ok(ADBUsbMessage::from_header_and_payload(header, vec![]))
}
fn find_readable_endpoint(&self) -> Result<Endpoint> {
let handle = self.get_raw_connection()?;
for n in 0..handle.device().device_descriptor()?.num_configurations() {
let config_desc = match handle.device().config_descriptor(n) {
Ok(c) => c,
Err(_) => continue,
};
for interface in config_desc.interfaces() {
for interface_desc in interface.descriptors() {
for endpoint_desc in interface_desc.endpoint_descriptors() {
if endpoint_desc.direction() == Direction::In
&& endpoint_desc.transfer_type() == TransferType::Bulk
&& interface_desc.class_code() == LIBUSB_CLASS_VENDOR_SPEC
&& interface_desc.sub_class_code() == 0x42
&& interface_desc.protocol_code() == 0x01
{
return Ok(Endpoint {
iface: interface_desc.interface_number(),
address: endpoint_desc.address(),
});
}
}
}
}
}
Err(RustADBError::USBNoDescriptorFound)
}
fn find_writable_endpoint(&self) -> Result<Endpoint> {
let handle = self.get_raw_connection()?;
for n in 0..handle.device().device_descriptor()?.num_configurations() {
let config_desc = match handle.device().config_descriptor(n) {
Ok(c) => c,
Err(_) => continue,
};
for interface in config_desc.interfaces() {
for interface_desc in interface.descriptors() {
for endpoint_desc in interface_desc.endpoint_descriptors() {
if endpoint_desc.direction() == Direction::Out
&& endpoint_desc.transfer_type() == TransferType::Bulk
&& interface_desc.class_code() == LIBUSB_CLASS_VENDOR_SPEC
&& interface_desc.sub_class_code() == 0x42
&& interface_desc.protocol_code() == 0x01
{
return Ok(Endpoint {
iface: interface_desc.interface_number(),
address: endpoint_desc.address(),
});
}
}
}
}
}
Err(RustADBError::USBNoDescriptorFound)
}
}
impl ADBTransport for USBTransport {
fn connect(&mut self) -> crate::Result<()> {
for d in rusb::devices()?.iter() {
if let Ok(descriptor) = d.device_descriptor() {
if descriptor.vendor_id() == self.vendor_id
&& descriptor.product_id() == self.product_id
{
self.handle = Some(Arc::new(d.open()?));
return Ok(());
}
}
}
Err(RustADBError::DeviceNotFound(format!(
"Cannot find device with vendor id {} and product id {}",
self.vendor_id, self.product_id
)))
}
fn disconnect(&mut self) -> crate::Result<()> {
let message = ADBUsbMessage::new(USBCommand::Clse, 0, 0, "".into());
self.write_message(message)
}
}

View File

@@ -0,0 +1,154 @@
use crate::{Result, RustADBError};
use base64::{engine::general_purpose::STANDARD, Engine};
use byteorder::{LittleEndian, WriteBytesExt};
use num_bigint::traits::ModInverse;
use num_bigint::BigUint;
use rand::rngs::OsRng;
use rsa::{Hash, PaddingScheme, PublicKeyParts, RSAPrivateKey};
use std::convert::TryInto;
// From project: https://github.com/hajifkd/webadb
#[derive(Debug)]
pub struct ADBRsaKey {
private_key: RSAPrivateKey,
}
impl ADBRsaKey {
pub fn random_with_size(size: usize) -> Result<Self> {
let mut rng = OsRng;
Ok(Self {
private_key: RSAPrivateKey::new(&mut rng, size)?,
})
}
pub fn from_pkcs8(pkcs8_content: &str) -> Result<Self> {
let der_encoded = pkcs8_content
.lines()
.filter(|line| !line.starts_with("-") && !line.is_empty())
.fold(String::new(), |mut data, line| {
data.push_str(line);
data
});
let der_bytes = STANDARD.decode(&der_encoded)?;
let private_key = RSAPrivateKey::from_pkcs8(&der_bytes)?;
Ok(ADBRsaKey { private_key })
}
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<()> {
for &v in data
.to_bytes_le()
.iter()
.chain(std::iter::repeat(&0))
.take(n_bytes)
{
writer.write_u8(v)?;
}
Ok(())
}
fn set_bit(n: usize) -> Result<BigUint> {
BigUint::parse_bytes(
&{
let mut bits = vec![b'1'];
bits.append(&mut vec![b'0'; n]);
bits
}[..],
2,
)
.ok_or(RustADBError::ConversionError)
}
#[test]
fn test_pubkey_gen() {
const DEFAULT_PRIV_KEY: &'static str = r"-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC4Dyn85cxDJnjM
uYXQl/w469MDKdlGdviLfmFMWeYLVfL2Mz1AVyvKqscrtlhbbgMQ/M+3lDvEdHS0
14RIGAwWRtrlTTmhLvM2/IO+eSKSYeCrCVc4KLG3E3WRryUXbs2ynA29xjTJVw+Z
xYxDyn/tAYPEyMm4v+HIJHcOtRzxtO2vjMJ2vBT/ywYxjhncXbFSO09q2E4XrHli
SIPyO82hZgCkpzTZRp+nyA17TYuV9++mvUr9lWH9RbC+o8EF3yitlBsE2uXr97EV
i2Qy8CE7FIxsihXlukppwKRuz+1rJrvmZPTn49ZS+sIS99WE9GoCpsyQvTpvehrM
SIDRsVZPAgMBAAECggEAWNXAzzXeS36zCSR1yILCknqHotw86Pyc4z7BGUe+dzQp
itiaNIaeNTgN3zQoGyDSzA0o+BLMcfo/JdVrHBy3IL1cAxYtvXTaoGxp7bGrlPk2
pXZhqVJCy/jRYtokzdWF5DHbk/+pFJA3kGE/XKzM54g2n/DFI61A/QdUiz2w1ZtI
vc5cM08EM8B/TSI3SeWB8zkh5SlIuLsFO2J2+tCak6PdFfKOVIrFv9dKJYLxx+59
+edZamw2EvNlnl/sewgUk0gaZvQKVf4ivHyM+KSHuV4RFfiLvGuVcyA6XhSjztsG
EA++jDHP5ib/Izes7UK09v9y7kow+z6vUtnDDQOvgQKBgQD8WWAn7FQt9aziCw19
gZynzHG1bXI7uuEVSneuA3UwJImmDu8W+Qb9YL9Dc2nV0M5pGGdXKi2jzq8gPar6
GPAmy7TOlov6Nm0pbMXTAfuovG+gIXxelp3US3FvyRupi0/7UQRRwvetFYbDFwJX
ydF5uEtZdGSHAjPeU5FLq6tBwQKBgQC6uN0JwwZn+eaxguyKOXvp0KykhFI0HI1A
MBDZ1uuKt6OW5+r9NeQtTLctGlNKVQ8wz+Wr0C/nLGIIv4lySS9WFyc5/FnFhDdy
LsEi6whcca4vq3jsMOukvQGFnERsou4LqBEI1Es7jjeeEq+/8WnNTi6Y1flZ6UAp
YAOeFI98DwKBgQDvyfHgHeajwZalOQF5qGb24AOQ9c4dyefGNnvhA/IgbCfMftZc
iwhETuGQM6R3A7KQFRtlrXOu+2BYD6Ffg8D37IwD3vRmL7+tJGoapwC/B0g+7nLi
4tZY+9Nv+LbrdbDry8GB+/UkKJdk3IFicCk4M5KOD1bTH5mwAtLHB/p1QQKBgDHi
k8M45GxA+p4wMUvYgb987bLiWyfq/N3KOaZJYhJkb4MwoLpXfIeRuFqHbvsr8GwF
DwIxE6s6U1KtAWaUIN5qPyOhxMYdRcbusNDIZCp2gKfhsuO/SiVwDYkJr8oqWVip
5SsrtJHLtBY6PdQVBkRAf/h7KiwYQfkL2suQCKmHAoGBAJAkYImBYPHuRcnSXikn
xGDK/moPvzs0CjdPlRcEN+Myy/G0FUrOaC0FcpNoJOdQSYz3F6URA4nX+zj6Ie7G
CNkECiepaGyquQaffwR1CAi8dH6biJjlTQWQPFcCLA0hvernWo3eaSfiL7fHyym+
ile69MHFENUePSpuRSiF3Z02
-----END PRIVATE KEY-----";
let priv_key =
ADBRsaKey::from_pkcs8(DEFAULT_PRIV_KEY).expect("cannot create rsa key from data");
let pub_key = priv_key
.encoded_public_key()
.expect("cannot encode public key");
let pub_key_adb = "\
QAAAAFH/pU9PVrHRgEjMGnpvOr2QzKYCavSE1fcSwvpS1uPn9GTmuyZr7c9up\
MBpSrrlFYpsjBQ7IfAyZIsVsffr5doEG5StKN8FwaO+sEX9YZX9Sr2m7/eVi0\
17Dcinn0bZNKekAGahzTvyg0hieawXTthqTztSsV3cGY4xBsv/FLx2woyv7bT\
xHLUOdyTI4b+4ycjEgwHtf8pDjMWZD1fJNMa9DZyyzW4XJa+RdRO3sSg4Vwmr\
4GGSInm+g/w28y6hOU3l2kYWDBhIhNe0dHTEO5S3z/wQA25bWLYrx6rKK1dAP\
TP28lUL5llMYX6L+HZG2SkD0+s4/JfQhbnMeCZDzOX8KQ+4ThLy/gDTqCSTjj\
ic8BykdUIqYPwAjBMgQwLOLY5WNJMpjGlFINRcCGhvFFZ73sJTLerECuV/Oae\
nFRcORwnGIRgMrYXj4tjmxJC7sq3cfNX96YIcSCDE9SZFdlKXVK8Jc4YMLGF3\
MI8k1KoTby34uaIyxPJvwM1WR4Rdj60fwMyikFXNaOR2fPteZ3UMBA7CMrOEm\
9iYjntyEppA4hQXIO1TWTzkA/Kfl1i67k5NuLIQdhPFEc5ox5IYVHusauPQ7d\
Awu6BlgK37TUn0JdK0Z6Z4RaEIaNiEI0d5CoP6zQKV2QQnlscYpdsaUW5t9/F\
LioVXPwrz0tx35JyIUZPPYwEAAQA= ";
assert_eq!(&pub_key[..pub_key_adb.len()], pub_key_adb);
}

View File

@@ -0,0 +1,447 @@
use byteorder::ReadBytesExt;
use rand::Rng;
use rusb::Device;
use rusb::DeviceDescriptor;
use rusb::UsbContext;
use std::fs::read_to_string;
use std::io::Cursor;
use std::io::Read;
use std::io::Seek;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use byteorder::LittleEndian;
use super::{ADBRsaKey, ADBUsbMessage};
use crate::constants::BUFFER_SIZE;
use crate::models::AdbStatResponse;
use crate::usb::adb_usb_message::{AUTH_RSAPUBLICKEY, AUTH_SIGNATURE, AUTH_TOKEN};
use crate::{
usb::usb_commands::{USBCommand, USBSubcommand},
ADBTransport, Result, RustADBError, USBTransport,
};
/// Represent a device reached directly over USB
#[derive(Debug)]
pub struct ADBUSBDevice {
private_key: ADBRsaKey,
pub(crate) transport: USBTransport,
}
fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Option<ADBRsaKey>> {
read_to_string(private_key_path.as_ref())
.map_err(RustADBError::from)
.map(|pk| match ADBRsaKey::from_pkcs8(&pk) {
Ok(pk) => Some(pk),
Err(e) => {
log::error!("Error while create RSA private key: {e}");
None
}
})
}
/// Search for adb devices with known interface class and subclass values
fn search_adb_devices() -> Result<Option<(u16, u16)>> {
let mut found_devices = vec![];
for device in rusb::devices()?.iter() {
let Ok(des) = device.device_descriptor() else {
continue;
};
if is_adb_device(&device, &des) {
log::debug!(
"Autodetect device {:04x}:{:04x}",
des.vendor_id(),
des.product_id()
);
found_devices.push((des.vendor_id(), des.product_id()));
}
}
match (found_devices.first(), found_devices.get(1)) {
(None, _) => Ok(None),
(Some(identifiers), None) => Ok(Some(*identifiers)),
(Some((vid1, pid1)), Some((vid2, pid2))) => Err(RustADBError::DeviceNotFound(format!(
"Found two Android devices {:04x}:{:04x} and {:04x}:{:04x}",
vid1, pid1, vid2, pid2
))),
}
}
fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
const ADB_CLASS: u8 = 0xff;
const ADB_SUBCLASS: u8 = 0x42;
const ADB_PROTOCOL: u8 = 0x1;
// Some devices require choosing the file transfer mode
// for usb debugging to take effect.
const BULK_CLASS: u8 = 0xdc;
const BULK_ADB_SUBCLASS: u8 = 2;
for n in 0..des.num_configurations() {
let Ok(config_des) = device.config_descriptor(n) else {
continue;
};
for interface in config_des.interfaces() {
for interface_des in interface.descriptors() {
let proto = interface_des.protocol_code();
let class = interface_des.class_code();
let subcl = interface_des.sub_class_code();
if proto == ADB_PROTOCOL
&& ((class == ADB_CLASS && subcl == ADB_SUBCLASS)
|| (class == BULK_CLASS && subcl == BULK_ADB_SUBCLASS))
{
return true;
}
}
}
}
false
}
fn get_default_adb_key_path() -> Result<PathBuf> {
homedir::my_home()
.ok()
.flatten()
.map(|home| home.join(".android").join("adbkey"))
.ok_or(RustADBError::NoHomeDirectory)
}
impl ADBUSBDevice {
/// Instantiate a new [ADBUSBDevice]
pub fn new(vendor_id: u16, product_id: u16) -> Result<Self> {
Self::new_with_custom_private_key(vendor_id, product_id, get_default_adb_key_path()?)
}
/// Instantiate a new [ADBUSBDevice] using a custom private key path
pub fn new_with_custom_private_key(
vendor_id: u16,
product_id: u16,
private_key_path: PathBuf,
) -> Result<Self> {
let private_key = match read_adb_private_key(private_key_path)? {
Some(pk) => pk,
None => ADBRsaKey::random_with_size(2048)?,
};
let mut s = Self {
private_key,
transport: USBTransport::new(vendor_id, product_id),
};
s.connect()?;
Ok(s)
}
/// autodetect connected ADB devices and establish a connection with the first device found
pub fn autodetect() -> Result<Self> {
Self::autodetect_with_custom_private_key(get_default_adb_key_path()?)
}
/// autodetect connected ADB devices and establish a connection with the first device found using a custom private key path
pub fn autodetect_with_custom_private_key(private_key_path: PathBuf) -> Result<Self> {
match search_adb_devices()? {
Some((vendor_id, product_id)) => {
ADBUSBDevice::new_with_custom_private_key(vendor_id, product_id, private_key_path)
}
_ => Err(RustADBError::DeviceNotFound(
"cannot find USB devices matching the signature of an ADB device".into(),
)),
}
}
/// Send initial connect
pub fn connect(&mut self) -> Result<()> {
self.transport.connect()?;
let message = ADBUsbMessage::new(
USBCommand::Cnxn,
0x01000000,
1048576,
format!("host::{}\0", env!("CARGO_PKG_NAME"))
.as_bytes()
.to_vec(),
);
self.transport.write_message(message)?;
let message = self.transport.read_message()?;
// At this point, we should have received either:
// - an AUTH message with arg0 == 1
// - a CNXN message
let auth_message = match message.header().command() {
USBCommand::Auth if message.header().arg0() == AUTH_TOKEN => message,
USBCommand::Auth if message.header().arg0() != AUTH_TOKEN => {
return Err(RustADBError::ADBRequestFailed(
"Received AUTH message with type != 1".into(),
))
}
c => {
return Err(RustADBError::ADBRequestFailed(format!(
"Wrong command received {}",
c
)))
}
};
let sign = self.private_key.sign(auth_message.into_payload())?;
let message = ADBUsbMessage::new(USBCommand::Auth, AUTH_SIGNATURE, 0, sign);
self.transport.write_message(message)?;
let received_response = self.transport.read_message()?;
if received_response.header().command() == USBCommand::Cnxn {
log::info!(
"Authentication OK, device info {}",
String::from_utf8(received_response.into_payload())?
);
return Ok(());
}
let mut pubkey = self.private_key.encoded_public_key()?.into_bytes();
pubkey.push(b'\0');
let message = ADBUsbMessage::new(USBCommand::Auth, AUTH_RSAPUBLICKEY, 0, pubkey);
self.transport.write_message(message)?;
let response = self
.transport
.read_message_with_timeout(Duration::from_secs(10))?;
match response.header().command() {
USBCommand::Cnxn => log::info!(
"Authentication OK, device info {}",
String::from_utf8(response.into_payload())?
),
_ => {
return Err(RustADBError::ADBRequestFailed(format!(
"wrong response {}",
response.header().command()
)))
}
}
Ok(())
}
/// Receive a message and acknowledge it by replying with an `OKAY` command
pub(crate) fn recv_and_reply_okay(
&mut self,
local_id: u32,
remote_id: u32,
) -> Result<ADBUsbMessage> {
let message = self.transport.read_message()?;
self.transport.write_message(ADBUsbMessage::new(
USBCommand::Okay,
local_id,
remote_id,
"".into(),
))?;
Ok(message)
}
/// Expect a message with an `OKAY` command after sending a message.
pub(crate) fn send_and_expect_okay(&mut self, message: ADBUsbMessage) -> Result<ADBUsbMessage> {
self.transport.write_message(message)?;
let message = self.transport.read_message()?;
let received_command = message.header().command();
if received_command != USBCommand::Okay {
return Err(RustADBError::ADBRequestFailed(format!(
"expected command OKAY after message, got {}",
received_command
)));
}
Ok(message)
}
pub(crate) fn recv_file<W: std::io::Write>(
&mut self,
local_id: u32,
remote_id: u32,
mut output: W,
) -> std::result::Result<(), RustADBError> {
let mut len: Option<u64> = None;
loop {
let payload = self
.recv_and_reply_okay(local_id, remote_id)?
.into_payload();
let mut rdr = Cursor::new(&payload);
while rdr.position() != payload.len() as u64 {
match len.take() {
Some(0) | None => {
rdr.seek_relative(4)?;
len.replace(rdr.read_u32::<LittleEndian>()? as u64);
}
Some(length) => {
log::debug!("len = {length}");
let remaining_bytes = payload.len() as u64 - rdr.position();
log::debug!(
"payload length {} - reader_position {} = {remaining_bytes}",
payload.len(),
rdr.position()
);
if length < remaining_bytes {
let read = std::io::copy(&mut rdr.by_ref().take(length), &mut output)?;
log::debug!(
"expected to read {length} bytes, actually read {read} bytes"
);
} else {
let read = std::io::copy(&mut rdr.take(remaining_bytes), &mut output)?;
len.replace(length - remaining_bytes);
log::debug!("expected to read {remaining_bytes} bytes, actually read {read} bytes");
// this payload is exhausted
break;
}
}
}
}
if Cursor::new(&payload[(payload.len() - 8)..(payload.len() - 4)])
.read_u32::<LittleEndian>()?
== USBSubcommand::Done as u32
{
break;
}
}
Ok(())
}
pub(crate) fn push_file<R: std::io::Read>(
&mut self,
local_id: u32,
remote_id: u32,
mut reader: R,
) -> std::result::Result<(), RustADBError> {
let mut buffer = [0; BUFFER_SIZE];
let amount_read = reader.read(&mut buffer)?;
let subcommand_data = USBSubcommand::Data.with_arg(amount_read as u32);
let mut serialized_message =
bincode::serialize(&subcommand_data).map_err(|_e| RustADBError::ConversionError)?;
serialized_message.append(&mut buffer[..amount_read].to_vec());
let message =
ADBUsbMessage::new(USBCommand::Write, local_id, remote_id, serialized_message);
self.send_and_expect_okay(message)?;
loop {
let mut buffer = [0; BUFFER_SIZE];
match reader.read(&mut buffer) {
Ok(0) => {
// Currently file mtime is not forwarded
let subcommand_data = USBSubcommand::Done.with_arg(0);
let serialized_message = bincode::serialize(&subcommand_data)
.map_err(|_e| RustADBError::ConversionError)?;
let message = ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
serialized_message,
);
self.send_and_expect_okay(message)?;
// Command should end with a Write => Okay
let received = self.transport.read_message()?;
match received.header().command() {
USBCommand::Write => return Ok(()),
c => {
return Err(RustADBError::ADBRequestFailed(format!(
"Wrong command received {}",
c
)))
}
}
}
Ok(size) => {
let subcommand_data = USBSubcommand::Data.with_arg(size as u32);
let mut serialized_message = bincode::serialize(&subcommand_data)
.map_err(|_e| RustADBError::ConversionError)?;
serialized_message.append(&mut buffer[..size].to_vec());
let message = ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
serialized_message,
);
self.send_and_expect_okay(message)?;
}
Err(e) => {
return Err(RustADBError::IOError(e));
}
}
}
}
pub(crate) fn begin_synchronization(&mut self) -> Result<(u32, u32)> {
let sync_directive = "sync:\0";
let mut rng = rand::thread_rng();
let message = ADBUsbMessage::new(
USBCommand::Open,
rng.gen(), /* Our 'local-id' */
0,
sync_directive.into(),
);
let message = self.send_and_expect_okay(message)?;
let local_id = message.header().arg1();
let remote_id = message.header().arg0();
Ok((local_id, remote_id))
}
pub(crate) fn stat_with_explicit_ids(
&mut self,
remote_path: &str,
local_id: u32,
remote_id: u32,
) -> Result<AdbStatResponse> {
let stat_buffer = USBSubcommand::Stat.with_arg(remote_path.len() as u32);
let message = ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
bincode::serialize(&stat_buffer).map_err(|_e| RustADBError::ConversionError)?,
);
self.send_and_expect_okay(message)?;
self.send_and_expect_okay(ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
remote_path.into(),
))?;
let response = self.transport.read_message()?;
// Skip first 4 bytes as this is the literal "STAT".
// Interesting part starts right after
bincode::deserialize(&response.into_payload()[4..])
.map_err(|_e| RustADBError::ConversionError)
}
pub(crate) fn end_transaction(&mut self, local_id: u32, remote_id: u32) -> Result<()> {
let quit_buffer = USBSubcommand::Quit.with_arg(0u32);
self.send_and_expect_okay(ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
bincode::serialize(&quit_buffer).map_err(|_e| RustADBError::ConversionError)?,
))?;
let _discard_close = self.transport.read_message()?;
Ok(())
}
}
impl Drop for ADBUSBDevice {
fn drop(&mut self) {
// Best effort here
let _ = self.transport.disconnect();
}
}

View File

@@ -0,0 +1,245 @@
use crate::{
models::AdbStatResponse,
usb::{ADBUsbMessage, USBCommand, USBSubcommand},
utils::check_extension_is_apk,
ADBDeviceExt, ADBUSBDevice, RebootType, Result, RustADBError,
};
use rand::Rng;
use std::{
fs::File,
io::{Read, Write},
};
use super::{USBShellWriter, USBWriter};
impl ADBDeviceExt for ADBUSBDevice {
fn shell_command<S: ToString, W: Write>(
&mut self,
command: impl IntoIterator<Item = S>,
mut output: W,
) -> Result<()> {
let message = ADBUsbMessage::new(
USBCommand::Open,
1,
0,
format!(
"shell:{}\0",
command
.into_iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" "),
)
.as_bytes()
.to_vec(),
);
self.transport.write_message(message)?;
let response = self.transport.read_message()?;
if response.header().command() != USBCommand::Okay {
return Err(RustADBError::ADBRequestFailed(format!(
"wrong command {}",
response.header().command()
)));
}
loop {
let response = self.transport.read_message()?;
if response.header().command() != USBCommand::Write {
break;
}
output.write_all(&response.into_payload())?;
}
Ok(())
}
fn shell<R: Read, W: Write + Send + 'static>(
&mut self,
mut reader: R,
mut writer: W,
) -> Result<()> {
let sync_directive = "shell:\0";
let mut rng = rand::thread_rng();
let message = ADBUsbMessage::new(
USBCommand::Open,
rng.gen(), /* Our 'local-id' */
0,
sync_directive.into(),
);
let message = self.send_and_expect_okay(message)?;
let local_id = message.header().arg1();
let remote_id = message.header().arg0();
let mut transport = self.transport.clone();
// Reading thread, reads response from adbd
std::thread::spawn(move || -> Result<()> {
loop {
let message = transport.read_message()?;
// Acknowledge for more data
let response = ADBUsbMessage::new(USBCommand::Okay, local_id, remote_id, vec![]);
transport.write_message(response)?;
match message.header().command() {
USBCommand::Write => {}
USBCommand::Okay => continue,
_ => return Err(RustADBError::ADBShellNotSupported),
}
writer.write_all(&message.into_payload())?;
writer.flush()?;
}
});
let mut shell_writer = USBShellWriter::new(self.transport.clone(), local_id, remote_id);
// Read from given reader (that could be stdin e.g), and write content to device adbd
if let Err(e) = std::io::copy(&mut reader, &mut shell_writer) {
match e.kind() {
std::io::ErrorKind::BrokenPipe => return Ok(()),
_ => return Err(RustADBError::IOError(e)),
}
}
Ok(())
}
fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
let (local_id, remote_id) = self.begin_synchronization()?;
let adb_stat_response = self.stat_with_explicit_ids(remote_path, local_id, remote_id)?;
self.end_transaction(local_id, remote_id)?;
Ok(adb_stat_response)
}
fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> {
let (local_id, remote_id) = self.begin_synchronization()?;
let source = source.as_ref();
let adb_stat_response = self.stat_with_explicit_ids(source, local_id, remote_id)?;
self.transport.write_message(ADBUsbMessage::new(
USBCommand::Okay,
local_id,
remote_id,
"".into(),
))?;
log::debug!("{:?}", adb_stat_response);
if adb_stat_response.file_perm == 0 {
return Err(RustADBError::UnknownResponseType(
"mode is 0: source file does not exist".to_string(),
));
}
let recv_buffer = USBSubcommand::Recv.with_arg(source.len() as u32);
let recv_buffer =
bincode::serialize(&recv_buffer).map_err(|_e| RustADBError::ConversionError)?;
self.send_and_expect_okay(ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
recv_buffer,
))?;
self.send_and_expect_okay(ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
source.into(),
))?;
self.recv_file(local_id, remote_id, output)?;
self.end_transaction(local_id, remote_id)?;
Ok(())
}
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
let (local_id, remote_id) = self.begin_synchronization()?;
let path_header = format!("{},0777", path.as_ref());
let send_buffer = USBSubcommand::Send.with_arg(path_header.len() as u32);
let mut send_buffer =
bincode::serialize(&send_buffer).map_err(|_e| RustADBError::ConversionError)?;
send_buffer.append(&mut path_header.as_bytes().to_vec());
self.send_and_expect_okay(ADBUsbMessage::new(
USBCommand::Write,
local_id,
remote_id,
send_buffer,
))?;
self.push_file(local_id, remote_id, stream)?;
self.end_transaction(local_id, remote_id)?;
Ok(())
}
fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
let mut rng = rand::thread_rng();
let message = ADBUsbMessage::new(
USBCommand::Open,
rng.gen(), // Our 'local-id'
0,
format!("reboot:{}\0", reboot_type).as_bytes().to_vec(),
);
self.transport.write_message(message)?;
let message = self.transport.read_message()?;
if message.header().command() != USBCommand::Okay {
return Err(RustADBError::ADBShellNotSupported);
}
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(),
)?)),
}
}
}

View File

@@ -0,0 +1,109 @@
use serde::{Deserialize, Serialize};
use super::usb_commands::USBCommand;
use crate::RustADBError;
pub const AUTH_TOKEN: u32 = 1;
pub const AUTH_SIGNATURE: u32 = 2;
pub const AUTH_RSAPUBLICKEY: u32 = 3;
#[derive(Debug)]
pub struct ADBUsbMessage {
header: ADBUsbMessageHeader,
payload: Vec<u8>,
}
#[derive(Debug, Serialize, Deserialize)]
#[repr(C)]
pub struct ADBUsbMessageHeader {
command: USBCommand, /* command identifier constant */
arg0: u32, /* first argument */
arg1: u32, /* second argument */
data_length: u32, /* length of payload (0 is allowed) */
data_crc32: u32, /* crc32 of data payload */
magic: u32, /* command ^ 0xffffffff */
}
impl ADBUsbMessageHeader {
pub fn new(command: USBCommand, arg0: u32, arg1: u32, data: &[u8]) -> Self {
Self {
command,
arg0,
arg1,
data_length: data.len() as u32,
data_crc32: Self::compute_crc32(data),
magic: Self::compute_magic(command),
}
}
pub fn command(&self) -> USBCommand {
self.command
}
pub fn arg0(&self) -> u32 {
self.arg0
}
pub fn arg1(&self) -> u32 {
self.arg1
}
pub fn data_length(&self) -> u32 {
self.data_length
}
pub fn data_crc32(&self) -> u32 {
self.data_crc32
}
pub(crate) fn compute_crc32(data: &[u8]) -> u32 {
data.iter().map(|&x| x as u32).sum()
}
fn compute_magic(command: USBCommand) -> u32 {
let command_u32 = command as u32;
command_u32 ^ 0xFFFFFFFF
}
pub fn as_bytes(&self) -> Result<Vec<u8>, RustADBError> {
bincode::serialize(&self).map_err(|_e| RustADBError::ConversionError)
}
}
impl ADBUsbMessage {
pub fn new(command: USBCommand, arg0: u32, arg1: u32, data: Vec<u8>) -> Self {
Self {
header: ADBUsbMessageHeader::new(command, arg0, arg1, &data),
payload: data,
}
}
pub fn from_header_and_payload(header: ADBUsbMessageHeader, payload: Vec<u8>) -> Self {
Self { header, payload }
}
pub fn check_message_integrity(&self) -> bool {
ADBUsbMessageHeader::compute_magic(self.header.command) == self.header.magic
&& ADBUsbMessageHeader::compute_crc32(&self.payload) == self.header.data_crc32
}
pub fn header(&self) -> &ADBUsbMessageHeader {
&self.header
}
pub fn payload(&self) -> &Vec<u8> {
&self.payload
}
pub fn into_payload(self) -> Vec<u8> {
self.payload
}
}
impl TryFrom<[u8; 24]> for ADBUsbMessageHeader {
type Error = RustADBError;
fn try_from(value: [u8; 24]) -> Result<Self, Self::Error> {
bincode::deserialize(&value).map_err(|_e| RustADBError::ConversionError)
}
}

14
adb_client/src/usb/mod.rs Normal file
View File

@@ -0,0 +1,14 @@
mod adb_rsa_key;
mod adb_usb_device;
mod adb_usb_device_commands;
mod adb_usb_message;
mod usb_commands;
mod usb_shell_writer;
mod usb_writer;
pub use adb_rsa_key::ADBRsaKey;
pub use adb_usb_device::ADBUSBDevice;
pub use adb_usb_message::{ADBUsbMessage, ADBUsbMessageHeader};
pub use usb_commands::{USBCommand, USBSubcommand};
pub use usb_shell_writer::USBShellWriter;
pub use usb_writer::USBWriter;

View File

@@ -0,0 +1,63 @@
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::Display;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
#[repr(u32)]
pub enum USBCommand {
/// Connect to a device
Cnxn = 0x4e584e43,
/// Close connection to a device
Clse = 0x45534c43,
/// Device ask for authentication
Auth = 0x48545541,
/// Open a data connection
Open = 0x4e45504f,
/// Write data to connection
Write = 0x45545257,
/// Server understood the message
Okay = 0x59414b4f,
// Sync 0x434e5953
// Stls 0x534C5453
}
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u32)]
pub enum USBSubcommand {
Stat = 0x54415453,
Send = 0x444E4553,
Recv = 0x56434552,
Quit = 0x54495551,
Fail = 0x4c494146,
Done = 0x454e4f44,
Data = 0x41544144,
List = 0x5453494c,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SubcommandWithArg {
subcommand: USBSubcommand,
arg: u32,
}
impl USBSubcommand {
pub fn with_arg(self, arg: u32) -> SubcommandWithArg {
SubcommandWithArg {
subcommand: self,
arg,
}
}
}
impl Display for USBCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
USBCommand::Cnxn => write!(f, "CNXN"),
USBCommand::Clse => write!(f, "CLSE"),
USBCommand::Auth => write!(f, "AUTH"),
USBCommand::Open => write!(f, "OPEN"),
USBCommand::Write => write!(f, "WRTE"),
USBCommand::Okay => write!(f, "OKAY"),
}
}
}

View File

@@ -0,0 +1,41 @@
use std::io::Write;
use crate::USBTransport;
use super::{ADBUsbMessage, USBCommand};
/// Wraps a `Writer` to hide underlying ADB protocol write logic.
pub struct USBShellWriter {
transport: USBTransport,
local_id: u32,
remote_id: u32,
}
impl USBShellWriter {
pub fn new(transport: USBTransport, local_id: u32, remote_id: u32) -> Self {
Self {
transport,
local_id,
remote_id,
}
}
}
impl Write for USBShellWriter {
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))?;
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}

View 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(())
}
}

24
adb_client/src/utils.rs Normal file
View File

@@ -0,0 +1,24 @@
use std::{ffi::OsStr, path::Path};
use crate::{Result, RustADBError};
pub fn u32_from_le(value: &[u8]) -> Result<u32> {
Ok(u32::from_le_bytes(
value
.try_into()
.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(())
}

BIN
assets/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

View File

@@ -1,149 +0,0 @@
use std::fs::File;
use std::net::Ipv4Addr;
use std::path::Path;
use adb_client::{AdbTcpConnection, Device, RebootType, RustADBError};
use clap::Parser;
#[derive(Parser, Debug)]
#[clap(about, version, author)]
pub struct Args {
/// Sets the listening address of ADB server
#[clap(short = 'a', long = "address", default_value = "127.0.0.1")]
pub address: Ipv4Addr,
/// Sets the listening port of ADB server
#[clap(short = 'p', long = "port", default_value = "5037")]
pub port: u16,
/// Serial id of a specific device. Every request will be sent to this device.
#[clap(short = 's', long = "serial")]
pub serial: Option<String>,
#[clap(subcommand)]
pub command: Command,
}
#[derive(Parser, Debug)]
pub enum Command {
/// Prints current ADB version.
Version,
/// Asks ADB server to quit immediately.
Kill,
/// List connected devices.
Devices {
#[clap(short = 'l', long = "long")]
long: bool,
},
/// Tracks new devices showing up.
TrackDevices,
/// Lists available server features.
HostFeatures,
/// Pushes 'filename' to the 'path' on device
Push { filename: String, path: String },
/// Pushes 'path' on the device to 'filename'
Pull { path: String, filename: String },
/// List files for 'path' on device
List { path: String },
/// Stat file specified as 'path' on device
Stat { path: String },
/// Run 'command' in a shell on the device, and return its output and error streams.
Shell { command: Vec<String> },
/// Reboots the device
Reboot {
#[clap(subcommand)]
sub_command: RebootTypeCommand,
},
}
#[derive(Parser, Debug)]
pub enum RebootTypeCommand {
System,
Bootloader,
Recovery,
Sideload,
SideloadAutoReboot,
}
impl From<RebootTypeCommand> for RebootType {
fn from(value: RebootTypeCommand) -> Self {
match value {
RebootTypeCommand::System => RebootType::System,
RebootTypeCommand::Bootloader => RebootType::Bootloader,
RebootTypeCommand::Recovery => RebootType::Recovery,
RebootTypeCommand::Sideload => RebootType::Sideload,
RebootTypeCommand::SideloadAutoReboot => RebootType::SideloadAutoReboot,
}
}
}
fn main() -> Result<(), RustADBError> {
let opt = Args::parse();
let mut connection = AdbTcpConnection::new(opt.address, opt.port)?;
match opt.command {
Command::Version => {
let version = connection.version()?;
println!("Android Debug Bridge version {}", version);
println!("Package version {}-rust", std::env!("CARGO_PKG_VERSION"));
}
Command::Kill => {
connection.kill()?;
}
Command::Devices { long } => {
if long {
println!("List of devices attached (extended)");
for device in connection.devices_long()? {
println!("{}", device);
}
} else {
println!("List of devices attached");
for device in connection.devices()? {
println!("{}", device);
}
}
}
Command::TrackDevices => {
let callback = |device: Device| {
println!("{}", device);
Ok(())
};
println!("Live list of devices attached");
connection.track_devices(callback)?;
}
Command::Pull { path, filename } => {
let mut output = File::create(Path::new(&filename)).unwrap(); // TODO: Better error handling
connection.recv(opt.serial, &path, &mut output)?;
println!("Downloaded {path} as {filename}");
}
Command::Push { filename, path } => {
let mut input = File::open(Path::new(&filename)).unwrap(); // TODO: Better error handling
connection.send(opt.serial, &mut input, &path)?;
println!("Uploaded {filename} to {path}");
}
Command::List { path } => {
connection.list(opt.serial, path)?;
}
Command::Stat { path } => {
let stat_response = connection.stat(opt.serial, path)?;
println!("{}", stat_response);
}
Command::Shell { command } => {
if command.is_empty() {
connection.shell(&opt.serial)?;
} else {
connection.shell_command(&opt.serial, command)?;
}
}
Command::HostFeatures => {
println!("Available host features");
for feature in connection.host_features(&opt.serial)? {
println!("- {}", feature);
}
}
Command::Reboot { sub_command } => {
println!("Reboots device");
connection.reboot(&opt.serial, sub_command.into())?
}
}
Ok(())
}

View File

@@ -1,125 +0,0 @@
use std::{
io::{Read, Write},
net::{Ipv4Addr, SocketAddrV4, TcpStream},
str,
str::FromStr,
};
use byteorder::{ByteOrder, LittleEndian};
use crate::{
models::{AdbCommand, AdbRequestStatus, SyncCommand},
Result, RustADBError,
};
/// Represents an ADB-over-TCP connection.
#[derive(Debug)]
pub struct AdbTcpConnection {
pub(crate) socket_addr: SocketAddrV4,
pub(crate) tcp_stream: TcpStream,
}
impl AdbTcpConnection {
/// Instantiates a new instance of [AdbTcpConnection]
pub fn new(address: Ipv4Addr, port: u16) -> Result<Self> {
let addr = SocketAddrV4::new(address, port);
Ok(Self {
socket_addr: addr,
tcp_stream: TcpStream::connect(addr)?,
})
}
/// Creates a new connection to ADB server.
///
/// Can be used after requests that closes connection.
pub(crate) fn new_connection(&mut self) -> Result<()> {
self.tcp_stream = TcpStream::connect(self.socket_addr)?;
Ok(())
}
pub(crate) fn proxy_connection(
&mut self,
adb_command: AdbCommand,
with_response: bool,
) -> Result<Vec<u8>> {
self.send_adb_request(adb_command)?;
if with_response {
let length = self.get_hex_body_length()?;
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
if length > 0 {
self.tcp_stream.read_exact(&mut body)?;
}
Ok(body)
} else {
Ok(vec![])
}
}
/// Sends the given [AdbCommand] to ADB server, and checks that the request has been taken in consideration.
/// If an error occurred, a [RustADBError] is returned with the response error string.
pub(crate) fn send_adb_request(&mut self, command: AdbCommand) -> Result<()> {
let adb_command_string = command.to_string();
let adb_request = format!("{:04x}{}", adb_command_string.len(), adb_command_string);
self.tcp_stream.write_all(adb_request.as_bytes())?;
// Reads returned status code from ADB server
let mut request_status = [0; 4];
self.tcp_stream.read_exact(&mut request_status)?;
match AdbRequestStatus::from_str(str::from_utf8(request_status.as_ref())?)? {
AdbRequestStatus::Fail => {
// We can keep reading to get further details
let length = self.get_body_length()?;
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
if length > 0 {
self.tcp_stream.read_exact(&mut body)?;
}
Err(RustADBError::ADBRequestFailed(String::from_utf8(body)?))
}
AdbRequestStatus::Okay => Ok(()),
}
}
/// Sends the given [SyncCommand] to ADB server, and checks that the request has been taken in consideration.
pub(crate) fn send_sync_request(&mut self, command: SyncCommand) -> Result<()> {
// First 4 bytes are the name of the command we want to send
// (e.g. "SEND", "RECV", "STAT", "LIST")
Ok(self.tcp_stream.write_all(command.to_string().as_bytes())?)
}
/// Gets the body length from hexadecimal value
pub(crate) fn get_hex_body_length(&mut self) -> Result<u32> {
let length_buffer = self.read_body_length()?;
Ok(u32::from_str_radix(str::from_utf8(&length_buffer)?, 16)?)
}
/// Gets the body length from a LittleEndian value
pub(crate) fn get_body_length(&mut self) -> Result<u32> {
let length_buffer = self.read_body_length()?;
Ok(LittleEndian::read_u32(&length_buffer))
}
/// Read 4 bytes representing body length
fn read_body_length(&mut self) -> Result<[u8; 4]> {
let mut length_buffer = [0; 4];
self.tcp_stream.read_exact(&mut length_buffer)?;
Ok(length_buffer)
}
}

View File

@@ -1,59 +0,0 @@
use std::io::Read;
use crate::{models::AdbCommand, AdbTcpConnection, Device, DeviceLong, Result, RustADBError};
impl AdbTcpConnection {
/// Gets a list of connected devices.
pub fn devices(&mut self) -> Result<Vec<Device>> {
let devices = self.proxy_connection(AdbCommand::Devices, true)?;
let mut vec_devices: Vec<Device> = vec![];
for device in devices.split(|x| x.eq(&b'\n')) {
if device.is_empty() {
break;
}
vec_devices.push(Device::try_from(device.to_vec())?);
}
Ok(vec_devices)
}
/// Gets an extended list of connected devices including the device paths in the state.
pub fn devices_long(&mut self) -> Result<Vec<DeviceLong>> {
let devices_long = self.proxy_connection(AdbCommand::DevicesLong, true)?;
let mut vec_devices: Vec<DeviceLong> = vec![];
for device in devices_long.split(|x| x.eq(&b'\n')) {
if device.is_empty() {
break;
}
vec_devices.push(DeviceLong::try_from(device.to_vec())?);
}
Ok(vec_devices)
}
/// Tracks new devices showing up.
// TODO: Change with Generator when feature stabilizes
pub fn track_devices(&mut self, callback: impl Fn(Device) -> Result<()>) -> Result<()> {
self.send_adb_request(AdbCommand::TrackDevices)?;
loop {
let length = self.get_hex_body_length()?;
if length > 0 {
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
self.tcp_stream.read_exact(&mut body)?;
callback(Device::try_from(body)?)?;
}
}
}
}

View File

@@ -1,23 +0,0 @@
use crate::{
models::{AdbCommand, HostFeatures},
AdbTcpConnection, Result,
};
impl AdbTcpConnection {
/// Lists available ADB server features.
pub fn host_features<S: ToString>(&mut self, serial: &Option<S>) -> Result<Vec<HostFeatures>> {
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
let features = self.proxy_connection(AdbCommand::HostFeatures, true)?;
Ok(features
.split(|x| x.eq(&b','))
.filter_map(|v| HostFeatures::try_from(v).ok())
.collect())
}
}

View File

@@ -1,8 +0,0 @@
use crate::{models::AdbCommand, AdbTcpConnection, Result};
impl AdbTcpConnection {
/// Asks the ADB server to quit immediately.
pub fn kill(&mut self) -> Result<()> {
self.proxy_connection(AdbCommand::Kill, false).map(|_| ())
}
}

View File

@@ -1,23 +0,0 @@
use crate::{
models::{AdbCommand, RebootType},
AdbTcpConnection, Result,
};
impl AdbTcpConnection {
/// Reboots the device
pub fn reboot<S: ToString>(
&mut self,
serial: &Option<S>,
reboot_type: RebootType,
) -> Result<()> {
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
self.proxy_connection(AdbCommand::Reboot(reboot_type), false)
.map(|_| ())
}
}

View File

@@ -1,75 +0,0 @@
use crate::{
models::{AdbCommand, SyncCommand},
AdbTcpConnection, Result, RustADBError,
};
use byteorder::{ByteOrder, LittleEndian};
use std::io::{Read, Write};
impl AdbTcpConnection {
/// Receives [path] to [stream] from the device.
pub fn recv<S: ToString, A: AsRef<str>>(
&mut self,
serial: Option<S>,
path: A,
stream: &mut dyn Write,
) -> Result<()> {
self.new_connection()?;
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
// Set device in SYNC mode
self.send_adb_request(AdbCommand::Sync)?;
// Send a recv command
self.send_sync_request(SyncCommand::Recv)?;
self.handle_recv_command(path, stream)
}
fn handle_recv_command<S: AsRef<str>>(
&mut self,
from: S,
output: &mut dyn Write,
) -> Result<()> {
// First send 8 byte common header
let mut len_buf = [0_u8; 4];
LittleEndian::write_u32(&mut len_buf, from.as_ref().len() as u32);
self.tcp_stream.write_all(&len_buf)?;
self.tcp_stream.write_all(from.as_ref().as_bytes())?;
// Then we receive the byte data in chunks of up to 64k
// Chunk looks like 'DATA' <length> <data>
let mut buffer = [0_u8; 64 * 1024]; // Should this be Boxed?
let mut data_header = [0_u8; 4]; // DATA
loop {
self.tcp_stream.read_exact(&mut data_header)?;
// Check if data_header is DATA or DONE or FAIL
match &data_header {
b"DATA" => {
// Handle received data
let length: usize = self.get_body_length()?.try_into().unwrap();
self.tcp_stream.read_exact(&mut buffer[..length])?;
output.write_all(&buffer)?;
}
b"DONE" => break, // We're done here
b"FAIL" => {
// Handle fail
let length: usize = self.get_body_length()?.try_into().unwrap();
self.tcp_stream.read_exact(&mut buffer[..length])?;
Err(RustADBError::ADBRequestFailed(String::from_utf8(
buffer[..length].to_vec(),
)?))?;
}
_ => panic!("Unknown response from device {:#?}", data_header),
}
}
// Connection should've left SYNC by now
Ok(())
}
}

View File

@@ -1,100 +0,0 @@
use crate::{
models::{AdbCommand, AdbRequestStatus, SyncCommand},
AdbTcpConnection, Result, RustADBError,
};
use byteorder::{ByteOrder, LittleEndian};
use std::{
convert::TryInto,
io::{Read, Write},
str::{self, FromStr},
time::SystemTime,
};
impl AdbTcpConnection {
/// Sends [stream] to [path] on the device.
pub fn send<S: ToString, A: AsRef<str>>(
&mut self,
serial: Option<S>,
stream: &mut dyn Read,
path: A,
) -> Result<()> {
self.new_connection()?;
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
// Set device in SYNC mode
self.send_adb_request(AdbCommand::Sync)?;
// Send a send command
self.send_sync_request(SyncCommand::Send)?;
self.handle_send_command(stream, path)
}
fn handle_send_command<S: AsRef<str>>(&mut self, input: &mut dyn Read, to: S) -> Result<()> {
// Append the permission flags to the filename
let to = to.as_ref().to_string() + ",0777";
// The name of command is already sent by send_sync_request
let mut len_buf = [0_u8; 4];
LittleEndian::write_u32(&mut len_buf, to.len() as u32);
self.tcp_stream.write_all(&len_buf)?;
// Send appends the filemode to the string sent
self.tcp_stream.write_all(to.as_bytes())?;
// Then we send the byte data in chunks of up to 64k
// Chunk looks like 'DATA' <length> <data>
let mut buffer = [0_u8; 64 * 1024];
loop {
let bytes_read = input.read(&mut buffer)?;
if bytes_read == 0 {
break;
}
let mut chunk_len_buf = [0_u8; 4];
LittleEndian::write_u32(&mut chunk_len_buf, bytes_read as u32);
self.tcp_stream.write_all(b"DATA")?;
self.tcp_stream.write_all(&chunk_len_buf)?;
self.tcp_stream.write_all(&buffer[..bytes_read])?;
}
// When we are done sending, we send 'DONE' <last modified time>
// Re-use len_buf to send the last modified time
let last_modified = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n,
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
};
LittleEndian::write_u32(&mut len_buf, last_modified.as_secs() as u32);
self.tcp_stream.write_all(b"DONE")?;
self.tcp_stream.write_all(&len_buf)?;
// We expect 'OKAY' response from this
let mut request_status = [0; 4];
self.tcp_stream.read_exact(&mut request_status)?;
match AdbRequestStatus::from_str(str::from_utf8(request_status.as_ref())?)? {
AdbRequestStatus::Fail => {
// We can keep reading to get further details
let length = self.get_body_length()?;
let mut body = vec![
0;
length
.try_into()
.map_err(|_| RustADBError::ConversionError)?
];
if length > 0 {
self.tcp_stream.read_exact(&mut body)?;
}
Err(RustADBError::ADBRequestFailed(String::from_utf8(body)?))
}
AdbRequestStatus::Okay => Ok(()),
}
}
}

View File

@@ -1,143 +0,0 @@
use std::io::{ErrorKind, Read, Write};
use crate::{
adb_termios::ADBTermios,
models::{AdbCommand, HostFeatures},
AdbTcpConnection, Result, RustADBError,
};
impl AdbTcpConnection {
/// Runs 'command' in a shell on the device, and return its output and error streams.
pub fn shell_command<S: ToString>(
&mut self,
serial: &Option<S>,
command: impl IntoIterator<Item = S>,
) -> Result<Vec<u8>> {
let supported_features = self.host_features(serial)?;
if !supported_features.contains(&HostFeatures::ShellV2)
&& !supported_features.contains(&HostFeatures::Cmd)
{
return Err(RustADBError::ADBShellNotSupported);
}
self.new_connection()?;
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
self.send_adb_request(AdbCommand::ShellCommand(
command
.into_iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" "),
))?;
const BUFFER_SIZE: usize = 512;
let result = (|| {
let mut result = Vec::new();
loop {
let mut buffer = [0; BUFFER_SIZE];
match self.tcp_stream.read(&mut buffer) {
Ok(size) => {
if size == 0 {
return Ok(result);
} else {
result.extend_from_slice(&buffer[..size]);
}
}
Err(e) => {
return Err(RustADBError::IOError(e));
}
}
}
})();
self.new_connection()?;
result
}
/// Starts an interactive shell session on the device. Redirects stdin/stdout/stderr as appropriate.
pub fn shell<S: ToString>(&mut self, serial: &Option<S>) -> Result<()> {
let mut adb_termios = ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
self.tcp_stream.set_nodelay(true)?;
// FORWARD CTRL+C !!
let supported_features = self.host_features(serial)?;
if !supported_features.contains(&HostFeatures::ShellV2)
&& !supported_features.contains(&HostFeatures::Cmd)
{
return Err(RustADBError::ADBShellNotSupported);
}
self.new_connection()?;
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
self.send_adb_request(AdbCommand::Shell)?;
// let read_stream = Arc::new(self.tcp_stream);
let mut read_stream = self.tcp_stream.try_clone()?;
// Writing thread
let mut write_stream = read_stream.try_clone()?;
let writer_t = std::thread::spawn(move || -> Result<()> {
let mut buf = [0; 1024];
loop {
let size = std::io::stdin().read(&mut buf)?;
write_stream.write_all(&buf[0..size])?;
}
});
// Reading thread
let reader_t = std::thread::spawn(move || -> Result<()> {
const BUFFER_SIZE: usize = 512;
loop {
let mut buffer = [0; BUFFER_SIZE];
match read_stream.read(&mut buffer) {
Ok(0) => {
return Ok(());
}
Ok(size) => {
std::io::stdout().write_all(&buffer[..size])?;
std::io::stdout().flush()?;
}
Err(e) => {
return Err(RustADBError::IOError(e));
}
}
}
});
if let Err(e) = reader_t.join().unwrap() {
match e {
RustADBError::IOError(e) if e.kind() == ErrorKind::BrokenPipe => {}
_ => {
return Err(e);
}
}
}
if let Err(e) = writer_t.join().unwrap() {
match e {
RustADBError::IOError(e) if e.kind() == ErrorKind::BrokenPipe => {}
_ => {
return Err(e);
}
}
}
Ok(())
}
}

View File

@@ -1,99 +0,0 @@
use std::{
fmt::Display,
io::{Read, Write},
time::{Duration, UNIX_EPOCH},
};
use byteorder::{ByteOrder, LittleEndian};
use chrono::{DateTime, Utc};
use crate::{
models::{AdbCommand, SyncCommand},
AdbTcpConnection, Result, RustADBError,
};
#[derive(Debug)]
pub struct AdbStatResponse {
pub file_perm: u32,
pub file_size: u32,
pub mod_time: u32,
}
impl From<[u8; 12]> for AdbStatResponse {
fn from(value: [u8; 12]) -> Self {
Self {
file_perm: LittleEndian::read_u32(&value[0..4]),
file_size: LittleEndian::read_u32(&value[4..8]),
mod_time: LittleEndian::read_u32(&value[8..]),
}
}
}
impl Display for AdbStatResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let d = UNIX_EPOCH + Duration::from_secs(self.mod_time.into());
// Create DateTime from SystemTime
let datetime = DateTime::<Utc>::from(d);
writeln!(f, "File permissions: {}", self.file_perm)?;
writeln!(f, "File size: {} bytes", self.file_size)?;
write!(
f,
"Modification time: {}",
datetime.format("%Y-%m-%d %H:%M:%S.%f %Z")
)?;
Ok(())
}
}
impl AdbTcpConnection {
fn handle_stat_command<S: AsRef<str>>(&mut self, path: S) -> Result<AdbStatResponse> {
let mut len_buf = [0_u8; 4];
LittleEndian::write_u32(&mut len_buf, path.as_ref().len() as u32);
// 4 bytes of command name is already sent by send_sync_request
self.tcp_stream.write_all(&len_buf)?;
self.tcp_stream
.write_all(path.as_ref().to_string().as_bytes())?;
// Reads returned status code from ADB server
let mut response = [0_u8; 4];
self.tcp_stream.read_exact(&mut response)?;
match std::str::from_utf8(response.as_ref())? {
"STAT" => {
let mut data = [0_u8; 12];
self.tcp_stream.read_exact(&mut data)?;
Ok(data.into())
}
x => Err(RustADBError::UnknownResponseType(format!(
"Unknown response {}",
x
))),
}
}
/// Stat file given as [path] on the device.
pub fn stat<S: ToString, A: AsRef<str>>(
&mut self,
serial: Option<S>,
path: A,
) -> Result<AdbStatResponse> {
self.new_connection()?;
match serial {
None => self.send_adb_request(AdbCommand::TransportAny)?,
Some(serial) => {
self.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?
}
}
// Set device in SYNC mode
self.send_adb_request(AdbCommand::Sync)?;
// Send a "Stat" command
self.send_sync_request(SyncCommand::Stat)?;
self.handle_stat_command(path)
}
}

View File

@@ -1,10 +0,0 @@
use crate::{models::AdbCommand, AdbTcpConnection, AdbVersion, Result};
impl AdbTcpConnection {
/// Gets server's internal version number.
pub fn version(&mut self) -> Result<AdbVersion> {
let version = self.proxy_connection(AdbCommand::Version, true)?;
AdbVersion::try_from(version)
}
}

View File

@@ -1,45 +0,0 @@
use thiserror::Error;
/// Custom Result type thrown by this crate.
pub type Result<T> = std::result::Result<T, RustADBError>;
/// Represents all error types that can be thrown by the crate.
#[derive(Error, Debug)]
pub enum RustADBError {
/// Indicates that an error occurred with I/O.
#[error(transparent)]
IOError(#[from] std::io::Error),
/// Indicates that an error occurred when sending ADB request.
#[error("ADB request failed - {0}")]
ADBRequestFailed(String),
/// Indicates that ADB server responded an unknown response type.
#[error("Unknown response type {0}")]
UnknownResponseType(String),
/// Indicates that ADB server responses an unknown device state.
#[error("Unknown device state {0}")]
UnknownDeviceState(String),
/// Indicates that an error occurred during UTF-8 parsing.
#[error(transparent)]
Utf8StrError(#[from] std::str::Utf8Error),
/// Indicates that an error occurred during UTF-8 parsing.
#[error(transparent)]
Utf8StringError(#[from] std::string::FromUtf8Error),
/// Indicates that the provided address is not a correct IP address.
#[error(transparent)]
AddrParseError(#[from] std::net::AddrParseError),
/// Indicates an error with regexps.
#[error(transparent)]
RegexError(#[from] regex::Error),
/// Indicates that parsing regex did not worked.
#[error("Regex parsing error: missing field")]
RegexParsingError,
/// Indicates an error with the integer conversion.
#[error(transparent)]
ParseIntError(#[from] std::num::ParseIntError),
/// Indicates that an error occurred when converting a value.
#[error("Conversion error")]
ConversionError,
/// Remote ADB server does not support shell feature.
#[error("Remote ADB server does not support shell feature")]
ADBShellNotSupported,
}

View File

@@ -1,14 +0,0 @@
#![crate_type = "lib"]
#![forbid(unsafe_code)]
#![forbid(missing_debug_implementations)]
#![forbid(missing_docs)]
#![doc = include_str!("../README.md")]
mod adb_tcp_connection;
mod adb_termios;
mod commands;
mod error;
mod models;
pub use adb_tcp_connection::AdbTcpConnection;
pub use error::{Result, RustADBError};
pub use models::{AdbVersion, Device, DeviceLong, DeviceState, RebootType};

View File

@@ -1,75 +0,0 @@
use std::fmt::Display;
use super::RebootType;
pub enum AdbCommand {
Version,
Kill,
Devices,
DevicesLong,
TrackDevices,
HostFeatures,
// TODO: NOT IMPLEMENTED YET
// Emulator(u16),
// Transport(String),
// TransportUSB,
// TransportLocal,
TransportAny,
TransportSerial(String),
// Serial((String, String)),
// USB(String),
// Local(String),
// Request(String),
// GetProduct(String),
// GetSerialNo(String),
// GetDevPath(String),
// GetState(String),
// Forward((String, String, String)),
// ForwardNoRebind((String, String, String)),
// KillForward((String, String)),
// KillForwardAll(String),
// ListForward(String),
ShellCommand(String),
Shell,
// Remount,
// DevPath(String),
// Tcp(u16),
// Tcp((u16, String)),
// Local(String),
// LocalReserved(String),
// LocalAbstract(String),
// LocalFileSystem(String),
// FrameBuffer,
// JDWP(u32),
// TrackJDWP,
Sync,
// Reverse(String),
Reboot(RebootType),
}
impl Display for AdbCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AdbCommand::Version => write!(f, "host:version"),
AdbCommand::Kill => write!(f, "host:kill"),
AdbCommand::Devices => write!(f, "host:devices"),
AdbCommand::DevicesLong => write!(f, "host:devices-l"),
AdbCommand::Sync => write!(f, "sync:"),
AdbCommand::TrackDevices => write!(f, "host:track-devices"),
AdbCommand::TransportAny => write!(f, "host:transport-any"),
AdbCommand::TransportSerial(serial) => write!(f, "host:transport:{serial}"),
AdbCommand::ShellCommand(command) => match std::env::var("TERM") {
Ok(term) => write!(f, "shell,TERM={term},raw:{command}"),
Err(_) => write!(f, "shell,raw:{command}"),
},
AdbCommand::Shell => match std::env::var("TERM") {
Ok(term) => write!(f, "shell,TERM={term},raw:"),
Err(_) => write!(f, "shell,raw:"),
},
AdbCommand::HostFeatures => write!(f, "host:features"),
AdbCommand::Reboot(reboot_type) => {
write!(f, "reboot:{reboot_type}")
}
}
}
}

View File

@@ -1,44 +1,98 @@
#[cfg(test)]
mod tests {
use std::net::Ipv4Addr;
use std::str::FromStr;
use std::io::Cursor;
use adb_client::AdbTcpConnection;
use adb_client::{ADBServer, ADBServerDevice, DeviceLong};
use rand::Rng;
fn new_client() -> AdbTcpConnection {
let address = Ipv4Addr::from_str("127.0.0.1").unwrap();
AdbTcpConnection::new(address, 5037).expect("Could not build ADB client...")
fn new_client() -> ADBServer {
ADBServer::default()
}
fn new_device() -> ADBServerDevice {
let mut client = new_client();
return client.get_device().expect("cannot get device");
}
#[test]
fn test_version() {
let mut adb = new_client();
adb.version().unwrap();
adb.version().expect("cannot get adb version");
}
#[test]
fn test_shell() {
let mut adb = new_client();
adb.shell_command(&None, vec!["ls"]).unwrap();
adb.shell_command(&None, vec!["pwd"]).unwrap();
fn test_shell_commands() {
let mut device = new_device();
device.shell_command(["ls"]).expect("error while executing `ls` command");
device.shell_command(["pwd"]).expect("error while executing `pwd` command");
}
#[test]
fn test_devices() {
let mut adb = new_client();
adb.devices().unwrap();
adb.devices().expect("cannot list devices");
}
#[test]
fn test_devices_long() {
let mut adb = new_client();
adb.devices_long().unwrap();
adb.devices_long().expect("cannot list devices long");
}
#[test]
#[should_panic]
fn test_wrong_addr() {
let address = Ipv4Addr::from_str("127.0.0.300").unwrap();
let _ = AdbTcpConnection::new(address, 5037).expect("Could not create ADB connection...");
fn test_static_devices_long() {
let inputs = ["7a5158f05122195aa device 1-5 product:gts210vewifixx model:SM_T813 device:gts210vewifi transport_id:4"];
for input in inputs {
DeviceLong::try_from(input.as_bytes().to_vec())
.expect(&format!("cannot parse input: '{input}'"));
}
}
#[test]
fn test_send_recv() {
// Create random "Reader" in memory
let mut key = [0u8; 1000];
rand::thread_rng().fill(&mut key[..]);
let mut c: Cursor<Vec<u8>> = Cursor::new(key.to_vec());
let mut device = new_device();
const TEST_FILENAME: &'static str = "/data/local/tmp/test_file";
// Send it
device
.send(&mut c, TEST_FILENAME)
.expect("cannot send file");
// Pull it to memory
let mut res = vec![];
device
.recv(TEST_FILENAME, &mut res)
.expect("cannot recv file");
// diff
assert_eq!(c.get_ref(), &res);
device
.shell_command::<&str>([format!("rm {TEST_FILENAME}").as_str()])
.expect("cannot remove test file");
}
#[test]
fn multiple_connexions() {
let mut connection = new_client();
for _ in 0..2 {
let _ = connection.devices().expect("cannot get version");
}
}
#[test]
fn command_emulator() {
let mut connection = new_client();
let mut emulator = connection
.get_emulator_device()
.expect("no emulator running");
emulator.hello().expect("cannot hello");
}
}