Beispiel #1
0
def delete_port_mapping(
        protocol: SupportedTransportProtocols = SupportedTransportProtocols.
    TCP,
        external_port: int = LISTENING_PORT):
    """
    Removes any UPnP temporary port forwards, to be executed on the application's closure
    :param protocol: Transport protocol of port to be deleted
    :param external_port: Port as viewable from internet
    """

    port_forward_deleted = False
    upnp = UPnP()
    upnp.discoverdelay = 20  # Gives up after 20 ms

    try:
        discovered_count = upnp.discover()
        if discovered_count > 0:
            upnp.selectigd()
            port_forward_deleted = upnp.deleteportmapping(
                external_port, protocol.value)
        if not port_forward_deleted:
            # Send signal to show failure message and how to set up static port forwarding
            print_err(
                2, "Failed to delete UPnP Port Mapping on {} port {}".format(
                    protocol.value, external_port))
            pass
    except Exception as e:
        print(type(e))
        print(str(e))
        pass
    print("UPnP Port Mapping Deleted")
Beispiel #2
0
def ensure_port_is_forwarded(
        protocol: SupportedTransportProtocols = SupportedTransportProtocols.
    TCP,
        external_port: int = LISTENING_PORT,
        internal_port=LISTENING_PORT):
    """
    Ensures that the given external to internal port mapping has been forwarded to allow inbound connections to this
    host on this port.
    If already done, whether by lack of NAT, firewall, or static port forwarding, no action is taken.
    Otherwise, the port is forwarded dynamically using UPnP (if supported by the router and OS) for the duration of the
    application's runtime.

    :param protocol: Transport protocol of port to be mapped
    :param external_port: Port as viewable from the internet
    :param internal_port: Port as viewable from the LAN
    """
    upnp = UPnP()
    upnp.discoverdelay = 20  # Gives up after 20 ms

    port_forwarded = False
    discovered_count = upnp.discover()

    if discovered_count > 0:
        upnp.selectigd()
        port_forwarded = upnp.addportmapping(external_port, protocol.value,
                                             upnp.lanaddr, internal_port,
                                             'UChat P2P Messaging', '')

    if not port_forwarded:
        # Send signal to show failure message and how to set up static port forwarding
        print_err(
            2, "Unable to open UPnP {} port {}".format(protocol.value,
                                                       external_port))
    print("UPnP Port Mapping added")
Beispiel #3
0
def upnp():
    u = UPnP()
    u.discoverdelay = 2000
    if u.discover():
        u.selectigd()
        eport = 81
         
        r = u.getspecificportmapping(eport, 'TCP')
        while r != None and eport < 65536:
            eport = eport + 1
	    r = u.getspecificportmapping(eport, 'TCP')
        
	u.addportmapping(eport, 'TCP', u.lanaddr, 80,
	                'HubDNS port forwarding', '')
Beispiel #4
0
from yaml import load, FullLoader
from miniupnpc import UPnP
import requests

# load the configuration file
with open('config.yaml') as f:
    config = load(f, Loader=FullLoader)

# use UPnP to get the external IP address
upnp = UPnP()

upnp.discoverdelay = 200
upnp.discover()
upnp.selectigd()
ip = upnp.externalipaddress()

# set up variables needed for api access
api_url = 'https://api.cloudflare.com/client/v4'
headers = {
    'Authorization': 'Bearer ' + config['api_token'],
    'Content-Type': 'application/json'
}

# get the DNS record so we can save the ID and check the IP
url = '{}/zones/{}/dns_records?name={}'.format(api_url, config['zone_id'],
                                               config['hostname'])
response = requests.get(url, headers=headers).json()

if response['result'][0]['content'] == ip:
    exit