/
visulate
executable file
·235 lines (165 loc) · 6.86 KB
/
visulate
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python
import json, pickle, re, socket, websockets
from functools import partial
from twisted.internet import protocol, reactor, tcp
from twisted.web import resource, server, static
# IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
#
# dec-octet = DIGIT ; 0-9
# / %x31-39 DIGIT ; 10-99
# / "1" 2DIGIT ; 100-199
# / "2" %x30-34 DIGIT ; 200-249
# / "25" %x30-35 ; 250-255
decOctet = '(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])'
ipv4Address = '{decOctet}\.{decOctet}\.{decOctet}\.{decOctet}'.format(decOctet=decOctet)
# http://www.iana.org/domains/root/db
generic = set('com,info,net,org'.split(','))
genericRestricted = set('biz,name,pro'.split(','))
sponsored = set('aero,asia,cat,coop,edu,gov,int,jobs,mil,mobi,museum,post,tel,travel,xxx'.split(','))
# SquidLogCode
SQUID_LOG_TCP_HIT = '1'
SQUID_LOG_TCP_REFRESH_HIT = '5'
SQUID_LOG_TCP_IMS_HIT = '9'
chiData = {}
pqsnData = {}
chiPqsnData = {}
# Connected WebSocket clients
transport = set()
# Connected log collation clients
clientCount = 0
# Log collation server
class factory(protocol.Factory):
class protocol:
entryCount = False
def connectionLost(ctx, reason):
global clientCount
clientCount -= 1
for itm in transport:
itm.write(json.dumps(clientCount))
def dataReceived(ctx, data):
# Ignore collation_secret
@partial(setattr, ctx, 'dataReceived')
def dataReceived(data):
# Read IP address of the client's host machine and proxy response
# transfer length for each entry in segment, update table and build
# delta of changed rows
chiDelta = {}
pqsnDelta = {}
if ctx.entryCount:
ctx.data += data
else:
ctx.data = data
ctx.entryCount = pickle.decode_long(ctx.data[20:24])
ctx.offset = 188
while ctx.entryCount:
if len(ctx.data) < ctx.offset + 16:
break
entryLen = pickle.decode_long(ctx.data[ctx.offset + 12:ctx.offset + 16])
if len(ctx.data) < ctx.offset + entryLen:
break
chi = socket.inet_ntop(socket.AF_INET, ctx.data[ctx.offset + 36:ctx.offset + 40])
crc = chr(pickle.decode_long(ctx.data[ctx.offset + 40:ctx.offset + 48]))
# LogAccessHttp::marshal_proxy_resp_squid_len()
psql = pickle.decode_long(ctx.data[ctx.offset + 56:ctx.offset + 64])
# LogAccessHttp::marshal_client_req_http_method()
start = ctx.offset + 64
end = ctx.data.index('\0', start)
cqhm = ctx.data[start:end]
# LogAccessHttp::marshal_client_req_url_canon()
start = (end + 1 - ctx.offset + 0x7 & ~0x7) + ctx.offset
end = ctx.data.index('\0', start)
cquc = ctx.data[start:end]
# LogAccessHttp::marshal_client_auth_user_name()
start = (end + 1 - ctx.offset + 0x7 & ~0x7) + ctx.offset
end = ctx.data.index('\0', start)
caun = ctx.data[start:end]
# LogAccessHttp::marshal_proxy_hierarchy_route()
start = (end + 1 - ctx.offset + 0x7 & ~0x7) + ctx.offset
end = start + 8
phr = ctx.data[start:end]
# LogAccessHttp::marshal_server_host_name()
start = end
end = ctx.data.index('\0', start)
pqsn = ctx.data[start:end]
# The syntax rule for host is ambiguous because it does not
# completely distinguish between an IPv4address and a reg-name. In
# order to disambiguate the syntax, we apply the "first-match-wins"
# algorithm: If host matches the rule for IPv4address, then it should
# be considered an IPv4 address literal and not a reg-name
if not re.match(ipv4Address, pqsn):
pqsn = pqsn.rsplit('.', 3)
try:
pqsn = '.'.join(pqsn[-2 if pqsn[-1] in generic | genericRestricted | sponsored or pqsn[-3] == 'www' else -3:])
except IndexError:
pqsn = '.'.join(pqsn[-2:])
# chi
try:
itm = chiData[chi]
except KeyError:
# TypeError: 'tuple' object does not support item assignment
itm = chiData[chi] = [[1, psql], [1, psql] if crc in (SQUID_LOG_TCP_HIT, SQUID_LOG_TCP_REFRESH_HIT, SQUID_LOG_TCP_IMS_HIT) else [0, 0]]
else:
itm[0][0] += 1
itm[0][1] += psql
if crc in (SQUID_LOG_TCP_HIT, SQUID_LOG_TCP_REFRESH_HIT, SQUID_LOG_TCP_IMS_HIT):
itm[1][0] += 1
itm[1][1] += psql
chiDelta[chi] = itm
# pqsn
try:
itm = pqsnData[pqsn]
except KeyError:
# TypeError: 'tuple' object does not support item assignment
itm = pqsnData[pqsn] = [[1, psql], [1, psql] if crc in (SQUID_LOG_TCP_HIT, SQUID_LOG_TCP_REFRESH_HIT, SQUID_LOG_TCP_IMS_HIT) else [0, 0]]
else:
itm[0][0] += 1
itm[0][1] += psql
if crc in (SQUID_LOG_TCP_HIT, SQUID_LOG_TCP_REFRESH_HIT, SQUID_LOG_TCP_IMS_HIT):
itm[1][0] += 1
itm[1][1] += psql
pqsnDelta[pqsn] = itm
# chiPqsn
try:
itm = chiPqsnData[chi]
except KeyError:
itm = chiPqsnData[chi] = {}
try:
itm = itm[pqsn]
except KeyError:
# TypeError: 'tuple' object does not support item assignment
itm[pqsn] = [[1, psql], [1, psql] if crc in (SQUID_LOG_TCP_HIT, SQUID_LOG_TCP_REFRESH_HIT, SQUID_LOG_TCP_IMS_HIT) else [0, 0]]
else:
itm[0][0] += 1
itm[0][1] += psql
if crc in (SQUID_LOG_TCP_HIT, SQUID_LOG_TCP_REFRESH_HIT, SQUID_LOG_TCP_IMS_HIT):
itm[1][0] += 1
itm[1][1] += psql
try:
crcData[crc] += 1
except KeyError:
crcData[crc] = 1
ctx.entryCount -= 1
ctx.offset += entryLen
for itm in transport:
itm.write(json.dumps((chiDelta, pqsnDelta)))
def makeConnection(ctx, nstTransport):
global clientCount
clientCount += 1
for itm in transport:
itm.write(json.dumps(clientCount))
tcp.Port(8085, factory()).startListening()
class open(resource.Resource):
render = lambda ctx, request: json.dumps((chiData, pqsnData, chiPqsnData, clientCount))
# Add to and remove from connected WebSocket clients
class factory(protocol.Factory):
class protocol:
def connectionLost(ctx, reason):
transport.remove(ctx.transport)
def makeConnection(ctx, nstTransport):
ctx.transport = nstTransport
transport.add(ctx.transport)
resource = static.File(__file__ + '/..')
resource.putChild('open', open())
resource.putChild('skt', websockets.WebSocketsResource(factory()))
tcp.Port(6011, server.Site(resource)).startListening()
reactor.run()