Exemplo n.º 1
0
def replay(options, replay_filename):
    if options.admin_check and options.IsRootRequired():
        platformsettings.rerun_as_administrator()
    configure_logging(options.log_level, options.log_file)
    server_manager = servermanager.ServerManager(options.record)
    cache_misses = None
    if options.cache_miss_file:
        if os.path.exists(options.cache_miss_file):
            logging.warning(
                'Cache Miss Archive file %s already exists; '
                'replay will load and append entries to archive file',
                options.cache_miss_file)
            cache_misses = cachemissarchive.CacheMissArchive.Load(
                options.cache_miss_file)
        else:
            cache_misses = cachemissarchive.CacheMissArchive(
                options.cache_miss_file)
    if options.server:
        AddDnsForward(server_manager, options.server)
    else:
        real_dns_lookup = dnsproxy.RealDnsLookup(
            name_servers=[platformsettings.get_original_primary_nameserver()])
        if options.record:
            httparchive.HttpArchive.AssertWritable(replay_filename)
            if options.append and os.path.exists(replay_filename):
                http_archive = httparchive.HttpArchive.Load(replay_filename)
                logging.info('Appending to %s (loaded %d existing responses)',
                             replay_filename, len(http_archive))
            else:
                http_archive = httparchive.HttpArchive()
        else:
            http_archive = httparchive.HttpArchive.Load(replay_filename)
            logging.info('Loaded %d responses from %s', len(http_archive),
                         replay_filename)
        server_manager.AppendRecordCallback(real_dns_lookup.ClearCache)
        server_manager.AppendRecordCallback(http_archive.clear)

        ipfw_dns_host = None
        if options.dns_forwarding or options.shaping_dummynet:
            # compute the ip/host used for the DNS server and traffic shaping
            ipfw_dns_host = options.host
            if not ipfw_dns_host:
                ipfw_dns_host = platformsettings.get_server_ip_address(
                    options.server_mode)

        if options.dns_forwarding:
            if not options.server_mode and ipfw_dns_host == '127.0.0.1':
                AddDnsForward(server_manager, ipfw_dns_host)
            AddDnsProxy(server_manager, options, ipfw_dns_host,
                        options.dns_port, real_dns_lookup, http_archive)
        if options.ssl and options.https_root_ca_cert_path is None:
            options.https_root_ca_cert_path = os.path.join(
                os.path.dirname(__file__), 'wpr_cert.pem')
        http_proxy_address = options.host
        if not http_proxy_address:
            http_proxy_address = platformsettings.get_httpproxy_ip_address(
                options.server_mode)
        AddWebProxy(server_manager, options, http_proxy_address,
                    real_dns_lookup, http_archive, cache_misses)
        AddTrafficShaper(server_manager, options, ipfw_dns_host)

    exit_status = 0
    try:
        server_manager.Run()
    except KeyboardInterrupt:
        logging.info('Shutting down.')
    except (dnsproxy.DnsProxyException, trafficshaper.TrafficShaperException,
            platformsettings.NotAdministratorError,
            platformsettings.DnsUpdateError) as e:
        logging.critical('%s: %s', e.__class__.__name__, e)
        exit_status = 1
    except:
        logging.critical(traceback.format_exc())
        exit_status = 2

    if options.record:
        http_archive.Persist(replay_filename)
        logging.info('Saved %d responses to %s', len(http_archive),
                     replay_filename)
    if cache_misses:
        cache_misses.Persist()
        logging.info('Saved %d cache misses and %d requests to %s',
                     cache_misses.get_total_cache_misses(),
                     len(cache_misses.request_counts.keys()),
                     options.cache_miss_file)
    return exit_status
