Example #1
0
    def test_get_host_addresses(self, mock_ifaddresses, mock_interfaces):
        mock_interfaces.return_value = ["eth0"]
        mock_ifaddresses.return_value = {
            socket.AF_INET: [{
                "addr": "127.0.0.1"
            }, {
                "addr": "1.2.3.4"
            }],
            socket.AF_INET6: [
                {
                    "addr": "::1"
                },
                {
                    "addr": "2a02:0db8:85a3:0000:0000:8a2e:0370:7334"
                },
                {
                    "addr": "fe80::1234:5678:9abc:def0%eth0"
                },
            ],
        }

        # IPv4 only
        addresses = ice.get_host_addresses(use_ipv4=True, use_ipv6=False)
        self.assertEqual(addresses, ["1.2.3.4"])

        # IPv6 only
        addresses = ice.get_host_addresses(use_ipv4=False, use_ipv6=True)
        self.assertEqual(addresses,
                         ["2a02:0db8:85a3:0000:0000:8a2e:0370:7334"])

        # both
        addresses = ice.get_host_addresses(use_ipv4=True, use_ipv6=True)
        self.assertEqual(
            addresses, ["1.2.3.4", "2a02:0db8:85a3:0000:0000:8a2e:0370:7334"])
Example #2
0
    def test_get_host_addresses(self, mock_ifaddresses, mock_interfaces):
        mock_interfaces.return_value = ['eth0']
        mock_ifaddresses.return_value = {
            socket.AF_INET: [
                {'addr': '127.0.0.1'},
                {'addr': '1.2.3.4'},
            ],
            socket.AF_INET6: [
                {'addr': '::1'},
                {'addr': '2a02:0db8:85a3:0000:0000:8a2e:0370:7334'},
                {'addr': 'fe80::1234:5678:9abc:def0%eth0'},
            ]
        }

        # IPv4 only
        addresses = ice.get_host_addresses(use_ipv4=True, use_ipv6=False)
        self.assertEqual(addresses, [
            '1.2.3.4',
        ])

        # IPv6 only
        addresses = ice.get_host_addresses(use_ipv4=False, use_ipv6=True)
        self.assertEqual(addresses, [
            '2a02:0db8:85a3:0000:0000:8a2e:0370:7334',
        ])

        # both
        addresses = ice.get_host_addresses(use_ipv4=True, use_ipv6=True)
        self.assertEqual(addresses, [
            '1.2.3.4',
            '2a02:0db8:85a3:0000:0000:8a2e:0370:7334',
        ])
Example #3
0
    async def listen(self, port=0, tls_port=0):
        loop = asyncio.get_event_loop()
        hostaddr = get_host_addresses(use_ipv4=True, use_ipv6=False)[0]

        # listen for TCP
        self.tcp_server = await loop.create_server(
            lambda: TurnServerTcpProtocol(server=self),
            host=hostaddr,
            port=port)
        self.tcp_address = self.tcp_server.sockets[0].getsockname()
        logger.info("Listening for TCP on %s", self.tcp_address)

        # listen for UDP
        transport, self.udp_server = await loop.create_datagram_endpoint(
            lambda: TurnServerUdpProtocol(server=self),
            local_addr=(hostaddr, port))
        self.udp_address = transport.get_extra_info("sockname")
        logger.info("Listening for UDP on %s", self.udp_address)

        # listen for TLS
        ssl_context = ssl.SSLContext()
        ssl_context.load_cert_chain(CERT_FILE, KEY_FILE)
        self.tls_server = await loop.create_server(
            lambda: TurnServerTcpProtocol(server=self),
            host=hostaddr,
            port=tls_port,
            ssl=ssl_context,
        )
        self.tls_address = self.tls_server.sockets[0].getsockname()
        logger.info("Listening for TLS on %s", self.tls_address)

        # start expiry loop
        self._expire_handle = asyncio.ensure_future(self._expire_allocations())