Fix portforwarding

This commit is contained in:
Dennis Schwerdel 2021-05-11 23:51:36 +02:00
parent a03d367293
commit a6b9bad583
3 changed files with 19 additions and 24 deletions

View File

@ -59,7 +59,7 @@ use crate::{
crypto::Crypto,
device::{AsyncTunTapDevice, TunTapDevice, Type},
engine::common::GenericCloud,
net::{AsyncNetSocket, listen_udp},
net::{AsyncNetSocket, NetSocket},
oldconfig::OldConfigFile,
payload::Protocol,
util::SystemTimeSource,
@ -176,10 +176,9 @@ fn setup_device(config: &Config) -> TunTapDevice {
}
#[allow(clippy::cognitive_complexity)]
fn run<P: Protocol>(config: Config, socket: UdpSocket) {
fn run<P: Protocol>(config: Config, socket: NetSocket) {
let device = setup_device(&config);
let port_forwarding = None;
//if config.port_forwarding { rt.block_on(socket.create_port_forwarding()) } else { None };
let port_forwarding = if config.port_forwarding { socket.create_port_forwarding() } else { None };
let stats_file = match config.stats_file {
None => None,
Some(ref name) => {
@ -227,7 +226,7 @@ fn run<P: Protocol>(config: Config, socket: UdpSocket) {
rt.block_on(async move {
// Warning: no async code outside this block, or it will break on daemonize
let device = AsyncTunTapDevice::from_sync(device);
let socket = try_fail!(AsyncNetSocket::from_socket(socket), "Failed to create async socket: {}");
let socket = try_fail!(AsyncNetSocket::from_sync(socket), "Failed to create async socket: {}");
let mut cloud = try_fail!(
GenericCloud::<AsyncTunTapDevice, P, AsyncNetSocket, SystemTimeSource>::new(
&config,
@ -364,7 +363,7 @@ fn main() {
return;
}
*/
let socket = try_fail!(listen_udp(&config.listen), "Failed to open socket {}: {}", config.listen);
let socket = try_fail!(NetSocket::listen(&config.listen), "Failed to open socket {}: {}", config.listen);
match config.device_type {
Type::Tap => run::<payload::Frame>(config, socket),
Type::Tun => run::<payload::Packet>(config, socket),

View File

@ -37,7 +37,6 @@ pub trait Socket: Sized + Clone + Send + Sync + 'static {
async fn receive(&mut self, buffer: &mut MsgBuffer) -> Result<SocketAddr, io::Error>;
async fn send(&mut self, data: &[u8], addr: SocketAddr) -> Result<usize, io::Error>;
async fn address(&self) -> Result<SocketAddr, io::Error>;
async fn create_port_forwarding(&self) -> Option<PortForwarding>;
}
pub fn parse_listen(addr: &str, default_port: u16) -> SocketAddr {
@ -54,9 +53,18 @@ pub fn parse_listen(addr: &str, default_port: u16) -> SocketAddr {
}
}
pub fn listen_udp(addr: &str) -> Result<UdpSocket, io::Error> {
pub struct NetSocket(UdpSocket);
impl NetSocket {
pub fn listen(addr: &str) -> Result<Self, io::Error> {
let addr = parse_listen(addr, DEFAULT_PORT);
UdpSocket::bind(addr)
Ok(Self(UdpSocket::bind(addr)?))
}
pub fn create_port_forwarding(&self) -> Option<PortForwarding> {
PortForwarding::new(self.0.local_addr().unwrap().port())
}
}
pub struct AsyncNetSocket(Arc<AsyncUdpSocket>);
@ -68,8 +76,8 @@ impl Clone for AsyncNetSocket {
}
impl AsyncNetSocket {
pub fn from_socket(sock: UdpSocket) -> Result<Self, io::Error> {
Ok(Self(Arc::new(AsyncUdpSocket::from_std(sock)?)))
pub fn from_sync(sock: NetSocket) -> Result<Self, io::Error> {
Ok(Self(Arc::new(AsyncUdpSocket::from_std(sock.0)?)))
}
}
@ -92,10 +100,6 @@ impl Socket for AsyncNetSocket {
addr.set_ip(get_ip());
Ok(addr)
}
async fn create_port_forwarding(&self) -> Option<PortForwarding> {
PortForwarding::new(self.address().await.unwrap().port())
}
}
thread_local! {
@ -174,10 +178,6 @@ impl Socket for MockSocket {
async fn address(&self) -> Result<SocketAddr, io::Error> {
Ok(self.address)
}
async fn create_port_forwarding(&self) -> Option<PortForwarding> {
None
}
}
#[cfg(feature = "bench")]

View File

@ -164,8 +164,4 @@ impl Socket for ProxyConnection {
async fn address(&self) -> Result<SocketAddr, io::Error> {
Ok(self.addr)
}
async fn create_port_forwarding(&self) -> Option<PortForwarding> {
None
}
}