def replay(options, replay_filename):
  if options.record and sys.version_info < (2, 7, 9):
    print ('Need Python 2.7.9 or greater for recording mode.\n'
           'For instructions on how to upgrade Python on Ubuntu 14.04, see:\n'
           'http://mbless.de/blog/2016/01/09/upgrade-to-python-2711-on-ubuntu-1404-lts.html\n')
  if options.admin_check and options.IsRootRequired():
    platformsettings.rerun_as_administrator()
  configure_logging(options.log_level, options.log_file)
  server_manager = servermanager.ServerManager(options.record)
  if options.server:
    AddDnsForward(server_manager, options.server)
  else:
    if options.record:
      httparchive.HttpArchive.AssertWritable(replay_filename)
      if options.append and os.path.exists(replay_filename):
        http_archive = httparchive.HttpArchive.Load(replay_filename)
        logging.info('Appending to %s (loaded %d existing responses)',
                     replay_filename, len(http_archive))
      else:
        http_archive = httparchive.HttpArchive()
    else:
      http_archive = httparchive.HttpArchive.Load(replay_filename)
      logging.info('Loaded %d responses from %s',
                   len(http_archive), replay_filename)
    server_manager.AppendRecordCallback(http_archive.clear)

    ipfw_dns_host = None
    if options.dns_forwarding or options.shaping_dummynet:
      # compute the ip/host used for the DNS server and traffic shaping
      ipfw_dns_host = options.host
      if not ipfw_dns_host:
        ipfw_dns_host = platformsettings.get_server_ip_address(
            options.server_mode)

    real_dns_lookup = dnsproxy.RealDnsLookup(
        name_servers=[platformsettings.get_original_primary_nameserver()],
        dns_forwarding=options.dns_forwarding,
        proxy_host=ipfw_dns_host,
        proxy_port=options.dns_port)
    server_manager.AppendRecordCallback(real_dns_lookup.ClearCache)

    if options.dns_forwarding:
      if not options.server_mode and ipfw_dns_host == '127.0.0.1':
        AddDnsForward(server_manager, ipfw_dns_host)
      AddDnsProxy(server_manager, options, ipfw_dns_host, options.dns_port,
                  real_dns_lookup, http_archive)
    if options.ssl and options.https_root_ca_cert_path is None:
      options.https_root_ca_cert_path = os.path.join(os.path.dirname(__file__),
                                                     'wpr_cert.pem')
    http_proxy_address = options.host
    if not http_proxy_address:
      http_proxy_address = platformsettings.get_httpproxy_ip_address(
          options.server_mode)
    AddWebProxy(server_manager, options, http_proxy_address, real_dns_lookup,
                http_archive)
    AddTrafficShaper(server_manager, options, ipfw_dns_host)

  exit_status = 0
  try:
    server_manager.Run()
  except KeyboardInterrupt:
    logging.info('Shutting down.')
  except (dnsproxy.DnsProxyException,
          trafficshaper.TrafficShaperException,
          platformsettings.NotAdministratorError,
          platformsettings.DnsUpdateError) as e:
    logging.critical('%s: %s', e.__class__.__name__, e)
    exit_status = 1
  except Exception:
    logging.critical(traceback.format_exc())
    exit_status = 2

  if options.record:
    http_archive.Persist(replay_filename)
    logging.info('Saved %d responses to %s', len(http_archive), replay_filename)
  return exit_status
