Exemplo n.º 1
0
    async def setUp(self):
        await super(TestResourceMonitor, self).setUp()

        mock_session = MockObject()
        mock_session.config = MockObject()
        mock_session.config.get_resource_monitor_history_size = lambda: 1
        mock_session.config.get_resource_monitor_poll_interval = lambda: 20
        mock_session.config.get_state_dir = lambda: path_util.Path(".")
        mock_session.config.get_log_dir = lambda: path_util.Path("logs")
        mock_session.config.get_resource_monitor_enabled = lambda: False
        self.resource_monitor = ResourceMonitor(mock_session)
        self.resource_monitor.session.notifier = MockObject()
        self.resource_monitor.session.notifier.notify = lambda subject, changeType, obj_id, *args: None
Exemplo n.º 2
0
 def test_get_set_methods_tunnel_community(self):
     """
     Check whether tunnel community get and set methods are working as expected.
     """
     self.tribler_config.set_tunnel_community_enabled(True)
     self.assertEqual(self.tribler_config.get_tunnel_community_enabled(),
                      True)
     self.tribler_config.set_tunnel_community_socks5_listen_ports([-1])
     self.assertNotEqual(
         self.tribler_config.get_tunnel_community_socks5_listen_ports(),
         [-1])
     self.tribler_config.set_tunnel_community_socks5_listen_ports([5])
     self.assertEqual(
         self.tribler_config.get_tunnel_community_socks5_listen_ports(),
         [5])
     self.tribler_config.set_tunnel_community_exitnode_enabled(True)
     self.assertEqual(
         self.tribler_config.get_tunnel_community_exitnode_enabled(), True)
     self.tribler_config.set_default_number_hops(True)
     self.assertEqual(self.tribler_config.get_default_number_hops(), True)
     self.tribler_config.set_default_anonymity_enabled(True)
     self.assertEqual(self.tribler_config.get_default_anonymity_enabled(),
                      True)
     self.tribler_config.set_default_safeseeding_enabled(True)
     self.assertEqual(self.tribler_config.get_default_safeseeding_enabled(),
                      True)
     self.tribler_config.set_default_destination_dir(get_home_dir())
     self.assertEqual(self.tribler_config.get_default_destination_dir(),
                      path_util.Path(get_home_dir()))
     self.tribler_config.set_tunnel_community_random_slots(10)
     self.assertEqual(
         self.tribler_config.get_tunnel_community_random_slots(), 10)
     self.tribler_config.set_tunnel_community_competing_slots(20)
     self.assertEqual(
         self.tribler_config.get_tunnel_community_competing_slots(), 20)
Exemplo n.º 3
0
    def check_watch_folder(self):
        if not self.session.config.get_watch_folder_path().is_dir():
            return

        # Make sure that we pass a str to os.walk
        watch_dir = str(self.session.config.get_watch_folder_path())

        for root, _, files in os.walk(watch_dir):
            root = path_util.Path(root)
            for name in files:
                if not name.endswith(".torrent"):
                    continue

                try:
                    tdef = TorrentDef.load(root / name)
                    if not tdef.get_metainfo():
                        self.cleanup_torrent_file(root, name)
                        continue
                except:  # torrent appears to be corrupt
                    self.cleanup_torrent_file(root, name)
                    continue

                infohash = tdef.get_infohash()

                if not self.session.dlmgr.download_exists(infohash):
                    self._logger.info("Starting download from torrent file %s", name)
                    dl_config = DownloadConfig()

                    anon_enabled = self.session.config.get_default_anonymity_enabled()
                    default_num_hops = self.session.config.get_default_number_hops()
                    dl_config.set_hops(default_num_hops if anon_enabled else 0)
                    dl_config.set_safe_seeding(self.session.config.get_default_safeseeding_enabled())
                    dl_config.set_dest_dir(self.session.config.get_default_destination_dir())
                    self.session.dlmgr.start_download(tdef=tdef, config=dl_config)
