Ejemplo n.º 1
0
    def start(self):
        cherrypy.process.plugins.Monitor.start(self)

        self.bus.log('[Downloader] Starting session')
        self.session = libtorrent.session()
        self.session.set_alert_mask(libtorrent.alert.category_t.error_notification | libtorrent.alert.category_t.status_notification | libtorrent.alert.category_t.storage_notification)
        self.session.start_dht()
        self.session.start_lsd()
        self.session.start_upnp()
        self.session.start_natpmp()
        self.session.listen_on(self.torrent_config['port'], self.torrent_config['port'])

        # Session settings
        session_settings = self.session.settings()
        session_settings.announce_to_all_tiers = True
        session_settings.announce_to_all_trackers = True
        session_settings.connection_speed = 100
        session_settings.peer_connect_timeout = 2
        session_settings.rate_limit_ip_overhead = True
        session_settings.request_timeout = 5
        session_settings.torrent_connect_boost = 100

        if self.torrent_config['max_download_rate'] > 0:
            session_settings.download_rate_limit = self.torrent_config['max_download_rate'] * 1024
        if self.torrent_config['max_upload_rate'] > 0:
            session_settings.upload_rate_limit = self.torrent_config['max_upload_rate'] * 1024
        self.session.set_settings(session_settings)

        # Encryption settings
        encryption_settings = libtorrent.pe_settings()
        encryption_settings.out_enc_policy = libtorrent.enc_policy(libtorrent.enc_policy.forced)
        encryption_settings.in_enc_policy = libtorrent.enc_policy(libtorrent.enc_policy.forced)
        encryption_settings.allowed_enc_level = libtorrent.enc_level.both
        encryption_settings.prefer_rc4 = True
        self.session.set_pe_settings(encryption_settings)
Ejemplo n.º 2
0
    def create_session(self, hops=0):
        settings = lt.session_settings()

        if hops == 0:
            settings.user_agent = 'Tribler/' + version_id
            # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
            fingerprint = ['TL'] + map(int, version_id.split('-')[0].split('.')) + [0]
            # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369)
            ltsession = lt.session(lt.fingerprint(*fingerprint), flags=1)
            enable_utp = self.trsession.get_libtorrent_utp()
            settings.enable_outgoing_utp = enable_utp
            settings.enable_incoming_utp = enable_utp

            pe_settings = lt.pe_settings()
            pe_settings.prefer_rc4 = True
            ltsession.set_pe_settings(pe_settings)
        else:
            settings.enable_outgoing_utp = True
            settings.enable_incoming_utp = True
            settings.enable_outgoing_tcp = False
            settings.enable_incoming_tcp = False
            settings.anonymous_mode = True
            # No PEX for anonymous sessions
            ltsession = lt.session(flags=0)
            ltsession.add_extension(lt.create_ut_metadata_plugin)
            ltsession.add_extension(lt.create_smart_ban_plugin)

        ltsession.set_settings(settings)
        ltsession.set_alert_mask(lt.alert.category_t.stats_notification |
                                 lt.alert.category_t.error_notification |
                                 lt.alert.category_t.status_notification |
                                 lt.alert.category_t.storage_notification |
                                 lt.alert.category_t.performance_warning |
                                 lt.alert.category_t.tracker_notification)

        # Load proxy settings
        if hops == 0:
            proxy_settings = self.trsession.get_libtorrent_proxy_settings()
        else:
            proxy_settings = list(self.trsession.get_anon_proxy_settings())
            proxy_host, proxy_ports = proxy_settings[1]
            proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
        self.set_proxy_settings(ltsession, *proxy_settings)

        # Set listen port & start the DHT
        if hops == 0:
            listen_port = self.trsession.get_listen_port()
            ltsession.listen_on(listen_port, listen_port + 10)
            if listen_port != ltsession.listen_port():
                self.trsession.set_listen_port_runtime(ltsession.listen_port())
            try:
                dht_state = open(os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME)).read()
                ltsession.start_dht(lt.bdecode(dht_state))
            except Exception, exc:
                self._logger.info("could not restore dht state, got exception: %r. starting from scratch" % exc)
                ltsession.start_dht(None)
Ejemplo n.º 3
0
 def __init__(self):
     self.ses = lt.session()
     self.ses.add_extension(lt.create_ut_pex_plugin)
     self.ses.start_dht()
     self.ses.add_dht_router("router.bittorrent.com", 6881)
     self.ses.add_dht_router("router.utorrent.com", 6881)
     pe = lt.pe_settings()
     pe.out_enc_policy = lt.enc_policy.forced
     pe.in_enc_policy = lt.enc_policy.forced
     self.ses.set_pe_settings(pe)
Ejemplo n.º 4
0
    def start(self):
        cherrypy.process.plugins.Monitor.start(self)

        self.bus.log('[Downloader] Starting session')
        self.session = libtorrent.session()
        self.session.set_alert_mask(libtorrent.alert.category_t.error_notification | libtorrent.alert.category_t.status_notification | libtorrent.alert.category_t.storage_notification)
        
        # Session settings
        session_settings = self.session.settings()
        session_settings.announce_to_all_tiers = True
        session_settings.announce_to_all_trackers = True
        session_settings.connection_speed = 100
        session_settings.peer_connect_timeout = 2
        session_settings.rate_limit_ip_overhead = True
        session_settings.request_timeout = 5
        session_settings.torrent_connect_boost = 100

        if self.torrent_config['max_download_rate'] > 0:
            session_settings.download_rate_limit = self.torrent_config['max_download_rate'] * 1024
        if self.torrent_config['max_upload_rate'] > 0:
            session_settings.upload_rate_limit = self.torrent_config['max_upload_rate'] * 1024
        self.session.set_settings(session_settings)

        # Proxy settings
        proxy_settings = libtorrent.proxy_settings()
        if self.torrent_config['proxy_type'] == 'SOCKS5':
            proxy_settings.hostname = self.torrent_config['proxy_host']
            proxy_settings.port     = self.torrent_config['proxy_port']
            if self.torrent_config['proxy_user']:
                proxy_settings.type = libtorrent.proxy_type.socks5_pw
                proxy_settings.username = self.torrent_config['proxy_user']
                proxy_settings.password = self.torrent_config['proxy_password']
            else:
                proxy_settings.type = libtorrent.proxy_type.socks5
        self.session.set_proxy(proxy_settings)

        # Encryption settings
        encryption_settings = libtorrent.pe_settings()
        encryption_settings.out_enc_policy = libtorrent.enc_policy(libtorrent.enc_policy.forced)
        encryption_settings.in_enc_policy = libtorrent.enc_policy(libtorrent.enc_policy.forced)
        encryption_settings.allowed_enc_level = libtorrent.enc_level.both
        encryption_settings.prefer_rc4 = True
        self.session.set_pe_settings(encryption_settings)

        self.session.start_dht()
        self.session.start_lsd()
        
        if self.torrent_config['upnp_natpmp_enabled']:
            self.session.start_upnp()
            self.session.start_natpmp()
        
        self.session.listen_on(self.torrent_config['port'], self.torrent_config['port'])
