This repository was archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwisted.py
More file actions
54 lines (39 loc) · 1.38 KB
/
twisted.py
File metadata and controls
54 lines (39 loc) · 1.38 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
import sys
from twisted.web import http, proxy
from twisted.internet import reactor, ssl
from twisted.python import log
class ProxyClient(proxy.ProxyRequest):
def handleHeader(self, key, value):
# change response header here
log.msg("Header: %s: %s" % (key, value))
proxy.ProxyClient.handleHeader(self, key, value)
def handleResponsePart(self, buffer):
# change response part here
log.msg("Content: %s" % (buffer[:50],))
proxy.ProxyClient.handleResponsePart(self, buffer)
class ProxyClientFactory(proxy.ProxyClientFactory):
protocol = ProxyClient
class ProxyRequest(proxy.ProxyRequest):
protocols = dict(http=ProxyClientFactory)
def connect(self):
log.msg(self.uri)
host, port = self.uri.decode("utf-8").split(":")
proxy.ProxyRequest.process(self)
# Request part
def process(self):
if self.method == b"CONNECT":
self.connect()
else:
log.msg(self.method)
for k,v in self.requestHeaders.getAllRawHeaders():
log.msg("%s : %s" % (k,v))
log.msg("\n \n")
proxy.ProxyRequest.process(self)
class LoggingProxy(proxy.Proxy):
requestFactory = ProxyRequest
class LoggingProxyFactory(http.HTTPFactory):
protocol = LoggingProxy
log.startLogging(sys.stdout)
#reactor.listenSSL(8000, LoggingProxyFactory(), ssl.DefaultOpenSSLContextFactory('/etc/pki/consumer/key.pem', '/etc/pki/consumer/cert.pem'))
reactor.listenTCP(8000, LoggingProxyFactory())
reactor.run()