Exemplo n.º 4
0
    def check_watch_folder(self):
        if not self.watch_folder.is_dir():
            return

        # Make sure that we pass a str to os.walk
        watch_dir = str(self.watch_folder)

        for root, _, files in os.walk(watch_dir):
            root = path_util.Path(root)
            for name in files:
                if not name.endswith(".torrent"):
                    continue

                try:
                    tdef = TorrentDef.load(root / name)
                    if not tdef.get_metainfo():
                        self.cleanup_torrent_file(root, name)
                        continue
                except:  # torrent appears to be corrupt
                    self.cleanup_torrent_file(root, name)
                    continue

                infohash = tdef.get_infohash()

                if not self.download_manager.download_exists(infohash):
                    self._logger.info("Starting download from torrent file %s",
                                      name)
                    self.download_manager.start_download(torrent_file=root /
                                                         name)
Exemplo n.º 5
0
 def get_files_with_length(self, exts=None):
     """ The list of files in the torrent def.
     @param exts (Optional) list of filename extensions (without leading .)
     to search for.
     @return A list of filenames.
     """
     videofiles = []
     for filename, length in self._get_all_files_as_unicode_with_length():
         ext = path_util.Path(filename).suffix
         if ext != "" and ext[0] == ".":
             ext = ext[1:]
         if exts is None or ext.lower() in exts:
             videofiles.append((filename, length))
     return videofiles
Exemplo n.º 6
0
    def get_index_of_file_in_files(self, file):
        if not self.metainfo:
            raise ValueError("TorrentDef does not have metainfo")
        info = self.metainfo[b'info']

        if file is not None and b'files' in info:
            for i in range(len(info[b'files'])):
                file_dict = info[b'files'][i]

                if b'path.utf-8' in file_dict:
                    intorrentpath = maketorrent.pathlist2filename(file_dict[b'path.utf-8'])
                else:
                    intorrentpath = maketorrent.pathlist2filename(file_dict[b'path'])

                if intorrentpath == path_util.Path(ensure_unicode(file, 'utf8')):
                    return i
            raise ValueError("File not found in torrent")
        else:
            raise ValueError("File not found in single-file torrent")
Exemplo n.º 7
0
    def __init__(self, *args, **kwargs):
        self.tribler_session = kwargs.pop('tribler_session', None)
        num_competing_slots = kwargs.pop('competing_slots', 15)
        num_random_slots = kwargs.pop('random_slots', 5)
        self.bandwidth_community = kwargs.pop('bandwidth_community', None)
        socks_listen_ports = kwargs.pop('socks_listen_ports', None)
        state_path = self.tribler_session.config.get_state_dir(
        ) if self.tribler_session else path_util.Path()
        self.exitnode_cache = kwargs.pop('exitnode_cache',
                                         state_path / 'exitnode_cache.dat')
        super().__init__(*args, **kwargs)
        self._use_main_thread = True

        if self.tribler_session:
            if self.tribler_session.config.get_tunnel_community_exitnode_enabled(
            ):
                self.settings.peer_flags.add(PEER_FLAG_EXIT_BT)
                self.settings.peer_flags.add(PEER_FLAG_EXIT_IPV8)
                self.settings.peer_flags.add(PEER_FLAG_EXIT_HTTP)

            if not socks_listen_ports:
                socks_listen_ports = self.tribler_session.config.get_tunnel_community_socks5_listen_ports(
                )
        elif socks_listen_ports is None:
            socks_listen_ports = range(1080, 1085)

        self.bittorrent_peers = {}
        self.dispatcher = TunnelDispatcher(self)
        self.download_states = {}
        self.competing_slots = [
            (0, None)
        ] * num_competing_slots  # 1st tuple item = token balance, 2nd = circuit id
        self.random_slots = [None] * num_random_slots
        self.reject_callback = None  # This callback is invoked with a tuple (time, balance) when we reject a circuit
        self.last_forced_announce = {}

        # Start the SOCKS5 servers
        self.socks_servers = []
        for port in socks_listen_ports:
            socks_server = Socks5Server(port, self.dispatcher)
            self.register_task('start_socks_%d' % port, socks_server.start)
            self.socks_servers.append(socks_server)

        self.dispatcher.set_socks_servers(self.socks_servers)

        self.add_message_handler(BandwidthTransactionPayload, self.on_payout)

        self.add_cell_handler(BalanceRequestPayload,
                              self.on_balance_request_cell)
        self.add_cell_handler(RelayBalanceRequestPayload,
                              self.on_relay_balance_request_cell)
        self.add_cell_handler(BalanceResponsePayload,
                              self.on_balance_response_cell)
        self.add_cell_handler(RelayBalanceResponsePayload,
                              self.on_relay_balance_response_cell)
        self.add_cell_handler(HTTPRequestPayload, self.on_http_request)
        self.add_cell_handler(HTTPResponsePayload, self.on_http_response)

        NO_CRYPTO_PACKETS.extend(
            [BalanceRequestPayload.msg_id, BalanceResponsePayload.msg_id])

        if self.exitnode_cache:
            self.restore_exitnodes_from_disk()
