-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflashproxy-reg-http
executable file
·130 lines (117 loc) · 3.71 KB
/
flashproxy-reg-http
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
import getopt
import httplib
import re
import socket
import sys
import urllib
import urllib2
DEFAULT_REMOTE_ADDRESS = ""
DEFAULT_REMOTE_PORT = 9000
DEFAULT_FACILITATOR_URL = "https://tor-facilitator.bamsoftware.com/"
class options(object):
remote_addr = None
facilitator_url = None
address_family = socket.AF_UNSPEC
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
(by default "%(fac_url)s").
-h, --help show this help. \
""" % {
"progname": sys.argv[0],
"fac_url": DEFAULT_FACILITATOR_URL,
"remote_addr": format_addr((DEFAULT_REMOTE_ADDRESS, DEFAULT_REMOTE_PORT)),
}
def parse_addr_spec(spec, defhost = None, defport = None):
host = None
port = None
af = 0
m = None
# IPv6 syntax.
if not m:
m = re.match(ur'^\[(.+)\]:(\d*)$', spec)
if m:
host, port = m.groups()
af = socket.AF_INET6
if not m:
m = re.match(ur'^\[(.+)\]$', spec)
if m:
host, = m.groups()
af = socket.AF_INET6
# IPv4/hostname/port-only syntax.
if not m:
try:
host, port = spec.split(":", 1)
except ValueError:
host = spec
if re.match(ur'^[\d.]+$', host):
af = socket.AF_INET
else:
af = 0
host = host or defhost
port = port or defport
if port is not None:
port = int(port)
return host, port
def format_addr(addr):
host, port = addr
if not host:
return u":%d" % port
# Numeric IPv6 address?
try:
addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST)
af = addrs[0][0]
except socket.gaierror, e:
af = 0
if af == socket.AF_INET6:
result = u"[%s]" % host
else:
result = "%s" % host
if port is not None:
result += u":%d" % port
return result
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"])
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()
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)
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
spec = format_addr(options.remote_addr)
try:
http = urllib2.urlopen(options.facilitator_url, urllib.urlencode({"client": spec}), 10)
except urllib2.HTTPError, e:
print >> sys.stderr, "Status code was %d, not 200" % e.code
sys.exit(1)
except urllib2.URLError, e:
print >> sys.stderr, "Failed to register: %s" % str(e.reason)
sys.exit(1)
except Exception, e:
print >> sys.stderr, "Failed to register: %s" % str(e)
sys.exit(1)
http.close()
print "Registered \"%s\" with %s." % (spec, options.facilitator_url)