Lots of log messages and blocked non-local access to server.
authorCameron Dale <camrdale@gmail.com>
Fri, 4 Jan 2008 02:58:53 +0000 (18:58 -0800)
committerCameron Dale <camrdale@gmail.com>
Fri, 4 Jan 2008 02:58:53 +0000 (18:58 -0800)
Remote peers can still access the shared files, but not make
requests that result in lookups or see the stats.

Also fixed a bug in the main findHash routines.
Also changed to use splitHostPort rather than manual splitting.

apt_dht/HTTPServer.py
apt_dht/PeerManager.py
apt_dht/apt_dht.py

index f5e969ccfc81c8565c77d56c56b3d60ea8662803..1e049c392a789a0f4250bf6a05ef8664e8f00709 100644 (file)
@@ -1,5 +1,6 @@
 import os.path, time
 
+from twisted.python import log
 from twisted.web2 import server, http, resource, channel
 from twisted.web2 import static, http_headers, responsecode
 
@@ -10,13 +11,17 @@ class FileDownloader(static.File):
         super(FileDownloader, self).__init__(path, defaultType, ignoredExts, processors, indexNames)
         
     def render(self, req):
+        log.msg('Got request for %s from %s' % (req.uri, req.remoteAddr))
         resp = super(FileDownloader, self).render(req)
+        log.msg('Initial response to %s: %r' % (req.uri, resp))
         
         if self.manager:
             path = 'http:/' + req.uri
             if resp != responsecode.NOT_FOUND:
+                log.msg('Checking freshness of %s' % req.uri)
                 return self.manager.check_freshness(path, resp.headers.getHeader('Last-Modified'), resp)
             
+            log.msg('Not found, trying other methods for %s' % req.uri)
             return self.manager.get_resp(path)
         
         return resp
@@ -24,6 +29,8 @@ class FileDownloader(static.File):
     def createSimilarFile(self, path):
         return self.__class__(path, self.manager, self.defaultType, self.ignoredExts,
                               self.processors, self.indexNames[:])
+        
+        
 class TopLevel(resource.Resource):
     addSlash = True
     
@@ -55,13 +62,20 @@ class TopLevel(resource.Resource):
             try:
                 loc = int(name[1:])
             except:
+                log.msg('Not found: %s from %s' % (request.uri, request.remoteAddr))
                 return None, ()
             
             if loc >= 0 and loc < len(self.subdirs) and self.subdirs[loc]:
+                log.msg('Sharing %s with %s' % (request.uri, request.remoteAddr))
                 return static.File(self.subdirs[loc]), segments[1:]
             else:
+                log.msg('Not found: %s from %s' % (request.uri, request.remoteAddr))
                 return None, ()
         
+        if request.remoteAddr.host != "127.0.0.1":
+            log.msg('Blocked illegal access to %s from %s' % (request.uri, request.remoteAddr))
+            return None, ()
+            
         if len(name) > 1:
             return FileDownloader(self.directory, self.manager), segments[0:]
         else:
index 33c0688e0875ae3cd2cb492c39b7f6c9bf79f589..25bd4f5959e1015aa26848ba18805e7f6741aa4e 100644 (file)
@@ -3,8 +3,10 @@ from random import choice
 from urlparse import urlparse, urlunparse
 
 from twisted.internet import reactor, defer
+from twisted.python import log
 from twisted.trial import unittest
 from twisted.web2 import stream as stream_mod
+from twisted.web2.http import splitHostPort
 
 from HTTPDownloader import HTTPClientManager
 
@@ -19,17 +21,12 @@ class PeerManager:
         @var locations: a list of the locations where the file can be found
         """
         url = choice(locations)
+        log.msg('Downloading %s' % url)
         parsed = urlparse(url)
         assert(parsed[0] == "http", "Only HTTP is supported, not '%s'" % parsed[0])
-        host = parsed[1]
+        host, port = splitHostPort(parsed[0], parsed[1])
         path = urlunparse(('', '') + parsed[2:])
-        
-        # Make sure a port is included for consistency
-        if host.find(':') >= 0:
-            host, port = host.split(':', 1)
-            port = int(port)
-        else:
-            port = 80
+
         return self.getPeer(host, port, path, method, modtime)
         
     def getPeer(self, host, port, path, method="GET", modtime=None):
index 448d44f9e915355ba74d56b0d02ae97f58150d20..83c48b84bc37272fce3b4b987f9b793f0b8c1fdd 100644 (file)
@@ -21,38 +21,50 @@ class AptDHT:
         return self.http_site
     
     def check_freshness(self, path, modtime, resp):
+        log.msg('Checking if %s is still fresh: %r' % (path, modtime))
         d = self.peers.get([path], "HEAD", modtime)
         d.addCallback(self.check_freshness_done, path, resp)
         return d
     
     def check_freshness_done(self, resp, path, orig_resp):
         if resp.code == "304":
+            log.msg('Still fresh: %s' % path)
             return orig_resp
         else:
+            log.msg('Stale, need to redownload: %s' % path)
             return self.get_resp(path)
     
     def get_resp(self, path):
         d = defer.Deferred()
         
+        log.msg('Trying to find hash for %s' % path)
         findDefer = self.mirrors.findHash(path)
         
-        findDefer.addcallback(self.findHash_done, path, d)
+        findDefer.addCallback(self.findHash_done, path, d)
+        findDefer.addErrback(self.findHash_error, path, d)
         return d
+    
+    def findHash_error(self, failure, path, d):
+        self.findHash_done((None, None), path, d)
         
     def findHash_done(self, (hash, size), path, d):
         if hash is None:
+            log.msg('Hash for %s was not found' % path)
             getDefer = self.peers.get([path])
             getDefer.addCallback(d.callback)
         else:
+            log.msg('Found hash %s for %s' % (hash, path))
             # Lookup hash from DHT
             lookupDefer = self.dht.getValue(hash)
             lookupDefer.addCallback(self.lookupHash_done, hash, size, path, d)
             
     def lookupHash_done(self, locations, hash, size, path, d):
         if not locations:
+            log.msg('Peers for %s were not found' % path)
             getDefer = self.peers.get([path])
             getDefer.addCallback(d.callback)
         else:
+            log.msg('Found peers for $s: %r' % (path, locations))
             # Download from the found peers
             getDefer = self.peers.get(locations)
             getDefer.addCallback(d.callback)