Exemplo n.º 1
0
    def map_upnp(self):
        upnp = upnpsock.connect()
        if upnp is None:
            return None

        router, location = upnp
        try:
            result = upnpsock.open_port(
                router,
                self.source_port,
            )
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                raise RaidenServicePortInUseError()
            raise

        if result is not None:
            self.storage['router'] = router
            self.storage['external_port'] = result[1]
            return PortMappedSocket(
                sock=self.socket,
                method='UPnP',
                external_ip=result[0],
                external_port=result[1],
                router_location=location,
            )

        return None
Exemplo n.º 2
0
 def map_upnp(self):
     upnp = upnpsock.connect()
     if upnp is not None:
         router, location = upnp
         result = upnpsock.open_port(
             router,
             self.source_port,
         )
         if result is not None:
             self.storage['router'] = router
             return PortMappedSocket(self.socket, 'UPnP', result[0], result[1],
                                     router_location=location)
Exemplo n.º 3
0
    def map_upnp(self):
        upnp = upnpsock.connect()
        if upnp is None:
            return

        try:
            router, location = upnp
            result = upnpsock.open_port(
                router,
                self.source_port,
            )
            if result is not None:
                self.storage['router'] = router
                self.storage['external_port'] = result[1]
                return PortMappedSocket(self.socket, 'UPnP', result[0], result[1],
                                        router_location=location)
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                raise RaidenServicePortInUseError()
            raise
Exemplo n.º 4
0
    def map_upnp(self):
        upnp = upnpsock.connect()
        if upnp is None:
            return

        try:
            router, location = upnp
            result = upnpsock.open_port(
                router,
                self.source_port,
            )
            if result is not None:
                self.storage['router'] = router
                return PortMappedSocket(self.socket,
                                        'UPnP',
                                        result[0],
                                        result[1],
                                        router_location=location)
        except socket.error as e:
            if e.errno == errno.EADDRINUSE:
                raise RaidenServicePortInUseError()
            raise
Exemplo n.º 5
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)