Ejemplo n.º 1
0
    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`
        :param request: A ``Request`` object for a page which should be
            protected by a CAPTCHA.
        :rtype: str
        :returns: A rendered HTML page containing a ReCaptcha challenge image
            for the client to solve.
        """
        self.setCSPHeader(request)

        rtl = False
        image, challenge = self.getCaptchaImage(request)

        try:
            langs = translations.getLocaleFromHTTPRequest(request)
            rtl = translations.usingRTLLang(langs)
            # TODO: this does not work for versions of IE < 8.0
            imgstr = 'data:image/jpeg;base64,%s' % base64.b64encode(image)
            template = lookup.get_template('captcha.html')
            rendered = template.render(strings,
                                       rtl=rtl,
                                       lang=langs[0],
                                       imgstr=imgstr,
                                       challenge_field=challenge)
        except Exception as err:
            rendered = replaceErrorPage(request, err, 'captcha.html')

        request.setHeader("Content-Type", "text/html; charset=utf-8")
        return rendered
Ejemplo n.º 2
0
    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`
        :param request: A ``Request`` object for a page which should be
            protected by a CAPTCHA.
        :rtype: str
        :returns: A rendered HTML page containing a ReCaptcha challenge image
            for the client to solve.
        """
        rtl = False
        image, challenge = self.getCaptchaImage(request)

        try:
            langs = translations.getLocaleFromHTTPRequest(request)
            rtl = translations.usingRTLLang(langs)
            # TODO: this does not work for versions of IE < 8.0
            imgstr = 'data:image/jpeg;base64,%s' % base64.b64encode(image)
            template = lookup.get_template('captcha.html')
            rendered = template.render(strings,
                                       rtl=rtl,
                                       lang=langs[0],
                                       imgstr=imgstr,
                                       challenge_field=challenge)
        except Exception as err:
            rendered = replaceErrorPage(err, 'captcha.html')

        request.setHeader("Content-Type", "text/html; charset=utf-8")
        return rendered
Ejemplo n.º 3
0
    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
            plain format can be requested from BridgeDB's web service by
            adding an ``&format=plain`` HTTP GET parameter to the URL. Also
            note that you won't get a QRCode, usage instructions, error
            messages, or any other fanciness if you use the plain format.

        :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.
        :type bridgeLines: list or None
        :param bridgeLines: A list of strings used to configure a Tor client
            to use a bridge. If ``None``, then the returned page will instead
            explain that there were no bridges of the type they requested,
            with instructions on how to proceed.
        :rtype: str
        :returns: A plaintext or HTML response to serve.
        """
        rtl = False
        format = self.getResponseFormat(request)

        if format == 'plain':
            request.setHeader("Content-Type", "text/plain")
            try:
                rendered = bytes('\n'.join(bridgeLines))
            except Exception as err:
                rendered = replaceErrorPage(request, err, html=False)
        else:
            request.setHeader("Content-Type", "text/html; charset=utf-8")
            qrcode = None
            qrjpeg = generateQR(bridgeLines)

            if qrjpeg:
                qrcode = 'data:image/jpeg;base64,%s' % base64.b64encode(qrjpeg)
            try:
                langs = translations.getLocaleFromHTTPRequest(request)
                rtl = translations.usingRTLLang(langs)
                template = lookup.get_template('bridges.html')
                rendered = template.render(strings,
                                           rtl=rtl,
                                           lang=langs[0],
                                           answer=bridgeLines,
                                           qrcode=qrcode)
            except Exception as err:
                rendered = replaceErrorPage(request, err)

        return rendered
