Exemplo n.º 1
0
 def retrieveContainer(self):
     """
     Recurring job: retrieve a container that a peer has offered to us and, if a slot can be
     made available, load it up.
     """
     requests = []
     self.singlesd
     try:
         self.logger.log('retrieving a %s' % (self.cname))
         query = requestQuery(self.session, self.config)
         query = query.filter(QueuedRequest.type=='%s-offer' % (self.cname.lower()))
         query = query.filter(QueuedRequest.state!='done')
         if self.singlesd:
             requests = [query.first()]
         else:
             requests = query.all()
     except Exception, e:
         self.logger.log('unable to get requests from the database: %s' % e)
         return
Exemplo n.º 2
0
    def performRequestedScan(self):
        """
        Recurring job: perform a scan that a peer has requested and return the result to them,
        as well as storing the result in our database for future reference.
        """
        try:
            request = requestQuery(self.session, self.config)\
                                .filter(QueuedRequest.type=='active-scan')\
                                .filter(QueuedRequest.state!='done')\
                                .first()
            if not request:
                #self.logger.log('no scans requested')
                return #nothing to do
            self.logger.log('Performing a requested scan')
            peer = request.peer
            self.logger.log('handling request %r' % request)

            wasprocessed = request.state == 'processed'
            done = request.state == 'done'

            if not wasprocessed and not done:
                self.logger.log('request unprocessed, sending to scanner')
                scanner = scanning.ScannableRequest(self.config, self.session, parentrequest=request)
                scan = scanner.localscan
                request.scan = scan
                request.state = 'processed'
            else:
                self.logger.log('request already processed, not re-scanning')
                scan = request.scan
                if not scan:
                    self.logger.log('Already scanned, but scan missing! marking as done anyway.')
                    request.state = 'done'
                    self.session.add(request)
                    self.session.commit()
                    return

            self.logger.log('sending result to peer who requested it')
            #retries = int(self.config.scanning.max_active_retries)
            successful = False
            #while retries > 0 and not successful:
            try:
                result = peer.transport.scanResult(self.config.owner.name, request.url,
                                        scan.hash, request.key, scan.malicious, scan.scannervv,
                                        scan.sigversion, time.mktime(scan.sigdate.timetuple()))
            except:
                self.logger.exception()
            else:
                if result == 'success':
                    self.logger.log('send successful, marking request done')
                    request.state = 'done'
                    successful = True
                else:
                    msg = 'send unsuccessful, peer responded with non-success response %r'
                    self.logger.log(msg % result)
            #    retries -= 1

            if wasprocessed and successful:
                request.state = 'done'

            self.session.add(request)
            self.session.commit()
            self.logger.log('done')
        except:
            self.logger.exception()
            raise