2017-07-22 14:49:53 +00:00
|
|
|
// VpnCloud - Peer-to-Peer VPN
|
2020-05-28 07:03:48 +00:00
|
|
|
// Copyright (C) 2015-2020 Dennis Schwerdel
|
2017-07-22 14:49:53 +00:00
|
|
|
// This software is licensed under GPL-3 or newer (see LICENSE.md)
|
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
#[cfg(feature = "nat")]
|
|
|
|
mod internal {
|
2016-08-10 09:34:13 +00:00
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
use std::{io, net::SocketAddrV4};
|
2016-08-10 09:34:13 +00:00
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
use igd::{search_gateway, AddAnyPortError, AddPortError, Gateway, PortMappingProtocol, SearchError};
|
2016-08-10 09:34:13 +00:00
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
use crate::util::{get_internal_ip, SystemTimeSource, Time, TimeSource};
|
2016-08-10 09:34:13 +00:00
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
const LEASE_TIME: u32 = 1800;
|
2016-08-10 09:34:13 +00:00
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
const DESCRIPTION: &str = "VpnCloud";
|
|
|
|
|
|
|
|
pub struct PortForwarding {
|
|
|
|
pub internal_addr: SocketAddrV4,
|
|
|
|
pub external_addr: SocketAddrV4,
|
|
|
|
gateway: Gateway,
|
|
|
|
pub next_extension: Option<Time>
|
|
|
|
}
|
2016-08-10 09:34:13 +00:00
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
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");
|
2019-12-22 22:21:47 +00:00
|
|
|
return None
|
|
|
|
}
|
2016-08-10 09:34:13 +00:00
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
error!("Port-forwarding: failed to find router: {}", err);
|
|
|
|
return None
|
2016-08-10 09:34:13 +00:00
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
};
|
|
|
|
info!("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);
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Ok((port, timeout)) = Self::get_any_forwarding(&gateway, internal_addr, port) {
|
|
|
|
info!("Port-forwarding: external IP is {}", external_ip);
|
|
|
|
let external_addr = SocketAddrV4::new(external_ip, port);
|
|
|
|
info!("Port-forwarding: sucessfully activated port forward on {}, timeout: {}", external_addr, timeout);
|
|
|
|
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
|
2016-08-10 09:34:13 +00:00
|
|
|
}
|
2019-12-22 21:51:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
fn get_any_forwarding(gateway: &Gateway, addr: SocketAddrV4, port: u16) -> Result<(u16, u32), ()> {
|
|
|
|
if let Ok(a) = Self::get_forwarding(gateway, addr, port) {
|
2019-12-22 21:51:40 +00:00
|
|
|
return Ok(a)
|
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
if let Ok(a) = Self::get_forwarding(gateway, addr, 0) {
|
2019-12-22 21:51:40 +00:00
|
|
|
return Ok(a)
|
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
for i in 1..5 {
|
|
|
|
if let Ok(a) = Self::get_forwarding(gateway, addr, port + i) {
|
|
|
|
return Ok(a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _ in 0..5 {
|
|
|
|
if let Ok(a) = Self::get_forwarding(gateway, addr, rand::random()) {
|
|
|
|
return Ok(a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(())
|
2019-12-22 21:51:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
fn get_forwarding(gateway: &Gateway, addr: SocketAddrV4, port: u16) -> Result<(u16, u32), ()> {
|
|
|
|
info!("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) => {
|
|
|
|
error!("Port-forwarding: failed to activate port forwarding: {}", err);
|
|
|
|
Err(())
|
|
|
|
}
|
2019-12-04 08:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
Err(err) => {
|
|
|
|
error!("Port-forwarding: failed to activate port forwarding: {}", err);
|
|
|
|
Err(())
|
|
|
|
}
|
2019-12-22 21:51:40 +00:00
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
} 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) => {
|
|
|
|
error!("Port-forwarding: failed to activate port forwarding: {}", err);
|
|
|
|
Err(())
|
|
|
|
}
|
2019-12-22 21:51:40 +00:00
|
|
|
}
|
2019-12-04 08:32:35 +00:00
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
Err(err) => {
|
|
|
|
error!("Port-forwarding: failed to activate port forwarding: {}", err);
|
|
|
|
Err(())
|
|
|
|
}
|
2019-12-22 21:51:40 +00:00
|
|
|
}
|
2019-12-04 08:32:35 +00:00
|
|
|
}
|
2019-12-22 21:51:40 +00:00
|
|
|
}
|
2016-08-10 09:34:13 +00:00
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
pub fn check_extend(&mut self) {
|
|
|
|
if let Some(deadline) = self.next_extension {
|
|
|
|
if deadline > SystemTimeSource::now() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-10 09:34:13 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
match self.gateway.add_port(
|
|
|
|
PortMappingProtocol::UDP,
|
|
|
|
self.external_addr.port(),
|
|
|
|
self.internal_addr,
|
|
|
|
LEASE_TIME,
|
|
|
|
DESCRIPTION
|
|
|
|
) {
|
|
|
|
Ok(()) => debug!("Port-forwarding: extended port forwarding"),
|
|
|
|
Err(err) => error!("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"),
|
|
|
|
Err(err) => error!("Port-forwarding: failed to deactivate port forwarding: {}", err)
|
|
|
|
}
|
2016-08-10 09:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
impl Drop for PortForwarding {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.deactivate()
|
2016-08-10 09:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:21:47 +00:00
|
|
|
#[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!()
|
|
|
|
}
|
2016-08-10 09:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:21:47 +00:00
|
|
|
|
|
|
|
pub use internal::*;
|