commit 2d1fd22de9ab2648ab81287079a9b9b9f7700f64 Author: Ximin Luo infinity0@gmx.com Date: Tue Nov 19 15:29:19 2013 +0000
factor out the M2Crypto check to flashproxy.keys --- flashproxy-reg-appspot | 15 +++------------ flashproxy-reg-email | 17 +++-------------- flashproxy-reg-url | 15 +++------------ flashproxy/keys.py | 19 +++++++++++++++++++ 4 files changed, 28 insertions(+), 38 deletions(-)
diff --git a/flashproxy-reg-appspot b/flashproxy-reg-appspot index 134e9ff..616b407 100755 --- a/flashproxy-reg-appspot +++ b/flashproxy-reg-appspot @@ -10,14 +10,14 @@ import sys import urlparse import urllib2
-from flashproxy.keys import PIN_GOOGLE_CA_CERT, PIN_GOOGLE_PUBKEY_SHA1, check_certificate_pin, temp_cert +from flashproxy.keys import PIN_GOOGLE_CA_CERT, PIN_GOOGLE_PUBKEY_SHA1, check_certificate_pin, ensure_M2Crypto, temp_cert from flashproxy.util import parse_addr_spec, format_addr
try: from M2Crypto import SSL except ImportError: # Defer the error reporting so that --help works even without M2Crypto. - SSL = None + pass
DEFAULT_REMOTE_ADDRESS = "" DEFAULT_REMOTE_PORT = 9000 @@ -181,16 +181,7 @@ else: usage(sys.stderr) sys.exit(1)
-if SSL is None: - print >> sys.stderr, """\ -This program requires the M2Crypto library, which is not installed. - -You can install it using one of the packages at -http://chandlerproject.org/Projects/MeTooCrypto#Downloads. - -On Debian-like systems, use the command "apt-get install python-m2crypto".\ -""" - sys.exit(1) +ensure_M2Crypto()
if options.address_family != socket.AF_UNSPEC: getaddrinfo = socket.getaddrinfo diff --git a/flashproxy-reg-email b/flashproxy-reg-email index 6309cec..7dac8cb 100755 --- a/flashproxy-reg-email +++ b/flashproxy-reg-email @@ -9,16 +9,14 @@ import socket import sys import urllib
-from flashproxy.keys import PIN_GOOGLE_CA_CERT, PIN_GOOGLE_PUBKEY_SHA1, DEFAULT_FACILITATOR_PUBKEY_PEM, check_certificate_pin, temp_cert +from flashproxy.keys import PIN_GOOGLE_CA_CERT, PIN_GOOGLE_PUBKEY_SHA1, DEFAULT_FACILITATOR_PUBKEY_PEM, check_certificate_pin, ensure_M2Crypto, temp_cert from flashproxy.util import parse_addr_spec, format_addr
try: from M2Crypto import BIO, RSA, SSL except ImportError: # Defer the error reporting so that --help works even without M2Crypto. - BIO = None - RSA = None - SSL = None + pass
DEFAULT_REMOTE_ADDRESS = "" DEFAULT_REMOTE_PORT = 9000 @@ -145,16 +143,7 @@ else: usage(sys.stderr) sys.exit(1)
-if SSL is None: - print >> sys.stderr, """\ -This program requires the M2Crypto library, which is not installed. - -You can install it using one of the packages at -http://chandlerproject.org/Projects/MeTooCrypto#Downloads. - -On Debian-like systems, use the command "apt-get install python-m2crypto".\ -""" - sys.exit(1) +ensure_M2Crypto()
if options.address_family != socket.AF_UNSPEC: getaddrinfo = socket.getaddrinfo diff --git a/flashproxy-reg-url b/flashproxy-reg-url index 4685f28..26109fc 100755 --- a/flashproxy-reg-url +++ b/flashproxy-reg-url @@ -7,14 +7,14 @@ import sys import urllib import urlparse
-from flashproxy.keys import DEFAULT_FACILITATOR_PUBKEY_PEM +from flashproxy.keys import DEFAULT_FACILITATOR_PUBKEY_PEM, ensure_M2Crypto from flashproxy.util import parse_addr_spec, format_addr
try: from M2Crypto import BIO, RSA except ImportError: # Defer the error reporting so that --help works even without M2Crypto. - RSA = None + pass
DEFAULT_REMOTE_ADDRESS = "" DEFAULT_REMOTE_PORT = 9000 @@ -88,16 +88,7 @@ if not remote_addr[0]: print >> sys.stderr, "An IP address (not just a port) is required." sys.exit(1)
-if RSA is None: - print >> sys.stderr, """\ -This program requires the M2Crypto library, which is not installed. - -You can install it using one of the packages at -http://chandlerproject.org/Projects/MeTooCrypto#Downloads. - -On Debian-like systems, use the command "apt-get install python-m2crypto".\ -""" - sys.exit(1) +ensure_M2Crypto()
reg_plain = build_reg(remote_addr, options.transport) rsa = get_facilitator_pubkey() diff --git a/flashproxy/keys.py b/flashproxy/keys.py index 08ffc46..1365f07 100644 --- a/flashproxy/keys.py +++ b/flashproxy/keys.py @@ -4,6 +4,12 @@ import tempfile
from hashlib import sha1
+try: + import M2Crypto +except ImportError: + # Defer the error so that the main program gets a chance to print help text + M2Crypto = None + # We trust no other CA certificate than this. # # To find the certificate to copy here, @@ -98,3 +104,16 @@ class temp_cert(object):
def __exit__(self, type, value, traceback): os.unlink(self.path) + +def ensure_M2Crypto(): + if M2Crypto is None: + print >> sys.stderr, """\ +This program requires the M2Crypto library, which is not installed. + +You can install it using one of the packages at +http://chandlerproject.org/Projects/MeTooCrypto#Downloads. + +On Debian-like systems, use the command "apt-get install python-m2crypto".\ +""" + sys.exit(1) +
tor-commits@lists.torproject.org