Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add reverse for USB/TCP direct devices #73

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4
- run: rustup component add clippy
- name: Run clippy
run: cargo clippy --all-features
run: cargo clippy --all-features -- -D warnings

fmt:
name: "fmt"
Expand Down
8 changes: 8 additions & 0 deletions adb_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ fn main() -> Result<()> {
device.install(&path)?;
}
DeviceCommands::Framebuffer { path } => device.framebuffer(&path)?,
DeviceCommands::Reverse { remote, local } => {
log::info!("Reverse port {} to local {}", remote, local);
device.reverse(remote, local)?;
}
DeviceCommands::ReverseRemoveAll => {
log::info!("Remove all previous forward rules");
device.reverse_remove_all()?;
}
}

Ok(())
Expand Down
10 changes: 10 additions & 0 deletions adb_cli/src/models/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;

use adb_client::ADBProtoPort;
use clap::Parser;

use super::RebootTypeCommand;
Expand Down Expand Up @@ -38,4 +39,13 @@ pub enum DeviceCommands {
/// Framebuffer image destination path
path: String,
},
/// Reverse socket connection from remote port to local port
Reverse {
/// Remote port
remote: ADBProtoPort,
/// Local port
local: ADBProtoPort,
},
/// Remove all previously applied reverse rules
ReverseRemoveAll,
}
2 changes: 1 addition & 1 deletion adb_cli/src/models/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Opts {
pub enum MainCommand {
/// Server related commands
Host(ServerCommand<HostCommand>),
/// Device related commands using server
/// Device related commands (using adb-server as a bridge)
Local(ServerCommand<LocalCommand>),
/// Emulator related commands
Emu(EmulatorCommand),
Expand Down
14 changes: 13 additions & 1 deletion adb_client/src/adb_device_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::Path;
use image::{ImageBuffer, ImageFormat, Rgba};

use crate::models::AdbStatResponse;
use crate::{RebootType, Result};
use crate::{ADBProtoPort, RebootType, Result};

/// Trait representing all features available on both [`crate::ADBServerDevice`] and [`crate::ADBUSBDevice`]
pub trait ADBDeviceExt {
Expand Down Expand Up @@ -62,6 +62,18 @@ pub trait ADBDeviceExt {
Ok(vec.into_inner())
}

/// Forward socket connection
fn forward(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()>;

/// Remove all previously applied forward rules
fn forward_remove_all(&mut self) -> Result<()>;

/// Reverse socket connection
fn reverse(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()>;

/// Remove all reverse rules
fn reverse_remove_all(&mut self) -> Result<()>;

/// Return a boxed instance representing this trait
fn boxed(self) -> Box<dyn ADBDeviceExt>
where
Expand Down
11 changes: 10 additions & 1 deletion adb_client/src/device/adb_message_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{models::MessageSubcommand, ADBTransportMessage, MessageCommand};

/// Generic structure representing an ADB device reachable over an [`ADBMessageTransport`].
/// Structure is totally agnostic over which transport is truly used.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ADBMessageDevice<T: ADBMessageTransport> {
transport: T,
local_id: Option<u32>,
Expand Down Expand Up @@ -235,6 +235,15 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
Ok(response)
}

pub(crate) fn set_random_local_id(&mut self) {
let mut rng = rand::thread_rng();
self.local_id = Some(rng.gen());
}

pub(crate) fn set_remote_id(&mut self, remote_id: u32) {
self.remote_id = Some(remote_id);
}

pub(crate) fn get_local_id(&self) -> Result<u32> {
self.local_id.ok_or(RustADBError::ADBRequestFailed(
"connection not opened, no local_id".into(),
Expand Down
20 changes: 19 additions & 1 deletion adb_client/src/device/adb_message_device_commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{models::AdbStatResponse, ADBDeviceExt, ADBMessageTransport, RebootType, Result};
use crate::{
models::AdbStatResponse, ADBDeviceExt, ADBMessageTransport, ADBProtoPort, RebootType, Result,
};
use std::{
io::{Read, Write},
path::Path,
Expand Down Expand Up @@ -38,4 +40,20 @@ impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> {
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.framebuffer_inner()
}

fn forward(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()> {
todo!()
}

fn forward_remove_all(&mut self) -> Result<()> {
todo!()
}

fn reverse(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()> {
self.reverse(remote, local)
}

fn reverse_remove_all(&mut self) -> Result<()> {
self.reverse_remove_all()
}
}
25 changes: 24 additions & 1 deletion adb_client/src/device/adb_tcp_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::{io::Read, net::SocketAddr};
use super::adb_message_device::ADBMessageDevice;
use super::models::MessageCommand;
use super::ADBTransportMessage;
use crate::{ADBDeviceExt, ADBMessageTransport, ADBTransport, Result, RustADBError, TcpTransport};
use crate::{
ADBDeviceExt, ADBMessageTransport, ADBProtoPort, ADBTransport, Result, RustADBError,
TcpTransport,
};

/// Represent a device reached and available over USB.
#[derive(Debug)]
Expand Down Expand Up @@ -106,6 +109,26 @@ impl ADBDeviceExt for ADBTcpDevice {
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.inner.framebuffer_inner()
}

#[inline]
fn forward(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()> {
self.inner.forward(remote, local)
}

#[inline]
fn forward_remove_all(&mut self) -> Result<()> {
self.inner.forward_remove_all()
}

#[inline]
fn reverse(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()> {
self.inner.reverse(remote, local)
}

#[inline]
fn reverse_remove_all(&mut self) -> Result<()> {
self.inner.reverse_remove_all()
}
}

impl Drop for ADBTcpDevice {
Expand Down
21 changes: 21 additions & 0 deletions adb_client/src/device/adb_usb_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{ADBRsaKey, ADBTransportMessage};
use crate::device::adb_transport_message::{AUTH_RSAPUBLICKEY, AUTH_SIGNATURE, AUTH_TOKEN};
use crate::ADBDeviceExt;
use crate::ADBMessageTransport;
use crate::ADBProtoPort;
use crate::ADBTransport;
use crate::{Result, RustADBError, USBTransport};

Expand Down Expand Up @@ -289,6 +290,26 @@ impl ADBDeviceExt for ADBUSBDevice {
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.inner.framebuffer_inner()
}

#[inline]
fn forward(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()> {
self.inner.forward(remote, local)
}

#[inline]
fn forward_remove_all(&mut self) -> Result<()> {
self.inner.forward_remove_all()
}

#[inline]
fn reverse(&mut self, remote: ADBProtoPort, local: ADBProtoPort) -> Result<()> {
self.inner.reverse(remote, local)
}

#[inline]
fn reverse_remove_all(&mut self) -> Result<()> {
self.inner.reverse_remove_all()
}
}

impl Drop for ADBUSBDevice {
Expand Down
1 change: 1 addition & 0 deletions adb_client/src/device/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ mod install;
mod pull;
mod push;
mod reboot;
mod reverse;
mod shell;
mod stat;
Loading
Loading