Ejemplo n.º 5
0
 def set_encryption(self):
     if self.pe_set is None:
         self.pe_set = lt.pe_settings()
     enc_req = app.config.get(prefs.BT_ENC_REQ)
     if enc_req != self.enc_req:
         self.enc_req = enc_req
         if enc_req:
             self.pe_set.in_enc_policy = lt.enc_policy.forced
             self.pe_set.out_enc_policy = lt.enc_policy.forced
         else:
             self.pe_set.in_enc_policy = lt.enc_policy.enabled
             self.pe_set.out_enc_policy = lt.enc_policy.enabled
         self.session.set_pe_settings(self.pe_set)
Ejemplo n.º 6
0
    def test_pe_settings(self) -> None:
        pe = lt.pe_settings()

        pe.out_enc_policy = 123
        self.assertEqual(pe.out_enc_policy, 123)

        pe.in_enc_policy = 123
        self.assertEqual(pe.in_enc_policy, 123)

        pe.allowed_enc_level = 123
        self.assertEqual(pe.allowed_enc_level, 123)

        self.assertFalse(pe.prefer_rc4)
        pe.prefer_rc4 = True
        self.assertTrue(pe.prefer_rc4)
Ejemplo n.º 7
0
 def test_deprecated(self) -> None:
     with self.assertWarns(DeprecationWarning):
         lt.pe_settings()
Ejemplo n.º 8
0
    def create_session(self, hops=0, store_listen_port=True):
        settings = {}

        # Due to a bug in Libtorrent 0.16.18, the outgoing_port and num_outgoing_ports value should be set in
        # the settings dictionary
        settings['outgoing_port'] = 0
        settings['num_outgoing_ports'] = 1

        # Copy construct so we don't modify the default list
        extensions = list(DEFAULT_LT_EXTENSIONS)

        # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
        fingerprint = ['TL'] + map(int, version_id.split('-')[0].split('.')) + [0]
        ltsession = lt.session(lt.fingerprint(*fingerprint), flags=0) if hops == 0 else lt.session(flags=0)

        if hops == 0:
            settings['user_agent'] = 'Tribler/' + version_id
            enable_utp = self.tribler_session.config.get_libtorrent_utp()
            settings['enable_outgoing_utp'] = enable_utp
            settings['enable_incoming_utp'] = enable_utp

            if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings['prefer_rc4'] = True
                settings["listen_interfaces"] = "0.0.0.0:%d" % self.tribler_session.config.get_libtorrent_port()
            else:
                pe_settings = lt.pe_settings()
                pe_settings.prefer_rc4 = True
                ltsession.set_pe_settings(pe_settings)

            mid = self.tribler_session.trustchain_keypair.key_to_hash()
            settings['peer_fingerprint'] = mid
            settings['handshake_client_version'] = 'Tribler/' + version_id + '/' + mid.encode('hex')
        else:
            settings['enable_outgoing_utp'] = True
            settings['enable_incoming_utp'] = True
            settings['enable_outgoing_tcp'] = False
            settings['enable_incoming_tcp'] = False
            settings['anonymous_mode'] = True
            settings['force_proxy'] = True

            if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings["listen_interfaces"] = "0.0.0.0:%d" % self.tribler_session.config.get_anon_listen_port()

            # No PEX for anonymous sessions
            if lt.create_ut_pex_plugin in extensions:
                extensions.remove(lt.create_ut_pex_plugin)

        ltsession.set_settings(settings)
        ltsession.set_alert_mask(self.default_alert_mask)

        # Load proxy settings
        if hops == 0:
            proxy_settings = self.tribler_session.config.get_libtorrent_proxy_settings()
        else:
            proxy_settings = list(self.tribler_session.config.get_anon_proxy_settings())
            proxy_host, proxy_ports = proxy_settings[1]
            proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
        self.set_proxy_settings(ltsession, *proxy_settings)

        for extension in extensions:
            ltsession.add_extension(extension)

        # Set listen port & start the DHT
        if hops == 0:
            listen_port = self.tribler_session.config.get_libtorrent_port()
            ltsession.listen_on(listen_port, listen_port + 10)
            if listen_port != ltsession.listen_port() and store_listen_port:
                self.tribler_session.config.set_libtorrent_port_runtime(ltsession.listen_port())
            try:
                lt_state = lt.bdecode(
                    open(os.path.join(self.tribler_session.config.get_state_dir(), LTSTATE_FILENAME)).read())
                if lt_state is not None:
                    ltsession.load_state(lt_state)
                else:
                    self._logger.warning("the lt.state appears to be corrupt, writing new data on shutdown")
            except Exception, exc:
                self._logger.info("could not load libtorrent state, got exception: %r. starting from scratch" % exc)
            ltsession.start_dht()
 def startSession(self):
     logging.info('Starting session...')
     self.session = lt.session(lt.fingerprint('LT', lt.version_major, lt.version_minor, 0, 0),
                          flags=int(lt.session_flags_t.add_default_plugins))
     alertMask = (lt.alert.category_t.error_notification | 
                  lt.alert.category_t.storage_notification | 
                  lt.alert.category_t.tracker_notification |
                  lt.alert.category_t.status_notification)
     if self.config.debugAlerts:
         alertMask |= lt.alert.category_t.debug_notification
     self.session.set_alert_mask(alertMask)
     
     settings = self.session.get_settings()
     settings["request_timeout"] = self.config.requestTimeout
     settings["peer_connect_timeout"] = self.config.peerConnectTimeout
     settings["announce_to_all_trackers"] = True
     settings["announce_to_all_tiers"] = True
     settings["torrent_connect_boost"] = self.config.torrentConnectBoost
     settings["connection_speed"] = self.config.connectionSpeed
     settings["min_reconnect_time"] = self.config.minReconnectTime
     settings["max_failcount"] = self.config.maxFailCount
     settings["recv_socket_buffer_size"] = 1024 * 1024
     settings["send_socket_buffer_size"] = 1024 * 1024
     settings["rate_limit_ip_overhead"] = True
     settings["min_announce_interval"] = 60
     settings["tracker_backoff"] = 0
     self.session.set_settings(settings)
     
     if self.config.stateFile != '':
         logging.info('Loading session state from %s' % (self.config.stateFile,))
         try:
             with open(self.config.stateFile, 'rb') as f:
                 bytes__ = f.read()
         except IOError as e:
             strerror = e.args
             logging.error(strerror)
         else:
             self.session.load_state(lt.bdecode(bytes__))
     
     rand = SystemRandom(time.time())
     portLower = self.config.listenPort
     if self.config.randomPort:
         portLower = rand.randint(0, 16374) + 49151
     portUpper = portLower + 10
     try:
         self.session.listen_on(portLower, portUpper)
     except IOError as e:
         strerror = e.args
         logging.error(strerror)
         raise
     
     settings = self.session.get_settings()
     if self.config.userAgent != '':
         settings['user_agent'] = self.config.userAgent
     if self.config.connectionsLimit >= 0:
         settings['connections_limit'] = self.config.connectionsLimit
     if self.config.maxDownloadRate >= 0:
         settings['download_rate_limit'] = self.config.maxDownloadRate * 1024
     if self.config.maxUploadRate >= 0:
         settings['upload_rate_limit'] = self.config.maxUploadRate * 1024
     settings['enable_incoming_tcp'] = self.config.enableTCP
     settings['enable_outgoing_tcp'] = self.config.enableTCP
     settings['enable_incoming_utp'] = self.config.enableUTP
     settings['enable_outgoing_utp'] = self.config.enableUTP
     self.session.set_settings(settings)
     
     if self.config.dhtRouters != '':
         routers = self.config.dhtRouters.split(',')
         for router in routers:
             router = router.strip()
             if router != '':
                 hostPort = router.split(':')
                 host = hostPort[0].strip()
                 try:
                     port = len(hostPort) > 1 and int(hostPort[1].strip()) or 6881
                 except ValueError as e:
                     strerror = e.args
                     logging.error(strerror)
                     raise
                 self.session.add_dht_router(host, port)
                 logging.info('Added DHT router: %s:%d' % (host, port))
     logging.info('Setting encryption settings')
     try:
         encryptionSettings = lt.pe_settings()
         encryptionSettings.out_enc_policy = lt.enc_policy(self.config.encryption)
         encryptionSettings.in_enc_policy = lt.enc_policy(self.config.encryption)
         encryptionSettings.allowed_enc_level = lt.enc_level.both
         encryptionSettings.prefer_rc4 = True
         self.session.set_pe_settings(encryptionSettings)
     except Exception as e:
         logging.info('Encryption not supported: %s' % (e.args,))
