-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathproxy.py
112 lines (92 loc) · 3.62 KB
/
proxy.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
from libmproxy import flow, controller
from libmproxy.flow import FlowMaster
import os
from flowcollection import FlowCollection
from dirdumper import DirDumper
class ProxyError(Exception): pass
class HoneyProxyMaster(FlowMaster):
"""
The HoneyProxy proxy core, in some parts pretty similar to mitmproxys DumpMaster.
"""
def __init__(self, server, options, sessionFactory):
FlowMaster.__init__(self, server, flow.State())
self.sessionFactory = sessionFactory
self.o = options
self.flows = FlowCollection()
self.anticache = options.anticache
self.anticomp = options.anticomp
if options.stickycookie:
self.set_stickycookie(options.stickycookie)
if options.stickyauth:
self.set_stickyauth(options.stickyauth)
if options.wfile:
path = os.path.abspath(os.path.expanduser(options.wfile))
directory = os.path.split(path)[0]
if not os.path.exists(directory):
os.makedirs(directory)
try:
f = file(path, "wb")
self.fwriter = flow.FlowWriter(f)
except IOError, v:
raise Exception(v.strerror)
if options.dumpdir:
path = os.path.expanduser(options.dumpdir)
if not os.path.exists(path):
os.makedirs(path)
if os.listdir(path):
print "Notice: Your dump directory (%s) is not empty." % path
print "HoneyProxy won't overwrite your files."
self.dirdumper = DirDumper(path)
if options.replacements:
for i in options.replacements:
self.replacehooks.add(*i)
if options.script:
err = self.load_script(options.script)
if err:
raise Exception(err)
if options.rfile:
path = os.path.expanduser(options.rfile)
try:
f = file(path, "rb")
freader = flow.FlowReader(f)
except IOError, v:
raise ProxyError(v.strerror)
try:
self.load_flows(freader)
except flow.FlowReadError, v:
print "Flow file corrupted. Stopped loading."
def getFlowCollection(self):
return self.flows
def start(self):
#see controller.Master.run()
global should_exit
should_exit = False
self.server.start_slave(controller.Slave, controller.Channel(self.masterq))
def tick(self):
if not should_exit:
controller.Master.tick(self, self.masterq)
else:
self.shutdown()
def shutdown(self):
if(self.o.wfile):
self.fwriter.fo.close()
return FlowMaster.shutdown(self)
def handle_request(self, request):
flow = FlowMaster.handle_request(self, request)
if flow:
request.reply()
#print "request to "+request.host
#self.sessionFactory.write(request.host)
return flow
def handle_response(self, response):
flow = FlowMaster.handle_response(self, response)
if flow:
response.reply()
flowId = self.flows.addFlow(flow)
if self.o.wfile:
self.fwriter.add(flow)
if self.o.dumpdir:
self.dirdumper.add(flow)
#print "response from "+flow.request.host
self.sessionFactory.onNewFlow(self.flows.getFlowsSerialized()[flowId])
return flow