def startTorProxy(self): """ start tor and open SocksPort proxy, after checkTorStatus function """ limit_try = 0 while True: try: self.checkTorStatus() tor_process.launch_tor_with_config( config={ 'SocksPort': str(self.socks_port), 'ControlPort': str(self.control_port), }, init_msg_handler=self.printBootstrapLines, timeout=300) logging.info('Start tor success, socks_port : {}'.format( self.socks_port)) break except Exception as e: logging.info(e) if limit_try == 10: logging.info('Init tor fail.') break limit_try += 1 logging.info( 'Start tor fail, limit try : {}'.format(limit_try)) continue
def ConfigureTor(self): def print_bootstrap_lines(line): print('{a}'.format(a=line)) if "Bootstrapped " in line: print(line) #print(term.format(line,'Green')) os.system('TASKKILL /F /IM tor.exe') print("存入代理,启动TOR") try: tor_process.launch_tor_with_config( tor_cmd=os.getcwd() + '\\Tor\\tor.exe', config={ 'SocksPort': '9050', 'ControlPort': '9051', 'DataDirectory': os.getcwd() + '\\Tor', 'GeoIPFile': os.getcwd() + '\\Tor\\geoip', 'GeoIPv6File': os.getcwd() + '\\Tor\\geoip6', 'Log notice file': os.getcwd() + '\\notices.log', 'HashedControlPassword': '******', 'CookieAuthentication': '1', 'HiddenServiceStatistics': '0', 'Log notice': 'stdout', 'MaxCircuitDirtiness': '60', 'Socks5Proxy': random.choice(self._torproxy) }, init_msg_handler=print_bootstrap_lines) except: self._torproxy = self.shodan_torsocks() self.ConfigureTor
def _startTorProxy(self): # self._checkTorStatus() tor_process.launch_tor_with_config( config={ 'SocksPort': str(self.socks_port), 'ControlPort': str(self.control_port), }, init_msg_handler=self._print_bootstrap_lines, timeout=300) print('Start tor success')
def _launch_tor(): return launch_tor_with_config( config={ 'SOCKSPort': str(proxy_port), 'ControlPort': str(ctrl_port) }, take_ownership=True)
def __init__(self, control_port, socks_port, data_directory, *args, circuit_number=0, **kwargs): super().__init__(*args, **kwargs) self._destroyed = False self._loop = asyncio.get_event_loop() self.socks_port = socks_port config = { 'ControlPort': str(control_port), 'SOCKSPort': str(socks_port), 'DataDirectory': data_directory, '__LeaveStreamsUnattached': '1', 'NewCircuitPeriod': '31536000', 'MaxCircuitDirtiness': '31536000', 'CookieAuthentication': '1', 'FetchDirInfoEarly': '1', 'FetchDirInfoExtraEarly': '1', 'FetchUselessDescriptors': '1', 'UseMicrodescriptors': '0', 'DownloadExtraInfo': '1', } self._tor_process = process.launch_tor_with_config(config) self._controller = control.Controller.from_port(port=control_port) self._controller.authenticate() self._controller.set_conf('__DisablePredictedCircuits', '1') stem.util.log.get_logger().setLevel(logging.ERROR) self._exit_nodes = None self._relay_nodes = None self._selected_circuit_id = 0 self._circuit_ids = {} self._circuit_creation_semaphore = asyncio.Semaphore(value=self.MAX_SIMULTANEOUS_CIRCUIT_REQUESTS) self._circuits_available_event = asyncio.Event() self._controller.add_event_listener(self._handle_new_circuit, control.EventType.CIRC) self._controller.add_event_listener(self._attach_stream, control.EventType.STREAM) asyncio.ensure_future(self._init_coro(circuit_number))
def __launch_tor_with_config(self, tor_config, tor_cmd): if not tor_config: tor_config = { 'SocksPort': str(self.socks_port), 'ControlPort': str(self.controller_port) } if not tor_cmd: if self.verbose: print( str(datetime.now()) + ' - ' + term.format( '''Missing tor_cmd argument. You need to specify TOR's location in your system''', term.Color.RED)) print( str(datetime.now()) + ' - ' + term.format( '''We will try using the default value, but it might fail''', term.Color.RED)) tor_cmd = 'tor' if self.verbose: init_msg_handler = self.__print_init_msg else: init_msg_handler = None return launch_tor_with_config(tor_cmd=tor_cmd, config=tor_config, init_msg_handler=init_msg_handler, take_ownership=True)
def __init__(self, take_ownership=True, # Tor dies when the Crawler does torrc_config={"CookieAuth": "1"}, tor_log="/var/log/tor/tor.log", tor_cell_log="/var/log/tor/tor_cell_seq.log", control_port=9051, socks_port=9050, run_in_xvfb=True, tbb_path=join("/opt","tbb","tor-browser_en-US"), tb_log_path=join(_log_dir,"firefox.log"), tb_tor_cfg=USE_RUNNING_TOR, page_load_timeout=20, wait_on_page=5, wait_after_closing_circuits=0, restart_on_sketchy_exception=True, additional_control_fields={}, db_handler=None): self.logger = setup_logging(_log_dir, "crawler") self.torrc_config = torrc_config self.socks_port = find_free_port(socks_port, control_port) self.torrc_config.update({"SocksPort": str(self.socks_port)}) self.control_port = find_free_port(control_port, self.socks_port) self.torrc_config.update({"ControlPort": str(self.control_port)}) self.torrc_config.update({"Log": "INFO file {}".format(tor_log)}) self.logger.info("Starting tor process with config " "{torrc_config}.".format(**locals())) self.tor_process = launch_tor_with_config(config=self.torrc_config, take_ownership=take_ownership) self.authenticate_to_tor_controlport() self.logger.info("Opening cell log stream...") self.cell_log = open(tor_cell_log, "rb") if run_in_xvfb: self.logger.info("Starting Xvfb...") self.run_in_xvfb = True self.virtual_framebuffer = start_xvfb() self.logger.info("Starting Tor Browser...") self.tb_driver = TorBrowserDriver(tbb_path=tbb_path, tor_cfg=tb_tor_cfg, tbb_logfile_path=tb_log_path, socks_port=self.socks_port, control_port=self.control_port) self.wait_after_closing_circuits = wait_after_closing_circuits self.page_load_timeout = page_load_timeout self.tb_driver.set_page_load_timeout(page_load_timeout) self.wait_on_page = wait_on_page self.restart_on_sketchy_exception = restart_on_sketchy_exception self.control_data = self.get_control_data(page_load_timeout, wait_on_page, wait_after_closing_circuits, additional_control_fields) self.db_handler = db_handler if db_handler: self.crawlid = self.db_handler.add_crawl(self.control_data)
def __init__(self, tor=False, tor_tries=10, save_directory="data", url_delay=10, random_pages=False, test=False): self.tor = tor self.tor_tries = tor_tries self.save_directory = save_directory self.url_delay = url_delay self.random_pages = random_pages self.sites = ["amazon", "apple", "nytimes", "google", "duckduckgo"] pathlib.Path(self.save_directory).mkdir(parents=True, exist_ok=True) self.display = SmartDisplay(backend='xephyr', visible=1, size=(1920, 1080)) self.display.start() self.firefox_profile_path = tempfile.mkdtemp() self.firefox_profile = webdriver.FirefoxProfile( self.firefox_profile_path) if self.tor: for i in range(self.tor_tries): try: self.socks_port = free_port() self.control_port = free_port() self.tor_data_dir = tempfile.mkdtemp() self.torrc = { 'ControlPort': str(self.control_port), 'SOCKSPort': str(self.socks_port), 'DataDirectory': self.tor_data_dir, 'EntryNodes': '{us}', 'ExitNodes': '{us}', 'StrictNodes': '1' } self.tor_process = launch_tor_with_config( config=self.torrc, tor_cmd="/usr/sbin/tor") except: print("Tor connection attempt {} failed.".format(i)) sys.stdout.flush() if i == self.tor_tries - 1: sys.exit() continue break self.firefox_profile.set_preference("network.proxy.type", 1) self.firefox_profile.set_preference("network.proxy.socks", "localhost") self.firefox_profile.set_preference("network.proxy.socks_port", self.socks_port) self.firefox_profile.set_preference("network.proxy.socks_version", 5) self.firefox_profile.update_preferences() self.driver = webdriver.Firefox(service_log_path=os.path.join( self.firefox_profile_path, "geckodriver.log"), firefox_profile=self.firefox_profile) self.driver.maximize_window() if test: self.driver.get("https://www.{}.com/".format( random.choice(self.sites))) time.sleep(self.url_delay)
def _launch_tor(self): return launch_tor_with_config( config={ 'SocksPort': str(self.proxy_port), 'ControlPort': str(self.ctrl_port) }, take_ownership=True)
def __enter__(self): """ Run at the beginning of `with` Starts tor """ # Start tor if self.mode == "tor": # Calculate the timeout based on which run # If the first run flag is set, unset it timeout = None if self.first_run: timeout = self.tor_timeouts["initial"] self.first_run = False else: timeout = self.tor_timeouts["regular"] # Launch the tor process self.tor_process = stem_process.launch_tor_with_config( config=self.tor_config, tor_cmd=self.tor_executable_path, timeout=timeout, take_ownership=True, # init_msg_handler = print ) # Return nothing return self
def start(self, mode: str): """ Starts tor if the given mode is tor """ self.logger.info("Starting the proxy in %s mode", mode) # Store the mode self.mode = mode # Start tor if mode == "tor": # Calculate the timeout based on which run # If the first run flag is set, unset it timeout = None if self.first_tor_run: timeout = self.tor_timeouts["initial"] self.first_tor_run = False else: timeout = self.tor_timeouts["regular"] self.logger.info("Starting TOR") # Launch the tor process self.tor_process = stem_process.launch_tor_with_config( config=self.tor_config, tor_cmd=str(self.tor_executable_path), timeout=timeout, take_ownership=True, # init_msg_handler = print ) elif mode == "normal": self.logger.info("Starting null proxy") else: raise Exception(f"Invalid mode: {mode}") self.logger.info("Started the proxy") # Return nothing return self
def __init__(self, port=9080, cont_port=9081): self._port, self._cont_port = port, cont_port self._tor_process = process.launch_tor_with_config( config={'SocksPort': str(port), 'ControlPort': str(cont_port)}) self._controller = control.Controller.from_port(port=cont_port) self._controller.authenticate() self.setsocket()
def _create_tor_proxy(self): while True: try: self.port = get_port(9000, 9999) self.control_port = get_port(9000, 9999) self.logger.info( "Attempting to open Tor proxy (socks: {}, control: {})". format(self.port, self.control_port)) self.tor_process = launch_tor_with_config( config={ 'SocksPort': str(self.port), 'ControlPort': str(self.control_port), 'ExitNodes': '{us}', # Only use USA IP's - Avoid looking like offshore mining. 'SafeSocks': '1', 'CookieAuthentication': '1', # We want cookie auth 'DataDirectory': '/var/lib/tor/{}'.format(self.identifier) } # We can't use a timeout UNLESS we're calling from the main thread. #timeout = 20 ) break except OSError as e: continue
def start_tor(socks: int, socks_port: int, control_port: int): control_ports = [] socks_ports = [] num_of_socks = range(socks) for i in num_of_socks: control_ports.append(f"{control_port + i}") socks_ports.append(f"{socks_port + i}") process.launch_tor_with_config(config={ 'ControlPort': control_ports, 'SocksPort': socks_ports, 'DNSPort': '53', 'DNSListenAddress': '0.0.0.0' })
def __init__(self, ports, privateKey=None): torControlPassword = randomPassword() torControlPasswordHashed = hashPassword(torControlPassword) self.tor = launch_tor_with_config(config={ "ControlPort": [ports.torControl], "HashedControlPassword": [torControlPasswordHashed], "SOCKSPort": [ports.torProxy], "SOCKSPolicy": [ "accept 127.0.0.1", "reject *", ], }, take_ownership=True) self.controller = Controller.from_port(port=ports.torControl) self.controller.authenticate(password=torControlPassword) options = {"key_type": "ED25519-V3", "key_content": "ED25519-V3"} if privateKey: options["key_type"], options["key_content"] = privateKey.split(":") response = self.controller.create_ephemeral_hidden_service( {80: ports.torHiddenService}, **options) self.serviceID = response.service_id print(self.serviceID)
def run(self): self.logger.debug("[%05d] Executing Tor Process" % self.id) try: self.process = launch_tor_with_config( config={ "ControlPort": str(self.control_port), "SOCKSPort": str(self.socks_port), "DataDirectory": self.data_directory, "AllowSingleHopCircuits": "1", "ExcludeSingleHopRelays": "0", **self.tor_config }, tor_cmd=self.tor_cmd, timeout=self.tor_timeout or None, init_msg_handler=self.print_bootstrapped_line) except Exception as e: self.logger.error("[%05d] Failed To Launch Tor Process: %s" % (self.id, str(e))) self.__is_shutdown = True return self self.logger.debug("[%05d] Creating Tor Controller" % self.id) self.controller = Controller.from_port(port=self.control_port) self.controller.authenticate() return self
def _setUp_tor(self): c_port = 9050 + int(self.miner_id) * 2 config = { "DataDirectory": f"./tordata{c_port}", 'ControlPort': f'{c_port}', "SocksPort": f"{c_port + 1}" } return process.launch_tor_with_config(config, tor_cmd=r"tor")
def launch(tor_bin: str, tor_datadir: str, torrc_extra: str) -> Optional[Controller]: ''' Launch and connect to Tor, returning the :class:`stem.control.Controller` on success, or ``None`` on failure. :param tor_bin: How to execute tor. I.e. either "tor" or "./path/to/tor" :param tor_datadir: DataDirectory to use :param torrc_extra: Extra arbitrary lines to add to the torrc we use ''' ''' Launch and connect to Tor using the given tor binary (or path to tor binary) and using the given Tor DataDirectory. Returns an authenticated stem Controller object when successful. If any error occurs, this module logs about it and returns ``None``. ''' opj = os.path.join os.makedirs(tor_datadir, mode=0o700, exist_ok=True) # Get a copy of the starting torrc without any dynamic options torrc = copy.deepcopy(TORRC_BASE) # Save a copy of this as it will be used a few times in this function sock_path = os.path.abspath(opj(tor_datadir, 'control')) # Update the torrc with everything that depends on runtime config torrc.update({ 'DataDirectory': tor_datadir, 'PidFile': opj(tor_datadir, 'tor.pid'), 'ControlSocket': sock_path, 'Log': ['NOTICE file ' + opj(tor_datadir, 'notice.log')], }) torrc = _parse_torrc_str(torrc_extra, torrc) # log.debug(torrc) # Blocks while launching Tor try: launch_tor_with_config(torrc, tor_cmd=tor_bin, init_msg_handler=log.debug, take_ownership=True) except OSError as e: log.error('Problem launching Tor: %s', e) return None c = _connect(sock_path) if c is None: log.error('Unable to connect to Tor') return None assert isinstance(c, Controller) log.info('Started and connected to Tor %s via %s', c.get_version(), sock_path) return c
def prox(): # test tor_process = process.launch_tor_with_config( config={ 'SocksPort': '8800', 'ControlPort': '8801', 'DataDirectory': '/temp/torserv' }, init_msg_handler=print_bootstrap_lines) return tor_process
def start_tor(): tor_process = process.launch_tor_with_config( config = { 'SocksPort': str(SOCKS_PORT), 'ControlPort': str(9051), 'ExitNodes': '{ru}', }, init_msg_handler = print_bootstrap_lines, ) return tor_process
def _start_tor(self): self.logger.debug("function was called") return launch_tor_with_config( config={ 'SocksPort': str(self.tor_socks_port), 'ControlPort': str(self.tor_ctrl_port) }, take_ownership=True, init_msg_handler=self.tor_messages, )
def get_nodes(): """Return network statuses from Tor consensus.""" nodes = [] p = process.launch_tor_with_config({'ControlPort': CONTROL_PORT}) with control.Controller.from_port(port=int(CONTROL_PORT)) as c: c.authenticate() for s in c.get_network_statuses(): nodes.append(((s.address, s.or_port), s.fingerprint, s.flags)) p.kill() return nodes
def connect(self, socks_port=9050, control_port=9051): '''Returns controller for new or existing tor process.''' # create a data folder, path format tor_data/torXXXX where # XXXX is the socks_port # data_directory = str(socks_port) if not os.path.exists('tor_data'): os.mkdir('tor_data') if not os.path.exists('tor_data/' + data_directory): os.mkdir('tor_data/' + data_directory) # hash the control password, start+return the subprocess hashed_password = self.hash_password(self.PASSWORD) config = { 'SocksPort': [socks_port], 'ControlPort': [control_port], 'Datadirectory': 'tor_data/' + data_directory, 'HashedControlPassword': hashed_password } try: launch_tor_with_config(config, timeout=None) except OSError: # unable to connect to 9050, eg, tor is running pass return _CurrentConnectionController(control_port, self.PASSWORD)
def start_tor(self): """Запуск сервера tor""" self.tor_process = launch_tor_with_config( config={ 'SocksPort': str(self.socks_port), 'ControlPort': str(self.control_port), # 'GeoIPFile': r'C:\tor-win32-0.2.8.9\Data\Tor\geoip', # 'GeoIPv6File': r'C:\tor-win32-0.2.8.9\Data\Tor\geoip6', # 'ExitNodes': '{ru}', # если вдруг пригодится }, tor_cmd=r'C:\Users\Oleg\Desktop\tor-win32-0.2.8.9\Tor\tor.exe', ) self.controller = Controller.from_port() self.controller.authenticate()
def launch(self): """ launches a new tor process :return: created process """ self.process = launch_tor_with_config( config=self.default_config, init_msg_handler=self._bootstrap_lines, take_ownership=True) self.controller = Controller.from_port(port=self.control_port) self.controller.authenticate() return self.process
def __init__(self): super(self.__class__, self).__init__() self.socks_port = config.TOR_SOCKS_PORT self.control_port = config.TOR_CONTROL_PORT self.proxy_port = self.socks_port self.tor_process = sp.launch_tor_with_config( config={ 'SocksPort': str(self.socks_port), 'ExitNodes': config.TOR_EXIT_NODES, 'ExcludeNodes': config.TOR_EXCLUDE_NODES, 'ExcludeExitNodes': config.TOR_EXCLUDE_NODES, 'StrictNodes': '1', 'MaxCircuitDirtiness': config.TOR_MAX_CIRCUIT_DIRTINESS } )
def __open(self): #Open Tor Process opened = False while not opened: with Timeout(PROCESS_TIMEOUT, False): self.__torProcess = launch_tor_with_config(config=self.__torConfig, tor_cmd=TOR_CMD, init_msg_handler=self.__torPrint) self.__torProcess.stdout.close() opened = True if not opened: self.__torProcess.terminate() #Open Tor Control self.__torCtrl = Controller.from_port(address=HOST, port=self.__ctrlPort) self.__torCtrl.authenticate(PASS_PHRASE)
def setupTor(id): c_port = 9050 + int(id) * 2 config = { # "Socks5Proxy": "127.0.0.1:10808", # "Bridge": "meek_lite 0.0.2.0: 2 97700DFE9F483596DDA6264C4D7DF7641E1E39CE", # "url = https://meek.azureedge.net/": "front = ajax.aspnetcdn.com", # "ClientOnionAuthDir": r"C:\Users\admin\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor\onion-auth", # "DataDirectory": r"C:\Users\admin\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor", # "GeoIPFile": r"C:\Users\admin\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor\geoip", # "GeoIPv6File": r"C:\Users\admin\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor\geoip6", # "UseBridges": "1", "DataDirectory": f"./tordata{c_port}", 'ControlPort': f'{c_port}', "SocksPort": f"{c_port + 1}" } return process.launch_tor_with_config(config, tor_cmd=r"tor")
def launch_tbb_tor_with_stem(tbb_path=None, torrc=None, tor_binary=None): if not (tor_binary or tbb_path): raise StemLaunchError("Either pass tbb_path or tor_binary") if not tor_binary and tbb_path: tor_binary = join(tbb_path, cm.DEFAULT_TOR_BINARY_PATH) if not isfile(tor_binary): raise StemLaunchError("Invalid Tor binary") modify_env_var("LD_LIBRARY_PATH", dirname(tor_binary)) temp_data_dir = tempfile.mkdtemp() if torrc is None: torrc = { 'ControlPort': str(cm.STEM_CONTROL_PORT), 'SOCKSPort': str(cm.STEM_SOCKS_PORT), 'DataDirectory': temp_data_dir } return launch_tor_with_config(config=torrc, tor_cmd=tor_binary)
def __init__(tor): tid = binascii.hexlify(os.urandom(16)).decode('utf-8') tor.socks_port, tor.control_port = next(self.port_it) tor.data_dir = os.path.join(self.base_dir, tid) tor.password = binascii.hexlify(os.urandom(8)).decode('utf-8') print(self._hash_password(tor.password)) print(type(self._hash_password(tor.password))) config = { 'HashedControlPassword': self._hash_password(tor.password), 'SocksPort': str(tor.socks_port), 'ControlPort': str(tor.control_port), 'DataDirectory': tor.data_dir, } # tor.proc = launch_tor_with_config(config, self.tor_exe, take_ownership=True) tor.proc = launch_tor_with_config(config, self.tor_exe, take_ownership=False)
def __open(self): #Open Tor Process opened = False while not opened: with Timeout(PROCESS_TIMEOUT, False): self.__torProcess = launch_tor_with_config( config=self.__torConfig, tor_cmd=TOR_CMD, init_msg_handler=self.__torPrint) self.__torProcess.stdout.close() opened = True if not opened: self.__torProcess.terminate() #Open Tor Control self.__torCtrl = Controller.from_port(address=HOST, port=self.__ctrlPort) self.__torCtrl.authenticate(PASS_PHRASE)
def start_process(tor_cmd=None, control_port=9051, socks_port=9050, OtherParams=dict()): if tor_cmd is None: tor_cmd = get_tor_path() config = { 'ControlPort': str(control_port), 'SocksPort': str(socks_port), 'Log': ['NOTICE stdout'], **OtherParams } tor_process = tpr.launch_tor_with_config(config, tor_cmd=tor_cmd, take_ownership=True, close_output=True) return tor_process
def launch_tbb_tor_with_stem(tbb_path=None, torrc=None, tor_binary=None): """Launch the Tor binary in tbb_path using Stem.""" if not (tor_binary or tbb_path): raise StemLaunchError("Either pass tbb_path or tor_binary") if not tor_binary and tbb_path: tor_binary = join(tbb_path, cm.DEFAULT_TOR_BINARY_PATH) if not isfile(tor_binary): raise StemLaunchError("Invalid Tor binary") prepend_to_env_var("LD_LIBRARY_PATH", dirname(tor_binary)) if torrc is None: torrc = {'ControlPort': str(cm.STEM_CONTROL_PORT), 'SOCKSPort': str(cm.STEM_SOCKS_PORT), 'DataDirectory': tempfile.mkdtemp()} return launch_tor_with_config(config=torrc, tor_cmd=tor_binary)
def run(self): self.logger.debug("[%05d] Executing Tor Process" % self.id) self.process = launch_tor_with_config( config={ "ControlPort": str(self.control_port), "SOCKSPort": str(self.socks_port), "DataDirectory": self.data_directory, "AllowSingleHopCircuits": "1", "ExcludeSingleHopRelays": "0", }, tor_cmd=self.tor_cmd, init_msg_handler=self.print_bootstrapped_line) self.logger.debug("[%05d] Creating Tor Controller" % self.id) self.controller = Controller.from_port(port=self.control_port) self.controller.authenticate() return self
def run(self): '''run *description* Run tor as the proxy server. ''' # seed = ''.join(self._seed_generator()) hashcode = subprocess.check_output(['tor', '--hash-password', seed()]) self.hashcode = HASHCODE_RE.search( hashcode.decode('utf-8')).group('code') self.process = tor_process.launch_tor_with_config( config={ 'SocksPort': [self.socks_port], 'ControlPort': [self.control_port], 'HashedControlPassword': self.hashcode, 'CookieAuthentication': '1', 'DataDirectory': self.tor_path, 'ExitNodes': '{%s}' % self.exit_country_code }, init_msg_handler=print_bootstrap_lines)
def __init__(self, take_ownership=True, # Tor dies when the Sorter does torrc_config={"ControlPort": "9051", "CookieAuth": "1"}, socks_port=9050, page_load_timeout=20, max_tasks=10, db_handler=None): self.logger = setup_logging(_log_dir, "sorter") self.db_handler = db_handler self.logger.info("Opening event loop for Sorter...") self.loop = asyncio.get_event_loop() self.max_tasks = max_tasks self.logger.info("Creating Sorter queue...") self.q = asyncio.Queue() # Start tor and create an aiohttp tor connector self.torrc_config = torrc_config self.socks_port = str(find_free_port(socks_port)) self.torrc_config.update({"SocksPort": self.socks_port}) self.logger.info("Starting tor process with config " "{self.torrc_config}.".format(**locals())) self.tor_process = launch_tor_with_config(config=self.torrc_config, take_ownership=take_ownership) onion_proxy = aiosocks.Socks5Addr('127.0.0.1', socks_port) conn = SocksConnector(proxy=onion_proxy, remote_resolve=True) # aiohttp's ClientSession does connection pooling and HTTP keep-alives # for us self.logger.info("Creating aiohttp ClientSession with our event loop " "and tor proxy connector...") self.session = aiohttp.ClientSession(loop=self.loop, connector=conn) # Pretend we're Tor Browser in order to get rejected by less sites/WAFs u = "Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0" self.headers = {'user-agent': u} self.page_load_timeout = page_load_timeout
def test_run_driver_with_stem_customized(self): """This test shows how to run the TorBrowserDriver with stem. It shows how to launch tor with stem listening to a custom SOCKS and Controller ports, and use a particular tor binary instead of the one installed in the system. """ from stem.control import Controller from stem.process import launch_tor_with_config # If you're running tor with the TBB binary, instead # of a tor installed in the system, you need to set # its path in the LD_LIBRARY_PATH: custom_tor_binary = join(TBB_PATH, cm.DEFAULT_TOR_BINARY_PATH) environ["LD_LIBRARY_PATH"] = dirname(custom_tor_binary) # Run tor custom_control_port, custom_socks_port = 9051, 9050 torrc = {'ControlPort': str(custom_control_port), 'SOCKSPort': str(custom_socks_port), 'DataDirectory': join(TBB_PATH, cm.DEFAULT_TOR_DATA_PATH)} # you can add your own settings to this torrc, # including the path and the level for logging in tor. # you can also use the DataDirectory property in torrc # to set a custom data directory for tor. # See other options: https://www.torproject.org/docs/tor-manual.html.en tor_process = launch_tor_with_config(config=torrc, tor_cmd=custom_tor_binary) with Controller.from_port(port=custom_control_port) as controller: controller.authenticate() # Visit the page with the TorBrowserDriver with TorBrowserDriver(TBB_PATH, socks_port=custom_socks_port) as driver: driver.get(cm.TEST_URL) sleep(1) # Kill tor process if tor_process: tor_process.kill()
def test_run_driver_with_stem_customized(self): """This test shows how to run the TorBrowserDriver with stem. It shows how to launch tor with stem listening to a custom SOCKS and Controller ports, and use a particular tor binary instead of the one installed in the system. """ from stem.control import Controller from stem.process import launch_tor_with_config # If you're running tor with the TBB binary, instead # of a tor installed in the system, you need to set # its path in the LD_LIBRARY_PATH: custom_tor_binary = join(TBB_PATH, cm.DEFAULT_TOR_BINARY_PATH) environ["LD_LIBRARY_PATH"] = dirname(custom_tor_binary) # Run tor custom_control_port, custom_socks_port = 9051, 9050 torrc = {'ControlPort': str(custom_control_port), 'DataDirectory': join(TBB_PATH, cm.DEFAULT_TOR_DATA_PATH)} # you can add your own settings to this torrc, # including the path and the level for logging in tor. # you can also use the DataDirectory property in torrc # to set a custom data directory for tor. # For other options see: https://www.torproject.org/docs/tor-manual.html.en tor_process = launch_tor_with_config(config=torrc, tor_cmd=custom_tor_binary) with Controller.from_port(port=custom_control_port) as controller: controller.authenticate() # Visit the page with the TorBrowserDriver with TorBrowserDriver(TBB_PATH, socks_port=custom_socks_port) as driver: driver.get(cm.TEST_URL) sleep(1) # Kill tor process if tor_process: tor_process.kill()
raise ValueError() except ValueError: print (term.format("\nPort needs to be an Integer 0..65535\n", term.Color.RED)) exit(255) if not match(r'\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b',argv[1]): print (term.format("\n'%s' does not seem to be a valid IP\n"%argv[1], term.Color.RED)) exit(255) path = ["%s:%d"%(argv[1],port)] try: process = process.launch_tor_with_config(config={ 'ControlPort':str(CONTROL_PORT), 'HashedControlPassword':'******', 'UseBridges':'1', 'bridge':"%s:%d"%(argv[1],port) }, #init_msg_handler=lambda x: print(x), timeout=10, take_ownership=True) controller = Controller.from_port(port=CONTROL_PORT) controller.authenticate("test") except OSError: print (term.format("\nThis %s:%d does not seem to be a functional bridge!\n"% (argv[1],port), term.Color.RED)) exit(1) else: print (term.format("\n%s:%d is a valid bridge.\n"% (argv[1],port),