def test_load_other_tunnel_community(self): """ Testing whether we do not load two different tunnel communities in the same session """ # Load/unload this community so we have a classification dispersy = self.session.lm.dispersy master_member = DummyTunnelCommunity.get_master_members(dispersy)[0] keypair = self.session.multichain_keypair dispersy_member = dispersy.get_member(private_key=keypair.key_to_bin()) community = DummyTunnelCommunity.init_community( dispersy, master_member, dispersy_member, tribler_session=self.session, settings=TunnelSettings()) yield community.unload_community() some_candidate = Candidate(("1.2.3.4", 1234), False) some_packet = self.create_valid_packet(community) dispersy.on_incoming_packets([ (some_candidate, some_packet), ]) tunnel_communities = 0 for community in dispersy.get_communities(): if isinstance(community, HiddenTunnelCommunity): tunnel_communities += 1 # We should only have one tunnel community, not multiple self.assertEqual(tunnel_communities, 1)
def test_on_data_invalid_encoding(self): """ When on_data receives an invalid encryption, crypto_in() should throw a CryptoException. """ # Prepare crypto and settings tunnel_crypto = object.__new__(TunnelCrypto) self.tunnel_community.settings = TunnelSettings() # Register a valid circuit circuit = Circuit(42L) hop = Hop(tunnel_crypto.generate_key(u"curve25519")) hop.session_keys = tunnel_crypto.generate_session_keys("1234") circuit.add_hop(hop) self.tunnel_community.circuits[42] = circuit # Encode data with a truncated encrypted string (empty in this case) packet = TunnelConversion.encode_data(42, ("127.0.0.1", 1337), ("127.0.0.1", 1337), "") # Simulate on_data() _, encrypted = TunnelConversion.split_encrypted_packet(packet, u"data") self.assertRaises(CryptoException, self.tunnel_community.crypto_in, 42, encrypted, is_data=True)
def load_communities(self): self._logger.info("tribler: Preparing communities...") now_time = timemod.time() default_kwargs = {'tribler_session': self.session} # Search Community if self.session.config.get_torrent_search_enabled(): from Tribler.community.search.community import SearchCommunity self.dispersy.define_auto_load(SearchCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # AllChannel Community if self.session.config.get_channel_search_enabled(): from Tribler.community.allchannel.community import AllChannelCommunity self.dispersy.define_auto_load(AllChannelCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # Channel Community if self.session.config.get_channel_community_enabled(): from Tribler.community.channel.community import ChannelCommunity self.dispersy.define_auto_load(ChannelCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # PreviewChannel Community if self.session.config.get_preview_channel_community_enabled(): from Tribler.community.channel.preview import PreviewChannelCommunity self.dispersy.define_auto_load(PreviewChannelCommunity, self.session.dispersy_member, kargs=default_kwargs) # Tunnel Community if self.session.config.get_tunnel_community_enabled(): tunnel_settings = TunnelSettings(tribler_session=self.session) tunnel_kwargs = {'tribler_session': self.session, 'settings': tunnel_settings} if self.session.config.get_trustchain_enabled(): trustchain_kwargs = {'tribler_session': self.session} # If the trustchain is enabled, we use the permanent trustchain keypair # for both the trustchain and the tunnel community keypair = self.session.trustchain_keypair dispersy_member = self.dispersy.get_member(private_key=keypair.key_to_bin()) from Tribler.community.triblerchain.community import TriblerChainCommunity self.dispersy.define_auto_load(TriblerChainCommunity, dispersy_member, load=True, kargs=trustchain_kwargs) else: keypair = self.dispersy.crypto.generate_key(u"curve25519") dispersy_member = self.dispersy.get_member(private_key=self.dispersy.crypto.key_to_bin(keypair)) from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity self.tunnel_community = self.dispersy.define_auto_load( HiddenTunnelCommunity, dispersy_member, load=True, kargs=tunnel_kwargs)[0] # We don't want to automatically load other instances of this community with other master members. self.dispersy.undefine_auto_load(HiddenTunnelCommunity) self._logger.info("tribler: communities are ready in %.2f seconds", timemod.time() - now_time)
def start_tunnel(self, options): """ Main method to startup a tunnel helper and add a signal handler. """ socks5_port = options["socks5"] introduce_port = options["introduce"] dispersy_port = options["dispersy"] crawl_keypair_filename = options["crawl"] settings = TunnelSettings() # For disabling anonymous downloading, limiting download to hidden services only. settings.min_circuits = 0 settings.max_circuits = 0 if socks5_port is not None: settings.socks_listen_ports = range(socks5_port, socks5_port + 5) else: settings.socks_listen_ports = [random.randint(1000, 65535) for _ in range(5)] settings.become_exitnode = bool(options["exit"]) if settings.become_exitnode: logger.info("Exit-node enabled") else: logger.info("Exit-node disabled") settings.enable_trustchain = bool(options["trustchain"]) if settings.enable_trustchain: logger.info("Trustchain enabled") else: logger.info("Trustchain disabled") tunnel = Tunnel(settings, crawl_keypair_filename, dispersy_port) StandardIO(LineHandler(tunnel)) def stop_tunnel_api(): if self.tunnel_site: return maybeDeferred(self.tunnel_site.stopListening) return succeed(None) def signal_handler(sig, _): msg("Received shut down signal %s" % sig) if not self._stopping: self._stopping = True msg("Setting the tunnel should_run variable to False") tunnel.should_run = False tunnel.stop().addCallback(lambda _: stop_tunnel_api().addCallback(lambda _: reactor.stop())) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) tunnel.start(introduce_port) if options["tunnelapi"] > 0: self.tunnel_site = self.site = reactor.listenTCP(options["tunnelapi"], server.Site(resource=TunnelRootEndpoint(tunnel)))
def define_communities(*args): assert isInIOThread() from Tribler.community.channel.community import ChannelCommunity from Tribler.community.channel.preview import PreviewChannelCommunity from Tribler.community.tunnel.tunnel_community import TunnelSettings from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity # make sure this is only called once session.remove_observer(define_communities) dispersy = session.get_dispersy_instance() self._logger.info("tribler: Preparing communities...") now = time() dispersy.attach_progress_handler(self.progressHandler) default_kwargs = {'tribler_session': session} # must be called on the Dispersy thread if session.get_barter_community_enabled(): from Tribler.community.bartercast4.community import BarterCommunity dispersy.define_auto_load(BarterCommunity, session.dispersy_member, load=True) # load metadata community dispersy.define_auto_load(ChannelCommunity, session.dispersy_member, load=True, kargs=default_kwargs) dispersy.define_auto_load(PreviewChannelCommunity, session.dispersy_member, kargs=default_kwargs) keypair = dispersy.crypto.generate_key(u"curve25519") dispersy_member = dispersy.get_member( private_key=dispersy.crypto.key_to_bin(keypair), ) settings = TunnelSettings(session.get_install_dir(), tribler_session=session) tunnel_kwargs = {'tribler_session': session, 'settings': settings} self.tunnel_community = dispersy.define_auto_load( HiddenTunnelCommunity, dispersy_member, load=True, kargs=tunnel_kwargs)[0] session.set_anon_proxy_settings( 2, ("127.0.0.1", session.get_tunnel_community_socks5_listen_ports())) diff = time() - now self._logger.info("tribler: communities are ready in %.2f seconds", diff)
def load_community(session): keypair = dispersy.crypto.generate_key(u"curve25519") dispersy_member = dispersy.get_member( private_key=dispersy.crypto.key_to_bin(keypair)) settings = TunnelSettings(tribler_session=session) if not crypto_enabled: settings.crypto = NoCrypto() settings.become_exitnode = become_exit_node return dispersy.define_auto_load(HiddenTunnelCommunity, dispersy_member, (session, settings), load=True)[0]
def load_communities(self, session, dispersy, crawler=False): keypair = dispersy.crypto.generate_key(u"curve25519") dispersy_member = dispersy.get_member( private_key=dispersy.crypto.key_to_bin(keypair)) settings = TunnelSettings(tribler_session=session) settings.become_exitnode = True if crawler: dispersy.define_auto_load(BarterCommunityCrawler, dispersy_member, (session, settings), load=True) else: dispersy.define_auto_load(BarterCommunity, dispersy_member, (session, settings), load=True)
def load_tunnel_community_in_session(self, session, exitnode=False): """ Load the tunnel community in a given session. We are using our own tunnel community here instead of the one used in Tribler. """ dispersy = session.get_dispersy_instance() keypair = dispersy.crypto.generate_key(u"curve25519") dispersy_member = dispersy.get_member(private_key=dispersy.crypto.key_to_bin(keypair)) settings = TunnelSettings(tribler_session=session) if not self.crypto_enabled: settings.crypto = NoCrypto() settings.become_exitnode = exitnode return dispersy.define_auto_load(HiddenTunnelCommunityTests, dispersy_member, (session, settings), load=True)[0]
def load_communities(self): self._logger.info("tribler: Preparing communities...") now_time = timemod.time() default_kwargs = {'tribler_session': self.session} # Search Community if self.session.config.get_torrent_search_enabled(): from Tribler.community.search.community import SearchCommunity self.dispersy.define_auto_load(SearchCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # AllChannel Community if self.session.config.get_channel_search_enabled(): from Tribler.community.allchannel.community import AllChannelCommunity self.dispersy.define_auto_load(AllChannelCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # Channel Community if self.session.config.get_channel_community_enabled(): from Tribler.community.channel.community import ChannelCommunity self.dispersy.define_auto_load(ChannelCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # PreviewChannel Community if self.session.config.get_preview_channel_community_enabled(): from Tribler.community.channel.preview import PreviewChannelCommunity self.dispersy.define_auto_load(PreviewChannelCommunity, self.session.dispersy_member, kargs=default_kwargs) # Tunnel Community mc_community = None if self.session.config.get_tunnel_community_enabled(): tunnel_settings = TunnelSettings(tribler_session=self.session) tunnel_kwargs = { 'tribler_session': self.session, 'settings': tunnel_settings } if self.session.config.get_trustchain_enabled(): trustchain_kwargs = {'tribler_session': self.session} # If the trustchain is enabled, we use the permanent trustchain keypair # for both the trustchain and the tunnel community keypair = self.session.trustchain_keypair dispersy_member = self.dispersy.get_member( private_key=keypair.key_to_bin()) from Tribler.community.triblerchain.community import TriblerChainCommunity mc_community = self.dispersy.define_auto_load( TriblerChainCommunity, dispersy_member, load=True, kargs=trustchain_kwargs)[0] else: keypair = self.dispersy.crypto.generate_key(u"curve25519") dispersy_member = self.dispersy.get_member( private_key=self.dispersy.crypto.key_to_bin(keypair)) from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity self.tunnel_community = self.dispersy.define_auto_load( HiddenTunnelCommunity, dispersy_member, load=True, kargs=tunnel_kwargs)[0] # We don't want to automatically load other instances of this community with other master members. self.dispersy.undefine_auto_load(HiddenTunnelCommunity) # Use the permanent TrustChain ID for Market community/TradeChain if it's available if self.session.config.get_market_community_enabled(): wallets = {} btc_wallet = BitcoinWallet( os.path.join(self.session.config.get_state_dir(), 'wallet'), testnet=self.session.config.get_btc_testnet()) wallets[btc_wallet.get_identifier()] = btc_wallet mc_wallet = TrustchainWallet(mc_community) wallets[mc_wallet.get_identifier()] = mc_wallet if self.session.config.get_dummy_wallets_enabled(): # For debugging purposes, we create dummy wallets dummy_wallet1 = DummyWallet1() wallets[dummy_wallet1.get_identifier()] = dummy_wallet1 dummy_wallet2 = DummyWallet2() wallets[dummy_wallet2.get_identifier()] = dummy_wallet2 from Tribler.community.market.community import MarketCommunity keypair = self.session.tradechain_keypair dispersy_member = self.dispersy.get_member( private_key=keypair.key_to_bin()) market_kwargs = { 'tribler_session': self.session, 'wallets': wallets } self.market_community = self.dispersy.define_auto_load( MarketCommunity, dispersy_member, load=True, kargs=market_kwargs)[0] self.session.config.set_anon_proxy_settings( 2, ("127.0.0.1", self.session.config.get_tunnel_community_socks5_listen_ports())) self._logger.info("tribler: communities are ready in %.2f seconds", timemod.time() - now_time)
def load_communities(): self._logger.info("tribler: Preparing communities...") now_time = timemod.time() default_kwargs = {'tribler_session': self.session} # Search Community if self.session.get_enable_torrent_search(): from Tribler.community.search.community import SearchCommunity self.dispersy.define_auto_load(SearchCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # AllChannel Community if self.session.get_enable_channel_search(): from Tribler.community.allchannel.community import AllChannelCommunity self.dispersy.define_auto_load(AllChannelCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # Bartercast Community if self.session.get_barter_community_enabled(): from Tribler.community.bartercast4.community import BarterCommunity self.dispersy.define_auto_load(BarterCommunity, self.session.dispersy_member, load=True) # Channel Community if self.session.get_channel_community_enabled(): from Tribler.community.channel.community import ChannelCommunity self.dispersy.define_auto_load(ChannelCommunity, self.session.dispersy_member, load=True, kargs=default_kwargs) # PreviewChannel Community if self.session.get_preview_channel_community_enabled(): from Tribler.community.channel.preview import PreviewChannelCommunity self.dispersy.define_auto_load(PreviewChannelCommunity, self.session.dispersy_member, kargs=default_kwargs) if self.session.get_tunnel_community_enabled(): tunnel_settings = TunnelSettings(tribler_session=self.session) tunnel_kwargs = {'tribler_session': self.session, 'settings': tunnel_settings} if self.session.get_enable_multichain(): multichain_kwargs = {'tribler_session': self.session} # If the multichain is enabled, we use the permanent multichain keypair # for both the multichain and the tunnel community keypair = self.session.multichain_keypair dispersy_member = self.dispersy.get_member(private_key=keypair.key_to_bin()) from Tribler.community.multichain.community import MultiChainCommunity self.dispersy.define_auto_load(MultiChainCommunity, dispersy_member, load=True, kargs=multichain_kwargs) from Tribler.community.tunnel.hidden_community_multichain import HiddenTunnelCommunityMultichain self.tunnel_community = self.dispersy.define_auto_load( HiddenTunnelCommunityMultichain, dispersy_member, load=True, kargs=tunnel_kwargs)[0] else: keypair = self.dispersy.crypto.generate_key(u"curve25519") dispersy_member = self.dispersy.get_member(private_key=self.dispersy.crypto.key_to_bin(keypair)) from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity self.tunnel_community = self.dispersy.define_auto_load( HiddenTunnelCommunity, dispersy_member, load=True, kargs=tunnel_kwargs)[0] self.session.set_anon_proxy_settings(2, ("127.0.0.1", self.session.get_tunnel_community_socks5_listen_ports())) self._logger.info("tribler: communities are ready in %.2f seconds", timemod.time() - now_time)
def main(argv): parser = argparse.ArgumentParser( description='Anonymous Tunnel CLI interface') try: parser.add_argument('-p', '--socks5', help='Socks5 port') parser.add_argument('-x', '--exit', help='Allow being an exit-node') parser.add_argument( '-i', '--introduce', help='Introduce the dispersy port of another tribler instance') parser.add_argument('-d', '--dispersy', help='Dispersy port') parser.add_argument( '-c', '--crawl', help= 'Enable crawler and use the keypair specified in the given filename' ) parser.add_argument( '-j', '--json', help='Enable JSON api, which will run on the provided port number ' + '(only available if the crawler is enabled)', type=int) parser.add_argument('-y', '--yappi', help="Profiling mode, either 'wall' or 'cpu'") parser.add_help = True args = parser.parse_args(sys.argv[1:]) except argparse.ArgumentError: parser.print_help() sys.exit(2) socks5_port = int(args.socks5) if args.socks5 else None introduce_port = int(args.introduce) if args.introduce else None dispersy_port = int(args.dispersy) if args.dispersy else -1 crawl_keypair_filename = args.crawl profile = args.yappi if args.yappi in ['wall', 'cpu'] else None if profile: yappi.set_clock_type(profile) yappi.start(builtins=True) logger.error("Profiling using %s time" % yappi.get_clock_type()['type']) if crawl_keypair_filename and not os.path.exists(crawl_keypair_filename): logger.error("Could not find keypair filename", crawl_keypair_filename) sys.exit(1) settings = TunnelSettings() # For disbling anonymous downloading, limiting download to hidden services only settings.min_circuits = 0 settings.max_circuits = 0 if socks5_port is not None: settings.socks_listen_ports = range(socks5_port, socks5_port + 5) else: settings.socks_listen_ports = [ random.randint(1000, 65535) for _ in range(5) ] settings.become_exitnode = True if args.exit in ['true'] else False if settings.become_exitnode: logger.info("Exit-node enabled") else: logger.info("Exit-node disabled") tunnel = Tunnel(settings, crawl_keypair_filename, dispersy_port) StandardIO(LineHandler(tunnel, profile)) tunnel.start(introduce_port) if crawl_keypair_filename and args.json > 0: cherrypy.config.update({ 'server.socket_host': '0.0.0.0', 'server.socket_port': args.json }) cherrypy.quickstart(tunnel) else: while True: time.sleep(1)