def test_sync_server_side_client_ip(): port_config = get_port_config([Port(80, 80, 'TCP'), Port(81, 81, 'TCP')]) port_drill = PortDrill(port_config, MockPortMapper(external_ip='10.1.1.1'), MockPortProber()) responses.add(responses.POST, "http://api.domain.com/domain/update", status=200, body="{'message': 'Domain was updated'}", content_type="application/json") user_platform_config = get_user_platform_config() user_platform_config.update_redirect('domain.com', 'http://api.domain.com') user_platform_config.update_domain('boris', 'some_update_token') dns = RedirectService(user_platform_config, test_version) dns.sync(port_drill, 'some_update_token', 'http', False, 'TCP') expected_request = ''' { "web_local_port": 80, "web_port": 80, "web_protocol": "http", "platform_version": "test", "token": "some_update_token", "map_local_address": true, "local_ip": "127.0.0.1" } ''' assertSingleRequest(expected_request)
def sync_one_mapping(self, local_port, protocol): self.logger.info('Sync one mapping: {0}'.format(local_port)) port_to_try = local_port lower_limit = 10000 found_external_port = None retries = 10 for i in range(1, retries): self.logger.info('Trying {0}'.format(port_to_try)) external_port = self.port_mapper.add_mapping( local_port, port_to_try, protocol) if not is_web_port(local_port): found_external_port = external_port break if self.port_prober.probe_port(external_port, port_to_protocol(local_port)): found_external_port = external_port break self.port_mapper.remove_mapping(local_port, external_port, protocol) if port_to_try == local_port: port_to_try = lower_limit else: port_to_try = external_port + 1 if not found_external_port: raise Exception( 'Unable to add mapping, tried {0} times'.format(retries)) mapping = Port(local_port, found_external_port, protocol) self.port_config.add_or_update(mapping)
def test_port_drill(): port_config = get_port_config([Port(80, 80, 'TCP')]) port_drill = PortDrill(port_config, MockPortMapper(external_ip='192.167.44.52'), NoneProber()) port_drill.sync_existing_ports() mapping = port_drill.get(80, 'TCP') assert mapping.external_port == 80 assert port_drill.external_ip() == '192.167.44.52'
def test_add_update_remove(): port_config = get_port_config([]) port_config.add_or_update(Port(80, 10000, 'TCP')) port_config.add_or_update(Port(80, 10001, 'TCP')) port_config.add_or_update(Port(81, 10002, 'UDP')) port_config.add_or_update(Port(81, 10003, 'UDP')) port_config.add_or_update(Port(81, 10004, 'TCP')) assert len(port_config.load()) == 3 assert port_config.get(80, 'TCP').external_port == 10001 assert port_config.get(81, 'UDP').external_port == 10003 assert port_config.get(81, 'TCP').external_port == 10004 port_config.remove(81, 'UDP') assert len(port_config.load()) == 2 assert port_config.get(80, 'TCP').external_port == 10001 assert port_config.get(81, 'TCP').external_port == 10004
def test_url_with_external_access(): user_platform_config = get_user_platform_config() user_platform_config.update_domain('device', 'token') user_platform_config.update_redirect('syncloud.it', 'api.url') user_platform_config.update_device_access(False, False, True, '1.1.1.1') port_config = get_port_config([Port(80, 10000, 'TCP')]) device_info = DeviceInfo(user_platform_config, port_config) assert device_info.url('app') == 'http://app.device.syncloud.it:10000'
def test_pre_protocol_support(): old_json = '[{"external_port": 81, "local_port": 80}]' port_config = get_port_config(convertible.from_json(old_json)) assert port_config.get(80, 'TCP').external_port == 81 port_config.add_or_update(Port(80, 10000, 'UDP')) assert port_config.get(80, 'UDP').external_port == 10000 print(open(port_config.filename, 'r').read()) assert len(port_config.load()) == 2
def sync_new_port(self, local_port, protocol): self.logger.info('Sync one mapping: {0}'.format(local_port)) port_to_try = local_port lower_limit = 10000 found_external_port = None retries = 10 message = 'no message from dns service' for i in range(1, retries): self.logger.info('Trying {0}'.format(port_to_try)) external_port = self.port_mapper.add_mapping( local_port, port_to_try, protocol) if not is_web_port(local_port): self.logger.info('not probing non http(s) ports') found_external_port = external_port break external_ip = self.port_mapper.external_ip() if external_ip is not None: ip_version = IP(external_ip).version() if ip_version == 6: self.logger.info('probing of IPv6 is not supported yet') found_external_port = external_port break probe_success, message = self.port_prober.probe_port( external_port, port_to_protocol(local_port), external_ip) if probe_success: found_external_port = external_port break self.port_mapper.remove_mapping(local_port, external_port, protocol) if port_to_try == local_port: port_to_try = lower_limit else: self.logger.info('external port: {0}'.format(external_port)) port_to_try = external_port + 1 if not found_external_port: raise Exception('Unable to verify open ports, {0}'.format(message)) mapping = Port(local_port, found_external_port, protocol) self.port_config.add_or_update(mapping) return mapping
def sync_one_mapping(self, local_port, protocol): self.logger.info('Sync one mapping: {0}'.format(local_port)) port_to_try = local_port lower_limit = 10000 found_external_port = None retries = 10 for i in range(1, retries): self.logger.info('Trying {0}'.format(port_to_try)) external_port = self.port_mapper.add_mapping( local_port, port_to_try, protocol) if not is_web_port(local_port): self.logger.info('not probing non http(s) ports') found_external_port = external_port break ip_version = IP(self.port_mapper.external_ip()).version() if ip_version == 6: self.logger.info('probing of IPv6 is not supported yet') found_external_port = external_port break if self.port_prober.probe_port(external_port, port_to_protocol(local_port)): found_external_port = external_port break self.port_mapper.remove_mapping(local_port, external_port, protocol) if port_to_try == local_port: port_to_try = lower_limit else: self.logger.info('external port: {0}'.format(external_port)) port_to_try = external_port + 1 if not found_external_port: raise Exception( 'Unable to verify open ports, tried {0} times'.format(retries)) mapping = Port(local_port, found_external_port, protocol) self.port_config.add_or_update(mapping) return mapping
def test_sync_server_error(): port_config = get_port_config([Port(80, 10000, 'TCP')]) port_drill = PortDrill(port_config, MockPortMapper(external_ip='192.167.44.52'), MockPortProber()) responses.add(responses.POST, "http://api.domain.com/domain/update", status=400, body='{"message": "Unknown update token"}', content_type="application/json") user_platform_config = get_user_platform_config() user_platform_config.update_redirect('domain.com', 'http://api.domain.com') user_platform_config.update_domain('boris', 'some_update_token') dns = RedirectService(user_platform_config, test_version) with pytest.raises(PassthroughJsonError) as context: dns.sync(port_drill, 'some_update_token', 'http', False, 'TCP') assert context.value.message == "Unknown update token"
def get(self, local_port, protocol): return Port(local_port, None, protocol)
def test_port_mapping(): expected = '{"external_port": "8080", "local_port": "80", "protocol": "TCP"}' actual = to_json(Port("80", "8080", "TCP")) assert str(expected) == str(actual)
def test_port_mapping(): expected = '{"protocol": "TCP", "external_port": "8080", "local_port": "80"}' actual = to_json(Port("80", "8080", "TCP")) assert expected == actual