def map_stun(self):
     try:
         external_ip, external_port, nat = stunsock.stun_socket(
             self.socket, self.source_ip, self.source_port, **self.kwargs)
         if external_port is not None:
             return PortMappedSocket(self.socket, 'STUN', external_ip,
                                     external_port, **nat)
     except STUNUnavailableException:
         pass
Esempio n. 2
0
 def map_stun(self):
     try:
         external_ip, external_port, nat = stunsock.stun_socket(
             self.socket,
             self.source_ip,
             self.source_port,
             **self.kwargs,
         )
         if external_port is not None:
             return PortMappedSocket(self.socket, 'STUN', external_ip, external_port, **nat)
     except STUNUnavailableException:
         pass
Esempio n. 3
0
def socket_factory(source_ip, source_port, *args, **kwargs):
    """
    Create a port mapped socket via uPnP or STUN.
    Args:
        source_ip (ip string): the network interface/ip to bind
        source_port (int): the local port to bind
        *args: generic args that are passed to the below implementations
        **kargs: generic kwargs that are passed to the below implementations
    Return:
        PortMappedSocket
    """
    # prefer uPnP over STUN
    upnp = upnpsock.connect()
    if upnp is not None:
        router, location = upnp
        result = upnpsock.open_port(
            router,
            source_port,
        )
        if result is not None:
            with stunsock.open_bare_socket(source_ip, source_port, *args, **kwargs) as sock:
                try:
                    yield PortMappedSocket(sock, 'uPnP', result[0], result[1], **dict(
                        router_location=location
                    ))
                finally:
                    upnpsock.release_port(router, source_port)

    else:
        with stunsock.stun_socket(source_ip, source_port, *args, **kwargs) as (
            sock,
            external_ip,
            external_port,
            nat
        ):
            if external_port is not None:
                yield PortMappedSocket(sock, 'STUN', external_ip, external_port, **nat)