From 97c4aa5137d0668c15dbbcf3909ec4d8304e6d9b Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Mon, 13 Nov 2023 20:30:24 +0800 Subject: [PATCH] rustfmt max_width = 140 --- rustfmt.toml | 2 +- src/http.rs | 10 +++------- src/lib.rs | 6 +----- src/setup.rs | 31 +++++-------------------------- src/socks.rs | 14 ++++---------- src/tun2proxy.rs | 38 +++++++++++++------------------------- src/wintuninterface.rs | 26 ++++++++------------------ 7 files changed, 35 insertions(+), 92 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index 7530651..8449be0 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1 @@ -max_width = 120 +max_width = 140 diff --git a/src/http.rs b/src/http.rs index 52b1b1d..81b05c5 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,8 +1,8 @@ use crate::{ error::Error, tun2proxy::{ - ConnectionInfo, ConnectionManager, Direction, IncomingDataEvent, IncomingDirection, OutgoingDataEvent, - OutgoingDirection, ProxyHandler, + ConnectionInfo, ConnectionManager, Direction, IncomingDataEvent, IncomingDirection, OutgoingDataEvent, OutgoingDirection, + ProxyHandler, }, }; use base64::Engine; @@ -61,11 +61,7 @@ static TRANSFER_ENCODING: &str = "Transfer-Encoding"; static CONTENT_LENGTH: &str = "Content-Length"; impl HttpConnection { - fn new( - info: &ConnectionInfo, - credentials: Option, - digest_state: Rc>>, - ) -> Result { + fn new(info: &ConnectionInfo, credentials: Option, digest_state: Rc>>) -> Result { let mut res = Self { state: HttpState::ExpectResponseHeaders, client_inbuf: VecDeque::default(), diff --git a/src/lib.rs b/src/lib.rs index 285b5ed..ebbc32e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,11 +150,7 @@ impl Options { } } -pub fn tun_to_proxy<'a>( - interface: &NetworkInterface, - proxy: &Proxy, - options: Options, -) -> Result, Error> { +pub fn tun_to_proxy<'a>(interface: &NetworkInterface, proxy: &Proxy, options: Options) -> Result, Error> { let mut ttp = TunToProxy::new(interface, options)?; let credentials = proxy.credentials.clone(); let server = proxy.addr; diff --git a/src/setup.rs b/src/setup.rs index 228653b..c3c69f1 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -62,12 +62,7 @@ where let command = cmdline.as_slice().join(" "); match String::from_utf8(output.stderr.clone()) { Ok(output) => Err(format!("[{}] Command `{}` failed: {}", nix::unistd::getpid(), command, output).into()), - Err(_) => Err(format!( - "Command `{:?}` failed with exit code {}", - command, - output.status.code().unwrap() - ) - .into()), + Err(_) => Err(format!("Command `{:?}` failed with exit code {}", command, output.status.code().unwrap()).into()), } } } @@ -103,11 +98,7 @@ impl Setup { ["ip", "-4", "route", "show"] }; - let routes = run_iproute( - route_show_args, - "failed to get routing table through the ip command", - true, - )?; + let routes = run_iproute(route_show_args, "failed to get routing table through the ip command", true)?; let mut route_info = Vec::<(IpCidr, Vec)>::new(); for line in routes.stdout.lines() { @@ -217,14 +208,7 @@ impl Setup { fn add_tunnel_routes(&self) -> Result<(), Error> { for route in &self.routes { run_iproute( - [ - "ip", - "route", - "add", - route.to_string().as_str(), - "dev", - self.tun.as_str(), - ], + ["ip", "route", "add", route.to_string().as_str(), "dev", self.tun.as_str()], "failed to add route", true, )?; @@ -238,9 +222,7 @@ impl Setup { let _ = Command::new("ip").args(["link", "del", self.tun.as_str()]).output(); for cidr in &self.delete_proxy_routes { - let _ = Command::new("ip") - .args(["route", "del", cidr.to_string().as_str()]) - .output(); + let _ = Command::new("ip").args(["route", "del", cidr.to_string().as_str()]).output(); } if self.unmount_resolvconf { @@ -297,10 +279,7 @@ impl Setup { loop { let res = fd.read_signal().unwrap().unwrap(); let signo = nix::sys::signal::Signal::try_from(res.ssi_signo as i32).unwrap(); - if signo == nix::sys::signal::SIGINT - || signo == nix::sys::signal::SIGTERM - || signo == nix::sys::signal::SIGQUIT - { + if signo == nix::sys::signal::SIGINT || signo == nix::sys::signal::SIGTERM || signo == nix::sys::signal::SIGQUIT { break; } } diff --git a/src/socks.rs b/src/socks.rs index 4ec8570..666eb11 100644 --- a/src/socks.rs +++ b/src/socks.rs @@ -1,8 +1,8 @@ use crate::{ error::{Error, Result}, tun2proxy::{ - ConnectionInfo, ConnectionManager, Direction, IncomingDataEvent, IncomingDirection, OutgoingDataEvent, - OutgoingDirection, ProxyHandler, + ConnectionInfo, ConnectionManager, Direction, IncomingDataEvent, IncomingDirection, OutgoingDataEvent, OutgoingDirection, + ProxyHandler, }, }; use socks5_impl::protocol::{self, handshake, password_method, Address, AuthMethod, StreamOperation, UserKey, Version}; @@ -34,12 +34,7 @@ struct SocksProxyImpl { } impl SocksProxyImpl { - fn new( - info: &ConnectionInfo, - credentials: Option, - version: Version, - command: protocol::Command, - ) -> Result { + fn new(info: &ConnectionInfo, credentials: Option, version: Version, command: protocol::Command) -> Result { let mut result = Self { info: info.clone(), state: SocksState::ServerHello, @@ -58,8 +53,7 @@ impl SocksProxyImpl { fn send_client_hello_socks4(&mut self) -> Result<(), Error> { let credentials = &self.credentials; - self.server_outbuf - .extend(&[self.version as u8, protocol::Command::Connect.into()]); + self.server_outbuf.extend(&[self.version as u8, protocol::Command::Connect.into()]); self.server_outbuf.extend(self.info.dst.port().to_be_bytes()); let mut ip_vec = Vec::::new(); let mut name_vec = Vec::::new(); diff --git a/src/tun2proxy.rs b/src/tun2proxy.rs index 121f298..fbad1bf 100644 --- a/src/tun2proxy.rs +++ b/src/tun2proxy.rs @@ -271,8 +271,7 @@ impl<'a> TunToProxy<'a> { let interests = Interest::READABLE | Interest::WRITABLE; #[cfg(target_family = "unix")] - poll.registry() - .register(&mut SourceFd(&tun.as_raw_fd()), TUN_TOKEN, interests)?; + poll.registry().register(&mut SourceFd(&tun.as_raw_fd()), TUN_TOKEN, interests)?; #[cfg(target_os = "windows")] { @@ -288,8 +287,7 @@ impl<'a> TunToProxy<'a> { poll.registry() .register(&mut exit_trigger, EXIT_TRIGGER_TOKEN, Interest::WRITABLE)?; - poll.registry() - .register(&mut exit_receiver, EXIT_TOKEN, Interest::READABLE)?; + poll.registry().register(&mut exit_receiver, EXIT_TOKEN, Interest::READABLE)?; let config = match tun.capabilities().medium { Medium::Ethernet => Config::new(smoltcp::wire::EthernetAddress([0x02, 0, 0, 0, 0, 0x01]).into()), @@ -585,15 +583,10 @@ impl<'a> TunToProxy<'a> { state.dns_over_tcp_expiry = Some(Self::common_udp_life_timeout()); let mut vecbuf = vec![]; - Self::read_data_from_tcp_stream( - &mut state.mio_stream, - IP_PACKAGE_MAX_SIZE, - &mut state.is_tcp_closed, - |data| { - vecbuf.extend_from_slice(data); - Ok(()) - }, - )?; + Self::read_data_from_tcp_stream(&mut state.mio_stream, IP_PACKAGE_MAX_SIZE, &mut state.is_tcp_closed, |data| { + vecbuf.extend_from_slice(data); + Ok(()) + })?; let data_event = IncomingDataEvent { direction: IncomingDirection::FromServer, @@ -1090,18 +1083,13 @@ impl<'a> TunToProxy<'a> { let mut vecbuf = vec![]; use std::io::{Error, ErrorKind}; - let r = Self::read_data_from_tcp_stream( - &mut state.mio_stream, - IP_PACKAGE_MAX_SIZE, - &mut state.is_tcp_closed, - |data| { - vecbuf.extend_from_slice(data); - if vecbuf.len() >= IP_PACKAGE_MAX_SIZE { - return Err(Error::new(ErrorKind::OutOfMemory, "IP_PACKAGE_MAX_SIZE exceeded")); - } - Ok(()) - }, - ); + let r = Self::read_data_from_tcp_stream(&mut state.mio_stream, IP_PACKAGE_MAX_SIZE, &mut state.is_tcp_closed, |data| { + vecbuf.extend_from_slice(data); + if vecbuf.len() >= IP_PACKAGE_MAX_SIZE { + return Err(Error::new(ErrorKind::OutOfMemory, "IP_PACKAGE_MAX_SIZE exceeded")); + } + Ok(()) + }); let len = vecbuf.len(); if let Err(error) = r { if error.kind() == ErrorKind::OutOfMemory { diff --git a/src/wintuninterface.rs b/src/wintuninterface.rs index 9706043..fe9abbe 100644 --- a/src/wintuninterface.rs +++ b/src/wintuninterface.rs @@ -22,8 +22,8 @@ use windows::{ NetworkManagement::{ IpHelper::{ GetAdaptersAddresses, SetInterfaceDnsSettings, DNS_INTERFACE_SETTINGS, DNS_INTERFACE_SETTINGS_VERSION1, - DNS_SETTING_NAMESERVER, GAA_FLAG_INCLUDE_GATEWAYS, GAA_FLAG_INCLUDE_PREFIX, IF_TYPE_ETHERNET_CSMACD, - IF_TYPE_IEEE80211, IP_ADAPTER_ADDRESSES_LH, + DNS_SETTING_NAMESERVER, GAA_FLAG_INCLUDE_GATEWAYS, GAA_FLAG_INCLUDE_PREFIX, IF_TYPE_ETHERNET_CSMACD, IF_TYPE_IEEE80211, + IP_ADAPTER_ADDRESSES_LH, }, Ndis::IfOperStatusUp, }, @@ -88,8 +88,9 @@ impl WinTunInterface { let guid = 324435345345345345_u128; let adapter = match wintun::Adapter::open(&wintun, tun_name) { Ok(a) => a, - Err(_) => wintun::Adapter::create(&wintun, tun_name, tun_name, Some(guid)) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?, + Err(_) => { + wintun::Adapter::create(&wintun, tun_name, tun_name, Some(guid)).map_err(|e| io::Error::new(io::ErrorKind::Other, e))? + } }; let session = adapter @@ -376,12 +377,7 @@ impl phy::TxToken for TxToken { let mut buffer = vec![0; len]; let result = f(&mut buffer); - let buffer = self - .pipe_server_cache - .borrow_mut() - .drain(..) - .chain(buffer) - .collect::>(); + let buffer = self.pipe_server_cache.borrow_mut().drain(..).chain(buffer).collect::>(); if buffer.is_empty() { // log::trace!("Wintun TxToken (pipe_server) is empty"); return result; @@ -433,11 +429,7 @@ impl event::Source for NamedPipeSource { pub(crate) fn run_command(command: &str, args: &[&str]) -> io::Result<()> { let out = std::process::Command::new(command).args(args).output()?; if !out.status.success() { - let err = String::from_utf8_lossy(if out.stderr.is_empty() { - &out.stdout - } else { - &out.stderr - }); + let err = String::from_utf8_lossy(if out.stderr.is_empty() { &out.stdout } else { &out.stderr }); let info = format!("{} failed with: \"{}\"", command, err); return Err(std::io::Error::new(std::io::ErrorKind::Other, info)); } @@ -463,9 +455,7 @@ pub(crate) fn set_interface_dns_settings(interface: GUID, dns: &[IpAddr]) -> io: pub(crate) fn get_active_network_interface_gateways() -> io::Result> { let mut addrs = vec![]; get_adapters_addresses(|adapter| { - if adapter.OperStatus == IfOperStatusUp - && [IF_TYPE_ETHERNET_CSMACD, IF_TYPE_IEEE80211].contains(&adapter.IfType) - { + if adapter.OperStatus == IfOperStatusUp && [IF_TYPE_ETHERNET_CSMACD, IF_TYPE_IEEE80211].contains(&adapter.IfType) { let mut current_gateway = adapter.FirstGatewayAddress; while !current_gateway.is_null() { let gateway = unsafe { &*current_gateway };