def exit_info(exit): """ Print info about my exit node """ torversion = get_system_tor_version() print "Tor version: %s\n" % torversion for i in exit['count']: print " nickname: %s" % exit['nickname'][i] print " address: %s" % exit['ipaddress'][i] print " fingerprint: %s\n" % exit['fingerprint'][i]
def run(self, do_onion=True, do_inet=True, client_tgen_listen_port=58888, client_tgen_connect_ip='0.0.0.0', client_tgen_connect_port=8080, client_tor_ctl_port=59050, client_tor_socks_port=59000, server_tgen_listen_port=8080, server_tor_ctl_port=59051, server_tor_socks_port=59001): ''' only `server_tgen_listen_port` are "public" and need to be opened on the firewall. if `client_tgen_connect_port` != `server_tgen_listen_port`, then you should have installed a forwarding rule in the firewall. all ports need to be unique though, and unique among multiple onionperf instances. here are some sane defaults: client_tgen_listen_port=58888, client_tgen_connect_port=8080, client_tor_ctl_port=59050, client_tor_socks_port=59000, server_tgen_listen_port=8080, server_tor_ctl_port=59051, server_tor_socks_port=59001 ''' self.threads = [] self.done_event = threading.Event() # if ctrl-c is pressed, shutdown child processes properly try: # make sure stem and Tor supports ephemeral HS (version >= 0.2.7.1-alpha) # and also the NEWNYM mode that clears descriptor cache (version >= 0.2.7.3-rc) if do_onion: try: tor_version = get_system_tor_version(self.tor_bin_path) if tor_version < Requirement.ADD_ONION or tor_version < Version( '0.2.7.3-rc'): # ADD_ONION is a stem 1.4.0 feature logging.warning( "OnionPerf in onion mode requires Tor version >= 0.2.7.3-rc, you have {0}, aborting" .format(tor_version)) return except: logging.warning( "OnionPerf in onion mode requires stem version >= 1.4.0, you have {0}, aborting" .format(stem_version)) return logging.info("Bootstrapping started...") logging.info( "Log files for the client and server processes will be placed in {0}" .format(self.datadir_path)) general_writables = [] tgen_client_writable, torctl_client_writable = None, None if do_onion or do_inet: general_writables.append( self.__start_tgen_server(server_tgen_listen_port)) if do_onion: logging.info( "Onion Service private keys will be placed in {0}".format( self.privatedir_path)) tor_writable, torctl_writable = self.__start_tor_server( server_tor_ctl_port, server_tor_socks_port, {client_tgen_connect_port: server_tgen_listen_port}) general_writables.append(tor_writable) general_writables.append(torctl_writable) if do_onion or do_inet: tor_writable, torctl_client_writable = self.__start_tor_client( client_tor_ctl_port, client_tor_socks_port) general_writables.append(tor_writable) server_urls = [] if do_onion and self.hs_service_id is not None and self.hs_v3_service_id is not None: server_urls.append("{0}.onion:{1}".format( self.hs_service_id, client_tgen_connect_port)) server_urls.append("{0}.onion:{1}".format( self.hs_v3_service_id, client_tgen_connect_port)) if do_inet: connect_ip = client_tgen_connect_ip if client_tgen_connect_ip != '0.0.0.0' else util.get_ip_address( ) server_urls.append("{0}:{1}".format(connect_ip, client_tgen_connect_port)) if do_onion or do_inet: assert len(server_urls) > 0 tgen_client_writable = self.__start_tgen_client( server_urls, client_tgen_listen_port, client_tor_socks_port) self.__start_log_processors(general_writables, tgen_client_writable, torctl_client_writable) logging.info("Bootstrapping finished, entering heartbeat loop") time.sleep(1) if self.oneshot: logging.info( "Onionperf is running in Oneshot mode. It will download a 5M file and shut down gracefully..." ) while True: # TODO add status update of some kind? maybe the number of files in the www directory? # logging.info("Heartbeat: {0} downloads have completed successfully".format(self.__get_download_count(tgen_client_writable.filename))) if self.oneshot: downloads = 0 while True: downloads = self.__get_download_count( tgen_client_writable.filename) if downloads >= 1: logging.info( "Onionperf has downloaded a 5M file in oneshot mode, and will now shut down." ) break else: continue break if self.__is_alive(): logging.info( "All helper processes seem to be alive :)") else: logging.warning( "Some parallel components failed too many times or have died :(" ) logging.info( "We are in a broken state, giving up and exiting now" ) break logging.info( "Next main process heartbeat is in 1 hour (helper processes run on their own schedule)" ) logging.info("press CTRL-C for graceful shutdown...") time.sleep(3600) else: logging.info("No measurement mode set, nothing to do") except KeyboardInterrupt: logging.info( "Interrupt received, please wait for graceful shutdown") self.__is_alive() finally: logging.info("Cleaning up child processes now...") if self.hs_service_id is not None: try: with Controller.from_port( port=self.hs_control_port) as torctl: torctl.authenticate() torctl.remove_ephemeral_hidden_service( self.hs_service_id) except: pass # this fails to authenticate if tor proc is dead if self.hs_v3_service_id is not None: try: with Controller.from_port( port=self.hs_v3_control_port) as torctl: torctl.authenticate() torctl.remove_ephemeral_hidden_service( self.hs_v3_service_id) except: pass # this fails to authenticate if tor proc is dead # logging.disable(logging.INFO) self.done_event.set() for t in self.threads: logging.info("Joining {0} thread...".format(t.getName())) t.join() time.sleep(1) # logging.disable(logging.NOTSET) logging.info("Child processes terminated") logging.info("Child process cleanup complete!") logging.info("Exiting")
def run(self, do_onion=True, do_inet=True, client_tgen_listen_port=58888, client_tgen_connect_ip='0.0.0.0', client_tgen_connect_port=8080, client_tor_ctl_port=59050, client_tor_socks_port=59000, server_tgen_listen_port=8080, server_tor_ctl_port=59051, server_tor_socks_port=59001, twistd_port=50080): ''' only `server_tgen_listen_port` and `twistd_port` are "public" and need to be opened on the firewall. if `client_tgen_connect_port` != `server_tgen_listen_port`, then you should have installed a forwarding rule in the firewall. all ports need to be unique though, and unique among multiple onionperf instances. here are some sane defaults: client_tgen_listen_port=58888, client_tgen_connect_port=8080, client_tor_ctl_port=59050, client_tor_socks_port=59000, server_tgen_listen_port=8080, server_tor_ctl_port=59051, server_tor_socks_port=59001, twistd_port=50080 ''' self.threads = [] self.done_event = threading.Event() # if ctrl-c is pressed, shutdown child processes properly try: # make sure stem and Tor supports ephemeral HS (version >= 0.2.7.1-alpha) # and also the NEWNYM mode that clears descriptor cache (version >= 0.2.7.3-rc) if do_onion: try: tor_version = get_system_tor_version(self.tor_bin_path) if tor_version < Requirement.ADD_ONION or tor_version < Version('0.2.7.3-rc'): # ADD_ONION is a stem 1.4.0 feature logging.warning("OnionPerf in onion mode requires Tor version >= 0.2.7.3-rc, you have {0}, aborting".format(tor_version)) return except: logging.warning("OnionPerf in onion mode requires stem version >= 1.4.0, you have {0}, aborting".format(stem_version)) return logging.info("Bootstrapping started...") logging.info("Log files for the client and server processes will be placed in {0}".format(self.datadir_path)) general_writables = [] tgen_client_writable, torctl_client_writable = None, None if do_onion or do_inet: general_writables.append(self.__start_tgen_server(server_tgen_listen_port)) if do_onion: tor_writable, torctl_writable = self.__start_tor_server(server_tor_ctl_port, server_tor_socks_port, {server_tgen_listen_port:client_tgen_connect_port}) general_writables.append(tor_writable) general_writables.append(torctl_writable) if do_onion or do_inet: tor_writable, torctl_client_writable = self.__start_tor_client(client_tor_ctl_port, client_tor_socks_port) general_writables.append(tor_writable) server_urls = [] if do_onion and self.hs_service_id is not None: server_urls.append("{0}.onion:{1}".format(self.hs_service_id, server_tgen_listen_port)) if do_inet: connect_ip = client_tgen_connect_ip if client_tgen_connect_ip != '0.0.0.0' else util.get_ip_address() server_urls.append("{0}:{1}".format(connect_ip, client_tgen_connect_port)) if do_onion or do_inet: assert len(server_urls) > 0 tgen_client_writable = self.__start_tgen_client(server_urls, client_tgen_listen_port, client_tor_socks_port) general_writables.append(self.__start_twistd(twistd_port)) self.__start_log_processors(general_writables, tgen_client_writable, torctl_client_writable) logging.info("Bootstrapping finished, entering heartbeat loop") time.sleep(1) while True: # TODO add status update of some kind? maybe the number of files in the twistd directory? # logging.info("Heartbeat: {0} downloads have completed successfully".format(self.__get_download_count(tgen_client_writable.filename))) if self.__is_alive(): logging.info("All helper processes seem to be alive :)") else: logging.warning("Some parallel components failed too many times or have died :(") logging.info("We are in a broken state, giving up and exiting now") break logging.info("Next main process heartbeat is in 1 hour (helper processes run on their own schedule)") logging.info("press CTRL-C for graceful shutdown...") time.sleep(3600) else: logging.info("No measurement mode set, nothing to do") except KeyboardInterrupt: logging.info("Interrupt received, please wait for graceful shutdown") self.__is_alive() finally: logging.info("Cleaning up child processes now...") if self.hs_service_id is not None: try: with Controller.from_port(port=self.hs_control_port) as torctl: torctl.authenticate() torctl.remove_ephemeral_hidden_service(self.hs_service_id) except: pass # this fails to authenticate if tor proc is dead # logging.disable(logging.INFO) self.done_event.set() for t in self.threads: logging.info("Joining {0} thread...".format(t.getName())) t.join() time.sleep(1) # logging.disable(logging.NOTSET) logging.info("Child processes terminated") logging.info("Child process cleanup complete!") logging.info("Exiting")
def run(self, do_onion=True, do_inet=True, tgen_model=None, tgen_client_conf=None, tgen_server_conf=None): ''' only `tgen_server_conf.listen_port` are "public" and need to be opened on the firewall. if `tgen_client_conf.connect_port` != `tgen_server_conf.listen_port`, then you should have installed a forwarding rule in the firewall. all ports need to be unique though, and unique among multiple onionperf instances. here are some sane defaults: tgen_client_conf.listen_port=58888, tgen_client_conf.connect_port=8080, tgen_client_conf.tor_ctl_port=59050, tgen_client_conf.tor_socks_port=59000, tgen_server_conf.listen_port=8080, tgen_server_conf.tor_ctl_port=59051, tgen_server_conf.tor_socks_port=59001 ''' self.threads = [] self.done_event = threading.Event() if tgen_client_conf is None: tgen_client_conf = TGenConf(listen_port=58888, connect_ip='0.0.0.0', connect_port=8080, tor_ctl_port=59050, tor_socks_port=59000) if tgen_server_conf is None: tgen_server_conf = TGenConf(listen_port=8080, tor_ctl_port=59051, tor_socks_port=59001) # if ctrl-c is pressed, shutdown child processes properly try: # make sure stem and Tor supports ephemeral HS (version >= 0.2.7.1-alpha) # and also the NEWNYM mode that clears descriptor cache (version >= 0.2.7.3-rc) if do_onion: try: tor_version = get_system_tor_version(self.tor_bin_path) if tor_version < Requirement.ADD_ONION or tor_version < Version( '0.2.7.3-rc'): # ADD_ONION is a stem 1.4.0 feature logging.warning( "OnionPerf in onion mode requires Tor version >= 0.2.7.3-rc, you have {0}, aborting" .format(tor_version)) return except: logging.warning( "OnionPerf in onion mode requires stem version >= 1.4.0, you have {0}, aborting" .format(stem_version)) return logging.info("Bootstrapping started...") logging.info( "Log files for the client and server processes will be placed in {0}" .format(self.datadir_path)) general_writables = [] tgen_client_writable, torctl_client_writable = None, None if do_onion or do_inet: tgen_model.port = tgen_server_conf.listen_port general_writables.append(self.__start_tgen_server(tgen_model)) if do_onion: logging.info( "Onion Service private keys will be placed in {0}".format( self.privatedir_path)) # one must not have an open socks port when running a single # onion service. see tor's man page for more information. if self.single_onion: tgen_server_conf.tor_socks_port = 0 tor_writable, torctl_writable = self.__start_tor_server( tgen_server_conf.tor_ctl_port, tgen_server_conf.tor_socks_port, { tgen_client_conf.connect_port: tgen_server_conf.listen_port }) general_writables.append(tor_writable) general_writables.append(torctl_writable) if do_onion or do_inet: tor_writable, torctl_client_writable = self.__start_tor_client( tgen_client_conf.tor_ctl_port, tgen_client_conf.tor_socks_port) general_writables.append(tor_writable) server_urls = [] if do_onion and self.hs_v3_service_id is not None: server_urls.append("{0}.onion:{1}".format( self.hs_v3_service_id, tgen_client_conf.connect_port)) if do_inet: connect_ip = tgen_client_conf.connect_ip if tgen_client_conf.connect_ip != '0.0.0.0' else util.get_ip_address( ) server_urls.append("{0}:{1}".format( connect_ip, tgen_client_conf.connect_port)) tgen_model.servers = server_urls if do_onion or do_inet: assert len(server_urls) > 0 tgen_model.port = tgen_client_conf.listen_port tgen_model.socks_port = tgen_client_conf.tor_socks_port tgen_client_writable = self.__start_tgen_client(tgen_model) self.__start_log_processors(general_writables, tgen_client_writable, torctl_client_writable) logging.info("Bootstrapping finished, entering heartbeat loop") time.sleep(1) while True: if tgen_model.num_transfers: # This function blocks until our TGen client process # terminated on its own. self.__wait_for_tgen_client() break if self.__is_alive(): logging.info( "All helper processes seem to be alive :)") else: logging.warning( "Some parallel components failed too many times or have died :(" ) logging.info( "We are in a broken state, giving up and exiting now" ) break logging.info( "Next main process heartbeat is in 1 hour (helper processes run on their own schedule)" ) logging.info("press CTRL-C for graceful shutdown...") time.sleep(3600) else: logging.info("No measurement mode set, nothing to do") except KeyboardInterrupt: logging.info( "Interrupt received, please wait for graceful shutdown") self.__is_alive() finally: logging.info("Cleaning up child processes now...") if self.hs_v3_service_id is not None: try: with Controller.from_port( port=self.hs_v3_control_port) as torctl: torctl.authenticate() torctl.remove_ephemeral_hidden_service( self.hs_v3_service_id) except: pass # this fails to authenticate if tor proc is dead # logging.disable(logging.INFO) self.done_event.set() for t in self.threads: logging.info("Joining {0} thread...".format(t.getName())) t.join() time.sleep(1) # logging.disable(logging.NOTSET) logging.info("Child processes terminated") logging.info("Child process cleanup complete!") logging.info("Exiting")