-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquid-redirect.py
executable file
·163 lines (133 loc) · 5.28 KB
/
squid-redirect.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
#!/usr/bin/env python3
## -*- coding: utf-8 -*-
import os
import logging
import sys
import re
import json
from collections import namedtuple, ChainMap
SquidRequest = namedtuple('SquidRequest', ('channel_id', 'url', 'ident'))
# If `url_rewrite_extras` are used, SquidRequest needs modifying
# Default extras: "%>a/%>A %un %>rm myip=%la myport=%lp" -> ip/fqdn ident method [urlgroup] kv-pair
log = logging.getLogger(__name__)
VERSION = '0.0'
# Utils ------------------------------------------------------------------------
def _postmortem(func, *args, **kwargs):
import traceback
import pdb
import sys
try:
return func(*args, **kwargs)
except Exception:
type, value, tb = sys.exc_info()
traceback.print_exc()
pdb.post_mortem(tb)
def load_data(*sources):
"""
Normalize sources from `python_dict`, `json_string` or `json_filename` -> `python_dict`
>>> load_data({'a': 1}, '{"b": 2}') == {'a': 1, 'b': 2}
True
"""
def _open_source(source):
assert source
if isinstance(source, dict):
return source
if isinstance(source, str) and source.startswith('{') and source.endswith('}'):
return json.loads(source)
if os.path.exists(source):
with open(source) as filehandle:
return json.load(filehandle)
return dict(ChainMap(*map(_open_source, sources)))
# Squid Request Processing ----------------------------------------------------
def process_squid_request(request_line, rewrite_rules={}, redirect_rules={}, **args):
"""
Reference:
http://www.squid-cache.org/Doc/config/url_rewrite_program/
https://wiki.squid-cache.org/Features/Redirectors
>>> rules = {
... 'rewrite_rules': {
... 'www\.site\.com': 'www.test.com',
... '/my/path': '/my/test_path',
... },
... 'redirect_rules': {
... 'www\.notreal\.com': 'www.real.com',
... }
... }
>>> process_squid_request('0 http://www.google.com/ -', **rules)
'0 ERR'
>>> process_squid_request('1 http://www.site.com/my/path.json?test=yes -', **rules)
'1 OK rewrite-url="http://www.test.com/my/path.json?test=yes"'
>>> process_squid_request('2 http://www.notreal.com/data?test=yes -', **rules)
'2 OK status=301 url="http://www.real.com/data?test=yes"'
"""
def _replace_url(url, rules, template):
for regex, replacement in rules.items():
if re.search(regex, url):
return template.format(url=re.sub(regex, replacement, url))
def _lookup_response(url):
try:
return next(filter(None, (
_replace_url(url, rewrite_rules, 'rewrite-url="{url}"'),
_replace_url(url, redirect_rules, 'status=301 url="{url}"'),
)))
except StopIteration:
return None
try:
request = SquidRequest(*map(lambda text: text.strip(), request_line.split(' ')))
except TypeError as ex:
log.debug(f'unable to process: {request_line}')
#return f'{request.channel_id} BH message="{ex}"'
# TODO: channel_id required in response
return 'BH'
response = _lookup_response(request.url)
if response:
return f'{request.channel_id} OK {response}'
return f'{request.channel_id} ERR'
def process_input_output_handlers(input_handle=sys.stdin, output_handle=sys.stdout, **args):
log.debug(f'start: {__file__} {args}')
while True:
request_line = input_handle.readline()
if not request_line or not request_line.strip():
break
log.debug(f'request: {request_line}')
response = process_squid_request(request_line, **args)
log.debug(f'response: {response}')
output_handle.write(f'{response}\n')
output_handle.flush()
log.debug(f'end: {__file__}')
# Commandline -----------------------------------------------------------------
def get_args():
import argparse
parser = argparse.ArgumentParser(
description=f'''
Example Useage:
python3 {__file__}
Tests:
pytest --doctest-modules {__file__}
''',
epilog=''' ''',
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument('--rewrite', nargs='+', help='json filename or string with key:value to rewrite', default=())
parser.add_argument('--redirect', nargs='+', help='json filename or string with key:value to redirect', default=())
parser.add_argument('--logfile', action='store', help='debug data logfile to write. Details start,stop,request,response')
parser.add_argument('--postmortem', action='store_true', help='Automatically drop into pdb shell on exception. Used for debuging')
parser.add_argument('--version', action='version', version=VERSION)
args = vars(parser.parse_args())
return args
if __name__ == "__main__":
args = get_args()
if args['logfile']:
logging.basicConfig(filename=args['logfile'], level=logging.DEBUG)
def main(**args):
process_input_output_handlers(
**{
f'{arg}_rules': load_data(*args.get(arg))
for arg in ('rewrite', 'redirect')
},
**args,
)
if args.get('postmortem'):
_postmortem(main, **args)
else:
main(**args)