-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.py
66 lines (55 loc) · 2.12 KB
/
server.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
from twisted.python import log
from twisted.web import http, proxy
from keyczar.keys import RsaPrivateKey,AesKey
__author__ = "Yashin Mehaboobe aka sp3ctr3"
class ProxyClient(proxy.ProxyClient):
"""Modify response as well as header here.
"""
def handleHeader(self, key, value):
"""
Modify header here
"""
#log.msg("Header: %s: %s" % (key, value))
proxy.ProxyClient.handleHeader(self, key, value)
def handleResponsePart(self, buffer):
"""
Modify buffer to modify response. For example replacing buffer with buffer[::-1] will lead to a reversed output.
This might cause content encoding errors. Currently test only on text only websites
"""
#log.msg("Content: %s" % (buffer,))
proxy.ProxyClient.handleResponsePart(self, buffer)
class ProxyClientFactory(proxy.ProxyClientFactory):
protocol = ProxyClient
class ProxyRequest(proxy.ProxyRequest):
protocols = dict(http=ProxyClientFactory)
class Proxy(proxy.Proxy):
requestFactory = ProxyRequest
def connectionMade(self):
self.assym_key=RsaPrivateKey.Generate()
pub_key=self.assym_key.public_key
self.transport.write(str(pub_key))
return proxy.ProxyClient.connectionMade(self)
def dataReceived(self,data):
straes=self.assym_key.Decrypt(data[:261])
self.AES=AesKey.Read(straes)
data=self.AES.Decrypt(data[261:])
print data,len(data)
return proxy.Proxy.dataReceived(self, data)
class ProxyFactory(http.HTTPFactory):
protocol = Proxy
portstr = "tcp:8080:interface=localhost" # serve on localhost:8080
if __name__ == '__main__':
import sys
from twisted.internet import endpoints, reactor
def shutdown(reason, reactor, stopping=[]):
"""Stop the reactor."""
if stopping: return
stopping.append(True)
if reason:
log.msg(reason.value)
reactor.callWhenRunning(reactor.stop)
log.startLogging(sys.stdout)
endpoint = endpoints.serverFromString(reactor, portstr)
d = endpoint.listen(ProxyFactory())
d.addErrback(shutdown, reactor)
reactor.run()