Ejemplo n.º 10
0
    def create_session(self, hops=0, store_listen_port=True):
        # Due to a bug in Libtorrent 0.16.18, the outgoing_port and num_outgoing_ports value should be set in
        # the settings dictionary
        settings = {'outgoing_port': 0,
                    'num_outgoing_ports': 1,
                    'allow_multiple_connections_per_ip': 0}

        # Copy construct so we don't modify the default list
        extensions = list(DEFAULT_LT_EXTENSIONS)

        # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
        fingerprint = ['TL'] + [int(x) for x in version_id.split('-')[0].split('.')] + [0]
        ltsession = lt.session(lt.fingerprint(*fingerprint), flags=0) if hops == 0 else lt.session(flags=0)

        if hops == 0:
            settings['user_agent'] = 'Tribler/' + version_id
            enable_utp = self.tribler_session.config.get_libtorrent_utp()
            settings['enable_outgoing_utp'] = enable_utp
            settings['enable_incoming_utp'] = enable_utp

            if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings['prefer_rc4'] = True
                settings["listen_interfaces"] = "0.0.0.0:%d" % self.tribler_session.config.get_libtorrent_port()
            else:
                pe_settings = lt.pe_settings()
                pe_settings.prefer_rc4 = True
                ltsession.set_pe_settings(pe_settings)

            mid = self.tribler_session.trustchain_keypair.key_to_hash()
            settings['peer_fingerprint'] = mid
            settings['handshake_client_version'] = 'Tribler/' + version_id + '/' + hexlify(mid)
        else:
            settings['enable_outgoing_utp'] = True
            settings['enable_incoming_utp'] = True
            settings['enable_outgoing_tcp'] = False
            settings['enable_incoming_tcp'] = False
            settings['anonymous_mode'] = True
            settings['force_proxy'] = True

            if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings["listen_interfaces"] = "0.0.0.0:%d" % self.tribler_session.config.get_anon_listen_port()

            # By default block all IPs except 1.1.1.1 (which is used to ensure libtorrent makes a connection to us)
            self.update_ip_filter(ltsession, ['1.1.1.1'])

        self.set_session_settings(ltsession, settings)
        ltsession.set_alert_mask(self.default_alert_mask)

        # Load proxy settings
        if hops == 0:
            proxy_settings = self.tribler_session.config.get_libtorrent_proxy_settings()
        else:
            proxy_settings = list(self.tribler_session.config.get_anon_proxy_settings())
            proxy_host, proxy_ports = proxy_settings[1]
            proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
        self.set_proxy_settings(ltsession, *proxy_settings)

        for extension in extensions:
            ltsession.add_extension(extension)

        # Set listen port & start the DHT
        if hops == 0:
            listen_port = self.tribler_session.config.get_libtorrent_port()
            ltsession.listen_on(listen_port, listen_port + 10)
            if listen_port != ltsession.listen_port() and store_listen_port:
                self.tribler_session.config.set_libtorrent_port_runtime(ltsession.listen_port())
            try:
                with open(self.tribler_session.config.get_state_dir() / LTSTATE_FILENAME, 'rb') as fp:
                    lt_state = bdecode_compat(fp.read())
                if lt_state is not None:
                    ltsession.load_state(lt_state)
                else:
                    self._logger.warning("the lt.state appears to be corrupt, writing new data on shutdown")
            except Exception as exc:
                self._logger.info("could not load libtorrent state, got exception: %r. starting from scratch" % exc)
        else:
            ltsession.listen_on(self.tribler_session.config.get_anon_listen_port(),
                                self.tribler_session.config.get_anon_listen_port() + 20)

            settings = {'upload_rate_limit': self.tribler_session.config.get_libtorrent_max_upload_rate(),
                        'download_rate_limit': self.tribler_session.config.get_libtorrent_max_download_rate()}
            self.set_session_settings(ltsession, settings)

        if self.tribler_session.config.get_libtorrent_dht_enabled():
            ltsession.start_dht()
            for router in DEFAULT_DHT_ROUTERS:
                ltsession.add_dht_router(*router)
            ltsession.start_lsd()

        self._logger.debug("Started libtorrent session for %d hops on port %d", hops, ltsession.listen_port())
        self.lt_session_shutdown_ready[hops] = False

        return ltsession
