Added feature to disable special NAT support

This commit is contained in:
Dennis Schwerdel 2019-12-22 23:21:47 +01:00
parent 54a2240f34
commit eb620781a8
3 changed files with 143 additions and 119 deletions

View File

@ -4,6 +4,7 @@ This project follows [semantic versioning](http://semver.org).
### Unreleased ### Unreleased
- [added] Added feature to disable special NAT support
- [changed] Improved port forwarding on quirky routers - [changed] Improved port forwarding on quirky routers
- [changed] Reduced peer timeout to 5min to work better with NAT - [changed] Reduced peer timeout to 5min to work better with NAT

View File

@ -24,7 +24,7 @@ rand = "0.7"
fnv = "1" fnv = "1"
net2 = "0.2" net2 = "0.2"
yaml-rust = "0.4" yaml-rust = "0.4"
igd = "0.9" igd = { version = "0.9", optional = true }
siphasher = "0.3" siphasher = "0.3"
daemonize = "0.4" daemonize = "0.4"
ring = "0.16" ring = "0.16"
@ -38,8 +38,9 @@ pkg-config = "0.3"
tempfile = "3" tempfile = "3"
[features] [features]
default = [] default = ["nat"]
bench = [] bench = []
nat = ["igd"]
[profile.release] [profile.release]
lto = true lto = true

View File

@ -2,24 +2,27 @@
// Copyright (C) 2015-2019 Dennis Schwerdel // Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md) // This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::{io, net::SocketAddrV4}; #[cfg(feature = "nat")]
mod internal {
use igd::{search_gateway, AddAnyPortError, AddPortError, Gateway, PortMappingProtocol, SearchError}; use std::{io, net::SocketAddrV4};
use super::util::{get_internal_ip, SystemTimeSource, Time, TimeSource}; use igd::{search_gateway, AddAnyPortError, AddPortError, Gateway, PortMappingProtocol, SearchError};
const LEASE_TIME: u32 = 1800; use crate::util::{get_internal_ip, SystemTimeSource, Time, TimeSource};
const DESCRIPTION: &str = "VpnCloud";
const LEASE_TIME: u32 = 1800;
pub struct PortForwarding { const DESCRIPTION: &str = "VpnCloud";
pub struct PortForwarding {
pub internal_addr: SocketAddrV4, pub internal_addr: SocketAddrV4,
pub external_addr: SocketAddrV4, pub external_addr: SocketAddrV4,
pub gateway: Gateway, gateway: Gateway,
pub next_extension: Option<Time> pub next_extension: Option<Time>
} }
impl PortForwarding { impl PortForwarding {
pub fn new(port: u16) -> Option<Self> { pub fn new(port: u16) -> Option<Self> {
// Get the gateway // Get the gateway
let gateway = match search_gateway(Default::default()) { let gateway = match search_gateway(Default::default()) {
@ -144,10 +147,29 @@ impl PortForwarding {
Err(err) => error!("Port-forwarding: failed to deactivate port forwarding: {}", err) Err(err) => error!("Port-forwarding: failed to deactivate port forwarding: {}", err)
} }
} }
} }
impl Drop for PortForwarding { impl Drop for PortForwarding {
fn drop(&mut self) { fn drop(&mut self) {
self.deactivate() 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::*;