commit 56ff2609cf9ec4f36357d92123c95922b4fbdc44 Author: Ximin Luo infinity0@gmx.com Date: Tue Nov 19 16:12:07 2013 +0000
migrate reg methods to argparse --- flashproxy-reg-appspot | 95 ++++++++++++-------------------- flashproxy-reg-email | 141 +++++++++++++++++++----------------------------- flashproxy-reg-http | 87 +++++++++++------------------- flashproxy-reg-url | 80 +++++++++++---------------- 4 files changed, 151 insertions(+), 252 deletions(-)
diff --git a/flashproxy-reg-appspot b/flashproxy-reg-appspot index 616b407..ca78419 100755 --- a/flashproxy-reg-appspot +++ b/flashproxy-reg-appspot @@ -1,7 +1,7 @@ #!/usr/bin/env python """Register with a facilitator through Google App Engine."""
-import getopt +import argparse import httplib import os import socket @@ -19,8 +19,7 @@ except ImportError: # Defer the error reporting so that --help works even without M2Crypto. pass
-DEFAULT_REMOTE_ADDRESS = "" -DEFAULT_REMOTE_PORT = 9000 +DEFAULT_REMOTE = ("", 9000) DEFAULT_TRANSPORT = "websocket"
# The domain to which requests appear to go. @@ -37,29 +36,6 @@ class options(object): transport = DEFAULT_TRANSPORT safe_logging = True
-def usage(f = sys.stdout): - print >> f, """\ -Usage: %(progname)s [REMOTE][:PORT] -Register with a flash proxy facilitator through a Google App Engine app. -By default the remote address registered is "%(remote_addr)s" (the -external IP address is guessed). - - -4 name lookups use only IPv4. - -6 name lookups use only IPv6. - --disable-pin don't check server public key against a known pin. - --facilitator-pubkey=FILENAME - encrypt registrations to the given PEM-formatted - public key (default built-in). - -h, --help show this help. - --transport=TRANSPORT register using the given transport - (default "%(transport)s"). - --unsafe-logging don't scrub IP addresses from logs.\ -""" % { - "progname": sys.argv[0], - "remote_addr": format_addr((DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT)), - "transport": DEFAULT_TRANSPORT, -} - def safe_str(s): """Return "[scrubbed]" if options.safe_logging is true, and s otherwise.""" if options.safe_logging: @@ -149,45 +125,44 @@ def get_external_ip(): finally: f.close()
-opt, args = getopt.gnu_getopt(sys.argv[1:], "46h", [ - "disable-pin", - "facilitator-pubkey=", - "help", - "transport=", - "unsafe-logging", -]) -for o, a in opt: - if o == "-4": - options.address_family = socket.AF_INET - elif o == "-6": - options.address_family = socket.AF_INET6 - elif o == "--disable-pin": - options.use_certificate_pin = False - elif o == "--facilitator-pubkey": - options.facilitator_pubkey_filename = a - elif o == "-h" or o == "--help": - usage() - sys.exit() - elif o == "--transport": - options.transport = a - elif o == "--unsafe-logging": - options.safe_logging = False - -if len(args) == 0: - remote_addr = (DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT) -elif len(args) == 1: - remote_addr = parse_addr_spec(args[0], DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT) -else: - usage(sys.stderr) - sys.exit(1) - -ensure_M2Crypto() - +parser = argparse.ArgumentParser( + description="Register with a facilitator through a Google App Engine app.") +# common opts +parser.add_argument("-4", help="name lookups use only IPv4.", + action="store_const", const=socket.AF_INET, dest="address_family") +parser.add_argument("-6", help="name lookups use only IPv6.", + action="store_const", const=socket.AF_INET6, dest="address_family") +parser.add_argument("--unsafe-logging", help="don't scrub IP addresses and " + "other sensitive information from logs.", action="store_true") +parser.add_argument("--disable-pin", help="disable all certificate pinning " + "checks", action="store_true",) +parser.add_argument("--facilitator-pubkey", help="encrypt registrations to " + "the given PEM-formatted public key file (default built-in).", + metavar='FILENAME') +parser.add_argument("--transport", + help="register using the given transport, default %(default)s.", + default=DEFAULT_TRANSPORT) +# common args +parser.add_argument("remote_addr", + help="remote to register, default %s - the external IP address is guessed." + % format_addr(DEFAULT_REMOTE), + metavar="REMOTE:PORT", default="", nargs="?", + type=lambda x: parse_addr_spec(x, *DEFAULT_REMOTE)) + +ns = parser.parse_args(sys.argv[1:]) +options.address_family = ns.address_family or socket.AF_UNSPEC if options.address_family != socket.AF_UNSPEC: getaddrinfo = socket.getaddrinfo def getaddrinfo_replacement(host, port, family, *args, **kwargs): return getaddrinfo(host, port, options.address_family, *args, **kwargs) socket.getaddrinfo = getaddrinfo_replacement +options.safe_logging = not ns.unsafe_logging +options.use_certificate_pin = not ns.disable_pin +options.facilitator_pubkey_filename = ns.facilitator_pubkey +options.transport = ns.transport +remote_addr = ns.remote_addr + +ensure_M2Crypto()
if not remote_addr[0]: try: diff --git a/flashproxy-reg-email b/flashproxy-reg-email index 7dac8cb..a7e7d23 100755 --- a/flashproxy-reg-email +++ b/flashproxy-reg-email @@ -1,7 +1,7 @@ #!/usr/bin/env python """Register with a facilitator using the email method."""
-import getopt +import argparse import os import re import smtplib @@ -18,12 +18,10 @@ except ImportError: # Defer the error reporting so that --help works even without M2Crypto. pass
-DEFAULT_REMOTE_ADDRESS = "" -DEFAULT_REMOTE_PORT = 9000 +DEFAULT_REMOTE = ("", 9000) DEFAULT_EMAIL_ADDRESS = "flashproxyreg.a@gmail.com" # dig MX gmail.com -DEFAULT_SMTP_HOST = "gmail-smtp-in.l.google.com" -DEFAULT_SMTP_PORT = 25 +DEFAULT_SMTP = ("gmail-smtp-in.l.google.com", 25) DEFAULT_TRANSPORT = "websocket"
# Use this to prevent Python smtplib from guessing and leaking our hostname. @@ -42,41 +40,6 @@ class options(object): transport = DEFAULT_TRANSPORT safe_logging = True
-def usage(f = sys.stdout): - print >> f, """\ -Usage: %(progname)s [REMOTE][:PORT] -Register with a flash proxy facilitator through email. Makes a STARTTLS -connection to an SMTP server and sends mail with a client IP address to a -designated address. By default the remote address registered is -"%(remote_addr)s" (the external IP address is guessed). - -Using an SMTP server or email address other than the defaults will not work -unless you have made special arrangements to connect them to a facilitator. - -This program requires the M2Crypto library for Python. - - -4 name lookups use only IPv4. - -6 name lookups use only IPv6. - -d, --debug enable debugging output (Python smtplib messages). - --disable-pin don't check server public key against a known pin. - -e, --email=ADDRESS send mail to ADDRESS (default "%(email_addr)s"). - --facilitator-pubkey=FILENAME - encrypt registrations to the given PEM-formatted - public key (default built-in). - -h, --help show this help. - -s, --smtp=HOST[:PORT] use the given SMTP server - (default "%(smtp_addr)s"). - --transport=TRANSPORT register using the given transport - (default "%(transport)s"). - --unsafe-logging don't scrub IP addresses from logs.\ -""" % { - "progname": sys.argv[0], - "remote_addr": format_addr((DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT)), - "email_addr": DEFAULT_EMAIL_ADDRESS, - "smtp_addr": format_addr((DEFAULT_SMTP_HOST, DEFAULT_SMTP_PORT)), - "transport": DEFAULT_TRANSPORT, -} - def safe_str(s): """Return "[scrubbed]" if options.safe_logging is true, and s otherwise.""" if options.safe_logging: @@ -99,57 +62,63 @@ def get_facilitator_pubkey(): else: return RSA.load_pub_key_bio(BIO.MemoryBuffer(DEFAULT_FACILITATOR_PUBKEY_PEM))
-options.email_addr = DEFAULT_EMAIL_ADDRESS -options.smtp_addr = (DEFAULT_SMTP_HOST, DEFAULT_SMTP_PORT) - -opts, args = getopt.gnu_getopt(sys.argv[1:], "46de:hs:", [ - "debug", - "disable-pin", - "email=", - "facilitator-pubkey=", - "help", - "smtp=", - "transport=", - "unsafe-logging", -]) -for o, a in opts: - if o == "-4": - options.address_family = socket.AF_INET - elif o == "-6": - options.address_family = socket.AF_INET6 - elif o == "-d" or o == "--debug": - options.debug = True - elif o == "--disable-pin": - options.use_certificate_pin = False - elif o == "-e" or o == "--email": - options.email_addr = a - elif o == "--facilitator-pubkey": - options.facilitator_pubkey_filename = a - elif o == "-h" or o == "--help": - usage() - sys.exit() - elif o == "-s" or o == "--smtp": - options.smtp_addr = parse_addr_spec(a, DEFAULT_SMTP_HOST, DEFAULT_SMTP_PORT) - elif o == "--transport": - options.transport = a - elif o == "--unsafe-logging": - options.safe_logging = False - -if len(args) == 0: - options.remote_addr = (DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT) -elif len(args) == 1: - options.remote_addr = parse_addr_spec(args[0], DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT) -else: - usage(sys.stderr) - sys.exit(1) - -ensure_M2Crypto() - +parser = argparse.ArgumentParser( + description="Register with a flash proxy facilitator through email. Makes " + "a STARTTLS connection to an SMTP server and sends mail with a client IP " + "address to a designated address.", + epilog="Using an SMTP server or email address other than the defaults will " + "not work unless you have made special arrangements to connect them to a " + "facilitator.") +# common opts +parser.add_argument("-4", help="name lookups use only IPv4.", + action="store_const", const=socket.AF_INET, dest="address_family") +parser.add_argument("-6", help="name lookups use only IPv6.", + action="store_const", const=socket.AF_INET6, dest="address_family") +parser.add_argument("--unsafe-logging", help="don't scrub IP addresses and " + "other sensitive information from logs.", action="store_true") +parser.add_argument("--disable-pin", help="disable all certificate pinning " + "checks", action="store_true",) +parser.add_argument("--facilitator-pubkey", help="encrypt registrations to " + "the given PEM-formatted public key file (default built-in).", + metavar='FILENAME') +parser.add_argument("--transport", + help="register using the given transport, default %(default)s.", + default=DEFAULT_TRANSPORT) +# common args +parser.add_argument("remote_addr", + help="remote to register, default %s - the external IP address is guessed." + % format_addr(DEFAULT_REMOTE), + metavar="REMOTE:PORT", default="", nargs="?", + type=lambda x: parse_addr_spec(x, *DEFAULT_REMOTE)) +# specific opts +parser.add_argument("-e", "--email", metavar="ADDRESS", + help="send mail to ADDRESS, default %(default)s.", + default=DEFAULT_EMAIL_ADDRESS) +parser.add_argument("-s", "--smtp", metavar="HOST[:PORT]", + help="use the given SMTP server, default %s." % format_addr(DEFAULT_SMTP), + default="", type=lambda x: parse_addr_spec(x, *DEFAULT_SMTP)) +parser.add_argument("-d", "--debug", + help="enable debugging output (Python smtplib messages).", + action="store_true") + +ns = parser.parse_args(sys.argv[1:]) +options.address_family = ns.address_family or socket.AF_UNSPEC if options.address_family != socket.AF_UNSPEC: getaddrinfo = socket.getaddrinfo def getaddrinfo_replacement(host, port, family, *args, **kwargs): return getaddrinfo(host, port, options.address_family, *args, **kwargs) socket.getaddrinfo = getaddrinfo_replacement +options.safe_logging = not ns.unsafe_logging +options.use_certificate_pin = not ns.disable_pin +options.facilitator_pubkey_filename = ns.facilitator_pubkey +options.transport = ns.transport +options.remote_addr = ns.remote_addr +# specific parsing +options.email_addr = ns.email +options.smtp_addr = ns.smtp +options.debug = ns.debug + +ensure_M2Crypto()
smtp = smtplib.SMTP(options.smtp_addr[0], options.smtp_addr[1], EHLO_FQDN)
diff --git a/flashproxy-reg-http b/flashproxy-reg-http index 9f85570..2fc0f23 100755 --- a/flashproxy-reg-http +++ b/flashproxy-reg-http @@ -1,7 +1,7 @@ #!/usr/bin/env python """Register with a facilitator using the HTTP method."""
-import getopt +import argparse import socket import sys import urllib @@ -9,8 +9,7 @@ import urllib2
from flashproxy.util import parse_addr_spec, format_addr
-DEFAULT_REMOTE_ADDRESS = "" -DEFAULT_REMOTE_PORT = 9000 +DEFAULT_REMOTE = ("", 9000) DEFAULT_FACILITATOR_URL = "https://fp-facilitator.org/" DEFAULT_TRANSPORT = "websocket"
@@ -22,27 +21,6 @@ class options(object): transport = DEFAULT_TRANSPORT safe_logging = True
-def usage(f = sys.stdout): - print >> f, """\ -Usage: %(progname)s [REMOTE][:PORT] -Register with a flash proxy facilitator using an HTTP POST. By default the -remote address registered is "%(remote_addr)s". - - -4 name lookups use only IPv4. - -6 name lookups use only IPv6. - -f, --facilitator=URL register with the given facilitator - (default "%(fac_url)s"). - -h, --help show this help. - --transport=TRANSPORT register using the given transport - (default "%(transport)s"). - --unsafe-logging don't scrub IP addresses from logs.\ -""" % { - "progname": sys.argv[0], - "fac_url": DEFAULT_FACILITATOR_URL, - "remote_addr": format_addr((DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT)), - "transport": DEFAULT_TRANSPORT, -} - def safe_str(s): """Return "[scrubbed]" if options.safe_logging is true, and s otherwise.""" if options.safe_logging: @@ -59,43 +37,40 @@ def build_reg(addr, transport): ("client-transport", transport), ))
-options.facilitator_url = DEFAULT_FACILITATOR_URL -options.remote_addr = (DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT) - -opts, args = getopt.gnu_getopt(sys.argv[1:], "46f:h", [ - "facilitator=", - "help", - "transport=", - "unsafe-logging", -]) -for o, a in opts: - if o == "-4": - options.address_family = socket.AF_INET - elif o == "-6": - options.address_family = socket.AF_INET6 - elif o == "-f" or o == "--facilitator": - options.facilitator_url = a - elif o == "-h" or o == "--help": - usage() - sys.exit() - elif o == "--transport": - options.transport = a - elif o == "--unsafe-logging": - options.safe_logging = False - -if len(args) == 0: - pass -elif len(args) == 1: - options.remote_addr = parse_addr_spec(args[0], DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT) -else: - usage(sys.stderr) - sys.exit(1) - +parser = argparse.ArgumentParser( + description="Register with a flash proxy facilitator using an HTTP POST.") +# common opts +parser.add_argument("-4", help="name lookups use only IPv4.", + action="store_const", const=socket.AF_INET, dest="address_family") +parser.add_argument("-6", help="name lookups use only IPv6.", + action="store_const", const=socket.AF_INET6, dest="address_family") +parser.add_argument("--unsafe-logging", help="don't scrub IP addresses and " + "other sensitive information from logs.", action="store_true") +parser.add_argument("--transport", + help="register using the given transport, default %(default)s.", + default=DEFAULT_TRANSPORT) +# common args +parser.add_argument("remote_addr", + help="remote to register, default %s - the external IP address is guessed." + % format_addr(DEFAULT_REMOTE), + metavar="REMOTE:PORT", default="", nargs="?", + type=lambda x: parse_addr_spec(x, *DEFAULT_REMOTE)) +# specific opts +parser.add_argument("-f", "--facilitator", metavar="URL", + help="register with the given facilitator, default %(default)s.", + default=DEFAULT_FACILITATOR_URL) + +ns = parser.parse_args(sys.argv[1:]) +options.address_family = ns.address_family or socket.AF_UNSPEC if options.address_family != socket.AF_UNSPEC: getaddrinfo = socket.getaddrinfo def getaddrinfo_replacement(host, port, family, *args, **kwargs): return getaddrinfo(host, port, options.address_family, *args, **kwargs) socket.getaddrinfo = getaddrinfo_replacement +options.safe_logging = not ns.unsafe_logging +options.transport = ns.transport +options.remote_addr = ns.remote_addr +options.facilitator_url = ns.facilitator
body = build_reg(options.remote_addr, options.transport) try: diff --git a/flashproxy-reg-url b/flashproxy-reg-url index 26109fc..fce6d05 100755 --- a/flashproxy-reg-url +++ b/flashproxy-reg-url @@ -1,8 +1,8 @@ #!/usr/bin/env python """Register with a facilitator using an indirect URL."""
+import argparse import base64 -import getopt import sys import urllib import urlparse @@ -16,8 +16,7 @@ except ImportError: # Defer the error reporting so that --help works even without M2Crypto. pass
-DEFAULT_REMOTE_ADDRESS = "" -DEFAULT_REMOTE_PORT = 9000 +DEFAULT_REMOTE = ("", 9000) DEFAULT_FACILITATOR_URL = "https://fp-facilitator.org/" DEFAULT_TRANSPORT = "websocket"
@@ -26,28 +25,6 @@ class options(object): facilitator_pubkey_filename = None transport = DEFAULT_TRANSPORT
-def usage(f = sys.stdout): - print >> f, """\ -Usage: %(progname)s REMOTE[:PORT] -Print a URL, which, when retrieved, will cause the client address -REMOTE[:PORT] to be registered with the flash proxy facilitator. The -default PORT is %(port)d. - - -f, --facilitator=URL register with the given facilitator - (default "%(fac_url)s"). - --facilitator-pubkey=FILENAME - encrypt registrations to the given PEM-formatted - public key (default built-in). - -h, --help show this help. - --transport=TRANSPORT register using the given transport - (default "%(transport)s").\ -""" % { - "progname": sys.argv[0], - "fac_url": DEFAULT_FACILITATOR_URL, - "port": DEFAULT_REMOTE_PORT, - "transport": DEFAULT_TRANSPORT, -} - def build_reg(addr, transport): return urllib.urlencode(( ("client", format_addr(addr)), @@ -60,36 +37,39 @@ def get_facilitator_pubkey(): else: return RSA.load_pub_key_bio(BIO.MemoryBuffer(DEFAULT_FACILITATOR_PUBKEY_PEM))
-options.facilitator_url = DEFAULT_FACILITATOR_URL +parser = argparse.ArgumentParser( + description="Print a URL, which, when retrieved, will cause the input " + "client address to be registered with the flash proxy facilitator.") +# common opts +parser.add_argument("--facilitator-pubkey", help="encrypt registrations to " + "the given PEM-formatted public key file (default built-in).", + metavar='FILENAME') +parser.add_argument("--transport", + help="register using the given transport, default %(default)s.", + default=DEFAULT_TRANSPORT) +# common args +parser.add_argument("remote_addr", + help="remote to register, default %s - the external IP address is guessed." + % format_addr(DEFAULT_REMOTE), + metavar="REMOTE:PORT", default="", nargs="?", + type=lambda x: parse_addr_spec(x, *DEFAULT_REMOTE)) +# specific opts +parser.add_argument("-f", "--facilitator", metavar="URL", + help="register with the given facilitator, default %(default)s.", + default=DEFAULT_FACILITATOR_URL) + +ns = parser.parse_args(sys.argv[1:]) +options.facilitator_pubkey_filename = ns.facilitator_pubkey +options.transport = ns.transport +remote_addr = ns.remote_addr +options.facilitator_url = ns.facilitator
-opt, args = getopt.gnu_getopt(sys.argv[1:], "f:h", [ - "facilitator=", - "facilitator-pubkey=", - "help", - "transport=", -]) -for o, a in opt: - if o == "-f" or o == "--facilitator": - options.facilitator_url = a - elif o == "--facilitator-pubkey": - options.facilitator_pubkey_filename = a - elif o == "-h" or o == "--help": - usage() - sys.exit() - elif o == "--transport": - options.transport = a - -if len(args) != 1: - usage(sys.stderr) - sys.exit(1) +ensure_M2Crypto()
-remote_addr = parse_addr_spec(args[0], DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT) -if not remote_addr[0]: +if not ns.remote_addr[0]: print >> sys.stderr, "An IP address (not just a port) is required." sys.exit(1)
-ensure_M2Crypto() - reg_plain = build_reg(remote_addr, options.transport) rsa = get_facilitator_pubkey() reg_crypt = rsa.public_encrypt(reg_plain, RSA.pkcs1_oaep_padding)