Exemplo n.º 8
0
 def get_desktop_dir():
     # http://www.mvps.org/access/api/api0054.htm
     # CSIDL_DESKTOPDIRECTORY = &H10
     # C:\Documents and Settings\username\Desktop
     return path_util.Path(shell.SHGetSpecialFolderPath(0, 0x10))
Exemplo n.º 9
0
 def get_picture_dir():
     # http://www.mvps.org/access/api/api0054.htm
     # CSIDL_MYPICTURES = &H27
     # C:\Documents and Settings\username\My Documents\My Pictures
     return path_util.Path(shell.SHGetSpecialFolderPath(0, 0x27))
Exemplo n.º 10
0
 def get_appstate_dir():
     # http://www.mvps.org/access/api/api0054.htm
     # CSIDL_APPDATA = &H1A
     # C:\Documents and Settings\username\Application Data
     return path_util.Path(
         shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_APPDATA))
Exemplo n.º 11
0
 def get_home_dir():
     # http://www.mvps.org/access/api/api0054.htm
     # CSIDL_PROFILE = &H28
     # C:\Documents and Settings\username
     return path_util.Path(
         shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_PROFILE))
Exemplo n.º 12
0
def create_torrent_file(file_path_list, params, torrent_filepath=None):
    fs = libtorrent.file_storage()

    # filter all non-files
    file_path_list_filtered = []
    for path in file_path_list:
        path = path_util.Path(path)
        if not path.exists():
            raise IOError('Path does not exist: %s' % path)
        elif path.is_file():
            file_path_list_filtered.append(path)

    # get the directory where these files are in. If there are multiple files, take the common directory they are in
    if len(file_path_list_filtered) == 1:
        base_path = path_util.split(file_path_list_filtered[0])[0]
    else:
        base_path = path_util.abspath(commonprefix(file_path_list_filtered))

    # the base_dir directory is the parent directory of the base_path and is passed to the set_piece_hash method

    if len(file_path_list_filtered) == 1:
        filename = path_util.basename(file_path_list_filtered[0])
        fs.add_file(filename, path_util.getsize(file_path_list_filtered[0]))
    else:
        for full_file_path in file_path_list_filtered:
            #FIXME: there should be a better, cleaner way to define this
            filename = path_util.join(
                *full_file_path.parts[len(base_path.parent.parts):])
            fs.add_file(str(filename), path_util.getsize(full_file_path))

    if params.get(b'piece length'):
        piece_size = params[b'piece length']
    else:
        piece_size = 0

    flags = libtorrent.create_torrent_flags_t.optimize

    # This flag doesn't exist anymore in libtorrent V1.1.0
    if hasattr(libtorrent.create_torrent_flags_t, 'calculate_file_hashes'):
        flags |= libtorrent.create_torrent_flags_t.calculate_file_hashes

    params = {
        k: (v.decode('utf-8') if isinstance(v, bytes) else v)
        for k, v in params.items()
    }

    torrent = libtorrent.create_torrent(fs, piece_size=piece_size, flags=flags)
    # Python2 wants binary, python3 want unicode
    if params.get(b'comment'):
        torrent.set_comment(params[b'comment'])
    if params.get(b'created by'):
        torrent.set_creator(params[b'created by'])
    # main tracker
    if params.get(b'announce'):
        torrent.add_tracker(params[b'announce'])
    # tracker list
    if params.get(b'announce-list'):
        tier = 1
        for tracker in params[b'announce-list']:
            torrent.add_tracker(tracker, tier=tier)
            tier += 1
    # DHT nodes
    # http://www.bittorrent.org/beps/bep_0005.html
    if params.get(b'nodes'):
        for node in params[b'nodes']:
            torrent.add_node(*node)
    # HTTP seeding
    # http://www.bittorrent.org/beps/bep_0017.html
    if params.get(b'httpseeds'):
        torrent.add_http_seed(params[b'httpseeds'])

    # Web seeding
    # http://www.bittorrent.org/beps/bep_0019.html
    if len(file_path_list) == 1:
        if params.get(b'urllist', False):
            torrent.add_url_seed(params[b'urllist'])

    # read the files and calculate the hashes
    if len(file_path_list) == 1:
        libtorrent.set_piece_hashes(torrent, str(base_path))
    else:
        libtorrent.set_piece_hashes(torrent, str(base_path.parent))

    t1 = torrent.generate()
    torrent = libtorrent.bencode(t1)

    if torrent_filepath:
        with open(torrent_filepath, 'wb') as f:
            f.write(torrent)

    return {
        'success': True,
        'base_path': base_path,
        'base_dir': base_path.parent,
        'torrent_file_path': torrent_filepath,
        'metainfo': torrent,
        'infohash': sha1(bencode(t1[b'info'])).digest()
    }
