-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_mesh.py
216 lines (165 loc) · 6.57 KB
/
proxy_mesh.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import argparse
import json
import rfc822
import socket
import tornado.web
import tornado.ioloop
import tornado.template
import zeroconf
from proxy import ProxyHandler, init_proxy_backend, get_proxy_backend, LimitTracker
from utils import get_ip
NOTIFY_SIMULTANEOUS_DOWNLOADS = 4
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--port", "-p", type=int, help="TCP port to listen on", default=8000)
parser.add_argument("--proxy-dir", help="A place to put the proxy data (default ~/proxy_mesh or %APPDATA%/proxy_mesh)")
parser.add_argument("--rebuild-db", default=False, action="store_true", help="Rebuild the database of download metadata")
return parser.parse_args()
class MeshRequestHandler(tornado.web.RequestHandler):
PAGE_LIMIT = 26
def data_received(self, chunk):
# not a streaming mode request handler
assert False
def get(self):
next_key = self.get_argument("next_key", None)
backend = get_proxy_backend()
entries, next_key = backend.list_entries(self.PAGE_LIMIT, next_key)
self.set_header("Content-type", "application/json")
self.finish(json.dumps({"entries": entries, "next_key": next_key}))
class MeshNotifyHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
backend = get_proxy_backend()
def our_finish():
self.add_header("Content-type", "application/json")
self.write(json.dumps({"result": "ok"}))
self.finish()
if backend is not None:
data = self.request.body
obj = json.loads(data)
url = obj["url"]
metadata = obj["metadata"]
proxy_ip = obj["proxy_ip"]
proxy_port = obj["proxy_port"]
print "GOT MESH-NOTIFY from %s:%d %s" % (proxy_ip, proxy_port, url)
last_modified = None
for key, val in metadata["headers"]:
if key.lower() == "last-modified":
last_modified = rfc822.mktime_tz(rfc822.parsedate_tz(val))
break
entry = {"url": url, "last_modified": last_modified}
tracker = LimitTracker(NOTIFY_SIMULTANEOUS_DOWNLOADS)
backend.download_entries(proxy_ip, proxy_port, [entry], our_finish, tracker)
else:
our_finish()
def data_received(self, chunk):
# not a streaming mode request handler
assert False
def main():
options = parse_args()
port = options.port
print "Getting IP"
ip = get_ip()
print "Setting up IP on zeroconf (for avahi access)"
ad = Advertisement()
ad.advertise_proxy("_apt_proxy._tcp.local.", ip, port)
try:
print "Starting HTTP proxy on port %d" % port
rebuild_db = options.rebuild_db
run_proxy(options.proxy_dir, ip, port, rebuild_db=rebuild_db)
finally:
ad.close()
class Advertisement(object):
def __init__(self):
self.zc = zeroconf.Zeroconf()
self.info_entries = []
self.our_ip = None
self.browser = zeroconf.ServiceBrowser(self.zc, "_apt_proxy._tcp.local.", self)
self.service_ip_ports_by_name = {}
def close(self):
self.cancel_our_ads()
self.browser.cancel()
if self.zc is not None:
self.zc.close()
self.zc = None
def advertise_proxy(self, service_type, ip, port):
friendly_name = "proxy-mesh on " + socket.gethostname()
name = friendly_name + "._apt_proxy._tcp.local."
print "zeroconf service type name " + zeroconf.service_type_name(name)
desc = {}
self.our_ip = socket.inet_aton(ip)
info = zeroconf.ServiceInfo(service_type, name, self.our_ip, port, 0, 0, desc, friendly_name + ".local.")
self.zc.register_service(info)
self.info_entries.append(info)
# noinspection PyUnusedLocal
def remove_service(self, zeroconf_obj, cur_type, name):
"""
:type zeroconf_obj: zeroconf.Zeroconf
:type cur_type: str
:type name: str
"""
print("Service %s removed" % (name,))
address, port = self.service_ip_ports_by_name.pop(name)
if address != self.our_ip:
self.on_remote_service_removed(socket.inet_ntoa(address), port)
def add_service(self, zeroconf_obj, cur_type, name):
"""
:type zeroconf_obj: zeroconf.Zeroconf
:type cur_type: str
:type name: str
"""
info = zeroconf_obj.get_service_info(cur_type, name)
print("Service %s added, service info: %s" % (name, info))
assert name not in self.service_ip_ports_by_name
self.service_ip_ports_by_name[name] = (info.address, info.port)
if info.address != self.our_ip:
self.on_remote_service_added(socket.inet_ntoa(info.address), info.port)
@staticmethod
def on_remote_service_added(ip, port):
backend = get_proxy_backend()
if backend is None:
print "deferring sync with proxy %s:%d until we are started" % (ip, port)
deferred_remote_service_inits.append((ip, port))
else:
backend.on_peer_added(ip, port)
@staticmethod
def on_remote_service_removed(ip, port):
backend = get_proxy_backend()
if backend is None:
try:
deferred_remote_service_inits.remove((ip, port))
except ValueError:
pass
else:
backend.on_peer_removed(ip, port)
def cancel_our_ads(self):
cur_info_entries = self.info_entries
self.info_entries = []
for info in cur_info_entries:
self.zc.unregister_service(info)
class MeshProxyHandler(ProxyHandler):
def __init__(self, *args, **kwargs):
super(MeshProxyHandler, self).__init__(*args, **kwargs)
deferred_remote_service_inits = []
def run_proxy(proxy_dir, our_ip, port_val, start_ioloop=True, rebuild_db=False):
"""
Run proxy on the specified port. If start_ioloop is True (default),
the tornado IOLoop will be started immediately.
"""
backend = init_proxy_backend(our_ip, port_val, proxy_dir, rebuild_db)
remotes = list(deferred_remote_service_inits)
deferred_remote_service_inits[:] = []
for ip, port in remotes:
backend.on_peer_added(ip, port)
app = tornado.web.Application([
# routes
(r"/mesh-request", MeshRequestHandler),
(r"/mesh-notify", MeshNotifyHandler),
(r'.*', MeshProxyHandler),
])
app.listen(port_val)
ioloop = tornado.ioloop.IOLoop.instance()
if start_ioloop:
ioloop.start()
if __name__ == "__main__":
main()