-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlr2irproxy.py
More file actions
168 lines (130 loc) · 5.42 KB
/
lr2irproxy.py
File metadata and controls
168 lines (130 loc) · 5.42 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
#coding: utf-8
from os.path import abspath, basename, dirname
from glob import glob
from urlparse import urlparse, parse_qs
from cgi import parse_header, parse_multipart
from urllib import quote
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from StringIO import StringIO
import lxml.html
from lptools import dpi_sock
#
# import all *.py file(s) and module folder(s) under ./plugins
# as submodule(s) of plugins module
#
# overwrite_rule : names of functions about mapping rules of overwriting
# (defined at ./plugins/__init__.py)
#
overwrite_rule = ['replace_output','edit_request','edit_response']
import_files = map(lambda s: basename(s)[:-3], glob(abspath(dirname(__file__))+'/plugins/*.py'))
import_dirs = map(lambda s: basename(dirname(s)), glob(abspath(dirname(__file__)) + '/plugins/*/__init__.py') )
fromlist = overwrite_rule + import_files + import_dirs
plugins = __import__('plugins',globals(),locals(),fromlist,)
# IPv4 address of www.dream-pro.info
WWW_DREAM_PRO_INFO = '202.215.80.119'
class lr2irproxy(BaseHTTPRequestHandler):
'''
'''
def create_request(self):
'''
create an original HTTP request message with body from class variables.
'''
out = '%s %s %s\r\n' % (self.command, self.path, self.request_version)
out += ''.join(self.headers.headers)
out += '\r\n'
out += self.req_body
return out
def do_all(self):
'''
for all methods, use this handling function.
'''
# self.protocol_version = 'HTTP/1.1'
# self.req_body = self.rfile.read(int(self.headers.getheader('content-length',0)))
self.req_body = ''
parsed_path = urlparse(self.path)
prsd_path = parsed_path.path
prsd_query = parsed_path.query
query_dict = parse_qs(prsd_query)
ctype = ''
rawbody = self.rfile.read(int(self.headers.getheader('content-length',0)))
if self.command == 'POST':
try:
ctype, pdict = parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
self.req_body = parse_multipart(StringIO(rawbody),pdict)
elif ctype == 'application/x-www-form-urlencoded':
self.req_body = parse_qs(rawbody)
else:
self.req_body = rawbody
except:
self.req_body = rawbody
# create original request message
req = dpi_sock.DPIReqMsg(self.command,self.path,self.request_version)
req.importmsg(self.headers)
req.setbody(self.req_body)
args = {'req' : req, 'path' : prsd_path, 'query' : query_dict}
pname = plugins.replace_output(args)
if pname:
# replace whole output
sub_mod =getattr(plugins,pname)
args = sub_mod.func(args)
else:
# send a request to www.dream-pro.info
req.setbody(rawbody)
plist = plugins.edit_request(args)
for pname in plist:
# modify http request header or body
sub_mod = getattr(plugins,pname)
args = sub_mod.func(args)
# send/recv HTTP message
res, res_body = req.send_and_recv()
if res_body.find('"mypageform">') != -1:
res_body = res_body.replace('<table border=0>','',1)
res_body = res_body.replace(
'"mypageform">',
'"mypageform"><table border="0">'
)
args['res'] = res
args['res_body'] = res_body
plist = plugins.edit_response(args)
for pname in plist:
# modify http response from www.dream-pro.info
sub_mod = getattr(plugins,pname)
args = sub_mod.func(args)
if 'res_etree' in args:
# update args['res_body'] because if args['res_etree'] exists
# that is newer than args['res_body']
def quote_unicode_to_cp932(a):
if not a.get('href'):return a
qs = parse_qs(urlparse(a.get('href')).query)
if qs.get('keyword'):
a.set('href',a.get('href').replace(qs['keyword'][0],quote(qs['keyword'][0].encode('cp932'))))
if qs.get('tag'):
a.set('href',a.get('href').replace(qs['tag'][0],quote(qs['tag'][0].encode('cp932'))))
return a
map(quote_unicode_to_cp932, args['res_etree'].xpath('//a'))
args['res_body'] = lxml.html.tostring(args['res_etree'],encoding='cp932')
# send response to client
self.send_response(args['res'].status,args['res'].reason)
if 'connection' in args['res'].msg.keys():
args['res'].msg.__delitem__('connection')
if 'keep-alive' in args['res'].msg.keys():
args['res'].msg.__delitem__('keep-alive')
args['res'].msg['content-length'] = str(len(args['res_body']))
for hdr in args['res'].msg:
self.send_header(hdr,args['res'].msg[hdr])
self.end_headers()
self.wfile.write(args['res_body'])
return
do_GET = do_all
do_POST = do_all
do_HEAD = do_all
def main():
server = HTTPServer(('www.dream-pro.info', 80), lr2irproxy)
print 'Starting server, use <Ctrl-C> to stop'
try:
server.serve_forever()
except:
pass
if __name__ == '__main__':
main()