Ejemplo n.º 11
0
    def create_session(self, hops=0, store_listen_port=True):
        settings = {}

        # Due to a bug in Libtorrent 0.16.18, the outgoing_port and num_outgoing_ports value should be set in
        # the settings dictionary
        settings['outgoing_port'] = 0
        settings['num_outgoing_ports'] = 1

        # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
        fingerprint = ['TL'] + map(int,
                                   version_id.split('-')[0].split('.')) + [0]
        ltsession = lt.session(lt.fingerprint(
            *fingerprint), flags=1) if hops == 0 else lt.session(flags=0)

        if hops == 0:
            settings['user_agent'] = 'Tribler/' + version_id
            enable_utp = self.trsession.get_libtorrent_utp()
            settings['enable_outgoing_utp'] = enable_utp
            settings['enable_incoming_utp'] = enable_utp

            if LooseVersion(
                    self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings['prefer_rc4'] = True
                settings[
                    "listen_interfaces"] = "0.0.0.0:%d" % self.trsession.get_listen_port(
                    )
            else:
                pe_settings = lt.pe_settings()
                pe_settings.prefer_rc4 = True
                ltsession.set_pe_settings(pe_settings)
        else:
            settings['enable_outgoing_utp'] = True
            settings['enable_incoming_utp'] = True
            settings['enable_outgoing_tcp'] = False
            settings['enable_incoming_tcp'] = False
            settings['anonymous_mode'] = True
            settings['force_proxy'] = True

            if LooseVersion(
                    self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings[
                    "listen_interfaces"] = "0.0.0.0:%d" % self.trsession.get_anon_listen_port(
                    )

            # No PEX for anonymous sessions
            ltsession = lt.session(flags=0)
            ltsession.add_extension(lt.create_ut_metadata_plugin)
            ltsession.add_extension(lt.create_smart_ban_plugin)

        ltsession.set_settings(settings)
        ltsession.set_alert_mask(lt.alert.category_t.stats_notification
                                 | lt.alert.category_t.error_notification
                                 | lt.alert.category_t.status_notification
                                 | lt.alert.category_t.storage_notification
                                 | lt.alert.category_t.performance_warning
                                 | lt.alert.category_t.tracker_notification)

        # Load proxy settings
        if hops == 0:
            proxy_settings = self.trsession.get_libtorrent_proxy_settings()
        else:
            proxy_settings = list(self.trsession.get_anon_proxy_settings())
            proxy_host, proxy_ports = proxy_settings[1]
            proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
        self.set_proxy_settings(ltsession, *proxy_settings)

        # Set listen port & start the DHT
        if hops == 0:
            listen_port = self.trsession.get_listen_port()
            ltsession.listen_on(listen_port, listen_port + 10)
            if listen_port != ltsession.listen_port() and store_listen_port:
                self.trsession.set_listen_port_runtime(ltsession.listen_port())
            try:
                lt_state = lt.bdecode(
                    open(
                        os.path.join(self.trsession.get_state_dir(),
                                     LTSTATE_FILENAME)).read())
                if lt_state is not None:
                    ltsession.load_state(lt_state)
                else:
                    self._logger.warning(
                        "the lt.state appears to be corrupt, writing new data on shutdown"
                    )
            except Exception, exc:
                self._logger.info(
                    "could not load libtorrent state, got exception: %r. starting from scratch"
                    % exc)
            ltsession.start_dht()
Ejemplo n.º 12
0
    def __init__(self, name, root, datacenter, bt_start_port, webip, webport, webdir, loglevel, lazy, create=False):
        self.name = name    # cluster name
        self.bj_name = self.name+'__'+uuid.uuid4().hex    # mDNS name
        if webport is not None:
            self.httpd = WebUIServer((webip, webport), WebUIHandler)
            print 'WebUIServer listening on: http://%s:%d/' % (webip, webport)
        else:
            self.httpd = type('blank', (object,), {})()
        self.httpd.api = {'webdir':webdir,
                'webip':webip,
                'webport':str(webport),
                'lazy':str(lazy),
                'name':name,
                'btport':str(bt_start_port),
                'root':root,
                'servicename':self.bj_name,
                'hostname':socket.gethostname(),
                'datacenter':datacenter,
                'mount':'-',
                'freespace': 0,
                'cluster_freespace': 0,
                'version':'v%s' % APP_VERSION
                }
        self.httpd.bt_handles = {}
        self.httpd.peers = {}
        self.httpd.nametoaddr = {}
        self.bootstrapping = True
        self.LOGLEVEL = loglevel
        self.lazy = lazy

        self.root = os.path.realpath(root)
        self.indexdir = os.path.join(self.root, 'meta', 'index').encode(FS_ENCODE)
        self.metadir = os.path.join(self.root, 'meta').encode(FS_ENCODE)
        self.chunksdir = os.path.join(self.root, 'chunks').encode(FS_ENCODE)
        self.tmp = os.path.join(self.root, 'tmp').encode(FS_ENCODE)
        if not os.path.isdir(self.root):
            os.mkdir(self.root)

        self.next_time_to_check_for_undermirrored_files = datetime.datetime.now() + datetime.timedelta(0,10+random.randint(0,30))
        self.last_read_file = {}

        self.bt_in_progress = set()
        self.bt_session = libtorrent.session()
        self.bt_session.listen_on(bt_start_port, bt_start_port+10)
        pe_settings = libtorrent.pe_settings()
        pe_enc_policy = {0:libtorrent.enc_policy.forced, 1:libtorrent.enc_policy.enabled, 2:libtorrent.enc_policy.disabled}
        pe_settings.out_enc_policy = libtorrent.enc_policy(pe_enc_policy[0])
        pe_settings.in_enc_policy = libtorrent.enc_policy(pe_enc_policy[0])
        pe_enc_level = {0:libtorrent.enc_level.plaintext, 1:libtorrent.enc_level.rc4, 2:libtorrent.enc_level.both}
        pe_settings.allowed_enc_level = libtorrent.enc_level(pe_enc_level[1])
        self.bt_session.set_pe_settings(pe_settings)
        self.bt_port = self.bt_session.listen_port()
        self.httpd.api['btport'] = self.bt_port

        # no libtorrent lsd for private if we use h.connect_peer
        self.bt_session.start_lsd()
        # self.bt_session.stop_lsd()

        # no libtorrent dht for private if we use h.connect_peer
        # self.bt_session.start_dht()
        self.bt_session.stop_dht()

        print 'libtorrent listening on:', self.bt_port
        # self.bt_session.add_dht_router('localhost', 10670)
        print '...dht_state()', self.bt_session.dht_state()

        thread = Thread(target=self.__start_webui)
        thread.daemon = True
        thread.start()
        thread = Thread(target=self.__bonjour_start_listening)
        thread.daemon = True
        thread.start()
        print 'give me a sec to look for other peers...'
        time.sleep(2)

        cnfn = os.path.join(self.metadir, '__delugefs__', 'cluster_name').encode(FS_ENCODE)
        if create:
            if os.listdir(self.root):
                files = [x for x in os.listdir(self.root) if x!=".git.meta" and x!="chunks" and x!="meta" and x!="tmp" ]
                if files:
                    raise Exception('--create specified, but %s is not empty' % self.root)
            if self.httpd.peers:
                raise Exception('--create specified, but i found %i peer%s using --id "%s" already' % (len(self.httpd.peers), 's' if len(self.httpd.peers)>1 else '', self.name))

            if not os.path.isdir(self.metadir): os.makedirs(self.metadir)
            if not os.path.isdir(os.path.dirname(cnfn)): os.mkdir(os.path.dirname(cnfn))
            with open(cnfn, 'w') as f:
                f.write(self.name)

            if not os.path.isdir(self.indexdir): os.makedirs(self.indexdir)
            with open(os.path.join(self.indexdir, '.__delugefs_dir__').encode(FS_ENCODE),'w') as f:
                f.write("git doesn't track empty dirs, so we add this file.")
        else:
            if os.path.isfile(cnfn):
                with open(cnfn, 'r') as f:
                    existing_cluster_name = f.read().strip()
                    if existing_cluster_name != self.name:
                        raise Exception('a cluster root exists at %s, but its name is "%s", not "%s"' % (self.root, existing_cluster_name, self.name))
            else:
                if os.listdir(self.root):
                    raise Exception('root %s is not empty, but no cluster was found' % self.root)
                if not self.httpd.peers:
                    raise Exception('--create not specified, no repo exists at %s and no peers of cluster "%s" found' % (self.root, self.name))
                if not os.path.isdir(self.metadir):
                    raise Exception('no repo exists at %s' % self.metadir)

        prune_empty_dirs(self.metadir)

        print '='*80

        if not os.path.isdir(self.tmp): os.makedirs(self.tmp)
        for fn in os.listdir(self.tmp): os.remove(os.path.join(self.tmp,fn).encode(FS_ENCODE))
        if not os.path.isdir(self.chunksdir): os.makedirs(self.chunksdir)

        self.rwlock = Lock()
        self.open_files = {} # used to track opened files except READONLY
        self.bootstrapping = False

        thread = Thread(target=self.__bonjour_register, args=())
        thread.daemon = True
        thread.start()
        thread = Thread(target=self.__load_local_torrents)
        thread.daemon = True
        thread.start()
        thread = Thread(target=self.__monitor)
        thread.daemon = True
        thread.start()
Ejemplo n.º 13
0
    # Initialize Session
    session = libtorrent.session()
    settings = libtorrent.session_settings()
    settings.min_announce_interval = 15

    if options.download_limit > 0 or options.upload_limit > 0:
        if options.download_limit > 0:
            session.set_download_rate_limit(options.download_limit << 10)
        if options.upload_limit > 0:
            session.set_upload_rate_limit(options.upload_limit << 10)

        settings.ignore_limits_on_local_network = False

    # Force Encryption
    if options.encryption:
        pe_settings = libtorrent.pe_settings()
        pe_settings.out_enc_policy = libtorrent.enc_policy.forced
        pe_settings.in_enc_policy = libtorrent.enc_policy.forced
        pe_settings.allowed_enc_level = libtorrent.enc_level.rc4
        pe_settings.prefer_rc4 = True
        session.set_pe_settings(pe_settings)

    session.set_settings(settings)
    session.listen_on(options.port, options.port + 10)

    # Start torrents
    info = libtorrent.torrent_info(libtorrent.bdecode(file(options.torrent).read()))
    torrent_parms = {'ti': info}
    torrent_parms["save_path"] = options.download_dir
    torrent_parms["storage_mode"] = libtorrent.storage_mode_t.storage_mode_sparse
    torrent_parms["paused"] = False
Ejemplo n.º 14
0
    def startSession(self):
        logging.info('Starting session...')
        self.session = lt.session(
            lt.fingerprint('LT', lt.version_major, lt.version_minor, 0, 0),
            flags=int(lt.session_flags_t.add_default_plugins))
        alertMask = (lt.alert.category_t.error_notification
                     | lt.alert.category_t.storage_notification
                     | lt.alert.category_t.tracker_notification
                     | lt.alert.category_t.status_notification)
        if self.config.debugAlerts:
            alertMask |= lt.alert.category_t.debug_notification
        self.session.set_alert_mask(alertMask)

        settings = self.session.get_settings()
        settings["request_timeout"] = self.config.requestTimeout
        settings["peer_connect_timeout"] = self.config.peerConnectTimeout
        settings["announce_to_all_trackers"] = True
        settings["announce_to_all_tiers"] = True
        settings["torrent_connect_boost"] = self.config.torrentConnectBoost
        settings["connection_speed"] = self.config.connectionSpeed
        settings["min_reconnect_time"] = self.config.minReconnectTime
        settings["max_failcount"] = self.config.maxFailCount
        settings["recv_socket_buffer_size"] = 1024 * 1024
        settings["send_socket_buffer_size"] = 1024 * 1024
        settings["rate_limit_ip_overhead"] = True
        settings["min_announce_interval"] = 60
        settings["tracker_backoff"] = 0
        self.session.set_settings(settings)

        if self.config.stateFile != '':
            logging.info('Loading session state from %s' %
                         (self.config.stateFile, ))
            try:
                with open(self.config.stateFile, 'rb') as f:
                    bytes__ = f.read()
            except IOError as e:
                strerror = e.args
                logging.error(strerror)
            else:
                self.session.load_state(lt.bdecode(bytes__))

        rand = SystemRandom(time.time())
        portLower = self.config.listenPort
        if self.config.randomPort:
            portLower = rand.randint(0, 16374) + 49151
        portUpper = portLower + 10
        try:
            self.session.listen_on(portLower, portUpper)
        except IOError as e:
            strerror = e.args
            logging.error(strerror)
            raise

        settings = self.session.get_settings()
        if self.config.userAgent != '':
            settings['user_agent'] = self.config.userAgent
        if self.config.connectionsLimit >= 0:
            settings['connections_limit'] = self.config.connectionsLimit
        if self.config.maxDownloadRate >= 0:
            settings[
                'download_rate_limit'] = self.config.maxDownloadRate * 1024
        if self.config.maxUploadRate >= 0:
            settings['upload_rate_limit'] = self.config.maxUploadRate * 1024
        settings['enable_incoming_tcp'] = self.config.enableTCP
        settings['enable_outgoing_tcp'] = self.config.enableTCP
        settings['enable_incoming_utp'] = self.config.enableUTP
        settings['enable_outgoing_utp'] = self.config.enableUTP
        self.session.set_settings(settings)

        if self.config.dhtRouters != '':
            routers = self.config.dhtRouters.split(',')
            for router in routers:
                router = router.strip()
                if router != '':
                    hostPort = router.split(':')
                    host = hostPort[0].strip()
                    try:
                        port = len(hostPort) > 1 and int(
                            hostPort[1].strip()) or 6881
                    except ValueError as e:
                        strerror = e.args
                        logging.error(strerror)
                        raise
                    self.session.add_dht_router(host, port)
                    logging.info('Added DHT router: %s:%d' % (host, port))
        logging.info('Setting encryption settings')
        try:
            encryptionSettings = lt.pe_settings()
            encryptionSettings.out_enc_policy = lt.enc_policy(
                self.config.encryption)
            encryptionSettings.in_enc_policy = lt.enc_policy(
                self.config.encryption)
            encryptionSettings.allowed_enc_level = lt.enc_level.both
            encryptionSettings.prefer_rc4 = True
            self.session.set_pe_settings(encryptionSettings)
        except Exception as e:
            logging.info('Encryption not supported: %s' % (e.args, ))
Ejemplo n.º 15
0
    def __init__(self, **options):
        self.log = logging.getLogger('{}.{}'.format('torrent', self.__class__.__name__))
        self._alert_handlers = {}
        self._files_list = {}
        self.options = options
        self.options.setdefault('save_path', '/tmp/')
        self.loop = options.get('loop', asyncio.get_event_loop())
        self.queue_event = asyncio.Event()

        self.http = web.Application()
        self.http.router.add_get('/{action:.*}', self.render_GET)
        self.http.on_shutdown.append(self.shutdown)

        self.log.info("libtorrent %s", libtorrent.version)
        self.session = session = libtorrent.session()
        self.queue_loop = self.loop.run_in_executor(None, self._alert_queue_loop)

        session.set_alert_mask(
            libtorrent.alert.category_t.tracker_notification |
            libtorrent.alert.category_t.storage_notification |
            libtorrent.alert.category_t.progress_notification |
            libtorrent.alert.category_t.status_notification |
            libtorrent.alert.category_t.error_notification
            )
        session.start_dht()
        session.start_lsd()
        session.start_upnp()
        session.start_natpmp()
        session.listen_on(options.get('min_port', 6881), options.get('max_port', 6889))

        session_settings = session.settings()
        session_settings.strict_end_game_mode = False
        session_settings.announce_to_all_tiers = True
        session_settings.announce_to_all_trackers = True
        session_settings.upload_rate_limit = int(1024 * 1024 / 8)
        session.set_settings(session_settings)

        session.add_dht_router("router.bittorrent.com", 6881)
        session.add_dht_router("router.utorrent.com", 6881)

        encryption_settings = libtorrent.pe_settings()
        encryption_settings.out_enc_policy = libtorrent.enc_policy(libtorrent.enc_policy.forced)
        encryption_settings.in_enc_policy = libtorrent.enc_policy(libtorrent.enc_policy.forced)
        encryption_settings.allowed_enc_level = libtorrent.enc_level.both
        encryption_settings.prefer_rc4 = True
        session.set_pe_settings(encryption_settings)

        for file in glob.glob(self.options.get('save_path') + '/*.fastresume'):
            try:
                if os.path.exists(file):
                    with open(file, 'rb') as fd:
                        self.add_torrent(resume_data=fd.read())
            except (IOError, EOFError, RuntimeError) as exception:
                self.log.error("Unable to load fastresume %s", exception)

        def torrent_checked_alert(alert):
            alert.handle.prioritize_pieces(alert.handle.get_torrent_info().num_pieces() * [TorrentStream.PAUSE])

        def metadata_received_alert(alert):
            self.log.info('got %d files', alert.handle.get_torrent_info().num_files())
            for i in range(alert.handle.get_torrent_info().num_files()):
                info = alert.handle.get_torrent_info().file_at(i)
                self._files_list[info.path] = FileInfo(id=i, handle=alert.handle, info=info)
            self._handle_alert([FilesListUpdateAlert(self.list_files())])

        def torrent_added_alert(alert):
            if alert.handle.get_torrent_info():
                metadata_received_alert(alert)

        def tracker_announce_alert(alert):
            self._handle_alert([FilesListUpdateAlert(self.list_files())])

        def torrent_removed_alert(alert):
            info_hash = str(alert.handle.info_hash())
            for path, handle in dict(self._files_list).items():
                if str(handle.handle.info_hash()) == info_hash:
                    del self._files_list[path]
                    self._handle_alert([FilesListUpdateAlert(self.list_files())])

        def torrent_error_alert(alert):
            self.session.remove_torrent(alert.handle)

        def torrent_finished_alert(alert):
            self.save_resume_data(alert.handle)

        def file_completed_alert(alert):
            self.save_resume_data(alert.handle)

        def save_resume_data_alert(alert):
            self.log.info("save_resume_data_alert %s", alert.handle.get_torrent_info().name())
            try:
                with open(alert.handle.save_path() + "/" + alert.handle.get_torrent_info().name() + ".fastresume", 'wb') as fd:
                    fd.write(libtorrent.bencode(alert.resume_data))
            except (IOError, EOFError) as e:
                self.log.error("Unable to save fastresume %s", e)

        self.add_alert_handler('torrent_added_alert', torrent_added_alert)
        self.add_alert_handler('metadata_received_alert', metadata_received_alert)
        self.add_alert_handler('torrent_checked_alert', torrent_checked_alert)
        self.add_alert_handler('torrent_removed_alert', torrent_removed_alert)
        self.add_alert_handler('torrent_error_alert', torrent_error_alert)
        self.add_alert_handler('torrent_finished_alert', torrent_finished_alert)
        self.add_alert_handler('file_completed_alert', file_completed_alert)
        self.add_alert_handler('save_resume_data_alert', save_resume_data_alert)
        self.add_alert_handler('tracker_announce_alert', tracker_announce_alert)
Ejemplo n.º 16
0
    def create_session(self, hops=0):
        settings = {}

        # Due to a bug in Libtorrent 0.16.18, the outgoing_port and num_outgoing_ports value should be set in
        # the settings dictionary
        settings['outgoing_port'] = 0
        settings['num_outgoing_ports'] = 1

        # Copy construct so we don't modify the default list
        extensions = list(DEFAULT_LT_EXTENSIONS)

        if hops == 0:
            settings['user_agent'] = 'Tribler/' + version_id
            # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
            fingerprint = ['TL'] + map(
                int,
                version_id.split('-')[0].split('.')) + [0]
            # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369)
            ltsession = lt.session(lt.fingerprint(*fingerprint), flags=0)
            enable_utp = self.tribler_session.config.get_libtorrent_utp()
            settings['enable_outgoing_utp'] = enable_utp
            settings['enable_incoming_utp'] = enable_utp

            pe_settings = lt.pe_settings()
            pe_settings.prefer_rc4 = True
            ltsession.set_pe_settings(pe_settings)
        else:
            settings['enable_outgoing_utp'] = True
            settings['enable_incoming_utp'] = True
            settings['enable_outgoing_tcp'] = False
            settings['enable_incoming_tcp'] = False
            settings['anonymous_mode'] = True
            settings['force_proxy'] = True
            ltsession = lt.session(flags=0)
            # No PEX for anonymous sessions
            if lt.create_ut_pex_plugin in extensions:
                extensions.remove(lt.create_ut_pex_plugin)

        ltsession.set_settings(settings)
        ltsession.set_alert_mask(self.default_alert_mask)

        # Load proxy settings
        if hops == 0:
            proxy_settings = self.tribler_session.config.get_libtorrent_proxy_settings(
            )
        else:
            proxy_settings = list(
                self.tribler_session.config.get_anon_proxy_settings())
            proxy_host, proxy_ports = proxy_settings[1]
            proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
        self.set_proxy_settings(ltsession, *proxy_settings)

        for extension in extensions:
            ltsession.add_extension(extension)

        # Set listen port & start the DHT
        if hops == 0:
            listen_port = self.tribler_session.config.get_libtorrent_port()
            ltsession.listen_on(listen_port, listen_port + 10)
            if listen_port != ltsession.listen_port():
                self.tribler_session.config.set_libtorrent_port_runtime(
                    ltsession.listen_port())
            try:
                lt_state = lt.bdecode(
                    open(
                        os.path.join(
                            self.tribler_session.config.get_state_dir(),
                            LTSTATE_FILENAME)).read())
                if lt_state is not None:
                    ltsession.load_state(lt_state)
                else:
                    self._logger.warning(
                        "the lt.state appears to be corrupt, writing new data on shutdown"
                    )
            except Exception, exc:
                self._logger.info(
                    "could not load libtorrent state, got exception: %r. starting from scratch"
                    % exc)
            ltsession.start_dht()
Ejemplo n.º 17
0
    def create_session(self, hops=0):
        settings = lt.session_settings()

        if hops == 0:
            settings.user_agent = 'Tribler/' + version_id
            # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
            fingerprint = ['TL'] + map(
                int,
                version_id.split('-')[0].split('.')) + [0]
            # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369)
            ltsession = lt.session(lt.fingerprint(*fingerprint), flags=1)
            enable_utp = self.trsession.get_libtorrent_utp()
            settings.enable_outgoing_utp = enable_utp
            settings.enable_incoming_utp = enable_utp

            pe_settings = lt.pe_settings()
            pe_settings.prefer_rc4 = True
            ltsession.set_pe_settings(pe_settings)
        else:
            settings.enable_outgoing_utp = True
            settings.enable_incoming_utp = True
            settings.enable_outgoing_tcp = False
            settings.enable_incoming_tcp = False
            settings.anonymous_mode = True
            # No PEX for anonymous sessions
            ltsession = lt.session(flags=0)
            ltsession.add_extension(lt.create_ut_metadata_plugin)
            ltsession.add_extension(lt.create_smart_ban_plugin)

        ltsession.set_settings(settings)
        ltsession.set_alert_mask(lt.alert.category_t.stats_notification
                                 | lt.alert.category_t.error_notification
                                 | lt.alert.category_t.status_notification
                                 | lt.alert.category_t.storage_notification
                                 | lt.alert.category_t.performance_warning
                                 | lt.alert.category_t.tracker_notification)

        # Load proxy settings
        if hops == 0:
            proxy_settings = self.trsession.get_libtorrent_proxy_settings()
        else:
            proxy_settings = list(self.trsession.get_anon_proxy_settings())
            proxy_host, proxy_ports = proxy_settings[1]
            proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
        self.set_proxy_settings(ltsession, *proxy_settings)

        # Set listen port & start the DHT
        if hops == 0:
            listen_port = self.trsession.get_listen_port()
            ltsession.listen_on(listen_port, listen_port + 10)
            if listen_port != ltsession.listen_port():
                self.trsession.set_listen_port_runtime(ltsession.listen_port())
            try:
                dht_state = open(
                    os.path.join(self.trsession.get_state_dir(),
                                 DHTSTATE_FILENAME)).read()
                ltsession.start_dht(lt.bdecode(dht_state))
            except Exception, exc:
                self._logger.info(
                    "could not restore dht state, got exception: %r. starting from scratch"
                    % exc)
                ltsession.start_dht(None)
Ejemplo n.º 18
0
    def create_session(self, hops=0, store_listen_port=True):
        settings = {}

        # Due to a bug in Libtorrent 0.16.18, the outgoing_port and num_outgoing_ports value should be set in
        # the settings dictionary
        settings['outgoing_port'] = 0
        settings['num_outgoing_ports'] = 1
        settings['allow_multiple_connections_per_ip'] = 0

        # Copy construct so we don't modify the default list
        extensions = list(DEFAULT_LT_EXTENSIONS)

        # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
        fingerprint = ['TL'] + [int(x) for x in version_id.split('-')[0].split('.')] + [0]
        ltsession = lt.session(lt.fingerprint(*fingerprint), flags=0) if hops == 0 else lt.session(flags=0)

        if hops == 0:
            settings['user_agent'] = 'Tribler/' + version_id
            enable_utp = self.tribler_session.config.get_libtorrent_utp()
            settings['enable_outgoing_utp'] = enable_utp
            settings['enable_incoming_utp'] = enable_utp

            if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings['prefer_rc4'] = True
                settings["listen_interfaces"] = "0.0.0.0:%d" % self.tribler_session.config.get_libtorrent_port()
            else:
                pe_settings = lt.pe_settings()
                pe_settings.prefer_rc4 = True
                ltsession.set_pe_settings(pe_settings)

            mid = self.tribler_session.trustchain_keypair.key_to_hash()
            settings['peer_fingerprint'] = mid
            settings['handshake_client_version'] = 'Tribler/' + version_id + '/' + str(hexlify(mid))
        else:
            settings['enable_outgoing_utp'] = True
            settings['enable_incoming_utp'] = True
            settings['enable_outgoing_tcp'] = False
            settings['enable_incoming_tcp'] = False
            settings['anonymous_mode'] = True
            settings['force_proxy'] = True

            if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
                settings["listen_interfaces"] = "0.0.0.0:%d" % self.tribler_session.config.get_anon_listen_port()

        self.set_session_settings(ltsession, settings)
        ltsession.set_alert_mask(self.default_alert_mask)

        # Load proxy settings
        if hops == 0:
            proxy_settings = self.tribler_session.config.get_libtorrent_proxy_settings()
        else:
            proxy_settings = list(self.tribler_session.config.get_anon_proxy_settings())
            proxy_host, proxy_ports = proxy_settings[1]
            proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
        self.set_proxy_settings(ltsession, *proxy_settings)

        for extension in extensions:
            ltsession.add_extension(extension)

        # Set listen port & start the DHT
        if hops == 0:
            listen_port = self.tribler_session.config.get_libtorrent_port()
            ltsession.listen_on(listen_port, listen_port + 10)
            if listen_port != ltsession.listen_port() and store_listen_port:
                self.tribler_session.config.set_libtorrent_port_runtime(ltsession.listen_port())
            try:
                lt_state = lt.bdecode(
                    open(os.path.join(self.tribler_session.config.get_state_dir(), LTSTATE_FILENAME)).read())
                if lt_state is not None:
                    ltsession.load_state(lt_state)
                else:
                    self._logger.warning("the lt.state appears to be corrupt, writing new data on shutdown")
            except Exception as exc:
                self._logger.info("could not load libtorrent state, got exception: %r. starting from scratch" % exc)
        else:
            ltsession.listen_on(self.tribler_session.config.get_anon_listen_port(),
                                self.tribler_session.config.get_anon_listen_port() + 20)

            ltsession_settings = ltsession.get_settings()
            ltsession_settings['upload_rate_limit'] = self.tribler_session.config.get_libtorrent_max_upload_rate()
            ltsession_settings['download_rate_limit'] = self.tribler_session.config.get_libtorrent_max_download_rate()
            self.set_session_settings(ltsession, ltsession_settings)

        if self.tribler_session.config.get_libtorrent_dht_enabled():
            ltsession.start_dht()
            for router in DEFAULT_DHT_ROUTERS:
                ltsession.add_dht_router(*router)
            ltsession.start_lsd()

        self._logger.debug("Started libtorrent session for %d hops on port %d", hops, ltsession.listen_port())
        self.lt_session_shutdown_ready[hops] = False

        return ltsession
Ejemplo n.º 19
0
    # Initialize Session
    session = libtorrent.session()
    settings = libtorrent.session_settings()
    settings.min_announce_interval = 15

    if options.download_limit > 0 or options.upload_limit > 0:
        if options.download_limit > 0:
            session.set_download_rate_limit(options.download_limit << 10)
        if options.upload_limit > 0:
            session.set_upload_rate_limit(options.upload_limit << 10)

        settings.ignore_limits_on_local_network = False

    # Force Encryption
    if options.encryption:
        pe_settings = libtorrent.pe_settings()
        pe_settings.out_enc_policy = libtorrent.enc_policy.forced
        pe_settings.in_enc_policy = libtorrent.enc_policy.forced
        pe_settings.allowed_enc_level = libtorrent.enc_level.rc4
        pe_settings.prefer_rc4 = True
        session.set_pe_settings(pe_settings)

    session.set_settings(settings)
    session.listen_on(options.port, options.port + 10)

    # Start torrents
    info = libtorrent.torrent_info(libtorrent.bdecode(file(options.torrent).read()))
    torrent_parms = {"ti": info}
    torrent_parms["save_path"] = options.download_dir
    torrent_parms["storage_mode"] = libtorrent.storage_mode_t.storage_mode_sparse
    torrent_parms["paused"] = False