From 7a1acc2b21d81a6eef12a72d6753532493d97d47 Mon Sep 17 00:00:00 2001 From: cli-s1n Date: Fri, 13 Dec 2024 10:26:20 +0100 Subject: [PATCH 1/4] chore: update adb_client README.md --- adb_cli/README.md | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/adb_cli/README.md b/adb_cli/README.md index 29ba48f..4336537 100644 --- a/adb_cli/README.md +++ b/adb_cli/README.md @@ -18,38 +18,29 @@ 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 +user@laptop ~/adb_client (main)> adb_cli local --help +Device related commands using server -Usage: adb_cli [OPTIONS] +Usage: adb_cli local [OPTIONS] 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 + pull Pull a file from device + push Push a file on device + stat Stat a file on device + run Run an activity on device specified by the intent reboot Reboot the device + install Install an APK on device framebuffer Dump framebuffer of device + host-features List available server features + list List a directory on 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
[default: 127.0.0.1:5037] -s, --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 From 0b97b0e1ec854e516c47fe7a02fc56b489d19898 Mon Sep 17 00:00:00 2001 From: cli-s1n Date: Fri, 13 Dec 2024 11:34:21 +0100 Subject: [PATCH 2/4] chore(internal): add assert_command() method on ADBTransportMessage --- adb_cli/src/main.rs | 5 ++- adb_client/src/device/adb_message_device.rs | 14 +++--- adb_client/src/device/adb_tcp_device.rs | 20 +++------ .../src/device/adb_transport_message.rs | 18 ++++++-- adb_client/src/device/adb_usb_device.rs | 43 +++++++------------ adb_client/src/device/commands/framebuffer.rs | 13 +++--- adb_client/src/device/commands/reboot.rs | 12 ++---- adb_client/src/device/message_writer.rs | 23 +++++----- adb_client/src/error.rs | 3 ++ 9 files changed, 70 insertions(+), 81 deletions(-) diff --git a/adb_cli/src/main.rs b/adb_cli/src/main.rs index e3cd784..fa12d3a 100644 --- a/adb_cli/src/main.rs +++ b/adb_cli/src/main.rs @@ -133,7 +133,10 @@ fn main() -> Result<()> { log::info!("Starting installation of APK {}...", path.display()); device.install(&path)?; } - DeviceCommands::Framebuffer { path } => device.framebuffer(&path)?, + DeviceCommands::Framebuffer { path } => { + device.framebuffer(&path)?; + log::info!("Successfully dumped framebuffer at path {path}"); + } } Ok(()) diff --git a/adb_client/src/device/adb_message_device.rs b/adb_client/src/device/adb_message_device.rs index ef7b3ce..79fc071 100644 --- a/adb_client/src/device/adb_message_device.rs +++ b/adb_client/src/device/adb_message_device.rs @@ -51,15 +51,11 @@ impl ADBMessageDevice { message: ADBTransportMessage, ) -> Result { self.transport.write_message(message)?; - let message = self.transport.read_message()?; - let received_command = message.header().command(); - if received_command != MessageCommand::Okay { - return Err(RustADBError::ADBRequestFailed(format!( - "expected command OKAY after message, got {}", - received_command - ))); - } - Ok(message) + + self.transport.read_message().and_then(|message| { + message.assert_command(MessageCommand::Okay)?; + Ok(message) + }) } pub(crate) fn recv_file( diff --git a/adb_client/src/device/adb_tcp_device.rs b/adb_client/src/device/adb_tcp_device.rs index b1c622b..e82d929 100644 --- a/adb_client/src/device/adb_tcp_device.rs +++ b/adb_client/src/device/adb_tcp_device.rs @@ -5,7 +5,7 @@ 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, ADBTransport, Result, TcpTransport}; /// Represent a device reached and available over USB. #[derive(Debug)] @@ -38,19 +38,13 @@ impl ADBTcpDevice { self.get_transport_mut().write_message(message)?; - let message = self.get_transport_mut().read_message()?; + // At this point, we should have received a STLS command indicating that the device wants to upgrade connection with TLS + self.get_transport_mut() + .read_message() + .and_then(|message| message.assert_command(MessageCommand::Stls))?; - // At this point, we should have received a STLS message - if message.header().command() != MessageCommand::Stls { - return Err(RustADBError::ADBRequestFailed(format!( - "Wrong command received {}", - message.header().command() - ))); - }; - - let message = ADBTransportMessage::new(MessageCommand::Stls, 1, 0, &[]); - - self.get_transport_mut().write_message(message)?; + self.get_transport_mut() + .write_message(ADBTransportMessage::new(MessageCommand::Stls, 1, 0, &[]))?; // Upgrade TCP connection to TLS self.get_transport_mut().upgrade_connection()?; diff --git a/adb_client/src/device/adb_transport_message.rs b/adb_client/src/device/adb_transport_message.rs index 006a3cd..d1ef0da 100644 --- a/adb_client/src/device/adb_transport_message.rs +++ b/adb_client/src/device/adb_transport_message.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::RustADBError; +use crate::{Result, RustADBError}; use super::models::MessageCommand; @@ -66,7 +66,7 @@ impl ADBTransportMessageHeader { command_u32 ^ 0xFFFFFFFF } - pub fn as_bytes(&self) -> Result, RustADBError> { + pub fn as_bytes(&self) -> Result> { bincode::serialize(&self).map_err(|_e| RustADBError::ConversionError) } } @@ -88,6 +88,18 @@ impl ADBTransportMessage { && ADBTransportMessageHeader::compute_crc32(&self.payload) == self.header.data_crc32 } + pub fn assert_command(&self, expected_command: MessageCommand) -> Result<()> { + let our_command = self.header().command(); + if expected_command == our_command { + return Ok(()); + } + + Err(RustADBError::WrongResponseReceived( + our_command.to_string(), + expected_command.to_string(), + )) + } + pub fn header(&self) -> &ADBTransportMessageHeader { &self.header } @@ -104,7 +116,7 @@ impl ADBTransportMessage { impl TryFrom<[u8; 24]> for ADBTransportMessageHeader { type Error = RustADBError; - fn try_from(value: [u8; 24]) -> Result { + fn try_from(value: [u8; 24]) -> Result { bincode::deserialize(&value).map_err(|_e| RustADBError::ConversionError) } } diff --git a/adb_client/src/device/adb_usb_device.rs b/adb_client/src/device/adb_usb_device.rs index b2d72d3..25a003a 100644 --- a/adb_client/src/device/adb_usb_device.rs +++ b/adb_client/src/device/adb_usb_device.rs @@ -181,21 +181,14 @@ impl ADBUSBDevice { self.get_transport_mut().write_message(message)?; let message = self.get_transport_mut().read_message()?; + message.assert_command(MessageCommand::Auth)?; - // At this point, we should have received either: - // - an AUTH message with arg0 == 1 - // - a CNXN message - let auth_message = match message.header().command() { - MessageCommand::Auth if message.header().arg0() == AUTH_TOKEN => message, - MessageCommand::Auth if message.header().arg0() != AUTH_TOKEN => { - return Err(RustADBError::ADBRequestFailed( - "Received AUTH message with type != 1".into(), - )) - } - c => { + // At this point, we should have receive an AUTH message with arg0 == 1 + let auth_message = match message.header().arg0() { + AUTH_TOKEN => message, + v => { return Err(RustADBError::ADBRequestFailed(format!( - "Wrong command received {}", - c + "Received AUTH message with type != 1 ({v})" ))) } }; @@ -225,20 +218,16 @@ impl ADBUSBDevice { let response = self .get_transport_mut() - .read_message_with_timeout(Duration::from_secs(10))?; - - match response.header().command() { - MessageCommand::Cnxn => log::info!( - "Authentication OK, device info {}", - String::from_utf8(response.into_payload())? - ), - _ => { - return Err(RustADBError::ADBRequestFailed(format!( - "wrong response {}", - response.header().command() - ))) - } - } + .read_message_with_timeout(Duration::from_secs(10)) + .and_then(|message| { + message.assert_command(MessageCommand::Cnxn)?; + Ok(message) + })?; + + log::info!( + "Authentication OK, device info {}", + String::from_utf8(response.into_payload())? + ); Ok(()) } diff --git a/adb_client/src/device/commands/framebuffer.rs b/adb_client/src/device/commands/framebuffer.rs index 5b8e87f..0a5a62a 100644 --- a/adb_client/src/device/commands/framebuffer.rs +++ b/adb_client/src/device/commands/framebuffer.rs @@ -89,13 +89,10 @@ impl ADBMessageDevice { v => return Err(RustADBError::UnimplementedFramebufferImageVersion(v)), }; - let message = self.get_transport_mut().read_message()?; - match message.header().command() { - MessageCommand::Clse => Ok(img), - c => Err(RustADBError::ADBRequestFailed(format!( - "Wrong command received {}", - c - ))), - } + self.get_transport_mut() + .read_message() + .and_then(|message| message.assert_command(MessageCommand::Clse))?; + + Ok(img) } } diff --git a/adb_client/src/device/commands/reboot.rs b/adb_client/src/device/commands/reboot.rs index 4bab219..79d4de4 100644 --- a/adb_client/src/device/commands/reboot.rs +++ b/adb_client/src/device/commands/reboot.rs @@ -1,18 +1,14 @@ use crate::{ device::{adb_message_device::ADBMessageDevice, MessageCommand}, - ADBMessageTransport, RebootType, Result, RustADBError, + ADBMessageTransport, RebootType, Result, }; impl ADBMessageDevice { pub(crate) fn reboot(&mut self, reboot_type: RebootType) -> Result<()> { self.open_session(format!("reboot:{}\0", reboot_type).as_bytes())?; - let message = self.get_transport_mut().read_message()?; - - if message.header().command() != MessageCommand::Okay { - return Err(RustADBError::ADBShellNotSupported); - } - - Ok(()) + self.get_transport_mut() + .read_message() + .and_then(|message| message.assert_command(MessageCommand::Okay)) } } diff --git a/adb_client/src/device/message_writer.rs b/adb_client/src/device/message_writer.rs index b17b9b4..e74022a 100644 --- a/adb_client/src/device/message_writer.rs +++ b/adb_client/src/device/message_writer.rs @@ -1,4 +1,4 @@ -use std::io::{ErrorKind, Write}; +use std::io::{Error, ErrorKind, Result, Write}; use crate::ADBMessageTransport; @@ -24,26 +24,25 @@ impl MessageWriter { } impl Write for MessageWriter { - fn write(&mut self, buf: &[u8]) -> std::io::Result { + fn write(&mut self, buf: &[u8]) -> Result { let message = ADBTransportMessage::new(MessageCommand::Write, self.local_id, self.remote_id, buf); self.transport .write_message(message) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; + .map_err(|e| Error::new(ErrorKind::InvalidData, e))?; match self.transport.read_message() { - Ok(response) => match response.header().command() { - MessageCommand::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)), + Ok(response) => { + response + .assert_command(MessageCommand::Okay) + .map_err(|e| Error::new(ErrorKind::Other, e))?; + Ok(buf.len()) + } + Err(e) => Err(Error::new(ErrorKind::Other, e)), } } - fn flush(&mut self) -> std::io::Result<()> { + fn flush(&mut self) -> Result<()> { Ok(()) } } diff --git a/adb_client/src/error.rs b/adb_client/src/error.rs index c53cb34..700ebce 100644 --- a/adb_client/src/error.rs +++ b/adb_client/src/error.rs @@ -15,6 +15,9 @@ pub enum RustADBError { /// Indicates that ADB server responded an unknown response type. #[error("Unknown response type {0}")] UnknownResponseType(String), + /// Indicated that an unexpected command has been received + #[error("Wrong response command received: {0}. Expected {1}")] + WrongResponseReceived(String, String), /// Indicates that ADB server responses an unknown device state. #[error("Unknown device state {0}")] UnknownDeviceState(String), From 4e65f3109330473b7d066bacb2dbb6e86ffd7301 Mon Sep 17 00:00:00 2001 From: cli-s1n Date: Fri, 13 Dec 2024 13:42:31 +0100 Subject: [PATCH 3/4] chore: minor internal changes --- .../emulator_device/adb_emulator_device.rs | 2 +- .../src/server_device/adb_server_device.rs | 12 ++--------- .../adb_server_device_commands.rs | 17 ++++++---------- .../src/server_device/commands/forward.rs | 4 ++-- .../src/server_device/commands/framebuffer.rs | 20 ++++++------------- .../server_device/commands/host_features.rs | 2 +- .../src/server_device/commands/install.rs | 6 +++--- adb_client/src/server_device/commands/list.rs | 16 ++++++--------- .../src/server_device/commands/reboot.rs | 2 +- .../src/server_device/commands/reconnect.rs | 2 +- adb_client/src/server_device/commands/recv.rs | 8 +++----- .../src/server_device/commands/reverse.rs | 4 ++-- adb_client/src/server_device/commands/send.rs | 14 +++++-------- adb_client/src/server_device/commands/stat.rs | 18 ++++++----------- .../src/server_device/commands/tcpip.rs | 2 +- adb_client/src/server_device/commands/usb.rs | 2 +- .../src/transports/tcp_server_transport.rs | 6 +++--- 17 files changed, 50 insertions(+), 87 deletions(-) diff --git a/adb_client/src/emulator_device/adb_emulator_device.rs b/adb_client/src/emulator_device/adb_emulator_device.rs index ed304ce..159f7a2 100644 --- a/adb_client/src/emulator_device/adb_emulator_device.rs +++ b/adb_client/src/emulator_device/adb_emulator_device.rs @@ -67,7 +67,7 @@ impl TryFrom for ADBEmulatorDevice { fn try_from(value: ADBServerDevice) -> std::result::Result { ADBEmulatorDevice::new( value.identifier.clone(), - Some(*value.get_transport().get_socketaddr().ip()), + Some(*value.transport.get_socketaddr().ip()), ) } } diff --git a/adb_client/src/server_device/adb_server_device.rs b/adb_client/src/server_device/adb_server_device.rs index 8db18af..afd2a17 100644 --- a/adb_client/src/server_device/adb_server_device.rs +++ b/adb_client/src/server_device/adb_server_device.rs @@ -7,7 +7,7 @@ pub struct ADBServerDevice { /// Unique device identifier. pub identifier: String, /// Internal [TCPServerTransport] - transport: TCPServerTransport, + pub(crate) transport: TCPServerTransport, } impl ADBServerDevice { @@ -25,19 +25,11 @@ impl ADBServerDevice { } } - 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()) + Ok(&mut self.transport) } } diff --git a/adb_client/src/server_device/adb_server_device_commands.rs b/adb_client/src/server_device/adb_server_device_commands.rs index 9571b40..27a93aa 100644 --- a/adb_client/src/server_device/adb_server_device_commands.rs +++ b/adb_client/src/server_device/adb_server_device_commands.rs @@ -1,5 +1,5 @@ use std::{ - io::{Read, Write}, + io::{ErrorKind, Read, Write}, path::Path, }; @@ -23,17 +23,13 @@ impl ADBDeviceExt for ADBServerDevice { let serial = self.identifier.clone(); self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .send_adb_request(AdbServerCommand::ShellCommand(command.join(" ")))?; const BUFFER_SIZE: usize = 4096; loop { let mut buffer = [0; BUFFER_SIZE]; - match self - .get_transport_mut() - .get_raw_connection()? - .read(&mut buffer) - { + match self.transport.get_raw_connection()?.read(&mut buffer) { Ok(size) => { if size == 0 { return Ok(()); @@ -67,10 +63,9 @@ impl ADBDeviceExt for ADBServerDevice { let serial = self.identifier.clone(); self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() - .send_adb_request(AdbServerCommand::Shell)?; + self.transport.send_adb_request(AdbServerCommand::Shell)?; - let mut read_stream = self.get_transport_mut().get_raw_connection()?.try_clone()?; + let mut read_stream = self.transport.get_raw_connection()?.try_clone()?; let mut write_stream = read_stream.try_clone()?; @@ -97,7 +92,7 @@ impl ADBDeviceExt for ADBServerDevice { // 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(()), + ErrorKind::BrokenPipe => return Ok(()), _ => return Err(RustADBError::IOError(e)), } } diff --git a/adb_client/src/server_device/commands/forward.rs b/adb_client/src/server_device/commands/forward.rs index 80b538a..263cc44 100644 --- a/adb_client/src/server_device/commands/forward.rs +++ b/adb_client/src/server_device/commands/forward.rs @@ -7,7 +7,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::Forward(remote, local), false) .map(|_| ()) } @@ -18,7 +18,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::ForwardRemoveAll, false) .map(|_| ()) } diff --git a/adb_client/src/server_device/commands/framebuffer.rs b/adb_client/src/server_device/commands/framebuffer.rs index b4863c8..ef942d4 100644 --- a/adb_client/src/server_device/commands/framebuffer.rs +++ b/adb_client/src/server_device/commands/framebuffer.rs @@ -15,11 +15,11 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .send_adb_request(AdbServerCommand::FrameBuffer)?; let version = self - .get_transport_mut() + .transport .get_raw_connection()? .read_u32::()?; @@ -28,9 +28,7 @@ impl ADBServerDevice { 1 => { let mut buf = [0u8; std::mem::size_of::()]; - self.get_transport_mut() - .get_raw_connection()? - .read_exact(&mut buf)?; + self.transport.get_raw_connection()?.read_exact(&mut buf)?; let framebuffer_info: FrameBufferInfoV1 = buf.try_into()?; @@ -41,9 +39,7 @@ impl ADBServerDevice { .try_into() .map_err(|_| RustADBError::ConversionError)? ]; - self.get_transport_mut() - .get_raw_connection()? - .read_exact(&mut data)?; + self.transport.get_raw_connection()?.read_exact(&mut data)?; Ok(ImageBuffer::, Vec>::from_vec( framebuffer_info.width, @@ -56,9 +52,7 @@ impl ADBServerDevice { 2 => { let mut buf = [0u8; std::mem::size_of::()]; - self.get_transport_mut() - .get_raw_connection()? - .read_exact(&mut buf)?; + self.transport.get_raw_connection()?.read_exact(&mut buf)?; let framebuffer_info: FrameBufferInfoV2 = buf.try_into()?; @@ -69,9 +63,7 @@ impl ADBServerDevice { .try_into() .map_err(|_| RustADBError::ConversionError)? ]; - self.get_transport_mut() - .get_raw_connection()? - .read_exact(&mut data)?; + self.transport.get_raw_connection()?.read_exact(&mut data)?; Ok(ImageBuffer::, Vec>::from_vec( framebuffer_info.width, diff --git a/adb_client/src/server_device/commands/host_features.rs b/adb_client/src/server_device/commands/host_features.rs index c993619..5f5ae4b 100644 --- a/adb_client/src/server_device/commands/host_features.rs +++ b/adb_client/src/server_device/commands/host_features.rs @@ -11,7 +11,7 @@ impl ADBServerDevice { .send_adb_request(AdbServerCommand::TransportSerial(serial))?; let features = self - .get_transport_mut() + .transport .proxy_connection(AdbServerCommand::HostFeatures, true)?; Ok(features diff --git a/adb_client/src/server_device/commands/install.rs b/adb_client/src/server_device/commands/install.rs index ae31f9a..dace45a 100644 --- a/adb_client/src/server_device/commands/install.rs +++ b/adb_client/src/server_device/commands/install.rs @@ -17,15 +17,15 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .send_adb_request(AdbServerCommand::Install(file_size))?; - let mut raw_connection = self.get_transport_mut().get_raw_connection()?; + let mut raw_connection = self.transport.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)?; + let read_amount = self.transport.get_raw_connection()?.read(&mut data)?; match &data[0..read_amount] { b"Success\n" => { diff --git a/adb_client/src/server_device/commands/list.rs b/adb_client/src/server_device/commands/list.rs index 27919be..f6053e9 100644 --- a/adb_client/src/server_device/commands/list.rs +++ b/adb_client/src/server_device/commands/list.rs @@ -16,12 +16,10 @@ impl ADBServerDevice { .send_adb_request(AdbServerCommand::TransportSerial(serial))?; // Set device in SYNC mode - self.get_transport_mut() - .send_adb_request(AdbServerCommand::Sync)?; + self.transport.send_adb_request(AdbServerCommand::Sync)?; // Send a list command - self.get_transport_mut() - .send_sync_request(SyncCommand::List)?; + self.transport.send_sync_request(SyncCommand::List)?; self.handle_list_command(path) } @@ -33,19 +31,17 @@ impl ADBServerDevice { 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.transport.get_raw_connection()?.write_all(&len_buf)?; // List send the string of the directory to list, and then the server send a list of files - self.get_transport_mut() + self.transport .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.get_transport_mut() + self.transport .get_raw_connection()? .read_exact(&mut response)?; match str::from_utf8(response.as_ref())? { @@ -57,7 +53,7 @@ impl ADBServerDevice { let mut mod_time = [0_u8; 4]; let mut name_len = [0_u8; 4]; - let mut connection = self.get_transport_mut().get_raw_connection()?; + let mut connection = self.transport.get_raw_connection()?; connection.read_exact(&mut file_mod)?; connection.read_exact(&mut file_size)?; connection.read_exact(&mut mod_time)?; diff --git a/adb_client/src/server_device/commands/reboot.rs b/adb_client/src/server_device/commands/reboot.rs index a547f61..0bf274d 100644 --- a/adb_client/src/server_device/commands/reboot.rs +++ b/adb_client/src/server_device/commands/reboot.rs @@ -10,7 +10,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::Reboot(reboot_type), false) .map(|_| ()) } diff --git a/adb_client/src/server_device/commands/reconnect.rs b/adb_client/src/server_device/commands/reconnect.rs index 4f65abe..8f2a3d5 100644 --- a/adb_client/src/server_device/commands/reconnect.rs +++ b/adb_client/src/server_device/commands/reconnect.rs @@ -7,7 +7,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::Reconnect, false) .map(|_| ()) } diff --git a/adb_client/src/server_device/commands/recv.rs b/adb_client/src/server_device/commands/recv.rs index bff7df8..1f6899c 100644 --- a/adb_client/src/server_device/commands/recv.rs +++ b/adb_client/src/server_device/commands/recv.rs @@ -76,12 +76,10 @@ impl ADBServerDevice { .send_adb_request(AdbServerCommand::TransportSerial(serial))?; // Set device in SYNC mode - self.get_transport_mut() - .send_adb_request(AdbServerCommand::Sync)?; + self.transport.send_adb_request(AdbServerCommand::Sync)?; // Send a recv command - self.get_transport_mut() - .send_sync_request(SyncCommand::Recv)?; + self.transport.send_sync_request(SyncCommand::Recv)?; self.handle_recv_command(path, stream) } @@ -91,7 +89,7 @@ impl ADBServerDevice { from: S, output: &mut dyn Write, ) -> Result<()> { - let mut raw_connection = self.get_transport().get_raw_connection()?; + let mut raw_connection = self.transport.get_raw_connection()?; let from_as_bytes = from.as_ref().as_bytes(); let mut buffer = Vec::with_capacity(4 + from_as_bytes.len()); diff --git a/adb_client/src/server_device/commands/reverse.rs b/adb_client/src/server_device/commands/reverse.rs index a7f3a56..326d2f5 100644 --- a/adb_client/src/server_device/commands/reverse.rs +++ b/adb_client/src/server_device/commands/reverse.rs @@ -7,7 +7,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::Reverse(remote, local), false) .map(|_| ()) } @@ -18,7 +18,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::ReverseRemoveAll, false) .map(|_| ()) } diff --git a/adb_client/src/server_device/commands/send.rs b/adb_client/src/server_device/commands/send.rs index 8dbf686..d22093d 100644 --- a/adb_client/src/server_device/commands/send.rs +++ b/adb_client/src/server_device/commands/send.rs @@ -50,12 +50,10 @@ impl ADBServerDevice { .send_adb_request(AdbServerCommand::TransportSerial(serial))?; // Set device in SYNC mode - self.get_transport_mut() - .send_adb_request(AdbServerCommand::Sync)?; + self.transport.send_adb_request(AdbServerCommand::Sync)?; // Send a send command - self.get_transport_mut() - .send_sync_request(SyncCommand::Send)?; + self.transport.send_sync_request(SyncCommand::Send)?; self.handle_send_command(stream, path) } @@ -64,7 +62,7 @@ impl ADBServerDevice { // 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()?; + let mut raw_connection = self.transport.get_raw_connection()?; // The name of the command is already sent by get_transport()?.send_sync_request let to_as_bytes = to.as_bytes(); @@ -99,7 +97,7 @@ impl ADBServerDevice { 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 length = self.transport.get_body_length()?; let mut body = vec![ 0; @@ -108,9 +106,7 @@ impl ADBServerDevice { .map_err(|_| RustADBError::ConversionError)? ]; if length > 0 { - self.get_transport() - .get_raw_connection()? - .read_exact(&mut body)?; + self.transport.get_raw_connection()?.read_exact(&mut body)?; } Err(RustADBError::ADBRequestFailed(String::from_utf8(body)?)) diff --git a/adb_client/src/server_device/commands/stat.rs b/adb_client/src/server_device/commands/stat.rs index 015ac61..c504409 100644 --- a/adb_client/src/server_device/commands/stat.rs +++ b/adb_client/src/server_device/commands/stat.rs @@ -13,24 +13,20 @@ impl ADBServerDevice { 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() + self.transport.get_raw_connection()?.write_all(&len_buf)?; + self.transport .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() + self.transport .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)?; + self.transport.get_raw_connection()?.read_exact(&mut data)?; Ok(data.into()) } @@ -48,12 +44,10 @@ impl ADBServerDevice { .send_adb_request(AdbServerCommand::TransportSerial(serial))?; // Set device in SYNC mode - self.get_transport_mut() - .send_adb_request(AdbServerCommand::Sync)?; + self.transport.send_adb_request(AdbServerCommand::Sync)?; // Send a "Stat" command - self.get_transport_mut() - .send_sync_request(SyncCommand::Stat)?; + self.transport.send_sync_request(SyncCommand::Stat)?; self.handle_stat_command(path) } diff --git a/adb_client/src/server_device/commands/tcpip.rs b/adb_client/src/server_device/commands/tcpip.rs index 8d06d31..0d3da56 100644 --- a/adb_client/src/server_device/commands/tcpip.rs +++ b/adb_client/src/server_device/commands/tcpip.rs @@ -7,7 +7,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::TcpIp(port), false) .map(|_| ()) } diff --git a/adb_client/src/server_device/commands/usb.rs b/adb_client/src/server_device/commands/usb.rs index 8d4fb07..7f55d0a 100644 --- a/adb_client/src/server_device/commands/usb.rs +++ b/adb_client/src/server_device/commands/usb.rs @@ -7,7 +7,7 @@ impl ADBServerDevice { self.connect()? .send_adb_request(AdbServerCommand::TransportSerial(serial))?; - self.get_transport_mut() + self.transport .proxy_connection(AdbServerCommand::Usb, false) .map(|_| ()) } diff --git a/adb_client/src/transports/tcp_server_transport.rs b/adb_client/src/transports/tcp_server_transport.rs index 0f0b1ba..ded03d5 100644 --- a/adb_client/src/transports/tcp_server_transport.rs +++ b/adb_client/src/transports/tcp_server_transport.rs @@ -1,4 +1,4 @@ -use std::io::{Read, Write}; +use std::io::{Error, ErrorKind, Read, Write}; use std::net::{Ipv4Addr, SocketAddrV4, TcpStream}; use std::str::FromStr; @@ -66,8 +66,8 @@ impl TCPServerTransport { 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, + .ok_or(RustADBError::IOError(Error::new( + ErrorKind::NotConnected, "not connected", ))) } From e0caf094531ad491815d433a4530ac8be23014b4 Mon Sep 17 00:00:00 2001 From: cli-s1n Date: Fri, 13 Dec 2024 14:06:06 +0100 Subject: [PATCH 4/4] chore: add server-only models --- adb_client/src/emulator_device/commands/rotate.rs | 6 ++---- adb_client/src/emulator_device/commands/sms.rs | 8 +++----- adb_client/src/emulator_device/mod.rs | 2 ++ .../models/adb_emulator_command.rs | 2 +- adb_client/src/emulator_device/models/mod.rs | 2 ++ adb_client/src/lib.rs | 4 +--- adb_client/src/models/mod.rs | 15 --------------- adb_client/src/server/commands/devices.rs | 4 ++-- adb_client/src/server/commands/mdns.rs | 3 +-- adb_client/src/server/commands/server_status.rs | 5 +---- adb_client/src/server/mod.rs | 2 ++ adb_client/src/{ => server}/models/adb_version.rs | 0 adb_client/src/{ => server}/models/device_long.rs | 0 .../src/{ => server}/models/device_short.rs | 0 .../src/{ => server}/models/device_state.rs | 0 .../src/{ => server}/models/mdns_services.rs | 0 adb_client/src/server/models/mod.rs | 13 +++++++++++++ .../src/{ => server}/models/server_status.rs | 9 +++++++++ .../server_device/adb_server_device_commands.rs | 1 - .../src/transports/tcp_emulator_transport.rs | 8 ++++---- 20 files changed, 43 insertions(+), 41 deletions(-) rename adb_client/src/{ => emulator_device}/models/adb_emulator_command.rs (96%) create mode 100644 adb_client/src/emulator_device/models/mod.rs rename adb_client/src/{ => server}/models/adb_version.rs (100%) rename adb_client/src/{ => server}/models/device_long.rs (100%) rename adb_client/src/{ => server}/models/device_short.rs (100%) rename adb_client/src/{ => server}/models/device_state.rs (100%) rename adb_client/src/{ => server}/models/mdns_services.rs (100%) create mode 100644 adb_client/src/server/models/mod.rs rename adb_client/src/{ => server}/models/server_status.rs (94%) diff --git a/adb_client/src/emulator_device/commands/rotate.rs b/adb_client/src/emulator_device/commands/rotate.rs index 9b3ad82..47a96b5 100644 --- a/adb_client/src/emulator_device/commands/rotate.rs +++ b/adb_client/src/emulator_device/commands/rotate.rs @@ -1,10 +1,8 @@ -use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result}; +use crate::{emulator_device::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(()) + self.connect()?.send_command(ADBEmulatorCommand::Rotate) } } diff --git a/adb_client/src/emulator_device/commands/sms.rs b/adb_client/src/emulator_device/commands/sms.rs index f325123..8c1e45b 100644 --- a/adb_client/src/emulator_device/commands/sms.rs +++ b/adb_client/src/emulator_device/commands/sms.rs @@ -1,13 +1,11 @@ -use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result}; +use crate::{emulator_device::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( + self.connect()?.send_command(ADBEmulatorCommand::Sms( phone_number.to_string(), content.to_string(), - ))?; - Ok(()) + )) } } diff --git a/adb_client/src/emulator_device/mod.rs b/adb_client/src/emulator_device/mod.rs index 41e1888..d3f0450 100644 --- a/adb_client/src/emulator_device/mod.rs +++ b/adb_client/src/emulator_device/mod.rs @@ -1,3 +1,5 @@ mod adb_emulator_device; mod commands; +mod models; pub use adb_emulator_device::ADBEmulatorDevice; +pub(crate) use models::ADBEmulatorCommand; diff --git a/adb_client/src/models/adb_emulator_command.rs b/adb_client/src/emulator_device/models/adb_emulator_command.rs similarity index 96% rename from adb_client/src/models/adb_emulator_command.rs rename to adb_client/src/emulator_device/models/adb_emulator_command.rs index 2f4e76e..55f6e91 100644 --- a/adb_client/src/models/adb_emulator_command.rs +++ b/adb_client/src/emulator_device/models/adb_emulator_command.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -pub(crate) enum ADBEmulatorCommand { +pub enum ADBEmulatorCommand { Authenticate(String), Sms(String, String), Rotate, diff --git a/adb_client/src/emulator_device/models/mod.rs b/adb_client/src/emulator_device/models/mod.rs new file mode 100644 index 0000000..e52c0ba --- /dev/null +++ b/adb_client/src/emulator_device/models/mod.rs @@ -0,0 +1,2 @@ +mod adb_emulator_command; +pub use adb_emulator_command::ADBEmulatorCommand; diff --git a/adb_client/src/lib.rs b/adb_client/src/lib.rs index bb0997f..fa509b8 100644 --- a/adb_client/src/lib.rs +++ b/adb_client/src/lib.rs @@ -21,9 +21,7 @@ pub use device::{ADBTcpDevice, ADBUSBDevice}; pub use emulator_device::ADBEmulatorDevice; pub use error::{Result, RustADBError}; pub use mdns::*; -pub use models::{ - AdbStatResponse, AdbVersion, DeviceLong, DeviceShort, DeviceState, MDNSBackend, RebootType, -}; +pub use models::{AdbStatResponse, RebootType}; pub use server::*; pub use server_device::ADBServerDevice; pub use transports::*; diff --git a/adb_client/src/models/mod.rs b/adb_client/src/models/mod.rs index be0ca74..66f3cef 100644 --- a/adb_client/src/models/mod.rs +++ b/adb_client/src/models/mod.rs @@ -1,30 +1,15 @@ -mod adb_emulator_command; mod adb_request_status; mod adb_server_command; mod adb_stat_response; -mod adb_version; -mod device_long; -mod device_short; -mod device_state; mod framebuffer_info; mod host_features; -mod mdns_services; mod reboot_type; -mod server_status; mod sync_command; -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_long::DeviceLong; -pub use device_short::DeviceShort; -pub use device_state::DeviceState; pub(crate) use framebuffer_info::{FrameBufferInfoV1, FrameBufferInfoV2}; pub use host_features::HostFeatures; -pub use mdns_services::MDNSServices; pub use reboot_type::RebootType; -pub use server_status::MDNSBackend; -pub use server_status::ServerStatus; pub use sync_command::SyncCommand; diff --git a/adb_client/src/server/commands/devices.rs b/adb_client/src/server/commands/devices.rs index a03f816..42a122b 100644 --- a/adb_client/src/server/commands/devices.rs +++ b/adb_client/src/server/commands/devices.rs @@ -1,8 +1,8 @@ use std::io::Read; use crate::{ - models::{AdbServerCommand, DeviceShort}, - ADBEmulatorDevice, ADBServer, ADBServerDevice, DeviceLong, Result, RustADBError, + models::AdbServerCommand, ADBEmulatorDevice, ADBServer, ADBServerDevice, DeviceLong, + DeviceShort, Result, RustADBError, }; impl ADBServer { diff --git a/adb_client/src/server/commands/mdns.rs b/adb_client/src/server/commands/mdns.rs index d8a1d87..b72a151 100644 --- a/adb_client/src/server/commands/mdns.rs +++ b/adb_client/src/server/commands/mdns.rs @@ -1,8 +1,7 @@ use std::io::BufRead; use crate::{ - models::{AdbServerCommand, MDNSBackend, MDNSServices}, - ADBServer, Result, + models::AdbServerCommand, server::models::MDNSBackend, ADBServer, MDNSServices, Result, }; const OPENSCREEN_MDNS_BACKEND: &str = "ADB_MDNS_OPENSCREEN"; diff --git a/adb_client/src/server/commands/server_status.rs b/adb_client/src/server/commands/server_status.rs index 570aade..ae85e5b 100644 --- a/adb_client/src/server/commands/server_status.rs +++ b/adb_client/src/server/commands/server_status.rs @@ -1,7 +1,4 @@ -use crate::{ - models::{AdbServerCommand, ServerStatus}, - ADBServer, Result, -}; +use crate::{models::AdbServerCommand, server::models::ServerStatus, ADBServer, Result}; impl ADBServer { /// Check ADB server status diff --git a/adb_client/src/server/mod.rs b/adb_client/src/server/mod.rs index 848a9f2..d55f131 100644 --- a/adb_client/src/server/mod.rs +++ b/adb_client/src/server/mod.rs @@ -1,4 +1,6 @@ mod adb_server; mod commands; +mod models; pub use adb_server::ADBServer; +pub use models::*; diff --git a/adb_client/src/models/adb_version.rs b/adb_client/src/server/models/adb_version.rs similarity index 100% rename from adb_client/src/models/adb_version.rs rename to adb_client/src/server/models/adb_version.rs diff --git a/adb_client/src/models/device_long.rs b/adb_client/src/server/models/device_long.rs similarity index 100% rename from adb_client/src/models/device_long.rs rename to adb_client/src/server/models/device_long.rs diff --git a/adb_client/src/models/device_short.rs b/adb_client/src/server/models/device_short.rs similarity index 100% rename from adb_client/src/models/device_short.rs rename to adb_client/src/server/models/device_short.rs diff --git a/adb_client/src/models/device_state.rs b/adb_client/src/server/models/device_state.rs similarity index 100% rename from adb_client/src/models/device_state.rs rename to adb_client/src/server/models/device_state.rs diff --git a/adb_client/src/models/mdns_services.rs b/adb_client/src/server/models/mdns_services.rs similarity index 100% rename from adb_client/src/models/mdns_services.rs rename to adb_client/src/server/models/mdns_services.rs diff --git a/adb_client/src/server/models/mod.rs b/adb_client/src/server/models/mod.rs new file mode 100644 index 0000000..1bb8870 --- /dev/null +++ b/adb_client/src/server/models/mod.rs @@ -0,0 +1,13 @@ +mod adb_version; +mod device_long; +mod device_short; +mod device_state; +mod mdns_services; +mod server_status; + +pub use adb_version::AdbVersion; +pub use device_long::DeviceLong; +pub use device_short::DeviceShort; +pub use device_state::DeviceState; +pub use mdns_services::MDNSServices; +pub use server_status::{MDNSBackend, ServerStatus}; diff --git a/adb_client/src/models/server_status.rs b/adb_client/src/server/models/server_status.rs similarity index 94% rename from adb_client/src/models/server_status.rs rename to adb_client/src/server/models/server_status.rs index 8beb48b..a00ff1f 100644 --- a/adb_client/src/models/server_status.rs +++ b/adb_client/src/server/models/server_status.rs @@ -91,14 +91,23 @@ impl Display for MDNSBackend { /// Structure representing current server status #[derive(Debug, Clone, Default, PartialEq)] pub struct ServerStatus { + /// Currently active USB backend pub usb_backend: UsbBackend, + /// Is USB backend forced ? pub usb_backend_forced: bool, + /// Currently active MDNS backend pub mdns_backend: MDNSBackend, + /// Is MDNS backend forced ? pub mdns_backend_forced: bool, + /// Server version pub version: String, + /// Server build information pub build: String, + /// Server executable absolute path pub executable_absolute_path: String, + /// Server logs absolute path pub log_absolute_path: String, + /// OS server is running on pub os: String, } diff --git a/adb_client/src/server_device/adb_server_device_commands.rs b/adb_client/src/server_device/adb_server_device_commands.rs index 27a93aa..e0eed63 100644 --- a/adb_client/src/server_device/adb_server_device_commands.rs +++ b/adb_client/src/server_device/adb_server_device_commands.rs @@ -26,7 +26,6 @@ impl ADBDeviceExt for ADBServerDevice { self.transport .send_adb_request(AdbServerCommand::ShellCommand(command.join(" ")))?; - const BUFFER_SIZE: usize = 4096; loop { let mut buffer = [0; BUFFER_SIZE]; match self.transport.get_raw_connection()?.read(&mut buffer) { diff --git a/adb_client/src/transports/tcp_emulator_transport.rs b/adb_client/src/transports/tcp_emulator_transport.rs index edddb86..f77209f 100644 --- a/adb_client/src/transports/tcp_emulator_transport.rs +++ b/adb_client/src/transports/tcp_emulator_transport.rs @@ -1,13 +1,13 @@ use std::{ fs::File, - io::{BufRead, BufReader, Read, Write}, + io::{BufRead, BufReader, Error, ErrorKind, Read, Write}, net::{SocketAddrV4, TcpStream}, }; use homedir::my_home; use super::ADBTransport; -use crate::{models::ADBEmulatorCommand, Result, RustADBError}; +use crate::{emulator_device::ADBEmulatorCommand, Result, RustADBError}; /// Emulator transport running on top on TCP. #[derive(Debug)] @@ -28,8 +28,8 @@ impl TCPEmulatorTransport { 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, + .ok_or(RustADBError::IOError(Error::new( + ErrorKind::NotConnected, "not connected", ))) }