예제 #1
0
파일: download.py 프로젝트: self20/Anomos
    def __init__(self, config, doneflag, listen_fail_ok=False):
        self.config = dict(config)
        Anomos.Crypto.init(self.config['data_dir'])
        self.sessionid = Anomos.Crypto.get_rand(8)
        self.certificate = Anomos.Crypto.Certificate(self.config['identity'])
        self.ssl_ctx = self.certificate.get_ctx(allow_unknown_ca=True)
        self.event_handler = EventHandler(doneflag)
        self.schedule = self.event_handler.schedule

        self.ratelimiter = RateLimiter(self.schedule)
        self.ratelimiter.set_parameters(config['max_upload_rate'],
                                        config['upload_unit_size'])

        self.nbr_mngrs = {}
        self.torrents = {}

        self.singleport_listener = SingleportListener(self.config, self.ssl_ctx)
        self.singleport_listener.find_port(listen_fail_ok)
        self.filepool = FilePool(config['max_files_open'])
        set_filesystem_encoding(config['filesystem_encoding'])
예제 #2
0
파일: download.py 프로젝트: Miserlou/Anomos
    def __init__(self, config, doneflag, listen_fail_ok=False):
        self.config = dict(config)
        Anomos.Crypto.init(self.config["data_dir"])
        self.sessionid = Anomos.Crypto.get_rand(8)
        self.certificate = Anomos.Crypto.Certificate(self.config["identity"])
        self.ssl_ctx = self.certificate.get_ctx(allow_unknown_ca=True)
        self.event_handler = EventHandler(doneflag)
        self.schedule = self.event_handler.schedule

        self.ratelimiter = RateLimiter(self.schedule)
        self.ratelimiter.set_parameters(config["max_upload_rate"], config["upload_unit_size"])

        self.nbr_mngrs = {}
        self.torrents = {}

        self.singleport_listener = SingleportListener(self.config, self.ssl_ctx)
        self.singleport_listener.find_port(listen_fail_ok)
        self.filepool = FilePool(config["max_files_open"])
        set_filesystem_encoding(config["filesystem_encoding"])
예제 #3
0
파일: download.py 프로젝트: Miserlou/Anomos
class Multitorrent(object):
    def __init__(self, config, doneflag, listen_fail_ok=False):
        self.config = dict(config)
        Anomos.Crypto.init(self.config["data_dir"])
        self.sessionid = Anomos.Crypto.get_rand(8)
        self.certificate = Anomos.Crypto.Certificate(self.config["identity"])
        self.ssl_ctx = self.certificate.get_ctx(allow_unknown_ca=True)
        self.event_handler = EventHandler(doneflag)
        self.schedule = self.event_handler.schedule

        self.ratelimiter = RateLimiter(self.schedule)
        self.ratelimiter.set_parameters(config["max_upload_rate"], config["upload_unit_size"])

        self.nbr_mngrs = {}
        self.torrents = {}

        self.singleport_listener = SingleportListener(self.config, self.ssl_ctx)
        self.singleport_listener.find_port(listen_fail_ok)
        self.filepool = FilePool(config["max_files_open"])
        set_filesystem_encoding(config["filesystem_encoding"])

    def close_listening_socket(self):
        self.singleport_listener.close_sockets()

    def start_torrent(self, metainfo, config, feedback, filename):

        if not self.nbr_mngrs.has_key(metainfo.announce):
            ### XXX: Is using the same cert on different trackers a threat to anonymity? Yes.
            self.nbr_mngrs[metainfo.announce] = NeighborManager(
                config, self.certificate, self.ssl_ctx, self.sessionid, self.schedule, self.ratelimiter
            )

        torrent = _SingleTorrent(
            self.event_handler,
            self.singleport_listener,
            self.ratelimiter,
            self.filepool,
            config,
            self.nbr_mngrs[metainfo.announce],
            self.certificate,
            self.sessionid,
        )
        self.event_handler.add_context(torrent)
        self.torrents[metainfo.infohash] = torrent

        def start():
            torrent.start_download(metainfo, feedback, filename)

        self.schedule(0, start, context=torrent)
        return torrent

    def set_option(self, option, value):
        if option not in self.config or self.config[option] == value:
            return
        if option not in "max_upload_rate upload_unit_size " "max_files_open minport maxport".split():
            return
        self.config[option] = value
        if option == "max_files_open":
            self.filepool.set_max_files_open(value)
        elif option == "max_upload_rate":
            self.ratelimiter.set_parameters(value, self.config["upload_unit_size"])
        elif option == "upload_unit_size":
            self.ratelimiter.set_parameters(self.config["max_upload_rate"], value)
        elif option == "maxport":
            if not self.config["minport"] <= self.singleport_listener.port <= self.config["maxport"]:
                self.singleport_listener.find_port()

    def get_completion(self, config, metainfo, save_path, filelist=False):
        if not config["data_dir"]:
            return None
        infohash = metainfo.infohash
        if metainfo.is_batch:
            myfiles = [os.path.join(save_path, f) for f in metainfo.files_fs]
        else:
            myfiles = [save_path]

        if metainfo.file_size == 0:
            if filelist:
                return None
            return 1
        try:
            s = Storage(None, None, zip(myfiles, metainfo.sizes), check_only=True)
        except:
            return None
        filename = os.path.join(config["data_dir"], "resume", infohash.encode("hex"))
        try:
            f = file(filename, "rb")
        except:
            f = None
        try:
            r = s.check_fastresume(f, filelist, metainfo.piece_length, len(metainfo.hashes), myfiles)
        except:
            r = None
        if f is not None:
            f.close()
        if r is None:
            return None
        if filelist:
            return r[0] / metainfo.file_size, r[1], r[2]
        return r / metainfo.file_size
