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

[or-cvs] r21894: {check} Make TorBulkExitList.py and TorCheck.py work as wsgi applica (check/trunk/cgi-bin)



Author: weasel
Date: 2010-03-09 18:16:36 +0000 (Tue, 09 Mar 2010)
New Revision: 21894

Modified:
   check/trunk/cgi-bin/TorBulkExitList.py
   check/trunk/cgi-bin/TorCheck.py
Log:
Make TorBulkExitList.py and TorCheck.py work as wsgi applications

Modified: check/trunk/cgi-bin/TorBulkExitList.py
===================================================================
--- check/trunk/cgi-bin/TorBulkExitList.py	2010-03-09 18:16:15 UTC (rev 21893)
+++ check/trunk/cgi-bin/TorBulkExitList.py	2010-03-09 18:16:36 UTC (rev 21894)
@@ -1,3 +1,4 @@
+#!/usr/bin/python
 import re
 import socket
 import stat
@@ -4,8 +5,7 @@
 import os
 import time
 import DNS
-from mod_python import apache
-from mod_python import util
+import cgi
 
 DNS.ParseResolvConf()
 def bulkCheck(RemoteServerIP, RemotePort):
@@ -72,7 +72,7 @@
     RawExitListURL = "http://exitlist.torproject.org/exitAddresses";
 
     # Currently fake this and return a static file:
-    RawExitList = '/var/lib/tordnsel/state/exit-addresses'
+    RawExitList = '/srv/check.torproject.org/tordnsel/state/exit-addresses'
 
     return RawExitList
 
@@ -179,10 +179,9 @@
         # that we don't understand. Return a failure code.
         return 2
 
-def parseAddress(req):
+def parseAddress(formSubmission):
     # Get the ip from apache
     user_supplied_ip = None
-    formSubmission=util.FieldStorage(req)
     user_supplied_ip = formSubmission.getfirst("ip", None)
 
     # Check the IP, fail with a None
@@ -197,10 +196,9 @@
 
     return parsed_ip 
 
-def parsePort(req):
+def parsePort(formSubmission):
     # Get the port from apache
     user_supplied_port = None
-    formSubmission = util.FieldStorage(req)
     user_supplied_port = formSubmission.getfirst("port", "80")
 
     # Verify that the port is a number between 1 and 65535
@@ -215,15 +213,15 @@
 
     return parsed_port
     
-def handler(req):
-    
-    req.send_http_header()
-    req.content_type = 'text/plain; charset=utf-8'
+def handler(req, environ, start_response):
+    formSubmission = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
 
-    RemoteServerIP = parseAddress(req)
-    RemotePort = parsePort(req)
+    RemoteServerIP = parseAddress(formSubmission)
+    RemotePort = parsePort(formSubmission)
     
     if RemoteServerIP is not None:
+        response_headers = [('Content-type', 'text/plain; charset=utf-8')]
+        start_response('200 OK', response_headers)
 
         updateCache()
         TestedExits = bulkCheck(RemoteServerIP, RemotePort)
@@ -231,8 +229,8 @@
         " on Port " + RemotePort + " #\n")
 
         querystring = "ip=%s" % RemoteServerIP
-	if RemotePort != "80":
-		querystring += "&port=%s" % RemotePort
+        if RemotePort != "80":
+            querystring += "&port=%s" % RemotePort
 
         req.write("# You can update this list by visiting " + \
         "https://check.torproject.org/cgi-bin/TorBulkExitList.py?%s #\n" % querystring)
@@ -243,12 +241,11 @@
         for exit in TestedExits:
             req.write(str(exit))
 
-        return apache.OK
-
+        return
     else:
+        response_headers = [('Content-type', 'text/html; charset=utf-8')]
+        start_response('200 OK', response_headers)
 
-        req.send_http_header()
-        req.content_type = 'text/html; charset=utf-8'
         req.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '\
         '"http://www.w3.org/TR/REC-html40/loose.dtd";>\n')
         req.write('<html>\n')
@@ -273,7 +270,7 @@
         req.write('\n')
 
         req.write('<img alt="' + ("Tor icon") + \
-        '" src="https://check.torproject.org/images/tor-on.png";>\n<br>')
+        '" src="/images/tor-on.png">\n<br>')
         req.write('<br>\n<br>\n')
        
         req.write('Welcome to the Tor Bulk Exit List exporting tool.<br><br>\n')
@@ -302,5 +299,15 @@
         req.write('</body>')
         req.write('</html>')
 
-        return apache.OK
+class FakeReq(list):
+    def write(self, str):
+        self.append(str)
 
+def application(environ, start_response):
+    req = FakeReq()
+    handler(req, environ, start_response)
+    return req
+
+# vim:set ts=4:
+# vim:set et:
+# vim:set shiftwidth=4:

Modified: check/trunk/cgi-bin/TorCheck.py
===================================================================
--- check/trunk/cgi-bin/TorCheck.py	2010-03-09 18:16:15 UTC (rev 21893)
+++ check/trunk/cgi-bin/TorCheck.py	2010-03-09 18:16:36 UTC (rev 21894)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python
 # -*- coding: utf-8 -*-
 """
  TorCheck.py
@@ -30,15 +30,18 @@
 except ImportError:
     antigravity = None
 
-import DNS 
+import cgi
+import DNS
 # This is pydns and can be downloaded from http://pydns.sourceforge.net/ 
 # Or use the Debian package listed above
-from mod_python import apache
-from mod_python import util
 import cgitb; cgitb.enable()
 import gettext
 import locale
+import os
 
+#os.environ['LOCPATH']='/usr/share/locale:/srv/check.torproject.org/trunk/i18n/locale'
+localedir ='/srv/check.torproject.org/trunk/i18n/locale'
+
 # We could also explictly query the remote EL server
 # This is not as good as using a cache for obvious reasons
 DNS.DiscoverNameServers()
@@ -148,19 +151,16 @@
     # Fall back to default
     return default_locale
     
-def showLogo(req):
+def showLogo(formSubmission):
     # By default, we'll show a logo
     show_logo = True
-    formSubmission=util.FieldStorage(req)
     hide_logo = formSubmission.getfirst("small", None)
     # hide_logo isn't cleaned - do not use it for anything else
     if hide_logo:
         return False 
     return show_logo
 
-def parseLang(req):
-    user_supplied_lang = None
-    formSubmission=util.FieldStorage(req)
+def parseLang(formSubmission):
     user_supplied_lang = formSubmission.getfirst("lang", None)
 
     # Find the best match for the requested language
@@ -168,7 +168,7 @@
 
     # i18n with Unicode!
     # Ensure you have properly installed TorCheck.{po,pot,mo} files
-    lang = gettext.translation('TorCheck', languages=[locale])
+    lang = gettext.translation('TorCheck', localedir=localedir, languages=[locale], fallback=True)
     lang.install()
 
 def printTorButton(req, UsingTor):
@@ -182,23 +182,22 @@
     req.write('</body></html>')
 
 # Now that we know everything we need, lets print the website
-def handler(req):
-
+def handler(req, environ, start_response):
     # Make a DNS request to the EL and decide what to tell the user
-    UsingTor = isUsingTor(req.connection.remote_ip, "80")
+    UsingTor = isUsingTor(environ['REMOTE_ADDR'], "80")
     # Try to hit a cornercase where the user can exit to 443 but not 80
     if UsingTor != 0:
-        UsingTor = isUsingTor(req.connection.remote_ip, "443")
+        UsingTor = isUsingTor(environ['REMOTE_ADDR'], "443")
 
-    req.content_type = 'text/html; charset=utf-8'
-    req.send_http_header()
+    response_headers = [('Content-type', 'text/html; charset=utf-8')]
+    start_response('200 OK', response_headers)
 
     # We want to catch TorButton queries so they don't output overhead
     # This is to make all the data fit into one cell
-    formSubmission=util.FieldStorage(req)
+    formSubmission = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
     if formSubmission.getfirst("TorButton", None):
         printTorButton(req, UsingTor)
-        return apache.OK
+        return
 
     # First lets construct the simple webpage:
     req.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '\
@@ -219,8 +218,8 @@
     req.write('</head>\n')
     req.write('<body>\n')
 
-    parseLang(req)
-    hideLogo = showLogo(req)
+    parseLang(formSubmission)
+    hideLogo = showLogo(formSubmission)
 
     req.write('<center>\n')
 
@@ -228,7 +227,7 @@
         req.write('\n')
         if hideLogo:
             req.write('<img alt="' + _("Congratulations. You are using Tor.") + \
-            '" src="https://check.torproject.org/images/tor-on.png";>\n<br>')
+            '" src="/images/tor-on.png">\n<br>')
         req.write('<h1 style="color: #0A0">\n')
         req.write(_('Congratulations. You are using Tor.'))
         req.write('<br>\n<br>\n')
@@ -241,7 +240,7 @@
         req.write('\n')
         if hideLogo:
             req.write('<img alt="' + _("Sorry. You are not using Tor.") + '" '\
-            'src="https://check.torproject.org/images/tor-off.png";>\n<br>')
+            'src="/images/tor-off.png">\n<br>')
         req.write('<h1 style="color: #A00">')
         req.write(_('Sorry. You are not using Tor.'))
         req.write('<br>\n<br>\n')
@@ -254,7 +253,7 @@
     elif UsingTor == 2:
         req.write('\n')
         req.write('<img alt="' + _("Sorry, your query failed or an unexpected response was received.") + '" '\
-        'src="https://check.torproject.org/images/tor-off.png";>\n<br>')
+        'src="/images/tor-off.png">\n<br>')
         req.write('<h1 style="color: #A00">\n')
         req.write(_('Sorry, your query failed or an unexpected response was received.'))
         req.write('<br>\n')
@@ -270,7 +269,7 @@
     req.write(_('Additional information: '))
     req.write('<br>\n')
     req.write(_('Your IP address appears to be: '))
-    req.write('<b>%s</b><br>\n' % req.connection.remote_ip )
+    req.write('<b>%s</b><br>\n' % environ['REMOTE_ADDR'] )
     req.write(_('This small script is powered by <a href="http://exitlist.torproject.org/";>tordnsel</a>'))
     req.write('<br>')
     req.write(_('You may also be interested in the <a href="/cgi-bin/TorBulkExitList.py">Tor Bulk Exit List Exporter</a>'))
@@ -294,4 +293,16 @@
     req.write('</body>')
     req.write('</html>')
 
-    return apache.OK
+
+class FakeReq(list):
+    def write(self, str):
+        self.append(str)
+
+def application(environ, start_response):
+    req = FakeReq()
+    handler(req, environ, start_response)
+    return req
+
+# vim:set ts=4:
+# vim:set et:
+# vim:set shiftwidth=4: