commit 7cdc8cf5a94e677b3a0163a6e42cd2e9785cb37a Author: Philipp Winter phw@nymity.ch Date: Wed Jan 29 16:42:31 2020 -0800
Port get-tor-exists script to Python 3.
This involved the following:
* Changing the shebang from "python" to "python3".
* Using the ipaddress instead of the ipaddr module.
* Some str/bytes conversions.
* Getting rid of WebClientContextFactory.
* Using io.TextIOBase instead of file.
* Make the calling proxy.py expect str instead of bytes. --- bridgedb/proxy.py | 2 +- scripts/get-tor-exits | 41 ++++++++++------------------------------- 2 files changed, 11 insertions(+), 32 deletions(-)
diff --git a/bridgedb/proxy.py b/bridgedb/proxy.py index fdb824d..654b40c 100644 --- a/bridgedb/proxy.py +++ b/bridgedb/proxy.py @@ -425,7 +425,7 @@ class ExitListProtocol(protocol.ProcessProtocol):
def outReceived(self, data): """Some data was received from stdout.""" - self.data.append(data) + self.data.append(data.decode("utf-8"))
def outConnectionLost(self): """This will be called when stdout is closed.""" diff --git a/scripts/get-tor-exits b/scripts/get-tor-exits index 6ab2201..c250dbe 100755 --- a/scripts/get-tor-exits +++ b/scripts/get-tor-exits @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # This file is part of BridgeDB, a Tor bridge distribution system. @@ -17,8 +17,9 @@ from __future__ import print_function import os.path import socket import sys +import io
-from ipaddr import IPAddress +from ipaddress import IPv4Address
from OpenSSL import SSL
@@ -57,10 +58,10 @@ def getSelfIPAddress(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('bridges.torproject.org', 443)) name = s.getsockname()[0] - ip = IPAddress(name) + ip = IPv4Address(name) if ip.is_link_local or ip.is_private or ip.is_reserved: name = s.getpeername()[0] - ip = IPAddress(name) + ip = IPv4Address(name) except ValueError as error: log.err("get-tor-exits: A socket gave us something that wasn't an IP: %s" % error) @@ -117,7 +118,7 @@ class FileWriter(protocol.Protocol): """Write a portion of the download with ``bytes`` size to disk.""" if self.remaining: display = bytes[:self.remaining] - self.fh.write(display) + self.fh.write(display.decode("utf-8")) self.fh.flush() self.remaining -= len(display)
@@ -128,32 +129,11 @@ class FileWriter(protocol.Protocol): self.finished.callback(None)
-class WebClientContextFactory(ssl.ClientContextFactory): - """An HTTPS client.""" - - def getContext(self, hostname, port): - """Get this connection's OpenSSL context. - - By default, :api:`twisted.internet.ssl.ClientContextFactory` uses - ``OpenSSL.SSL.SSLv23_METHOD``, which allows SSLv2, SSLv3, and TLSv1, - then they disable SSLv2 in the - :api:`twisted.internet.ssl.ClientContextFactory.getContext` method. - - We disable SSLv3 also. - - :rtype: ``OpenSSL.SSL.Context`` - :returns: An OpenSSL context with options set. - """ - ctx = self._contextFactory(self.method) - ctx.set_options(SSL.OP_NO_SSLv2 ^ SSL.OP_NO_SSLv3) - return ctx - - def main(filename=None, address=None, port=None):
fh = filename if filename: - if (not isinstance(filename, file)) and (filename is not sys.stdout): + if (not isinstance(filename, io.TextIOBase)) and (filename is not sys.stdout): fh = open(filename, 'w')
if not address: @@ -170,9 +150,8 @@ def main(filename=None, address=None, port=None):
log.msg("get-tor-exits: Requesting %s..." % check)
- contextFactory = WebClientContextFactory() - agent = client.Agent(reactor, contextFactory) - d = agent.request("GET", check) + agent = client.Agent(reactor) + d = agent.request(b"GET", check.encode("utf-8")) d.addCallback(writeToFile, fh) d.addErrback(handle) d.addCallbacks(log.msg, log.err) @@ -182,7 +161,7 @@ def main(filename=None, address=None, port=None): reactor.run()
if filename: - if (not isinstance(filename, file)) and (filename is not sys.stdout): + if (not isinstance(filename, io.TextIOBase)) and (filename is not sys.stdout): fh.flush() fh.close()
tor-commits@lists.torproject.org