[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r12811: More tweaks on bridgedb (in bridgedb/trunk: . lib/bridgedb)



Author: nickm
Date: 2007-12-14 14:11:32 -0500 (Fri, 14 Dec 2007)
New Revision: 12811

Modified:
   bridgedb/trunk/
   bridgedb/trunk/TODO
   bridgedb/trunk/lib/bridgedb/Main.py
   bridgedb/trunk/lib/bridgedb/Server.py
Log:
 r17148@catbus:  nickm | 2007-12-14 14:10:17 -0500
 More tweaks on bridgedb



Property changes on: bridgedb/trunk
___________________________________________________________________
 svk:merge ticket from /bridgedb/trunk [r17148] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: bridgedb/trunk/TODO
===================================================================
--- bridgedb/trunk/TODO	2007-12-14 06:15:59 UTC (rev 12810)
+++ bridgedb/trunk/TODO	2007-12-14 19:11:32 UTC (rev 12811)
@@ -1,7 +1,5 @@
 
 For dec:
-- make mail get configured right.
-- actually invoke server stuff.
 - reload bridges on sighup
 - write a README
 
@@ -15,3 +13,4 @@
 - decent template for web interface
 - decent template for mail interface
 - implement 'help' command
+ 
\ No newline at end of file

Modified: bridgedb/trunk/lib/bridgedb/Main.py
===================================================================
--- bridgedb/trunk/lib/bridgedb/Main.py	2007-12-14 06:15:59 UTC (rev 12810)
+++ bridgedb/trunk/lib/bridgedb/Main.py	2007-12-14 19:11:32 UTC (rev 12811)
@@ -9,6 +9,8 @@
 
 import bridgedb.Bridges as Bridges
 import bridgedb.Dist as Dist
+import bridgedb.Time as Time
+import bridgedb.Server as Server
 
 class Conf:
     def __init__(self, **attrs):
@@ -25,9 +27,11 @@
 
     HTTPS_DIST = True,
     HTTPS_SHARE=10,
+    HTTPS_BIND_IP=None,
     HTTPS_PORT=6789,
     HTTPS_CERT_FILE="cert",
     HTTPS_KEY_FILE="key",
+    HTTP_UNENCRYPTED_BIND_IP=None,
     HTTP_UNENCRYPTED_PORT=6788,
     HTTPS_N_BRIDGES_PER_ANSWER=2,
 
@@ -36,7 +40,9 @@
     EMAIL_DOMAINS = [ "gmail.com", "yahoo.com" ],
     EMAIL_DOMAIN_MAP = { "mail.google.com" : "gmail.com",
                          "googlemail.com" : "gmail.com", },
+    EMAIL_BIND_IP=None,
     EMAIL_PORT=6725,
+    EMAIL_N_BRIDGES_PER_ANSWER=2,
 
     RESERVED_SHARE=2,
   )
@@ -80,8 +86,9 @@
 
 def startup(cfg):
     key = getKey(MASTER_KEY_FILE)
+    dblogfile = None
 
-    store = anydbm.open(cfg.DB_FILE, "c", 0600)
+    baseStore = store = anydbm.open(cfg.DB_FILE, "c", 0600)
     if DB_LOG_FILE:
         dblogfile = open(cfg.DB_LOG_FILE, "a+", 0)
         store = LogDB("db", store, dblogfile)
@@ -94,6 +101,7 @@
                                  Dist.N_IP_CLUSTERS,
                                  Bridges.get_hmac(key, "HTTPS-IP-Dist-Key"))
         splitter.addRing(ipDistributor, "https", cfg.HTTPS_SHARE)
+        webSchedule = Time.IntervalSchedule("day", 2)
 
     if cfg.EMAIL_DIST and cfg.EMAIL_SHARE:
         for d in cfg.EMAIL_DOMAINS:
@@ -103,6 +111,7 @@
             Bridges.PrefixStore(store, "em|"),
             cfg.EMAIL_DOMAIN_MAP.copy())
         splitter.addRing(emailDistributor, "email", cfg.EMAIL_SHARE)
+        emailSchedule = Time.IntervalSchedule("day", 1)
 
     if cfg.RESERVED_SHARE:
         splitter.addRing(Bridges.UnallocatedHolder(),
@@ -115,5 +124,16 @@
 
     load(cfg, splitter)
 
-    # XXXX create twisted listeners.
+    if cfg.HTTPS_DIST and cfg.HTTPS_SHARE:
+        Server.addWebServer(cfg, ipDistributor, webSchedule)
 
+    if cfg.EMAIL_DIST and cfg.EMAIL_SHARE:
+        Server.addSMTPServer(cfg, emailDistributor, emailSchedule)
+
+    try:
+        Server.run()
+    finally:
+        baseStore.close()
+        if dblogfile is not None:
+            dblogfile.close()
+

Modified: bridgedb/trunk/lib/bridgedb/Server.py
===================================================================
--- bridgedb/trunk/lib/bridgedb/Server.py	2007-12-14 06:15:59 UTC (rev 12810)
+++ bridgedb/trunk/lib/bridgedb/Server.py	2007-12-14 19:11:32 UTC (rev 12811)
@@ -40,13 +40,15 @@
     resource = WebResource(dist, sched, cfg.HTTPS_N_BRIDGES_PER_ANSWER)
     site = Site(resource)
     if cfg.HTTP_UNENCRYPTED_PORT:
-        reactor.listenTCP(cfg.HTTP_UNENCRYPTED_PORT, site)
+        ip = cfg.HTTPS_BIND_IP or ""
+        reactor.listenTCP(cfg.HTTP_UNENCRYPTED_PORT, site, interface=ip)
     if cfg.HTTPS_PORT:
         from twisted.internet.ssl import DefaultOpenSSLContextFactory
         from OpenSSL.SSL import SSLv3_METHOD
+        ip = cfg.HTTP_UNENCRYPTED_BIND_IP or ""
         factory = DefaultOpenSSLContextFactory(cfg.HTTPS_KEY_FILE,
                                                cfg.HTTPS_CERT_FILE)
-        reactor.listenSSL(cfg.HTTPS_PORT, site, factory)
+        reactor.listenSSL(cfg.HTTPS_PORT, site, factory, interface=ip)
     return site
 
 class MailFile:
@@ -120,19 +122,20 @@
     return d
 
 class MailContext:
-    def __init__(self, cfg):
+    def __init__(self, cfg, dist, sched):
         self.username = "bridges"
         self.maximumSize = 32*1024
-        self.smtpServer
-        self.smtpPort
-        self.fromAddr
-        self.distributor
-        self.schedule
+        self.smtpServer = "127.0.0.1"
+        self.smtpPort = 25
+        self.fromAddr = "bridges@xxxxxxxxxxxxxxxxxxxxxx"
+        self.distributor = dist
+        self.schedule = sched
+        self.N = cfg.EMAIL_N_BRIDGES_PER_ANSWER
 
 class MailMessage:
     implements(twisted.mail.smtp.IMessage)
 
-    def __init__(self, ctx)
+    def __init__(self, ctx):
         self.ctx = ctx
         self.lines = []
         self.nBytes = 0
@@ -182,8 +185,12 @@
         return p
 
 def addSMTPServer(cfg, dist, sched):
-    ctx = MailContext() #XXXX
+    ctx = MailContext(cfg)
     factory = MailFactory()
     factory.setBridgeDBContext(ctx)
-    reactor.listenTCP(cfg.EMAIL_PORT, factory)
+    ip = cfg.EMAIL_BIND_IP or ""
+    reactor.listenTCP(cfg.EMAIL_PORT, factory, interface=ip)
     return factory
+
+def runServers():
+    reactor.run()