def get_server_rtt(self, server): """Retrieves the round trip time (rtt) to the server Args: server: the hostname of the server Returns: round trip time to the server in seconds, or 0 if unavailable """ if server not in self.server_rtt: platform_settings = platformsettings.get_platform_settings() self.server_rtt[server] = platform_settings.ping(server) return self.server_rtt[server]
def __init__( self, dont_use=None, host="127.0.0.1", port="80", ssl_port="443", dns_port="53", up_bandwidth="0", down_bandwidth="0", delay_ms="0", packet_loss_rate="0", init_cwnd="0", use_loopback=True, ): """Start shaping traffic. Args: host: a host string (name or IP) for the web proxy. port: a port string (e.g. '80') for the web proxy. ssl_port: a port string (e.g. '443') for the SSL web proxy. dns_port: a port string for the dns proxy (for unit testing). up_bandwidth: Upload bandwidth down_bandwidth: Download bandwidth Bandwidths measured in [K|M]{bit/s|Byte/s}. '0' means unlimited. delay_ms: Propagation delay in milliseconds. '0' means no delay. packet_loss_rate: Packet loss rate in range [0..1]. '0' means no loss. init_cwnd: the initial cwnd setting. '0' means no change. use_loopback: True iff shaping is done on the loopback (or equiv) adapter. """ assert dont_use is None # Force args to be named. self.platformsettings = platformsettings.get_platform_settings() self.host = host self.port = port self.ssl_port = ssl_port self.dns_port = dns_port self.up_bandwidth = up_bandwidth self.down_bandwidth = down_bandwidth self.delay_ms = delay_ms self.packet_loss_rate = packet_loss_rate self.init_cwnd = init_cwnd self.use_loopback = use_loopback if not self._BANDWIDTH_RE.match(self.up_bandwidth): raise BandwidthValueError(self.up_bandwidth) if not self._BANDWIDTH_RE.match(self.down_bandwidth): raise BandwidthValueError(self.down_bandwidth) self.is_shaping = False
def __init__(self, dont_use=None, host='127.0.0.1', port='80', ssl_port='443', dns_port='53', up_bandwidth='0', down_bandwidth='0', delay_ms='0', packet_loss_rate='0', init_cwnd='0', use_loopback=True): """Start shaping traffic. Args: host: a host string (name or IP) for the web proxy. port: a port string (e.g. '80') for the web proxy. ssl_port: a port string (e.g. '443') for the SSL web proxy. dns_port: a port string for the dns proxy (for unit testing). up_bandwidth: Upload bandwidth down_bandwidth: Download bandwidth Bandwidths measured in [K|M]{bit/s|Byte/s}. '0' means unlimited. delay_ms: Propagation delay in milliseconds. '0' means no delay. packet_loss_rate: Packet loss rate in range [0..1]. '0' means no loss. init_cwnd: the initial cwnd setting. '0' means no change. use_loopback: True iff shaping is done on the loopback (or equiv) adapter. """ assert dont_use is None # Force args to be named. self.platformsettings = platformsettings.get_platform_settings() self.host = host self.port = port self.ssl_port = ssl_port self.dns_port = dns_port self.up_bandwidth = up_bandwidth self.down_bandwidth = down_bandwidth self.delay_ms = delay_ms self.packet_loss_rate = packet_loss_rate self.init_cwnd = init_cwnd self.use_loopback = use_loopback if not self._BANDWIDTH_RE.match(self.up_bandwidth): raise BandwidthValueError(self.up_bandwidth) if not self._BANDWIDTH_RE.match(self.down_bandwidth): raise BandwidthValueError(self.down_bandwidth) self.is_shaping = False
def __init__(self, use_forwarding, passthrough_filter=None, host='', port=53, handler=UdpDnsHandler): """Initialize DnsProxyServer. Args: use_forwarding: a boolean that if true, changes primary DNS to host. passthrough_filter: a function that resolves a host to its real IP, or None, if it should resolve to the dnsproxy's address. host: a host string (name or IP) to bind the dns proxy and to which DNS requests will be resolved. port: an integer port on which to bind the proxy. """ self.use_forwarding = use_forwarding self.passthrough_filter = passthrough_filter or (lambda host: None) self.platform_settings = platformsettings.get_platform_settings() try: SocketServer.ThreadingUDPServer.__init__( self, (host, port), handler) except socket.error, (error_number, msg): if error_number == errno.EACCES: raise DnsProxyException( 'Unable to bind DNS server on (%s:%s)' % (host, port)) raise
def __init__(self, dont_use=None, host='127.0.0.1', port='80', dns_port='53', up_bandwidth='0', down_bandwidth='0', delay_ms='0', packet_loss_rate='0', init_cwnd='0'): """Start shaping traffic. Args: host: a host string (name or IP) for the web proxy. port: a port string (e.g. '80') for the web proxy. dns_port: a port string for the dns proxy (for unit testing). up_bandwidth: Upload bandwidth down_bandwidth: Download bandwidth Bandwidths measured in [K|M]{bit/s|Byte/s}. '0' means unlimited. delay_ms: Propagation delay in milliseconds. '0' means no delay. packet_loss_rate: Packet loss rate in range [0..1]. '0' means no loss. init_cwnd: the initial cwnd setting. '0' means no change. """ assert dont_use is None # Force args to be named. self.platformsettings = platformsettings.get_platform_settings() self.host = host self.port = port self.dns_port = dns_port self.up_bandwidth = up_bandwidth self.down_bandwidth = down_bandwidth self.delay_ms = delay_ms self.packet_loss_rate = packet_loss_rate self.init_cwnd = init_cwnd if not self._BANDWIDTH_RE.match(self.up_bandwidth): raise BandwidthValueError(self.up_bandwidth) if not self._BANDWIDTH_RE.match(self.down_bandwidth): raise BandwidthValueError(self.down_bandwidth)
def main(options, replay_filename): exit_status = 0 platform_settings = platformsettings.get_platform_settings() if options.server: resolve_dns_to_remote_replay_server(platform_settings, options.server) return exit_status host = platform_settings.get_server_ip_address(options.server_mode) web_server_class = httpproxy.HttpProxyServer web_server_kwargs = { 'host': host, 'port': options.port, } if options.spdy: assert not options.record, 'spdy cannot be used with --record.' web_server_class = replayspdyserver.ReplaySpdyServer web_server_kwargs['use_ssl'] = options.spdy != 'no-ssl' web_server_kwargs['certfile'] = options.certfile web_server_kwargs['keyfile'] = options.keyfile if options.record: http_archive = httparchive.HttpArchive() http_archive.AssertWritable(replay_filename) else: http_archive = httparchive.HttpArchive.Load(replay_filename) logging.info('Loaded %d responses from %s', len(http_archive), replay_filename) custom_handlers = customhandlers.CustomHandlers(options.screenshot_dir) real_dns_lookup = dnsproxy.RealDnsLookup() if options.record: http_archive_fetch = httpclient.RecordHttpArchiveFetch( http_archive, real_dns_lookup, options.deterministic_script) else: http_archive_fetch = httpclient.ReplayHttpArchiveFetch( http_archive, options.diff_unknown_requests) dns_passthrough_filter = None if options.dns_private_passthrough: skip_passthrough_hosts = set(request.host for request in http_archive) dns_passthrough_filter = dnsproxy.DnsPrivatePassthroughFilter( real_dns_lookup, skip_passthrough_hosts) dns_class = dnsproxy.DummyDnsServer if options.dns_forwarding: dns_class = dnsproxy.DnsProxyServer try: with dns_class(options.dns_forwarding, dns_passthrough_filter, host): with web_server_class(http_archive_fetch, custom_handlers, **web_server_kwargs): with trafficshaper.TrafficShaper( host=host, port=options.shaping_port, up_bandwidth=options.up, down_bandwidth=options.down, delay_ms=options.delay_ms, packet_loss_rate=options.packet_loss_rate, init_cwnd=options.init_cwnd): while True: time.sleep(1) except KeyboardInterrupt: logging.info('Shutting down.') except (dnsproxy.DnsProxyException, trafficshaper.TrafficShaperException) as e: logging.critical(e) exit_status = 1 except: print 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
# See the License for the specific language governing permissions and # limitations under the License. """Retrieve web resources over http.""" import copy import httparchive import httplib import logging import os import platformsettings import re import util HTML_RE = re.compile(r'^.{,256}?<html.*?>', re.IGNORECASE | re.DOTALL) HEAD_RE = re.compile(r'^.{,256}?<head.*?>', re.IGNORECASE | re.DOTALL) TIMER = platformsettings.get_platform_settings().timer class HttpClientException(Exception): """Base class for all exceptions in httpclient.""" pass def GetInjectScript(scripts): """Loads |scripts| from disk and returns a string of their content.""" lines = [] for script in scripts: if os.path.exists(script): lines += open(script).read() elif util.resource_exists(script): lines += util.resource_string(script)
def replay(options, replay_filename): platform_settings = platformsettings.get_platform_settings() if options.IsRootRequired(): platform_settings.rerun_as_administrator() configure_logging(platform_settings, 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, platform_settings, options.server) else: host = platform_settings.get_server_ip_address(options.server_mode) real_dns_lookup = dnsproxy.RealDnsLookup( name_servers=[platform_settings.get_original_primary_dns()]) if options.record: http_archive = httparchive.HttpArchive() http_archive.AssertWritable(replay_filename) 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, platform_settings, host) AddDnsProxy(server_manager, options, host, real_dns_lookup, http_archive) if options.ssl and options.certfile is None: options.certfile = platform_settings.get_certfile_name() server_manager.AppendStartStopFunctions( [platform_settings.create_certfile, options.certfile], [os.unlink, options.certfile]) AddWebProxy(server_manager, options, host, 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
def __init__(self, name_servers=None): self.resolver = dns.resolver.get_default_resolver() self.resolver.nameservers = [ platformsettings.get_platform_settings().get_original_primary_dns()] self.dns_cache_lock = threading.Lock() self.dns_cache = {}
def setUp(self): platform_settings = platformsettings.get_platform_settings() self.host = platform_settings.get_server_ip_address() self.dns_port = TEST_DNS_PORT self.timer = TIMER
def setUp(self): platform_settings = platformsettings.get_platform_settings() self.host = platform_settings.get_server_ip_address() self.port = TEST_HTTP_PORT self.tcp_socket_creator = TcpTestSocketCreator(self.host, self.port) self.timer = TIMER
""" import daemonserver import logging import multiprocessing import platformsettings import socket import SocketServer import trafficshaper import unittest RESPONSE_SIZE_KEY = 'response-size:' TEST_DNS_PORT = 5555 TEST_HTTP_PORT = 8888 TIMER = platformsettings.get_platform_settings().timer def GetElapsedMs(start_time, end_time): """Return milliseconds elapsed between |start_time| and |end_time|. Args: start_time: seconds as a float (or string representation of float). end_time: seconds as a float (or string representation of float). Return: milliseconds elapsed as integer. """ return int((float(end_time) - float(start_time)) * 1000) class TrafficShaperTest(unittest.TestCase):