Ejemplo n.º 4
0
    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
            plain format can be requested from BridgeDB's web service by
            adding an ``&format=plain`` HTTP GET parameter to the URL. Also
            note that you won't get a QRCode, usage instructions, error
            messages, or any other fanciness if you use the plain format.

        :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.
        :type bridgeLines: list or None
        :param bridgeLines: A list of strings used to configure a Tor client
            to use a bridge. If ``None``, then the returned page will instead
            explain that there were no bridges of the type they requested,
            with instructions on how to proceed.
        :rtype: str
        :returns: A plaintext or HTML response to serve.
        """
        rtl = False
        format = self.getResponseFormat(request)

        if format == 'plain':
            request.setHeader("Content-Type", "text/plain")
            try:
                rendered = bytes('\n'.join(bridgeLines))
            except Exception as err:
                rendered = replaceErrorPage(request, err, html=False)
        else:
            request.setHeader("Content-Type", "text/html; charset=utf-8")
            qrcode = None
            qrjpeg = generateQR(bridgeLines)

            if qrjpeg:
                qrcode = 'data:image/jpeg;base64,%s' % base64.b64encode(qrjpeg)
            try:
                langs = translations.getLocaleFromHTTPRequest(request)
                rtl = translations.usingRTLLang(langs)
                template = lookup.get_template('bridges.html')
                rendered = template.render(strings,
                                           rtl=rtl,
                                           lang=langs[0],
                                           answer=bridgeLines,
                                           qrcode=qrcode)
            except Exception as err:
                rendered = replaceErrorPage(request, err)

        return rendered
Ejemplo n.º 5
0
 def render_GET(self, request):
     rtl = False
     try:
         langs = translations.getLocaleFromHTTPRequest(request)
         rtl = translations.usingRTLLang(langs)
         template = lookup.get_template('howto.html')
         rendered = template.render(strings, rtl=rtl, lang=langs[0])
     except Exception as err:  # pragma: no cover
         rendered = replaceErrorPage(err)
     request.setHeader("Content-Type", "text/html; charset=utf-8")
     return rendered
Ejemplo n.º 6
0
 def render_GET(self, request):
     rtl = False
     try:
         langs = translations.getLocaleFromHTTPRequest(request)
         rtl = translations.usingRTLLang(langs)
         template = lookup.get_template('howto.html')
         rendered = template.render(strings, rtl=rtl, lang=langs[0])
     except Exception as err:  # pragma: no cover
         rendered = replaceErrorPage(err)
     request.setHeader("Content-Type", "text/html; charset=utf-8")
     return rendered
Ejemplo n.º 7
0
    def test_getLocaleFromHTTPRequest_withLangParam_AcceptLanguage(self):
        """This request uses a '?lang=ar' param, with an 'Accept-Language'
        header which includes: ['de-de', 'en-gb', 'en', 'en-us'].

        The request result should be: ['fa', 'de-de', 'en-gb', 'en', 'en-us'].
        """
        request = DummyRequest([b"options"])
        request.headers.update(ACCEPT_LANGUAGE_HEADER)
        request.args.update({'lang': ['fa']})

        parsed = translations.getLocaleFromHTTPRequest(request)

        self.assertEqual(len(parsed), 3)
        self.assertEqual(parsed[0], 'fa')
        self.assertEqual(parsed[1], 'en')
        self.assertEqual(parsed[2], 'en_US')
Ejemplo n.º 8
0
    def test_getLocaleFromHTTPRequest_withLangParam_AcceptLanguage(self):
        """This request uses a '?lang=ar' param, with an 'Accept-Language'
        header which includes: ['de-de', 'en-gb', 'en', 'en-us'].

        The request result should be: ['fa', 'de-de', 'en-gb', 'en', 'en-us'].
        """
        request = DummyRequest([b"options"])
        request.headers.update(ACCEPT_LANGUAGE_HEADER)
        request.args.update({b'lang': [b'fa']})

        parsed = translations.getLocaleFromHTTPRequest(request)
        self.assertEqual(parsed[0], 'fa')
        self.assertEqual(parsed[1], 'en')
        self.assertEqual(parsed[2], 'en_US')
        #self.assertEqual(parsed[3], 'en-gb')
        self.assertEqual(len(parsed), 3)
Ejemplo n.º 9
0
    def test_getLocaleFromHTTPRequest_withLangParam(self):
        """This request uses a '?lang=ar' param, without an 'Accept-Language'
        header.

        The request result should be: ['ar', 'en', 'en-US'].
        """
        request = DummyRequest([b"bridges"])
        request.headers.update(REALISH_HEADERS)
        request.args.update({
            b'transport': [b'obfs3',],
            b'lang': [b'ar',],
        })

        parsed = translations.getLocaleFromHTTPRequest(request)
        self.assertEqual(parsed[0], 'ar')
        self.assertEqual(parsed[1], 'en')
        self.assertEqual(parsed[2], 'en_US')
        self.assertEqual(len(parsed), 3)
Ejemplo n.º 10
0
    def renderAnswer(self, request, bridgeLines=None, format=None):
        """Generate a response for a client which includes **bridges**.

        The generated response can be plaintext or HTML.

        :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.
        :type bridgeLines: list or None
        :param bridgeLines: A list of strings used to configure a Tor client
            to use a bridge.
        :type format: str or None
        :param format: If ``'plain'``, return a plaintext response. Otherwise,
            use the :file:`bridgedb/templates/bridges.html` template to render
            an HTML response page which includes the **bridges**.
        :rtype: str
        :returns: A plaintext or HTML response to serve.
        """
        rtl = False

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

            if qrjpeg:
                qrcode = 'data:image/jpeg;base64,%s' % base64.b64encode(qrjpeg)
            try:
                langs = translations.getLocaleFromHTTPRequest(request)
                rtl = translations.usingRTLLang(langs)
                template = lookup.get_template('bridges.html')
                rendered = template.render(strings,
                                           rtl=rtl,
                                           lang=langs[0],
                                           answer=bridgeLines,
                                           qrcode=qrcode)
            except Exception as err:
                rendered = replaceErrorPage(err)

        return rendered
Ejemplo n.º 11
0
    def render_GET(self, request):
        """Handles requests for the webserver root document.

        For example, this function handles requests for
        https://bridges.torproject.org/.

        :type request: :api:`twisted.web.server.Request`
        :param request: An incoming request.
        """
        rtl = False
        try:
            langs = translations.getLocaleFromHTTPRequest(request)
            rtl = translations.usingRTLLang(langs)
            template = lookup.get_template('index.html')
            rendered = template.render(strings,
                                       rtl=rtl,
                                       lang=langs[0])
        except Exception as err:
            rendered = replaceErrorPage(err)

        return rendered
Ejemplo n.º 12
0
    def render_GET(self, request):
        """Handles requests for the webserver root document.

        For example, this function handles requests for
        https://bridges.torproject.org/.

        :type request: :api:`twisted.web.server.Request`
        :param request: An incoming request.
        """
        rtl = False
        try:
            langs = translations.getLocaleFromHTTPRequest(request)
            rtl = translations.usingRTLLang(langs)
            template = lookup.get_template('index.html')
            rendered = template.render(strings,
                                       rtl=rtl,
                                       lang=langs[0])
        except Exception as err:
            rendered = replaceErrorPage(err)

        return rendered