Repository navigation
Expand file tree
/
Copy pathroxy.py
More file actions
executable file
·353 lines (263 loc) · 10.5 KB
/
Copy pathroxy.py
File metadata and controls
executable file
·353 lines (263 loc) · 10.5 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2019 Tobi Schäfer.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import re
import traceback
import zlib
from datetime import datetime, timedelta, timezone
from gzip import compress
from http.client import HTTPConnection, HTTPSConnection
from io import StringIO
from ipaddress import ip_address
from socket import SOCK_STREAM, create_connection, getaddrinfo
from sys import exc_info
from wsgiref.handlers import format_date_time
from urllib.error import HTTPError
from urllib.request import (
HTTPDefaultErrorHandler,
HTTPErrorProcessor,
HTTPHandler,
HTTPRedirectHandler,
HTTPSHandler,
OpenerDirector,
ProxyHandler,
Request,
UnknownHandler
)
# Local settings are optional, see `local.py` in the README
try:
from local import allow_private_hosts
except ImportError:
allow_private_hosts = False
# The headers of the proxied response that are passed on with the response of
# Roxy. Any other would take effect on the origin of the proxy: `Set-Cookie`
# could set its cookies, `Strict-Transport-Security` or `Clear-Site-Data`
# would apply to it, and `Cache-Control` would undo the caching set by Roxy
PASSED_HEADERS = ('etag', 'last-modified')
def is_passed_header(name):
name = name.lower()
return name in PASSED_HEADERS or name.startswith('x-roxy-')
class ForbiddenUrl(Exception):
def __init__(self, status, message):
super().__init__(message)
self.status = status
# Bounds how much of another server’s bandwidth (and this one’s) a single
# request can spend – this is a public, unauthenticated proxy, so nothing
# else stops a request for an arbitrarily large file. Applied to both the
# raw bytes read off the wire and, separately, the decompressed size (a
# small gzip payload can still expand into a much larger one)
MAX_CONTENT_LENGTH = 10 * 1024 * 1024
# An XML document’s declared encoding, which must be within its very first
# bytes if present at all
XML_ENCODING_PATTERN = re.compile(rb'^\s*<\?xml[^>]*\bencoding=["\']([^"\']+)["\']')
def get_declared_charset(content_type, content):
# The transport-level charset, when given, takes priority over the
# document’s own encoding declaration
if content_type:
match = re.search(r';\s*charset=["\']?([^;"\'\s]+)', content_type, re.IGNORECASE)
if match:
return match.group(1)
match = XML_ENCODING_PATTERN.match(content[:200])
if match:
return match.group(1).decode('ascii', 'ignore')
return None
def decode_content(content, content_type):
# Neither a bogus declared charset nor a feed that isn’t actually UTF-8
# should make this raise; Latin-1 never fails to decode, so it is always
# the last resort, not the first guess
for charset in (get_declared_charset(content_type, content), 'utf-8-sig'):
if not charset:
continue
try:
return content.decode(charset)
except (LookupError, UnicodeDecodeError):
pass
return content.decode('iso-8859-1')
def check_scheme(request):
# Rely on the request’s own URL parsing to avoid any mismatch with what is opened eventually
if request.type not in ('http', 'https'):
raise ForbiddenUrl(400, 'Only http and https URLs are supported')
def is_public(address):
ip = ip_address(address.partition('%')[0])
# Look at the embedded IPv4 address of IPv4-mapped addresses like `::ffff:127.0.0.1`
if ip.version == 6 and ip.ipv4_mapped:
ip = ip.ipv4_mapped
return ip.is_global
def create_public_connection(address, *args, **kwargs):
host, port = address
addresses = [info[4][0] for info in getaddrinfo(host, port, type=SOCK_STREAM)]
if not allow_private_hosts and not all(map(is_public, addresses)):
raise ForbiddenUrl(403, 'Requests to non-public addresses are not allowed')
error = None
# Connect to the addresses just checked instead of resolving the host name a
# second time, so DNS rebinding cannot sneak in a different address
for ip in addresses:
try:
return create_connection((ip, port), *args, **kwargs)
except OSError as exc:
error = exc
raise error
class PublicHTTPConnection(HTTPConnection):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._create_connection = create_public_connection
class PublicHTTPSConnection(HTTPSConnection):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._create_connection = create_public_connection
class PublicHTTPHandler(HTTPHandler):
def do_open(self, http_class, req, **http_conn_args):
return super().do_open(PublicHTTPConnection, req, **http_conn_args)
class PublicHTTPSHandler(HTTPSHandler):
def do_open(self, http_class, req, **http_conn_args):
return super().do_open(PublicHTTPSConnection, req, **http_conn_args)
class PublicRedirectHandler(HTTPRedirectHandler):
def redirect_request(self, *args, **kwargs):
request = super().redirect_request(*args, **kwargs)
if request:
check_scheme(request)
return request
# Unlike `urllib.request.urlopen` this opener has neither a file, FTP nor data
# handler, and every connection – including those following a redirect – goes
# through `create_public_connection`
opener = OpenerDirector()
for handler in (
ProxyHandler(),
UnknownHandler(),
PublicHTTPHandler(),
PublicHTTPSHandler(),
PublicRedirectHandler(),
HTTPDefaultErrorHandler(),
HTTPErrorProcessor()
):
opener.add_handler(handler)
def get_url(url, request_headers):
content = ''
message = ''
headers = {}
status = 200
try:
request = Request(url, None, request_headers)
check_scheme(request)
response = opener.open(request, None, 3)
headers.update(response.headers)
url = headers['X-Roxy-Url'] = response.geturl()
status = headers['X-Roxy-Status'] = response.status
etag = headers.get('ETag')
if etag:
# Remove suffix like `-gzip` from etag to make it work
headers['ETag'] = re.sub('-[^"]+("?)$', '\\1', etag)
raw_content = response.read(MAX_CONTENT_LENGTH + 1)
if len(raw_content) > MAX_CONTENT_LENGTH:
raise ForbiddenUrl(502, 'Response too large to proxy')
if response.headers.get('Content-Encoding') == 'gzip':
decompressor = zlib.decompressobj(16 + zlib.MAX_WBITS)
content = decompressor.decompress(raw_content, MAX_CONTENT_LENGTH)
if decompressor.unconsumed_tail:
raise ForbiddenUrl(502, 'Response too large to proxy')
else:
content = raw_content
except HTTPError as error:
status = headers['X-Roxy-Status'] = error.getcode()
message = headers['X-Roxy-Error'] = error.msg
except ForbiddenUrl as error:
status = headers['X-Roxy-Status'] = error.status
message = headers['X-Roxy-Error'] = str(error)
except:
#traceback.print_exc()
status = headers['X-Roxy-Status'] = 500
message = headers['X-Roxy-Error'] = str(exc_info()[1])
else:
content_type = headers.get('Content-Type')
if content_type and (content_type.startswith('text/') or
content_type.startswith('application/') or
content_type.endswith('xml')):
content = decode_content(content, content_type)
return {
'status': status,
'content': content,
'headers': headers,
'message': message,
'url': url
}
def roxy(request, make_response):
response_headers = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}
def send_response(status = 200, headers = {}, content = ''):
response = make_response(content, status)
response.headers = headers
return response
url = request.args.get('url')
callback = request.args.get('callback')
if not url:
return send_response(400, response_headers)
content = None
now = datetime.now(timezone.utc)
resource = {
'content': '',
'date': now,
'headers': {},
'status': 200
}
headers = { 'Accept-Encoding': 'gzip, deflate' }
def set_header(header_name, value):
if value:
headers[header_name] = value
# The cookies of the client belong to the origin of the proxy, so they must
# not be sent to any other server
set_header('Accept', request.headers.get('Accept'))
set_header('Referer', request.referrer)
set_header('User-Agent', str(request.user_agent))
data = get_url(url, headers)
content = data.get('content')
if content: resource['content'] = content
resource.update({
'headers': data.get('headers'),
'status': data.get('status')
})
for key in resource['headers']:
if is_passed_header(key):
response_headers.setdefault(key, resource['headers'].get(key))
response_headers['Expires'] = format_date_time(now.timestamp() + 60)
data = ''
content = None
status = resource.get('status') or 200
if request.method == 'GET':
content = resource.get('content')
try:
content = content.decode('utf-8')
except:
pass
data = json.dumps({
'content': content,
'headers': resource.get('headers')
})
if status == 304:
# If request was unconditional we need to return 200 with content no matter what
for key in request.headers.keys():
if key.startswith('If-'): continue
status = 200
break
if callback:
response_headers['Content-Type'] = 'application/javascript'
data = '%s(%s)' % (callback, data)
if request.headers.get('Accept-Encoding', '').find('gzip') > -1:
response_headers['Content-Encoding'] = 'gzip'
data = compress(data.encode('utf-8'))
return send_response(status, response_headers, data)