Exemplo n.º 13
0
    def __init__(self, *args, **kwargs):
        self.tribler_session = kwargs.pop('tribler_session', None)
        num_competing_slots = kwargs.pop('competing_slots', 15)
        num_random_slots = kwargs.pop('random_slots', 5)
        self.bandwidth_wallet = kwargs.pop('bandwidth_wallet', None)
        socks_listen_ports = kwargs.pop('socks_listen_ports', None)
        state_path = self.tribler_session.config.get_state_dir(
        ) if self.tribler_session else path_util.Path()
        self.exitnode_cache = kwargs.pop('exitnode_cache',
                                         state_path / 'exitnode_cache.dat')
        super(TriblerTunnelCommunity, self).__init__(*args, **kwargs)
        self._use_main_thread = True

        if self.tribler_session:
            if self.tribler_session.config.get_tunnel_community_exitnode_enabled(
            ):
                self.settings.peer_flags.add(PEER_FLAG_EXIT_ANY)
                self.settings.peer_flags.add(PEER_FLAG_EXIT_HTTP)

            if not socks_listen_ports:
                socks_listen_ports = self.tribler_session.config.get_tunnel_community_socks5_listen_ports(
                )
        elif socks_listen_ports is None:
            socks_listen_ports = range(1080, 1085)

        self.bittorrent_peers = {}
        self.dispatcher = TunnelDispatcher(self)
        self.download_states = {}
        self.competing_slots = [
            (0, None)
        ] * num_competing_slots  # 1st tuple item = token balance, 2nd = circuit id
        self.random_slots = [None] * num_random_slots
        self.reject_callback = None  # This callback is invoked with a tuple (time, balance) when we reject a circuit
        self.last_forced_announce = {}

        # Start the SOCKS5 servers
        self.socks_servers = []
        for port in socks_listen_ports:
            socks_server = Socks5Server(port, self.dispatcher)
            self.register_task('start_socks_%d' % port, socks_server.start)
            self.socks_servers.append(socks_server)

        self.dispatcher.set_socks_servers(self.socks_servers)

        self.decode_map.update({
            chr(23): self.on_payout_block,
        })

        self.decode_map_private.update({
            chr(24): self.on_balance_request_cell,
            chr(25): self.on_relay_balance_request_cell,
            chr(26): self.on_balance_response_cell,
            chr(27): self.on_relay_balance_response_cell,
            chr(28): self.on_http_request,
            chr(29): self.on_http_response
        })

        message_to_payload["balance-request"] = (24, BalanceRequestPayload)
        message_to_payload["relay-balance-request"] = (25,
                                                       BalanceRequestPayload)
        message_to_payload["balance-response"] = (26, BalanceResponsePayload)
        message_to_payload["relay-balance-response"] = (27,
                                                        BalanceResponsePayload)
        message_to_payload["http-request"] = (28, HTTPRequestPayload)
        message_to_payload["http-response"] = (29, HTTPResponsePayload)

        NO_CRYPTO_PACKETS.extend([24, 26])

        if self.exitnode_cache:
            self.restore_exitnodes_from_disk()