-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.py
55 lines (40 loc) · 1.4 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
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
An example of a proxy which logs all requests processed through it.
Usage:
$ python logging-proxy.py
Then configure your web browser to use localhost:8080 as a proxy, and visit a
URL (This is not a SOCKS proxy). When browsing in this configuration, this
example will proxy connections from the browser to the server indicated by URLs
which are visited. The client IP and the request hostname will be logged for
each request.
HTTP is supported. HTTPS is not supported.
See also proxy.py for a simpler proxy example.
"""
from twisted.internet import reactor
from twisted.web import proxy, http
counter = 1
class LoggingProxyRequest(proxy.ProxyRequest):
def process(self):
global counter
temp_req = self.getAllHeaders()['host']
f = open('tmp/requests.log','a')
if not "mavroudisv" in temp_req:
f.write(temp_req + '\n') # python will convert \n to os.linesep
print(self.getAllHeaders()['host'])
else:
f.close()
counter += 1
print('Log was created.')
try:
proxy.ProxyRequest.process(self)
except KeyError:
print("HTTPS is not supported at the moment!")
class LoggingProxy(proxy.Proxy):
requestFactory = LoggingProxyRequest
class LoggingProxyFactory(http.HTTPFactory):
def buildProtocol(self, addr):
return LoggingProxy()
reactor.listenTCP(8081, LoggingProxyFactory())
reactor.run()