Exemplo n.º 3
0
def replay(options, replay_filename):
  if options.record and sys.version_info < (2, 7, 9):
    print ('Need Python 2.7.9 or greater for recording mode.\n'
           'For instructions on how to upgrade Python on Ubuntu 14.04, see:\n'
           'http://mbless.de/blog/2016/01/09/upgrade-to-python-2711-on-ubuntu-1404-lts.html\n')
    return 1
  if options.admin_check and options.IsRootRequired():
    platformsettings.rerun_as_administrator()
  configure_logging(options.log_level, options.log_file)
  server_manager = servermanager.ServerManager(options.record)
  if options.server:
    AddDnsForward(server_manager, options.server)
  else:
    real_dns_lookup = dnsproxy.RealDnsLookup(
        name_servers=[platformsettings.get_original_primary_nameserver()])
    if options.record:
      httparchive.HttpArchive.AssertWritable(replay_filename)
      if options.append and os.path.exists(replay_filename):
        http_archive = httparchive.HttpArchive.Load(replay_filename)
        logging.info('Appending to %s (loaded %d existing responses)',
                     replay_filename, len(http_archive))
      else:
        http_archive = httparchive.HttpArchive()
    else:
      http_archive = httparchive.HttpArchive.Load(replay_filename)
      logging.info('Loaded %d responses from %s',
                   len(http_archive), replay_filename)
    server_manager.AppendRecordCallback(real_dns_lookup.ClearCache)
    server_manager.AppendRecordCallback(http_archive.clear)

    ipfw_dns_host = None
    if options.dns_forwarding or options.shaping_dummynet:
      # compute the ip/host used for the DNS server and traffic shaping
      ipfw_dns_host = options.host
      if not ipfw_dns_host:
        ipfw_dns_host = platformsettings.get_server_ip_address(
            options.server_mode)

    if options.dns_forwarding:
      if not options.server_mode and ipfw_dns_host == '127.0.0.1':
        AddDnsForward(server_manager, ipfw_dns_host)
      AddDnsProxy(server_manager, options, ipfw_dns_host, options.dns_port,
                  real_dns_lookup, http_archive)
    if options.ssl and options.https_root_ca_cert_path is None:
      options.https_root_ca_cert_path = os.path.join(os.path.dirname(__file__),
                                                     'wpr_cert.pem')
    http_proxy_address = options.host
    if not http_proxy_address:
      http_proxy_address = platformsettings.get_httpproxy_ip_address(
          options.server_mode)
    AddWebProxy(server_manager, options, http_proxy_address, real_dns_lookup,
                http_archive)
    AddTrafficShaper(server_manager, options, ipfw_dns_host)

  exit_status = 0
  try:
    server_manager.Run()
  except KeyboardInterrupt:
    logging.info('Shutting down.')
  except (dnsproxy.DnsProxyException,
          trafficshaper.TrafficShaperException,
          platformsettings.NotAdministratorError,
          platformsettings.DnsUpdateError) as e:
    logging.critical('%s: %s', e.__class__.__name__, e)
    exit_status = 1
  except Exception:
    logging.critical(traceback.format_exc())
    exit_status = 2

  if options.record:
    http_archive.Persist(replay_filename)
    logging.info('Saved %d responses to %s', len(http_archive), replay_filename)
  return exit_status
Exemplo n.º 4
0
def replay(options, replay_filename):
  if options.admin_check and options.IsRootRequired():
    platformsettings.rerun_as_administrator()
  configure_logging(options.log_level, options.log_file)
  server_manager = servermanager.ServerManager(options.record)
  cache_misses = None
  if options.cache_miss_file:
    if os.path.exists(options.cache_miss_file):
      logging.warning('Cache Miss Archive file %s already exists; '
                      'replay will load and append entries to archive file',
                      options.cache_miss_file)
      cache_misses = cachemissarchive.CacheMissArchive.Load(
          options.cache_miss_file)
    else:
      cache_misses = cachemissarchive.CacheMissArchive(
          options.cache_miss_file)
  if options.server:
    AddDnsForward(server_manager, options.server)
  else:
    real_dns_lookup = dnsproxy.RealDnsLookup(
        name_servers=[platformsettings.get_original_primary_nameserver()])
    if options.record:
      httparchive.HttpArchive.AssertWritable(replay_filename)
      if options.append and os.path.exists(replay_filename):
        http_archive = httparchive.HttpArchive.Load(replay_filename)
        logging.info('Appending to %s (loaded %d existing responses)',
                     replay_filename, len(http_archive))
      else:
        http_archive = httparchive.HttpArchive()
    else:
      http_archive = httparchive.HttpArchive.Load(replay_filename)
      logging.info('Loaded %d responses from %s',
                   len(http_archive), replay_filename)
    server_manager.AppendRecordCallback(real_dns_lookup.ClearCache)
    server_manager.AppendRecordCallback(http_archive.clear)

    ipfw_dns_host = None
    if options.dns_forwarding or options.shaping_dummynet:
      # compute the ip/host used for the DNS server and traffic shaping
      ipfw_dns_host = options.host
      if not ipfw_dns_host:
        ipfw_dns_host = platformsettings.get_server_ip_address(
            options.server_mode)

    if options.dns_forwarding:
      if not options.server_mode and ipfw_dns_host == '127.0.0.1':
        AddDnsForward(server_manager, ipfw_dns_host)
      AddDnsProxy(server_manager, options, ipfw_dns_host, options.dns_port,
                  real_dns_lookup, http_archive)
    if options.ssl and options.https_root_ca_cert_path is None:
      options.https_root_ca_cert_path = os.path.join(os.path.dirname(__file__),
                                                     'wpr_cert.pem')
    http_proxy_address = options.host
    if not http_proxy_address:
      http_proxy_address = platformsettings.get_httpproxy_ip_address(
          options.server_mode)
    AddWebProxy(server_manager, options, http_proxy_address, real_dns_lookup,
                http_archive, cache_misses)
    AddTrafficShaper(server_manager, options, ipfw_dns_host)

  exit_status = 0
  try:
    server_manager.Run()
  except KeyboardInterrupt:
    logging.info('Shutting down.')
  except (dnsproxy.DnsProxyException,
          trafficshaper.TrafficShaperException,
          platformsettings.NotAdministratorError,
          platformsettings.DnsUpdateError) as e:
    logging.critical('%s: %s', e.__class__.__name__, e)
    exit_status = 1
  except:
    logging.critical(traceback.format_exc())
    exit_status = 2

  if options.record:
    http_archive.Persist(replay_filename)
    logging.info('Saved %d responses to %s', len(http_archive), replay_filename)
  if cache_misses:
    cache_misses.Persist()
    logging.info('Saved %d cache misses and %d requests to %s',
                 cache_misses.get_total_cache_misses(),
                 len(cache_misses.request_counts.keys()),
                 options.cache_miss_file)
  return exit_status
