Beispiel #1
0
def gracefull_shutdown(signum, frame):
    """Make a gracefull shutdown, and tell the server about it"""
    global token

    web = WebConnect(args.address)
    log.debug("Closing connection...")
    web.post('/disconnect/', {'uuid': token})
    exit(1)
Beispiel #2
0
def main():
    global token
    post_args = {}

    # Register a trap for a gracefull shutdown
    signal.signal(signal.SIGINT, gracefull_shutdown)

    """This is our methods for connecting.
    At least one of them must return true"""
    client_cap = {
            'upnp': False,
            'nat_pmp': False,
            'udp_preserve': False,
            'udp_sequential': False}

    # Choose a random port (stop "early" to be sure we get a port)
    lport = randint(1025, 60000)

    # Make the udpKnocker and socket. Get the maybe new lport
    knocker = udpKnock(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), lport)
    lport = knocker.lport

    # Build default post_args dict, and ready a place for the external IP
    post_args = {'lport': lport}
    external_address = False

    # Test the natPMP capabilities
    if not args.no_natpmp:
        log.info("NAT-PMP - Testing for NAT-PMP...    ")
        npmp = natPMP()
        nat_pmp = npmp.map_external_port(lport=lport)
        if nat_pmp:
            log.info("NAT-PMP - [SUCCESS]")
            client_cap['nat_pmp'] = True
            post_args['lport'] = nat_pmp[0]
            external_address = npmp.get_external_address()
        else:
            log.info("NAT-PMP - [FAILED]")

    # Test the UPnP-IGD capabilities
    if not args.no_upnp and not client_cap['nat_pmp']:
        log.info("UPnP-IGD - Testing for UPnP-IDG...")

        # Find IP-Address of local machine
        # TODO: Find fix for IPv6-addresses
        ip = find_ip(args.address.split(':')[1][2:])

        # Creating the UPnP device checker
        upnp = upnp_igd()
        if upnp.search() and upnp.AddPortMapping(ip, lport, 'UDP'):
            log.info("UPnP-IGD - [SUCCESS]")
            external_address = upnp.GetExternalIPAddress()
            client_cap['upnp'] = True
        else:
            log.info("UPnP-IGD - [FAILED]")

    # Get external ip-address and test what NAT type we are behind
    if not args.no_stun and not external_address:
        stun, port_mapping, stun_port = test_stun()
        external_address = stun[0][1]
        if port_mapping == PRESERVES_PORT:
            client_cap['udp_preserve'] = True
        if port_mapping == SEQUENTIAL_PORT:
            client_cap['udp_seqential'] = True

    # Connect to the webserver for connection and such
    web = WebConnect(args.address)

    # Add client caps and external_address to post_args
    post_args['client_cap'] = client_cap
    post_args['stun_ip'] = external_address

    # Get token from server
    token = web.get('/')['token']
    post_args['uuid'] = token
    log.info("Token is: "+token)

    if args.peer:
        """Connect and tell you want 'token'"""
        post_args['token'] = args.peer
        respons = web.post('/connect/', post_args)
        if respons.get('err'):
            log.info("Got error: "+respons['err'])
            exit(1)
    else:
        """Connect and wait for someone to access 'token'"""
        respons = web.post('/me/', post_args)
        while(True):
            peer = input("Enter token of your peer: ")
            respons = web.post('/ready/', {'uuid': token, 'token': peer})
            if not respons.get('err'):
                s = knocker.knock(respons['peer.ip'], int(respons['peer.lport']))
                break

    log.debug(respons)
    raddr = respons['peer.ip']
    rport = respons['peer.lport']
    lVPNaddr = respons['me.VPNaddr']
    rVPNaddr = respons['peer.VPNaddr']
    mode = respons['me.mode']
    key = write_key(respons['me.key'])

    # Tell if we are maybe running into trouble
    if mode == 'p2p-fallback':
        log.info("Running in p2p-fallback mode, may not be able to connect...")
        sleep(2)
        mode = 'p2p'

    knocker.s.close()
    vpn = Process(target=startVPN, args=(lport, raddr, rport, lVPNaddr, rVPNaddr, mode, key))
    vpn.start()
    vpn.join()

    # Cleanup
    os.remove(key)