Exemple #1
0
    def __init__(self, myid, tracker, hash, interval, peers, timeout,
                 rawserver, disallow, isdisallowed):
        self.tracker = tracker
        self.interval = interval
        self.hash = hash
        self.operatinginterval = interval
        self.peers = peers
        self.rawserver = rawserver
        self.disallow = disallow
        self.isdisallowed = isdisallowed
        self.active = True
        self.busy = False
        self.errors = 0
        self.rejected = 0
        self.trackererror = False
        self.peerlists = []
        cfg = {'min_peers': peers,
               'max_initiate': peers,
               'rerequest_interval': interval,
               'http_timeout': timeout}
        self.rerequester = Rerequester(
            0, myid, hash, [[tracker]], cfg, rawserver.add_task,
            self.errorfunc, excfunc, self.addtolist, R_0, R_1, R_0, R_0, R_0,
            R_0, threading.Event())

        # stagger announces
        if self.isactive():
            rawserver.add_task(self.refresh,
                               random.randrange(int(self.interval / 10),
                                                self.interval))
Exemple #2
0
class T2TConnection:
    def __init__(self, myid, tracker, hash, interval, peers, timeout,
                 rawserver, disallow, isdisallowed):
        self.tracker = tracker
        self.interval = interval
        self.hash = hash
        self.operatinginterval = interval
        self.peers = peers
        self.rawserver = rawserver
        self.disallow = disallow
        self.isdisallowed = isdisallowed
        self.active = True
        self.busy = False
        self.errors = 0
        self.rejected = 0
        self.trackererror = False
        self.peerlists = []
        cfg = {'min_peers': peers,
               'max_initiate': peers,
               'rerequest_interval': interval,
               'http_timeout': timeout}
        self.rerequester = Rerequester(
            0, myid, hash, [[tracker]], cfg, rawserver.add_task,
            self.errorfunc, excfunc, self.addtolist, R_0, R_1, R_0, R_0, R_0,
            R_0, threading.Event())

        # stagger announces
        if self.isactive():
            rawserver.add_task(self.refresh,
                               random.randrange(int(self.interval / 10),
                                                self.interval))

    def isactive(self):
        if self.isdisallowed(self.tracker):    # whoops!
            self.deactivate()
        return self.active

    def deactivate(self):
        self.active = False

    def refresh(self):
        if not self.isactive():
            return
        self.lastsuccessful = True
        self.newpeerdata = []
        if DEBUG:
            print('contacting %s for info_hash=%s' %
                  (self.tracker, urllib.parse.quote(self.hash)))
        self.rerequester.snoop(self.peers, self.callback)

    def callback(self):
        self.busy = False
        if self.lastsuccessful:
            self.errors = 0
            self.rejected = 0
            if self.rerequester.announce_interval > (3 * self.interval):
                # I think I'm stripping from a regular tracker
                # boost the number of peers requested
                self.peers = int(
                    self.peers *
                    (self.rerequester.announce_interval / self.interval))
            self.operatinginterval = self.rerequester.announce_interval
            if DEBUG:
                print("{} with info_hash={} returned {:d} peers".format(
                      self.tracker, urllib.parse.quote(self.hash),
                      len(self.newpeerdata)))
            self.peerlists.append(self.newpeerdata)
            # keep up to the last 10 announces
            self.peerlists = self.peerlists[-10:]
        if self.isactive():
            self.rawserver.add_task(self.refresh, self.operatinginterval)

    def addtolist(self, peers):
        for peer in peers:
            self.newpeerdata.append((peer[1], peer[0][0], peer[0][1]))

    def errorfunc(self, r):
        self.lastsuccessful = False
        if DEBUG:
            print("{} with info_hash={} gives error: '{}'".format(
                  self.tracker, urllib.parse.quote(self.hash), r))
        if r == self.rerequester.rejectedmessage + 'disallowed':   # whoops!
            if DEBUG:
                print(' -- disallowed - deactivating')
            self.deactivate()
            # signal other torrents on this tracker
            self.disallow(self.tracker)
            return
        # tracker rejected this particular torrent
        if r[:8].lower() == 'rejected':
            self.rejected += 1
            if self.rejected == 3:     # rejected 3 times
                if DEBUG:
                    print(' -- rejected 3 times - deactivating')
                self.deactivate()
            return
        self.errors += 1
        if self.errors >= 3:    # three or more errors in a row
            self.operatinginterval += self.interval  # lengthen the interval
            if DEBUG:
                print(' -- lengthening interval to {} seconds'.format(
                      self.operatinginterval))

    def harvest(self):
        x = []
        for list in self.peerlists:
            x += list
        self.peerlists = []
        return x