Example #1
0
    def locateChild(self, request, segments):
        """Process the incoming request."""
        log.msg('Got HTTP request for %s from %s' % (request.uri, request.remoteAddr))
        name = segments[0]
        
        # If the request is for a shared file (from a peer)
        if name == '~':
            if len(segments) != 2:
                log.msg('Got a malformed request from %s' % request.remoteAddr)
                return None, ()
            
            # Find the file in the database
            # Have to unquote_plus the uri, because the segments are unquoted by twisted
            hash = unquote_plus(request.uri[3:])
            files = self.db.lookupHash(hash)
            if files:
                # If it is a file, return it
                if 'path' in files[0]:
                    log.msg('Sharing %s with %s' % (files[0]['path'].path, request.remoteAddr))
                    return FileUploader(files[0]['path'].path), ()
                else:
                    # It's not for a file, but for a piece string, so return that
                    log.msg('Sending torrent string %s to %s' % (b2a_hex(hash), request.remoteAddr))
                    return PiecesUploader(bencode({'t': files[0]['pieces']}), 'application/x-bencoded'), ()
            else:
                log.msg('Hash could not be found in database: %r' % hash)
                return None, ()

        if len(name) > 1:
            # It's a request from apt

            # Only local requests (apt) get past this point
            if request.remoteAddr.host != "127.0.0.1":
                log.msg('Blocked illegal access to %s from %s' % (request.uri, request.remoteAddr))
                return None, ()

            # Block access to index .diff files (for now)
            if 'Packages.diff' in segments or 'Sources.diff' in segments or name == 'favicon.ico':
                return None, ()
             
            return FileDownloader(self.directory.path, self.manager), segments[0:]
        else:
            # Will render the statistics page

            # Only local requests for stats are allowed
            if not config.getboolean('DEFAULT', 'REMOTE_STATS') and request.remoteAddr.host != "127.0.0.1":
                log.msg('Blocked illegal access to %s from %s' % (request.uri, request.remoteAddr))
                return None, ()

            return self, ()
        
        log.msg('Got a malformed request for "%s" from %s' % (request.uri, request.remoteAddr))
        return None, ()
Example #2
0
 def joinComplete(self, result):
     """Complete the DHT join process and determine our download information.
     
     Called by the DHT when the join has been completed with information
     on the external IP address and port of this peer.
     """
     my_addr = findMyIPAddr(result,
                            config.getint(config.get('DEFAULT', 'DHT'), 'PORT'),
                            config.getboolean('DEFAULT', 'LOCAL_OK'))
     if not my_addr:
         raise RuntimeError, "IP address for this machine could not be found"
     self.my_contact = compact(my_addr, config.getint('DEFAULT', 'PORT'))
     if not self.nextRefresh or not self.nextRefresh.active():
         self.nextRefresh = reactor.callLater(60, self.refreshFiles)
     return (my_addr, config.getint('DEFAULT', 'PORT'))
Example #3
0
 def joinComplete(self, result):
     """Complete the DHT join process and determine our download information.
     
     Called by the DHT when the join has been completed with information
     on the external IP address and port of this peer.
     """
     my_addr = findMyIPAddr(
         result, config.getint(config.get('DEFAULT', 'DHT'), 'PORT'),
         config.getboolean('DEFAULT', 'LOCAL_OK'))
     if not my_addr:
         raise RuntimeError, "IP address for this machine could not be found"
     self.my_contact = compact(my_addr, config.getint('DEFAULT', 'PORT'))
     if not self.nextRefresh or not self.nextRefresh.active():
         self.nextRefresh = reactor.callLater(60, self.refreshFiles)
     return (my_addr, config.getint('DEFAULT', 'PORT'))
Example #4
0
    def locateChild(self, request, segments):
        """Process the incoming request."""
        log.msg('Got HTTP request for %s from %s' %
                (request.uri, request.remoteAddr))
        name = segments[0]

        # If the request is for a shared file (from a peer)
        if name == '~':
            if len(segments) != 2:
                log.msg('Got a malformed request from %s' % request.remoteAddr)
                return None, ()

            # Find the file in the database
            # Have to unquote_plus the uri, because the segments are unquoted by twisted
            hash = unquote_plus(request.uri[3:])
            files = self.db.lookupHash(hash)
            if files:
                # If it is a file, return it
                if 'path' in files[0]:
                    log.msg('Sharing %s with %s' %
                            (files[0]['path'].path, request.remoteAddr))
                    return FileUploader(files[0]['path'].path), ()
                else:
                    # It's not for a file, but for a piece string, so return that
                    log.msg('Sending torrent string %s to %s' %
                            (b2a_hex(hash), request.remoteAddr))
                    return PiecesUploader(bencode({'t': files[0]['pieces']}),
                                          'application/x-bencoded'), ()
            else:
                log.msg('Hash could not be found in database: %r' % hash)
                return None, ()

        if len(name) > 1:
            # It's a request from apt

            # Only local requests (apt) get past this point
            if request.remoteAddr.host != "127.0.0.1":
                log.msg('Blocked illegal access to %s from %s' %
                        (request.uri, request.remoteAddr))
                return None, ()

            # Block access to index .diff files (for now)
            if 'Packages.diff' in segments or 'Sources.diff' in segments or name == 'favicon.ico':
                return None, ()

            return FileDownloader(self.directory.path,
                                  self.manager), segments[0:]
        else:
            # Will render the statistics page

            # Only local requests for stats are allowed
            if not config.getboolean(
                    'DEFAULT',
                    'REMOTE_STATS') and request.remoteAddr.host != "127.0.0.1":
                log.msg('Blocked illegal access to %s from %s' %
                        (request.uri, request.remoteAddr))
                return None, ()

            return self, ()

        log.msg('Got a malformed request for "%s" from %s' %
                (request.uri, request.remoteAddr))
        return None, ()