From 1805c60e32cb4a8b87661a436c019e9600eff45f Mon Sep 17 00:00:00 2001 From: LIAUD Corentin Date: Wed, 17 Jul 2024 20:42:03 +0200 Subject: [PATCH] feat: add disconnect + rework with new archi --- examples/adb_cli.rs | 44 ++++++++++-------------- src/models/adb_command.rs | 16 +++++---- src/models/device_long.rs | 8 ++--- src/server/adb_server.rs | 5 ++- src/server/server_commands/connect.rs | 12 ++++--- src/server/server_commands/disconnect.rs | 16 +++++++++ src/server/server_commands/mod.rs | 1 + src/server/server_commands/pair.rs | 12 ++++--- tests/tests.rs | 9 ----- 9 files changed, 64 insertions(+), 59 deletions(-) create mode 100644 src/server/server_commands/disconnect.rs diff --git a/examples/adb_cli.rs b/examples/adb_cli.rs index 006c268..77ffb38 100644 --- a/examples/adb_cli.rs +++ b/examples/adb_cli.rs @@ -3,18 +3,14 @@ use anyhow::Result; use clap::Parser; use std::fs::File; use std::io::{self, Write}; -use std::net::Ipv4Addr; +use std::net::SocketAddrV4; use std::path::Path; #[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, + #[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, @@ -64,14 +60,12 @@ pub enum HostCommand { }, /// Track new devices showing up. TrackDevices, - /// Pair new device on a specific port with a given code - Pair { - address: Ipv4Addr, - port: u16, - code: u32, - }, + /// Pair device with a given code + Pair { address: SocketAddrV4, code: u32 }, /// Connect device over WI-FI - Connect { address: Ipv4Addr, port: u16 }, + Connect { address: SocketAddrV4 }, + /// Disconnect device over WI-FI + Disconnect { address: SocketAddrV4 }, } #[derive(Parser, Debug)] @@ -98,7 +92,7 @@ impl From for RebootType { fn main() -> Result<()> { let opt = Args::parse(); - let mut adb_server = ADBServer::new(opt.address, opt.port); + let mut adb_server = ADBServer::new(opt.address); match opt.command { Command::LocalCommand(local) => { @@ -171,17 +165,17 @@ fn main() -> Result<()> { println!("Live list of devices attached"); adb_server.track_devices(callback)?; } - HostCommand::Pair { - address, - port, - code, - } => { - println!("Pairing device"); - adb_server.pair(address, port, code)? + HostCommand::Pair { address, code } => { + adb_server.pair(address, code)?; + println!("paired device {address}"); } - HostCommand::Connect { address, port } => { - println!("Connect device"); - adb_server.connect_device(address, port)? + HostCommand::Connect { address } => { + adb_server.connect_device(address)?; + println!("connected to {address}"); + } + HostCommand::Disconnect { address } => { + adb_server.disconnect_device(address)?; + println!("disconnected {address}"); } }, } diff --git a/src/models/adb_command.rs b/src/models/adb_command.rs index 05dfc26..8a71213 100644 --- a/src/models/adb_command.rs +++ b/src/models/adb_command.rs @@ -1,7 +1,7 @@ use std::fmt::Display; -use std::net::Ipv4Addr; use super::RebootType; +use std::net::SocketAddrV4; pub(crate) enum AdbCommand { Version, @@ -10,8 +10,9 @@ pub(crate) enum AdbCommand { DevicesLong, TrackDevices, HostFeatures, - Connect(Ipv4Addr, u16), - Pair(Ipv4Addr, u16, u32), + Connect(SocketAddrV4), + Disconnect(SocketAddrV4), + Pair(SocketAddrV4, u32), // TODO: NOT IMPLEMENTED YET // Emulator(u16), // Transport(String), @@ -72,9 +73,12 @@ impl Display for AdbCommand { AdbCommand::HostFeatures => write!(f, "host:features"), AdbCommand::Reboot(reboot_type) => { write!(f, "reboot:{reboot_type}") - }, - AdbCommand::Connect(addr, port) => write!(f, "host:connect:{addr}:{port}"), - AdbCommand::Pair(addr, port, code) => write!(f, "host:pair:{code}:{addr}:{port}") + } + AdbCommand::Connect(addr) => write!(f, "host:connect:{}", addr), + AdbCommand::Disconnect(addr) => write!(f, "host:disconnect:{}", addr), + AdbCommand::Pair(addr, code) => { + write!(f, "host:pair:{code}:{}", addr) + } } } } diff --git a/src/models/device_long.rs b/src/models/device_long.rs index e90dc44..a222c98 100644 --- a/src/models/device_long.rs +++ b/src/models/device_long.rs @@ -3,11 +3,11 @@ use std::str::FromStr; use std::{fmt::Display, str}; use crate::{DeviceState, RustADBError}; -use lazy_static::lazy_static; use regex::bytes::Regex; lazy_static! { - static ref DEVICES_LONG_REGEX: Regex = Regex::new("^(?P\\S+)\\s+(?P\\w+) (usb:(?P.*) )?(product:(?P\\w+) model:(?P\\w+) device:(?P\\w+) )?transport_id:(?P\\d+)$").unwrap(); + static ref DEVICES_LONG_REGEX: Regex = Regex::new("^(?P\\S+)\\s+(?P\\w+) ((usb:(?P.*)|(?P\\d-\\d)) )?(product:(?P\\w+) model:(?P\\w+) device:(?P\\w+) )?transport_id:(?P\\d+)$").expect("cannot build devices long regex"); + } /// Represents a new device with more informations helded. @@ -45,10 +45,6 @@ impl Display for DeviceLong { } } -lazy_static! { - static ref DEVICES_LONG_REGEX: Regex = Regex::new("^(?P\\S+)\\s+(?P\\w+) ((usb:(?P.*)|(?P\\d-\\d)) )?(product:(?P\\w+) model:(?P\\w+) device:(?P\\w+) )?transport_id:(?P\\d+)$").expect("cannot build devices long regex"); -} - impl TryFrom> for DeviceLong { type Error = RustADBError; diff --git a/src/server/adb_server.rs b/src/server/adb_server.rs index 90ec471..a779c79 100644 --- a/src/server/adb_server.rs +++ b/src/server/adb_server.rs @@ -2,7 +2,6 @@ use crate::Result; use crate::RustADBError; use crate::TCPServerProtocol; use crate::Transport; -use std::net::Ipv4Addr; use std::net::SocketAddrV4; /// Represents an ADB Server @@ -16,10 +15,10 @@ pub struct ADBServer { impl ADBServer { /// Instantiates a new [ADBServer] - pub fn new(ip: Ipv4Addr, port: u16) -> Self { + pub fn new(address: SocketAddrV4) -> Self { Self { transport: None, - socket_addr: Some(SocketAddrV4::new(ip, port)), + socket_addr: Some(address), } } diff --git a/src/server/server_commands/connect.rs b/src/server/server_commands/connect.rs index 9cb36e6..bfaca38 100644 --- a/src/server/server_commands/connect.rs +++ b/src/server/server_commands/connect.rs @@ -1,14 +1,16 @@ -use crate::{models::AdbCommand, ADBServer, Result, RustADBError}; -use std::net::Ipv4Addr; +use crate::{models::AdbCommand, ADBServer, Result, RustADBError}; +use std::net::SocketAddrV4; impl ADBServer { /// Connect device over tcp with address and port - pub fn connect_device(&mut self, address: Ipv4Addr, port: u16) -> Result<()> { - let response = self.connect()?.proxy_connection(AdbCommand::Connect(address, port), true)?; + pub fn connect_device(&mut self, address: SocketAddrV4) -> Result<()> { + let response = self + .connect()? + .proxy_connection(AdbCommand::Connect(address), true)?; match String::from_utf8(response).unwrap() { s if s.starts_with("connected to") => Ok(()), - s => Err(RustADBError::ADBRequestFailed(s)) + s => Err(RustADBError::ADBRequestFailed(s)), } } } diff --git a/src/server/server_commands/disconnect.rs b/src/server/server_commands/disconnect.rs new file mode 100644 index 0000000..c5948d5 --- /dev/null +++ b/src/server/server_commands/disconnect.rs @@ -0,0 +1,16 @@ +use crate::{models::AdbCommand, 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(AdbCommand::Disconnect(address), true)?; + + match String::from_utf8(response).unwrap() { + s if s.starts_with("disconnected") => Ok(()), + s => Err(RustADBError::ADBRequestFailed(s)), + } + } +} diff --git a/src/server/server_commands/mod.rs b/src/server/server_commands/mod.rs index f67f702..64310a3 100644 --- a/src/server/server_commands/mod.rs +++ b/src/server/server_commands/mod.rs @@ -1,5 +1,6 @@ mod connect; mod devices; +mod disconnect; mod kill; mod pair; mod version; diff --git a/src/server/server_commands/pair.rs b/src/server/server_commands/pair.rs index c5847e7..9a26492 100644 --- a/src/server/server_commands/pair.rs +++ b/src/server/server_commands/pair.rs @@ -1,15 +1,17 @@ -use std::net::Ipv4Addr; -use crate::{ADBServer, Result, RustADBError}; use crate::models::AdbCommand; +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: Ipv4Addr, port: u16, code: u32) -> Result<()> { - let response = self.connect()?.proxy_connection(AdbCommand::Pair(address, port, code), true)?; + pub fn pair(&mut self, address: SocketAddrV4, code: u32) -> Result<()> { + let response = self + .connect()? + .proxy_connection(AdbCommand::Pair(address, code), true)?; match String::from_utf8(response).unwrap() { s if s.starts_with("Successfully paired to") => Ok(()), - s => Err(RustADBError::ADBRequestFailed(s)) + s => Err(RustADBError::ADBRequestFailed(s)), } } } diff --git a/tests/tests.rs b/tests/tests.rs index 47c7ed5..e2f3dda 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,8 +1,6 @@ #[cfg(test)] mod tests { use std::io::Cursor; - use std::net::Ipv4Addr; - use std::str::FromStr; use adb_client::{ADBServer, ADBServerDevice, DeviceLong}; use rand::Rng; @@ -51,13 +49,6 @@ mod tests { } } - #[test] - #[should_panic] - fn test_wrong_addr() { - let address = Ipv4Addr::from_str("127.0.0.300").unwrap(); - let _ = ADBServer::new(address, 5037); - } - #[test] fn test_send_recv() { // Create random "Reader" in memory