Skip to content

Commit

Permalink
fixup! fixup! Move HTTP templates to https/ distributor directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
isislovecruft committed Apr 16, 2015
1 parent aac364a commit e015ac7
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions lib/bridgedb/https/server.py
Expand Up @@ -6,8 +6,8 @@
# :copyright: (c) 2007-2015, The Tor Project, Inc.
# (c) 2013-2015, Isis Lovecruft
# :license: see LICENSE for licensing information
"""

"""
.. py:module:: bridgedb.https.server
:synopsis: Servers which interface with clients and distribute bridges
over HTTP(S).
Expand All @@ -16,16 +16,16 @@
=====================
Servers which interface with clients and distribute bridges over HTTP(S).
"""
import base64
import gettext
import logging
"""

import base64
import gettext
import logging
import random
import re
import time
import os
import re
import time
import os

from functools import partial

from ipaddr import IPv4Address
Expand All @@ -34,13 +34,13 @@
from mako.template import Template
from mako.lookup import TemplateLookup

from twisted.internet import reactor
from twisted.internet import reactor
from twisted.internet.error import CannotListenError
from twisted.web import resource
from twisted.web import static
from twisted.web import static
from twisted.web.server import NOT_DONE_YET
from twisted.web.server import Site
from twisted.web.util import redirectTo
from twisted.web.util import redirectTo

from bridgedb import captcha
from bridgedb import crypto
Expand All @@ -62,7 +62,7 @@

TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
rtl_langs = ('ar', 'he', 'fa', 'gu_IN', 'ku')