Exemplo n.º 5
0
def replay(options, replay_filename):
    if options.IsRootRequired():
        platformsettings.rerun_as_administrator()
    configure_logging(options.log_level, options.log_file)
    server_manager = servermanager.ServerManager(options.record)
    cache_misses = None
    if options.cache_miss_file:
        if os.path.exists(options.cache_miss_file):
            logging.warning(
                "Cache Miss Archive file %s already exists; " "replay will load and append entries to archive file",
                options.cache_miss_file,
            )
            cache_misses = cachemissarchive.CacheMissArchive.Load(options.cache_miss_file)
        else:
            cache_misses = cachemissarchive.CacheMissArchive(options.cache_miss_file)
    if options.server:
        AddDnsForward(server_manager, options.server)
    else:
        host = platformsettings.get_server_ip_address(options.server_mode)
        real_dns_lookup = dnsproxy.RealDnsLookup(name_servers=[platformsettings.get_original_primary_nameserver()])
        if options.record:
            httparchive.HttpArchive.AssertWritable(replay_filename)
            if options.append and os.path.exists(replay_filename):
                http_archive = httparchive.HttpArchive.Load(replay_filename)
                logging.info("Appending to %s (loaded %d existing responses)", replay_filename, len(http_archive))
            else:
                http_archive = httparchive.HttpArchive()
        else:
            http_archive = httparchive.HttpArchive.Load(replay_filename)
            logging.info("Loaded %d responses from %s", len(http_archive), replay_filename)
        server_manager.AppendRecordCallback(real_dns_lookup.ClearCache)
        server_manager.AppendRecordCallback(http_archive.clear)

        if options.dns_forwarding:
            if not options.server_mode:
                AddDnsForward(server_manager, host)
            AddDnsProxy(server_manager, options, host, real_dns_lookup, http_archive)
        if options.ssl and options.certfile is None:
            options.certfile = os.path.join(os.path.dirname(__file__), "wpr_cert.pem")
        http_proxy_address = platformsettings.get_httpproxy_ip_address(options.server_mode)
        AddWebProxy(server_manager, options, http_proxy_address, real_dns_lookup, http_archive, cache_misses)
        AddTrafficShaper(server_manager, options, host)

    exit_status = 0
    try:
        server_manager.Run()
    except KeyboardInterrupt:
        logging.info("Shutting down.")
    except (
        dnsproxy.DnsProxyException,
        trafficshaper.TrafficShaperException,
        platformsettings.NotAdministratorError,
        platformsettings.DnsUpdateError,
    ) as e:
        logging.critical("%s: %s", e.__class__.__name__, e)
        exit_status = 1
    except:
        logging.critical(traceback.format_exc())
        exit_status = 2

    if options.record:
        http_archive.Persist(replay_filename)
        logging.info("Saved %d responses to %s", len(http_archive), replay_filename)
    if cache_misses:
        cache_misses.Persist()
        logging.info(
            "Saved %d cache misses and %d requests to %s",
            cache_misses.get_total_cache_misses(),
            len(cache_misses.request_counts.keys()),
            options.cache_miss_file,
        )
    return exit_status