forked from Chinmay1292/Circuit-Level-Firewall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit_level_firewall.py
66 lines (55 loc) · 2.07 KB
/
circuit_level_firewall.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
from datetime import datetime, timedelta
import time
import logo
from tqdm import tqdm
print('Choose 1 for Blocking sites and 2 for Unblocking: \n')
print('1. Blocking. \n2. Unblocking')
choice = int(input("\nMake your choice 1 or 2: "))
if choice == 1:
print("Enter time and date details for blocking the sites upto desired time.")
year = int(input("Enter year: "))
mon = int(input("Enter month: "))
day = int(input("Enter day: "))
hour = int(input("Enter hour: "))
min = int(input("Enter minutes: "))
end_time = datetime(year, mon, day, hour, min) # y, m, d, h, min
sites_to_block = list(map(str,input("Enter sites to be blocked using spaces: ").split()))
print("List of sites entered to be blocked: ", sites_to_block)
elif choice == 2:
ini_time_for_now = datetime.now()
end_time = ini_time_for_now - timedelta(days = 1) # y, m, d, h, min
sites_to_block = list(map(str,input("Enter sites to unblocked using spaces: ").split()))
else:
print("Invalid input")
print("Initiating...")
for i in tqdm (range (100),
ascii=False, ncols=75):
time.sleep(0.03)
print("Almost here!")
for i in tqdm (range (100),
ascii=False, ncols=75):
time.sleep(0.03)
hosts_path = '/etc/hosts'
redirect = "127.0.0.1"
#Block sites till the end time
def block_websites():
if datetime.now() < end_time:
with open(hosts_path, 'r+') as hostfile:
hosts_content = hostfile.read()
for site in sites_to_block:
if site not in hosts_content:
hostfile.write(redirect + ' ' + site + '\n')
print('Site(s) Blocked!')
else:
#Removing the blocked list
with open(hosts_path, 'r+') as hostfile:
lines = hostfile.readlines()
hostfile.seek(0)
for line in lines:
if not any(site in line for site in sites_to_block):
hostfile.write(line)
hostfile.truncate() # This removes all the rest remaining portion
print('Site(s) Unblocked')
# sudo python main.py
if __name__ == '__main__':
block_websites()