Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ log = "0.4"
stun = "0.4"
tokio = "1.27"
pretty_env_logger = "0.5"
fast-socks5 = "0.9.1"
async-trait = "0.1.74"
webrtc-util = "0.7.0"

[profile.release]
strip = true
Expand Down
87 changes: 78 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use async_trait::async_trait;
use fast_socks5::client::Socks5Datagram;
use fast_socks5::util::target_addr::TargetAddr;
use log::info;
use stun::addr::*;
use stun::agent::*;
Expand All @@ -7,8 +10,11 @@ use stun::xoraddr::*;
use stun::Error;

use clap::{arg, command, Arg, ArgAction};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::{lookup_host, UdpSocket};
use tokio::net::lookup_host;
use tokio::net::TcpStream;
use tokio::net::UdpSocket;

#[tokio::main]
async fn main() -> Result<(), Error> {
Expand All @@ -28,6 +34,13 @@ async fn main() -> Result<(), Error> {
.help("Do not use IPv6 addresses when connecting to STUN server")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("socks5")
.long("socks5")
.short('s')
.required(false)
.help("Use SOCKS5 udp proxy <Host:Port>"),
)
.get_matches();

let (handler_tx, mut handler_rx) = tokio::sync::mpsc::unbounded_channel();
Expand All @@ -52,16 +65,25 @@ async fn main() -> Result<(), Error> {

info!("Server address is: {}", addr);

let conn = UdpSocket::bind(if addr.is_ipv4() {
"0.0.0.0:0"
} else {
"[::]:0"
})
.await?;
let mut client = if let Some(proxy) = matches.get_one::<String>("socks5") {
let backing_socket = TcpStream::connect(proxy).await?;
let u = fast_socks5::client::Socks5Datagram::bind(backing_socket, "0.0.0.0:0")
.await
.map_err(|e| Error::Other(e.to_string()))?;

conn.connect(addr).await?;
let conn = Arc::new(Socks5Udp(u, TargetAddr::Ip(addr)));
ClientBuilder::new().with_conn(conn).build()?
} else {
let conn = UdpSocket::bind(if addr.is_ipv4() {
"0.0.0.0:0"
} else {
"[::]:0"
})
.await?;

let mut client = ClientBuilder::new().with_conn(Arc::new(conn)).build()?;
conn.connect(addr).await?;
ClientBuilder::new().with_conn(Arc::new(conn)).build()?
};

let mut msg = Message::new();
msg.build(&[Box::<TransactionId>::default(), Box::new(BINDING_REQUEST)])?;
Expand All @@ -86,3 +108,50 @@ async fn main() -> Result<(), Error> {

Ok(())
}

struct Socks5Udp(Socks5Datagram<TcpStream>, TargetAddr);
use webrtc_util::Result as WResult;
#[async_trait]
impl webrtc_util::Conn for Socks5Udp {
async fn connect(&self, _addr: SocketAddr) -> WResult<()> {
unimplemented!()
}
async fn recv(&self, buf: &mut [u8]) -> WResult<usize> {
let r = self.0.recv_from(buf).await;
match r {
Ok((n, _)) => WResult::Ok(n),
Err(e) => Err(webrtc_util::Error::Other(e.to_string())),
}
}
async fn recv_from(&self, _buf: &mut [u8]) -> WResult<(usize, SocketAddr)> {
unimplemented!()
}
async fn send(&self, buf: &[u8]) -> WResult<usize> {
let r = match self.1 {
TargetAddr::Ip(addr) => self.0.send_to(buf, addr).await,
TargetAddr::Domain(ref domain, port) => {
self.0.send_to(buf, (domain.as_ref(), port)).await
}
};
match r {
Ok(n) => WResult::Ok(n),
Err(e) => Err(webrtc_util::Error::Other(e.to_string())),
}
}
async fn send_to(&self, buf: &[u8], target: SocketAddr) -> WResult<usize> {
let r = self.0.send_to(buf, target).await;
match r {
Ok(n) => WResult::Ok(n),
Err(e) => Err(webrtc_util::Error::Other(e.to_string())),
}
}
async fn close(&self) -> WResult<()> {
Ok(())
}
fn local_addr(&self) -> WResult<SocketAddr> {
unimplemented!()
}
fn remote_addr(&self) -> Option<SocketAddr> {
unimplemented!()
}
}