def _StartReplayServer(self): """Start the replay server and return the started local_ports.""" self._StopReplayServer() # In case it was already running. local_ports = self._wpr_port_pairs.local_ports self._wpr_server = webpagereplay.ReplayServer( self._archive_path, self.host_ip, local_ports.http, local_ports.https, local_ports.dns, self._ReplayCommandLineArgs()) return self._wpr_server.StartServer()
def _BringUpWpr(self): """Start the WPR server on the host and the forwarder on the device.""" print 'Starting WPR on host...' _DownloadFromCloudStorage(self._WPR_BUCKET, self._wpr_archive_hash) args = ['--use_closest_match'] if self._is_test_ca_installed: args.extend(['--should_generate_certs', '--https_root_ca_cert_path=' + self._wpr_ca_cert_path]) wpr_server = webpagereplay.ReplayServer(self._wpr_archive, '127.0.0.1', 0, 0, None, args) ports = wpr_server.StartServer()[:-1] self._wpr_server = wpr_server self._host_http_port = ports[0] self._host_https_port = ports[1]
def _WprHost(wpr_archive_path, record=False, network_condition_name=None, disable_script_injection=False, wpr_ca_cert_path=None): assert wpr_archive_path wpr_server_args = ['--use_closest_match'] if record: wpr_server_args.append('--record') if os.path.exists(wpr_archive_path): os.remove(wpr_archive_path) else: assert os.path.exists(wpr_archive_path) if network_condition_name: condition = emulation.NETWORK_CONDITIONS[network_condition_name] if record: logging.warning('WPR network condition is ignored when recording.') else: wpr_server_args.extend([ '--down', emulation.BandwidthToString(condition['download']), '--up', emulation.BandwidthToString(condition['upload']), '--delay_ms', str(condition['latency']), '--shaping_type', 'proxy' ]) if disable_script_injection: # Remove default WPR injected scripts like deterministic.js which # overrides Math.random. wpr_server_args.extend(['--inject_scripts', '']) if wpr_ca_cert_path: wpr_server_args.extend([ '--should_generate_certs', '--https_root_ca_cert_path=' + wpr_ca_cert_path ]) # Set up WPR server and device forwarder. wpr_server = webpagereplay.ReplayServer(wpr_archive_path, '127.0.0.1', 0, 0, None, wpr_server_args) http_port, https_port = wpr_server.StartServer()[:-1] logging.info('WPR server listening on HTTP=%s, HTTPS=%s (options=%s)' % (http_port, https_port, wpr_server_args)) try: yield http_port, https_port finally: wpr_server.StopServer()
def __init__(self, archive_path): """ Creates a WPR server using archive_path and pre-set arguments. @param archive_path: path to the .wpr archive to be used. """ port = utils.get_unused_port() self._http_port = port if port else 8080 port = utils.get_unused_port() self._https_port = port if port else 8713 self._server = webpagereplay.ReplayServer( archive_path=archive_path, replay_host=WebPageReplayWrapper._REPLAY_HOST, http_port=self._http_port, https_port=self._https_port, dns_port=None, replay_options=[])
def WprHost(device, wpr_archive_path, record=False, network_condition_name=None, disable_script_injection=False): """Launches web page replay host. Args: device: Android device. wpr_archive_path: host sided WPR archive's path. network_condition_name: Network condition name available in chrome_setup.NETWORK_CONDITIONS. record: Enables or disables WPR archive recording. Returns: Additional flags list that may be used for chromium to load web page through the running web page replay host. """ assert device if wpr_archive_path == None: assert not record, 'WPR cannot record without a specified archive.' assert not network_condition_name, ('WPR cannot emulate network condition' + ' without a specified archive.') yield [] return wpr_server_args = ['--use_closest_match'] if record: wpr_server_args.append('--record') if os.path.exists(wpr_archive_path): os.remove(wpr_archive_path) else: assert os.path.exists(wpr_archive_path) if network_condition_name: condition = chrome_setup.NETWORK_CONDITIONS[network_condition_name] if record: logging.warning('WPR network condition is ignored when recording.') else: wpr_server_args.extend([ '--down', chrome_setup.BandwidthToString(condition['download']), '--up', chrome_setup.BandwidthToString(condition['upload']), '--delay_ms', str(condition['latency']), '--shaping_type', 'proxy']) if disable_script_injection: # Remove default WPR injected scripts like deterministic.js which # overrides Math.random. wpr_server_args.extend(['--inject_scripts', '']) # Deploy certification authority to the device. temp_certificate_dir = tempfile.mkdtemp() wpr_ca_cert_path = os.path.join(temp_certificate_dir, 'testca.pem') certutils.write_dummy_ca_cert(*certutils.generate_dummy_ca_cert(), cert_path=wpr_ca_cert_path) device_cert_util = adb_install_cert.AndroidCertInstaller( device.adb.GetDeviceSerial(), None, wpr_ca_cert_path) device_cert_util.install_cert(overwrite_cert=True) wpr_server_args.extend(['--should_generate_certs', '--https_root_ca_cert_path=' + wpr_ca_cert_path]) # Set up WPR server and device forwarder. wpr_server = webpagereplay.ReplayServer(wpr_archive_path, '127.0.0.1', 0, 0, None, wpr_server_args) ports = wpr_server.StartServer()[:-1] host_http_port = ports[0] host_https_port = ports[1] forwarder.Forwarder.Map([(0, host_http_port), (0, host_https_port)], device) device_http_port = forwarder.Forwarder.DevicePortForHostPort(host_http_port) device_https_port = forwarder.Forwarder.DevicePortForHostPort(host_https_port) try: yield [ '--host-resolver-rules="MAP * 127.0.0.1,EXCLUDE localhost"', '--testing-fixed-http-port={}'.format(device_http_port), '--testing-fixed-https-port={}'.format(device_https_port)] finally: forwarder.Forwarder.UnmapDevicePort(device_http_port, device) forwarder.Forwarder.UnmapDevicePort(device_https_port, device) wpr_server.StopServer() # Remove certification authority from the device. device_cert_util.remove_cert() shutil.rmtree(temp_certificate_dir)
def _WprHost(wpr_archive_path, record=False, network_condition_name=None, disable_script_injection=False, wpr_ca_cert_path=None, out_log_path=None): assert wpr_archive_path def PathWorkaround(path): # webpagereplay.ReplayServer is doing a os.path.exist(os.path.dirname(p)) # that fails if p = 'my_file.txt' because os.path.dirname(p) = '' != '.'. # This workaround just sends absolute path to work around this bug. return os.path.abspath(path) wpr_server_args = ['--use_closest_match'] if record: wpr_server_args.append('--record') if os.path.exists(wpr_archive_path): os.remove(wpr_archive_path) else: assert os.path.exists(wpr_archive_path) if network_condition_name: condition = emulation.NETWORK_CONDITIONS[network_condition_name] if record: logging.warning('WPR network condition is ignored when recording.') else: wpr_server_args.extend([ '--down', emulation.BandwidthToString(condition['download']), '--up', emulation.BandwidthToString(condition['upload']), '--delay_ms', str(condition['latency']), '--shaping_type', 'proxy' ]) if disable_script_injection: # Remove default WPR injected scripts like deterministic.js which # overrides Math.random. wpr_server_args.extend(['--inject_scripts', '']) if wpr_ca_cert_path: wpr_server_args.extend([ '--should_generate_certs', '--https_root_ca_cert_path=' + PathWorkaround(wpr_ca_cert_path) ]) if out_log_path: # --log_level debug to extract the served URLs requests from the log. wpr_server_args.extend([ '--log_level', 'debug', '--log_file', PathWorkaround(out_log_path) ]) # Don't append to previously existing log. if os.path.exists(out_log_path): os.remove(out_log_path) # Set up WPR server and device forwarder. wpr_server = webpagereplay.ReplayServer(PathWorkaround(wpr_archive_path), '127.0.0.1', 0, 0, None, wpr_server_args) http_port, https_port = wpr_server.StartServer()[:-1] logging.info('WPR server listening on HTTP=%s, HTTPS=%s (options=%s)' % (http_port, https_port, wpr_server_args)) try: yield http_port, https_port finally: wpr_server.StopServer()
def _ReplayServer(self, archive_path, host_ip, http_port, https_port, dns_port, wpr_args): return webpagereplay.ReplayServer(archive_path, host_ip, http_port, https_port, dns_port, wpr_args)