예제 #4
0
파일: download.py 프로젝트: self20/Anomos
class Multitorrent(object):

    def __init__(self, config, doneflag, listen_fail_ok=False):
        self.config = dict(config)
        Anomos.Crypto.init(self.config['data_dir'])
        self.sessionid = Anomos.Crypto.get_rand(8)
        self.certificate = Anomos.Crypto.Certificate(self.config['identity'])
        self.ssl_ctx = self.certificate.get_ctx(allow_unknown_ca=True)
        self.event_handler = EventHandler(doneflag)
        self.schedule = self.event_handler.schedule

        self.ratelimiter = RateLimiter(self.schedule)
        self.ratelimiter.set_parameters(config['max_upload_rate'],
                                        config['upload_unit_size'])

        self.nbr_mngrs = {}
        self.torrents = {}

        self.singleport_listener = SingleportListener(self.config, self.ssl_ctx)
        self.singleport_listener.find_port(listen_fail_ok)
        self.filepool = FilePool(config['max_files_open'])
        set_filesystem_encoding(config['filesystem_encoding'])
        
        

    def close_listening_socket(self):
        self.singleport_listener.close_sockets()

    def start_torrent(self, metainfo, config, feedback, filename):

        if not self.nbr_mngrs.has_key(metainfo.announce):
            ### XXX: Is using the same cert on different trackers a threat to anonymity? Yes.
            self.nbr_mngrs[metainfo.announce] = \
                    NeighborManager(config, self.certificate, \
                                    self.ssl_ctx, self.sessionid, \
                                    self.schedule, self.ratelimiter)

        torrent = _SingleTorrent(self.event_handler, \
                                 self.singleport_listener,\
                                 self.ratelimiter, self.filepool, config,\
                                 self.nbr_mngrs[metainfo.announce],\
                                 self.certificate, self.sessionid)
        self.event_handler.add_context(torrent)
        self.torrents[metainfo.infohash] = torrent
        def start():
            torrent.start_download(metainfo, feedback, filename)
        self.schedule(0, start, context=torrent)
        return torrent

    def set_option(self, option, value):
        if option not in self.config or self.config[option] == value:
            return
        if option not in 'max_upload_rate upload_unit_size '\
               'max_files_open minport maxport'.split():
            return
        self.config[option] = value
        if option == 'max_files_open':
            self.filepool.set_max_files_open(value)
        elif option == 'max_upload_rate':
            self.ratelimiter.set_parameters(value,
                                            self.config['upload_unit_size'])
        elif option == 'upload_unit_size':
            self.ratelimiter.set_parameters(self.config['max_upload_rate'],
                                            value)
        elif option == 'maxport':
            if not self.config['minport'] <= self.singleport_listener.port <= \
                   self.config['maxport']:
                self.singleport_listener.find_port()

    def get_completion(self, config, metainfo, save_path, filelist=False):
        if not config['data_dir']:
            return None
        infohash = metainfo.infohash
        if metainfo.is_batch:
            myfiles = [os.path.join(save_path, f) for f in metainfo.files_fs]
        else:
            myfiles = [save_path]

        if metainfo.file_size == 0:
            if filelist:
                return None
            return 1
        try:
            s = Storage(None, None, zip(myfiles, metainfo.sizes),
                        check_only=True)
        except:
            return None
        filename = os.path.join(config['data_dir'], 'resume',
                                infohash.encode('hex'))
        try:
            f = file(filename, 'rb')
        except:
            f = None
        try:
            r = s.check_fastresume(f, filelist, metainfo.piece_length,
                                   len(metainfo.hashes), myfiles)
        except:
            r = None
        if f is not None:
            f.close()
        if r is None:
            return None
        if filelist:
            return r[0] / metainfo.file_size, r[1], r[2]
        return r / metainfo.file_size