# Setting `filesystem_checks` to False is recommended for production servers,
# due to potential speed increases. This means that the atimes of the Mako
# template files aren't rechecked every time the template is requested
Expand Down Expand Up @@ -213,10 +213,10 @@ def __init__(self, publicKey=None, secretKey=None,
resource.Resource.__init__(self)
self.publicKey = publicKey
self.secretKey = secretKey
self.useForwardedHeader = useForwardedHeader
self.useForwardedHeader = useForwardedHeader
self.resource = protectedResource
def getClientIP(self, request):

def getClientIP(self, request):
"""Get the client's IP address from the :header:`X-Forwarded-For`
header, or from the :api:`request <twisted.web.server.Request>`.
Expand All @@ -227,7 +227,7 @@ def getClientIP(self, request):
:returns: The client's IP address, if it was obtainable.
"""
return getClientIP(request, self.useForwardedHeader)

def getCaptchaImage(self, request=None):
"""Get a CAPTCHA image.
Expand Down Expand Up @@ -268,7 +268,7 @@ def checkSolution(self, request):
"""
return False

def render_GET(self, request):
def render_GET(self, request):
"""Retrieve a ReCaptcha from the API server and serve it to the client.
:type request: :api:`twisted.web.http.Request`
Expand Down Expand Up @@ -297,8 +297,8 @@ def render_GET(self, request):

request.setHeader("Content-Type", "text/html; charset=utf-8")
return rendered
def render_POST(self, request):

def render_POST(self, request):
"""Process a client's CAPTCHA solution.
If the client's CAPTCHA solution is valid (according to
Expand Down Expand Up @@ -327,8 +327,8 @@ def render_POST(self, request):
logging.debug("Client failed a CAPTCHA; returning redirect to %s"
% request.uri)
return redirectTo(request.uri, request)


class GimpCaptchaProtectedResource(CaptchaProtectedResource):
"""A web resource which uses a local cache of CAPTCHAs, generated with
gimp-captcha_, to protect another resource.
Expand Down Expand Up @@ -625,9 +625,9 @@ def render_POST(self, request):
class BridgesResource(resource.Resource):
"""This resource displays bridge lines in response to a request."""

isLeaf = True
def __init__(self, distributor, schedule, N=1, useForwardedHeader=False,
isLeaf = True

def __init__(self, distributor, schedule, N=1, useForwardedHeader=False,
includeFingerprints=True):
"""Create a new resource for displaying bridges to a client.
Expand All @@ -642,16 +642,16 @@ def __init__(self, distributor, schedule, N=1, useForwardedHeader=False,
X-Forwarded-For header instead of the source IP address.
:param bool includeFingerprints: Do we include the bridge's
fingerprint in the response?
"""
gettext.install("bridgedb", unicode=True)
"""
gettext.install("bridgedb", unicode=True)
resource.Resource.__init__(self)
self.distributor = distributor
self.schedule = schedule
self.nBridgesToGive = N
self.useForwardedHeader = useForwardedHeader
self.includeFingerprints = includeFingerprints
def render(self, request):
self.distributor = distributor
self.schedule = schedule
self.nBridgesToGive = N
self.useForwardedHeader = useForwardedHeader
self.includeFingerprints = includeFingerprints

def render(self, request):
"""Render a response for a client HTTP request.
Presently, this method merely wraps :meth:`getBridgeRequestAnswer` to
Expand All @@ -673,7 +673,7 @@ def render(self, request):
response = self.renderAnswer(request)

return response

def getClientIP(self, request):
"""Get the client's IP address from the :header:`X-Forwarded-For`
header, or from the :api:`request <twisted.web.server.Request>`.
Expand All @@ -686,7 +686,7 @@ def getClientIP(self, request):
"""
return getClientIP(request, self.useForwardedHeader)

def getBridgeRequestAnswer(self, request):
def getBridgeRequestAnswer(self, request):
"""Respond to a client HTTP request for bridges.
:type request: :api:`twisted.web.http.Request`
Expand All @@ -701,7 +701,7 @@ def getBridgeRequestAnswer(self, request):

logging.info("Replying to web request from %s. Parameters were %r"
% (ip, request.args))

if ip:
bridgeRequest = HTTPSBridgeRequest()
bridgeRequest.client = ip
Expand All @@ -710,18 +710,18 @@ def getBridgeRequestAnswer(self, request):
bridgeRequest.withPluggableTransportType(request.args)
bridgeRequest.withoutBlockInCountry(request)
bridgeRequest.generateFilters()

bridges = self.distributor.getBridgesForIP(bridgeRequest, interval,
self.nBridgesToGive)
bridgeLines = "".join("%s\n" %
bridge.getBridgeLine(bridgeRequest, self.includeFingerprints)
for bridge in bridges)

return self.renderAnswer(request, bridgeLines)

def getResponseFormat(self, request):
"""Determine the requested format for the response.
:type request: :api:`twisted.web.http.Request`
:param request: A ``Request`` object containing the HTTP method, full
URI, and any URL/POST arguments and headers present.
Expand All @@ -735,16 +735,16 @@ def getResponseFormat(self, request):
if format and len(format):
format = format[0] # Choose the first arg
return format

def renderAnswer(self, request, bridgeLines=None):
"""Generate a response for a client which includes **bridgesLines**.
.. note: The generated response can be plain or HTML. A plain response
looks like::
voltron 1.2.3.4:1234 ABCDEF01234567890ABCDEF01234567890ABCDEF
voltron 5.5.5.5:5555 0123456789ABCDEF0123456789ABCDEF01234567
That is, there is no HTML, what you see is what you get, and what
you get is suitable for pasting directly into Tor Launcher (or
into a torrc, if you prepend ``"Bridge "`` to each line). The
Expand All @@ -767,11 +767,11 @@ def renderAnswer(self, request, bridgeLines=None):
rtl = False
format = self.getResponseFormat(request)

if format == 'plain':
request.setHeader("Content-Type", "text/plain")
if format == 'plain':
request.setHeader("Content-Type", "text/plain")
rendered = bridgeLines
else:
request.setHeader("Content-Type", "text/html; charset=utf-8")
else:
request.setHeader("Content-Type", "text/html; charset=utf-8")
qrcode = None
qrjpeg = generateQR(bridgeLines)

Expand Down Expand Up @@ -822,7 +822,7 @@ def addWebServer(config, distributor):
:raises SystemExit: if the servers cannot be started.
:rtype: :api:`twisted.web.server.Site`
:returns: A webserver.
"""
"""
captcha = None
fwdHeaders = config.HTTP_USE_IP_FROM_FORWARDED_HEADER
numBridges = config.HTTPS_N_BRIDGES_PER_ANSWER
Expand Down Expand Up @@ -877,12 +877,12 @@ def addWebServer(config, distributor):
protectedResource=bridges)
root.putChild('bridges', protected)
logging.info("Protecting resources with %s." % captcha.func.__name__)
else:
else:
root.putChild('bridges', bridges)

site = Site(root)
site.displayTracebacks = False

if config.HTTP_UNENCRYPTED_PORT: # pragma: no cover
ip = config.HTTP_UNENCRYPTED_BIND_IP or ""
port = config.HTTP_UNENCRYPTED_PORT or 80
Expand All @@ -891,7 +891,7 @@ def addWebServer(config, distributor):
except CannotListenError as error:
raise SystemExit(error)
logging.info("Started HTTP server on %s:%d" % (str(ip), int(port)))

if config.HTTPS_PORT: # pragma: no cover
ip = config.HTTPS_BIND_IP or ""
port = config.HTTPS_PORT or 443
Expand All @@ -903,5 +903,5 @@ def addWebServer(config, distributor):
except CannotListenError as error:
raise SystemExit(error)
logging.info("Started HTTPS server on %s:%d" % (str(ip), int(port)))
return site

return site

0 comments on commit e015ac7

Please sign in to comment.