def single_setup(socks_port, run_cmd_over_tor): global tb_dir, tb_launcher, userjs_dir client = None tb_proc = None set_port(socks_port) # launch tor browser tb_launcher_fullpath = os.path.join(tb_dir, tb_launcher) userjs_fulldir = os.path.join(tb_dir, userjs_dir) if not os.path.isfile(tb_launcher_fullpath): exit( 'Tor browser launcher not found: %s, change variables tb_dir and tb_launcher and try again.' % tb_launcher_fullpath) if (not os.path.isdir(tb_dir)) or (not os.path.isdir(userjs_fulldir)): exit( 'Tor browser profile directory not found: %s, change variables tb_dir and userjs_dir and try again.' % tb_dir) # launch with marionette support and custom starup url # (when using your own tor instance the default startpage reports a problem) tb_proc = subprocess.Popen([ tb_launcher_fullpath, '--class', 'Tor Browser', '-profile', userjs_fulldir, '-marionette', '-url', 'about:blank', '-foreground' ]) # wait until browser has started listening on marionette port sleep(2) # connect to marionette client = Marionette('localhost', port=2828, socket_timeout=360) client.start_session() log.debug("tor browser started.") log.debug('default pageload timeout: %s' % str(client.timeout.page_load)) # in ms. default 60s. client.set_page_load_timeout(360000) client.timeout.page_load = 360 log.debug('set timeout to: %s' % str(client.timeout.page_load)) log.debug("marionette created.") return client, tb_proc
def _open_with_timeout(browser, page, timeout=config.DURATION_LIMIT, burst_wait=3, bridge=None): '''navigates browser to url while capturing the packet dump, aborts after timeout. If bridge, that is the IP address of the connected bridge, just capture traffic to there (need to set this by hand) ''' client = Marionette('localhost', port=2828, socket_timeout=(timeout)) try: client.start_session() client.set_page_load_timeout((timeout) * 1000) except socket.timeout: _kill(browser) raise (url, domain) = _normalize_url(page) (tshark_process, file_name) = _open_packet_dump(domain, bridge) # thread = threading.Thread(target=client.navigate, args=(url,)) thread = threading.Thread(target=_navigate_or_fail, args=(client, url, file_name)) thread.daemon = True thread.start() # todo: this is code duplication for both _open functions start = time.time() while thread.is_alive(): time.sleep(.1) # will this still happen after the timeout is set like above? if time.time() - start > timeout + 30: _handle_exception("aborted after timeout2", file_name, client) _kill(browser, tshark_process) raise SystemExit("download aborted after timeout") time.sleep(burst_wait) _kill(browser, tshark_process)