From bf2f6f4c26903234becfaaab8f89ffc013808916 Mon Sep 17 00:00:00 2001 From: Erdem MEYDANLI Date: Thu, 11 Apr 2024 21:12:06 +0200 Subject: [PATCH] Revert "vsock_proxy: refactor" This reverts commit e8670a52baab5ea2f2e18ffd03d29959f445c2cc. --- vsock_proxy/src/lib.rs | 10 ---------- vsock_proxy/src/main.rs | 15 +++++---------- vsock_proxy/src/starter.rs | 22 +++++++++++----------- vsock_proxy/tests/connection_test.rs | 5 +++-- 4 files changed, 19 insertions(+), 33 deletions(-) diff --git a/vsock_proxy/src/lib.rs b/vsock_proxy/src/lib.rs index e7f5c498..6e372236 100644 --- a/vsock_proxy/src/lib.rs +++ b/vsock_proxy/src/lib.rs @@ -2,13 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 pub mod starter; - -#[derive(Copy, Clone, PartialEq)] -pub enum IpAddrType { - /// Only allows IP4 addresses - IPAddrV4Only, - /// Only allows IP6 addresses - IPAddrV6Only, - /// Allows both IP4 and IP6 addresses - IPAddrMixed -} \ No newline at end of file diff --git a/vsock_proxy/src/main.rs b/vsock_proxy/src/main.rs index 37dd71d7..b66a68e3 100644 --- a/vsock_proxy/src/main.rs +++ b/vsock_proxy/src/main.rs @@ -10,7 +10,7 @@ use clap::{App, AppSettings, Arg}; use env_logger::init; use log::info; -use vsock_proxy::{starter::{Proxy, VsockProxyResult}, IpAddrType}; +use vsock_proxy::starter::{Proxy, VsockProxyResult}; fn main() -> VsockProxyResult<()> { init(); @@ -75,14 +75,8 @@ fn main() -> VsockProxyResult<()> { .parse::() .map_err(|_| "Local port is not valid")?; - let ipv4_only = matches.is_present("ipv4"); - let ipv6_only = matches.is_present("ipv6"); - let ip_addr_type : IpAddrType = match (ipv4_only, ipv6_only) { - (true, false) => IpAddrType::IPAddrV4Only, - (false, true) => IpAddrType::IPAddrV6Only, - _ => IpAddrType::IPAddrMixed, - }; - + let only_4 = matches.is_present("ipv4"); + let only_6 = matches.is_present("ipv6"); let remote_addr = matches .value_of("remote_addr") // This argument is required, so clap ensures it's available @@ -112,7 +106,8 @@ fn main() -> VsockProxyResult<()> { remote_port, num_workers, config_file, - ip_addr_type + only_4, + only_6, ) .map_err(|err| format!("Could not create proxy: {}", err))?; diff --git a/vsock_proxy/src/starter.rs b/vsock_proxy/src/starter.rs index 94544358..ed78c326 100644 --- a/vsock_proxy/src/starter.rs +++ b/vsock_proxy/src/starter.rs @@ -18,8 +18,6 @@ use threadpool::ThreadPool; use vsock::{VsockAddr, VsockListener}; use yaml_rust::YamlLoader; -use crate::IpAddrType; - const BUFF_SIZE: usize = 8192; pub const VSOCK_PROXY_CID: u32 = 3; pub const VSOCK_PROXY_PORT: u32 = 8000; @@ -32,7 +30,8 @@ pub fn check_allowlist( remote_host: &str, remote_port: u16, config_file: Option<&str>, - ip_addr_type: IpAddrType, + only_4: bool, + only_6: bool, ) -> VsockProxyResult { if let Some(config_file) = config_file { let mut f = File::open(config_file).map_err(|_| "Could not open the file")?; @@ -47,7 +46,7 @@ pub fn check_allowlist( .ok_or("No allowlist field")?; // Obtain the remote server's IP address. - let mut addrs = Proxy::parse_addr(remote_host, ip_addr_type) + let mut addrs = Proxy::parse_addr(remote_host, only_4, only_6) .map_err(|err| format!("Could not parse remote address: {}", err))?; let remote_addr = *addrs.first().ok_or("No IP address found")?; @@ -70,7 +69,7 @@ pub fn check_allowlist( } // If hostname matching failed, attempt to match against IPs. - addrs = Proxy::parse_addr(addr, ip_addr_type)?; + addrs = Proxy::parse_addr(addr, only_4, only_6)?; for addr in addrs.into_iter() { if addr == remote_addr { info!("Matched with host IP \"{}\" and port \"{}\"", addr, port); @@ -98,13 +97,14 @@ impl Proxy { remote_port: u16, num_workers: usize, config_file: Option<&str>, - ip_addr_type: IpAddrType + only_4: bool, + only_6: bool, ) -> VsockProxyResult { if num_workers == 0 { return Err("Number of workers must not be 0".to_string()); } info!("Checking allowlist configuration"); - let remote_addr = check_allowlist(remote_host, remote_port, config_file, ip_addr_type) + let remote_addr = check_allowlist(remote_host, remote_port, config_file, only_4, only_6) .map_err(|err| format!("Error at checking the allowlist: {}", err))?; let pool = ThreadPool::new(num_workers); let sock_type = SockType::Stream; @@ -123,7 +123,7 @@ impl Proxy { } /// Resolve a DNS name (IDNA format) into an IP address (v4 or v6) - pub fn parse_addr(addr: &str, ip_addr_type: IpAddrType) -> VsockProxyResult> { + pub fn parse_addr(addr: &str, only_4: bool, only_6: bool) -> VsockProxyResult> { // IDNA parsing let addr = domain_to_ascii(addr).map_err(|_| "Could not parse domain name")?; @@ -142,16 +142,16 @@ impl Proxy { }; // If there is no restriction, choose randomly - if IpAddrType::IPAddrMixed == ip_addr_type { + if !only_4 && !only_6 { return Ok(ips.into_iter().collect()); } // Split the IPs in v4 and v6 let (ips_v4, ips_v6): (Vec<_>, Vec<_>) = ips.into_iter().partition(IpAddr::is_ipv4); - if IpAddrType::IPAddrV4Only == ip_addr_type && !ips_v4.is_empty() { + if only_4 && !ips_v4.is_empty() { Ok(ips_v4.into_iter().collect()) - } else if IpAddrType::IPAddrV6Only == ip_addr_type && !ips_v6.is_empty() { + } else if only_6 && !ips_v6.is_empty() { Ok(ips_v6.into_iter().collect()) } else { Err("No accepted IP was found".to_string()) diff --git a/vsock_proxy/tests/connection_test.rs b/vsock_proxy/tests/connection_test.rs index 39577de2..000a3ca3 100644 --- a/vsock_proxy/tests/connection_test.rs +++ b/vsock_proxy/tests/connection_test.rs @@ -11,7 +11,7 @@ use std::thread; use tempfile::NamedTempFile; use vsock::{VsockAddr, VsockStream}; -use vsock_proxy::{starter::Proxy, IpAddrType}; +use vsock_proxy::starter::Proxy; fn vsock_connect(port: u32) -> VsockStream { let sockaddr = VsockAddr::new(vsock_proxy::starter::VSOCK_PROXY_CID, port); @@ -35,7 +35,8 @@ fn test_tcp_connection() { 9000, 2, file.path().to_str(), - IpAddrType::IPAddrMixed, + false, + false, ) .unwrap();