Skip to content

Commit

Permalink
Now compiles on windows
Browse files Browse the repository at this point in the history
The client actually works, but the redirector doesn't. However, we won't
need that with the current implementation of the EPI Framework.
  • Loading branch information
Lut99 committed Apr 5, 2023
1 parent b7241cd commit cfcf9ce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
7 changes: 6 additions & 1 deletion socksx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ human-panic = "1"
itertools = "0.10"
libc = "0.2"
log = "0.4"
nix = "0.21"
num-derive = "0.3"
num-traits = "0.2"
thiserror = "1"
tokio = { version = "1", features = ["full"] }
url = "2.2"

[target.'cfg(unix)'.dependencies]
nix = "0.21"

[target.'cfg(windows)'.dependencies]
windows = { version = "0.48", features = ["Win32_Networking_WinSock"] }

[dev-dependencies]
chacha20 = "0.7"
pin-project-lite = "0.2"
3 changes: 3 additions & 0 deletions socksx/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ async fn connect_v5(
proxy_addr: String,
dest_addr: String,
) -> Result<()> {
println!("Creating client...");
let client = Socks5Client::new(proxy_addr, None).await?;
println!("Connecting...");
let (mut outgoing, _) = client.connect(dest_addr).await?;

println!("Writing message!");
outgoing.write(String::from("Hello, world!\n").as_bytes()).await?;

Ok(())
Expand Down
43 changes: 34 additions & 9 deletions socksx/src/common/util.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
use anyhow::Result;
use std::{net::SocketAddr, os};
use std::net::SocketAddr;
use tokio::net::{self, TcpStream};

///
///
///
#[cfg(any(target_os = "linux"))]
pub fn get_original_dst<S: os::unix::io::AsRawFd>(socket: &S) -> Result<SocketAddr> {
#[cfg(target_os = "linux")]
pub fn get_original_dst<S: std::os::unix::io::AsRawFd>(socket: &S) -> Result<SocketAddr> {
use nix::sys::socket::{self, sockopt, InetAddr};

let orignal_dst = socket::getsockopt(socket.as_raw_fd(), sockopt::OriginalDst)?;
let orignal_dst = InetAddr::V4(orignal_dst).to_std();
let original_dst = socket::getsockopt(socket.as_raw_fd(), sockopt::OriginalDst)?;
let original_dst = InetAddr::V4(original_dst).to_std();

Ok(orignal_dst)
println!("{original_dst}");
Ok(original_dst)
}

#[cfg(not(any(target_os = "linux")))]
pub fn get_original_dst<S: os::unix::io::AsRawFd>(_socket: &S) -> Result<SocketAddr> {
todo!()
#[cfg(target_os = "windows")]
pub fn get_original_dst<S: std::os::windows::io::AsRawSocket>(socket: &S) -> Result<SocketAddr> {
use std::str::FromStr;
use windows::core::PSTR;
use windows::Win32::Networking::WinSock::{SO_ORIGINAL_DST, SOCKET, SOL_SOCKET, getsockopt};

// Attempt to recover the original destination
let original_dst: String = unsafe {
// Write the socket option to a buffer
let mut original_dst : [u8; 256] = [0; 256];
let mut n_bytes : i32 = 256;
if getsockopt(SOCKET(socket.as_raw_socket() as usize), SOL_SOCKET, SO_ORIGINAL_DST as i32, PSTR((&mut original_dst) as *mut u8), &mut n_bytes) != 0 {
panic!("Failed to get original address from socket");
}

// Parse it as an address
String::from_utf8_lossy(&original_dst[..n_bytes as usize]).into()
};

// Now return the parsed socket address
println!("{original_dst}");
Ok(SocketAddr::from_str(&original_dst)?)
}

#[cfg(not(any(target_os = "linux", target_os = "windows")))]
pub fn get_original_dst<S>(socket: S) -> Result<SocketAddr> {
todo!();
}

///
Expand Down

0 comments on commit cfcf9ce

Please sign in to comment.