-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterceptProxy.py
86 lines (72 loc) · 2.92 KB
/
interceptProxy.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
#interceptProxy.py
#Ben Jones
#ECE 649 Fall 2012
#interceptProxy.py: this file contains code that will insert and retreive
# tracking cookies sent to the proxy.
from twisted.internet import protocol, reactor
from random import randint
class TrackInfo():
def __init__(self):
self.trackers = {}
self.genTrackID()
self.trackers[self.trackID] = []
def genTrackID(self):
self.trackID = randint(1, 400000000)
def addEqual(self, newTracker):
"""Check if the new tracker is already in the equivalency list. If it is,
do nothing, otherwise add the tracker to the equivalency list"""
if(newTracker in self.trackers[self.trackID]):
return
else:
self.trackers[self.trackID].append(newTracker)
class ConsoleWriter():
def write(self, data, type):
if(data):
lines = data.split("\n")
prefix = "<" if type == "request" else ">"
for line in lines:
sys.stdout.write("%s %s\n" % (prefix, line))
else:
sys.stdout.write("No response from server\n")
#protocol to insert and retrieve cookies from the requests and responses
class ClientProtocol(protocol.Protocol):
"""Client protocol which will insert and retreive tracking cookies from the
requests and responses"""
def __init__(self, serverTransport, proxyTransport):
self.serverTransport = serverTransport
self.proxyTransport = proxyTransport
def sendMessage(self, data):
self.getCookie(data)
self.transport.write(data)
def dataReceived(self, data):
self.data = data
ConsoleWriter().write(self.data)
self.insertCookie(data)
self.
self.transport.loseConnection()
def connectionLost(self, reason):
self.serverTransport.write(self.data)
self.serverTransport.lostConnection()
class ServerProtocol(protocol.Protocol):
def dataReceived(self, data):
self.data = data
ConsoleWriter().write(self.data, "request")
client = protocol.ClientCreator(reactor, ClientProtocol, self.transport)
client.trackerInfo = self.trackerInfo
d = client.connectTCP(self.factory.targetHost, self.factory.targetPort)
d.addCallback(self.forwardToClient, client)
def forwardToClient(self, client, data):
client.sendMessage(self.data):
class ServerFactory(protocol.ServerFactory):
protocol = ServerProtocol
def __init__(self, targetHost, targetPort, tracker):
self.targetHost = targetHost
self.targetPort = targetPort
self.trackerInfo = tracker
sourcePort=8000 #the port which Tor will forward its traffic to
targetHost="localhost" #the address the other proxy operates on
targetPort=8080 #the port the other proxy operates on
if __name__ == "__main__":
tracker = TrackerInfo()
reactor.listenTCP(sourcePort, ServerFactory(targetHost, targetPort, tracker))
reactor.run()