forked from apurai2/ParentalControls
/
squidParser.py
54 lines (48 loc) · 1.7 KB
/
squidParser.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
#written in python 3
import sys
import os
import calendar, datetime
#need to do: sudo chmod a+r /var/log/squid/access.log
log = open("/var/log/squid/access.log","r")
data = log.readlines()
log.close()
websiteList = []
for i in range(len(data)):
a = data[i].split()
if a[3] != "TCP_DENIED/403":
websiteList.append(a[6])
websiteList.append(a[2])
websiteList.append(datetime.datetime.fromtimestamp(int(float(a[0]))))
#####################################################################################
web = ["www.facebook.com","piazza.com","twitter"]
ifBan = [".facebook.com",".piazza.com",".twitter.com"] #needs format .website.___
timeLimits = [2,4,10] #in minutes
######################################################################################
time = []
allTimes = []
for i in range(len(web)):
time.append([])
allTimes.append([])
for i in range(0,len(websiteList),3):
ap = 0
for j in range(len(web)):
if web[j] in websiteList[i]:
print(websiteList[i+1], " visited: ", websiteList[i], "\ton: ", websiteList[i+2])
time[j].append(websiteList[i+2])
for i in range(len(time)):
pts = time[i]
totalTime = 0
for indx,data in enumerate(pts):
if(indx < (len(pts)-1)):
elapsedTime = pts[indx+1] - data
minVisited = divmod(elapsedTime.total_seconds(),60)[0]
if(minVisited < 5):
totalTime += minVisited
allTimes[i] = totalTime
#sudo chmod a+w /etc/squid/ban_domains.txt
bansFD = open("/etc/squid/ban_domains.txt","a")
for i in range(len(timeLimits)):
if(allTimes[i] > timeLimits[i]):
print("time exceeded for: ", web[i])
bansFD.write(ifBan[i]+'\n')
bansFD.close()