1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Automatically forward a port to qaul-hubd to allow easy reverse
//! connections form a public router.

use igd::{search_gateway, Gateway, PortMappingProtocol as Protocol};
use ipnetwork::IpNetwork;
use pnet::datalink;
use std::io::Read;
use std::net::{Ipv4Addr, SocketAddrV4, TcpListener};
use tracing::trace;

//fn ip_is_local(ip: IpV4Addr)

/// Check if an IP is in one of the local IP ranges
///
/// We perform this check to see if an IP address _could_ be one that
/// a gateway can reach.  This auto-detection can go wrong though if
/// there are multiple address spaces availabe (for example via a VPN)
fn check_local(ip: &Ipv4Addr) -> bool {
    let [a, b, c, d] = ip.octets();
    match (a, b, c, d) {
        (10, _, _, _) => true,
        (192, 168, _, _) => true,
        (172, n, _, _) if n > 16 && n < 31 => true,
        (_, _, _, _) => false,
    }
}

fn find_local_ip() -> Option<Ipv4Addr> {
    datalink::interfaces()
        .into_iter()
        .map(|_if| _if.ips)
        .fold(None, |res, ips| {
            let mut new = res;
            for ip in ips {
                use IpNetwork::*;
                match (res, ip) {
                    (None, V4(n)) if check_local(&n.ip()) => {
                        new = Some(n.ip());
                        break;
                    }
                    (_, _) => {}
                }
            }

            new
        })
}

pub(crate) fn open_port(port: u16) -> Option<()> {
    let gw = search_gateway(Default::default()).ok()?;

    let ip = gw.get_external_ip().ok()?;
    trace!("Publicly accessible via: {}", ip);

    let local_ip =
        find_local_ip().unwrap_or_else(|| crate::elog("Couldn't find IP to bind to", 128));
    trace!("Local ip: {}", local_ip);

    let local_addr = SocketAddrV4::new(local_ip, 8080);

    gw.add_port(Protocol::TCP, port, local_addr, 0, "qaul-hubd tcp driver")
        .unwrap_or_else(|e| crate::elog(format!("{:?}", e), 128));
    trace!("UPNP port {} opened with infinite lease!", port);
    Some(())
}