vpncloud/src/port_forwarding.rs

186 lines
7.0 KiB
Rust
Raw Normal View History

2017-07-22 14:49:53 +00:00
// VpnCloud - Peer-to-Peer VPN
2021-02-08 09:11:20 +00:00
// Copyright (C) 2015-2021 Dennis Schwerdel
2017-07-22 14:49:53 +00:00
// This software is licensed under GPL-3 or newer (see LICENSE.md)
#[cfg(feature = "nat")]
mod internal {
use std::{io, net::SocketAddrV4};
use igd::{search_gateway, AddAnyPortError, AddPortError, Gateway, PortMappingProtocol, SearchError};
use crate::util::{get_internal_ip, SystemTimeSource, Time, TimeSource};
const LEASE_TIME: u32 = 1800;
const DESCRIPTION: &str = "VpnCloud";
pub struct PortForwarding {
pub internal_addr: SocketAddrV4,
pub external_addr: SocketAddrV4,
gateway: Gateway,
2021-04-06 10:28:31 +00:00
pub next_extension: Option<Time>,
}
impl PortForwarding {
pub fn new(port: u16) -> Option<Self> {
// Get the gateway
let gateway = match search_gateway(Default::default()) {
Ok(gateway) => gateway,
Err(err) => {
if let SearchError::IoError(ref err) = err {
if err.kind() == io::ErrorKind::WouldBlock {
// Why this code?
2020-02-20 21:51:22 +00:00
info!("Port-forwarding: no router found");
2021-04-06 10:28:31 +00:00
return None;
}
}
error!("Port-forwarding: failed to find router: {}", err);
2021-04-06 10:28:31 +00:00
return None;
}
};
2020-10-24 22:24:11 +00:00
debug!("Port-forwarding: found router at {}", gateway.addr);
let internal_addr = SocketAddrV4::new(get_internal_ip(), port);
// Query the external address
let external_ip = match gateway.get_external_ip() {
Ok(ip) => ip,
Err(err) => {
error!("Port-forwarding: failed to obtain external IP: {}", err);
2021-04-06 10:28:31 +00:00
return None;
}
};
if let Ok((port, timeout)) = Self::get_any_forwarding(&gateway, internal_addr, port) {
2020-10-24 22:24:11 +00:00
debug!("Port-forwarding: external IP is {}", external_ip);
let external_addr = SocketAddrV4::new(external_ip, port);
2020-10-24 22:24:11 +00:00
debug!("Port-forwarding has timeout {}", timeout);
info!("Port-forwarding: successfully activated port forward on {}", external_addr);
let next_extension =
if timeout > 0 { Some(SystemTimeSource::now() + Time::from(timeout) - 60) } else { None };
Some(PortForwarding { internal_addr, external_addr, gateway, next_extension })
} else {
None
}
}
fn get_any_forwarding(gateway: &Gateway, addr: SocketAddrV4, port: u16) -> Result<(u16, u32), ()> {
if let Ok(a) = Self::get_forwarding(gateway, addr, port) {
2021-04-06 10:28:31 +00:00
return Ok(a);
}
if let Ok(a) = Self::get_forwarding(gateway, addr, 0) {
2021-04-06 10:28:31 +00:00
return Ok(a);
}
for i in 1..5 {
if let Ok(a) = Self::get_forwarding(gateway, addr, port + i) {
2021-04-06 10:28:31 +00:00
return Ok(a);
}
}
for _ in 0..5 {
if let Ok(a) = Self::get_forwarding(gateway, addr, rand::random()) {
2021-04-06 10:28:31 +00:00
return Ok(a);
}
}
2020-10-24 22:24:11 +00:00
warn!("Failed to activate port forwarding");
Err(())
}
fn get_forwarding(gateway: &Gateway, addr: SocketAddrV4, port: u16) -> Result<(u16, u32), ()> {
2020-10-24 22:24:11 +00:00
debug!("Trying external port {}", port);
if port == 0 {
match gateway.add_any_port(PortMappingProtocol::UDP, addr, LEASE_TIME, DESCRIPTION) {
Ok(port) => Ok((port, LEASE_TIME)),
Err(AddAnyPortError::OnlyPermanentLeasesSupported) => {
match gateway.add_any_port(PortMappingProtocol::UDP, addr, 0, DESCRIPTION) {
Ok(port) => Ok((port, 0)),
Err(err) => {
2020-10-24 22:24:11 +00:00
debug!("Port-forwarding: failed to activate port forwarding: {}", err);
Err(())
}
2019-12-04 08:32:35 +00:00
}
}
Err(err) => {
2020-10-24 22:24:11 +00:00
debug!("Port-forwarding: failed to activate port forwarding: {}", err);
Err(())
}
}
} else {
match gateway.add_port(PortMappingProtocol::UDP, port, addr, LEASE_TIME, DESCRIPTION) {
Ok(()) => Ok((port, LEASE_TIME)),
Err(AddPortError::OnlyPermanentLeasesSupported) => {
match gateway.add_port(PortMappingProtocol::UDP, port, addr, 0, DESCRIPTION) {
Ok(()) => Ok((port, 0)),
Err(err) => {
2020-10-24 22:24:11 +00:00
debug!("Port-forwarding: failed to activate port forwarding: {}", err);
Err(())
}
}
2019-12-04 08:32:35 +00:00
}
Err(err) => {
2020-10-24 22:24:11 +00:00
debug!("Port-forwarding: failed to activate port forwarding: {}", err);
Err(())
}
}
2019-12-04 08:32:35 +00:00
}
}
pub fn check_extend(&mut self) {
if let Some(deadline) = self.next_extension {
if deadline > SystemTimeSource::now() {
2021-04-06 10:28:31 +00:00
return;
}
} else {
2021-04-06 10:28:31 +00:00
return;
}
match self.gateway.add_port(
PortMappingProtocol::UDP,
self.external_addr.port(),
self.internal_addr,
LEASE_TIME,
2021-04-06 10:28:31 +00:00
DESCRIPTION,
) {
Ok(()) => debug!("Port-forwarding: extended port forwarding"),
2021-04-06 10:28:31 +00:00
Err(err) => debug!("Port-forwarding: failed to extend port forwarding: {}", err),
};
self.next_extension = Some(SystemTimeSource::now() + Time::from(LEASE_TIME) - 60);
}
fn deactivate(&self) {
match self.gateway.remove_port(PortMappingProtocol::UDP, self.external_addr.port()) {
Ok(()) => info!("Port-forwarding: successfully deactivated port forwarding"),
2021-04-06 10:28:31 +00:00
Err(err) => debug!("Port-forwarding: failed to deactivate port forwarding: {}", err),
}
}
2020-11-03 17:49:35 +00:00
pub fn get_internal_ip(&self) -> SocketAddrV4 {
self.internal_addr
}
pub fn get_external_ip(&self) -> SocketAddrV4 {
self.external_addr
}
}
impl Drop for PortForwarding {
fn drop(&mut self) {
self.deactivate()
}
}
}
#[cfg(not(feature = "nat"))]
mod internal {
pub struct PortForwarding;
impl PortForwarding {
pub fn new(_port: u16) -> Option<Self> {
warn!("Compiled without feature 'nat', skipping port forwarding.");
None
}
pub fn check_extend(&mut self) {
unreachable!()
}
}
}
pub use internal::*;