-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebsiteblocker.py
More file actions
108 lines (97 loc) · 4.12 KB
/
websiteblocker.py
File metadata and controls
108 lines (97 loc) · 4.12 KB
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
import time
import sys
import platform
import os
import psutil
#function to fetch the URLs that have been saved in urllist.txt for blocking
def fetchurls():
urllist = list()
with open('urllist.txt') as file :
urllist = [line.rstrip() for line in file]
return urllist
# Fetch URLs from the file
sites_to_be_blocked = fetchurls()
redirect = "127.0.0.1"
#identifying the underlying Operating System to decide the directory the hosts file resides in
#Currently working for Microsoft Windows, Linux Distributions and MacOS
if platform.system() == 'Windows':
host_file = r"C:\Windows\System32\drivers\etc\hosts"
firefox_profile = os.path.expanduser("~\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\")
elif platform.system() == "Linux" or platform.system() == "darwin":
host_file = "/etc/hosts"
firefox_profile = os.path.expanduser("~/.mozilla/firefox/")
else:
print("Expected your OS to be Windows, Linux or MacOS but found something else, hence quitting...")
sys.exit(1)
# Function to disable DNS over HTTPS (DoH) in Firefox
def disable_firefox_doh():
global firefox_profile
if not os.path.exists(firefox_profile):
print("Firefox profile directory not found.")
return
# Locate the correct Firefox profile folder
for folder in os.listdir(firefox_profile):
prefs_file = os.path.join(firefox_profile, folder, "prefs.js")
if os.path.exists(prefs_file):
try:
with open(prefs_file, "r+") as file:
lines = file.readlines()
file.seek(0)
for line in lines:
# Remove existing DoH settings
if "network.trr.mode" not in line:
file.write(line)
# Append the new setting to force system DNS resolution
file.write('user_pref("network.trr.mode", 5);\n')
file.truncate()
print("DNS over HTTPS (DoH) disabled in Firefox.")
except Exception as e:
print(f"Error modifying Firefox prefs.js: {e}")
return
# Function to clear Firefox's DNS cache by restarting it
def restart_firefox():
for proc in psutil.process_iter(['pid', 'name']):
if "firefox" in proc.info['name'].lower():
print("Restarting Firefox...")
proc.terminate()
time.sleep(2) # Give Firefox time to close
os.system("firefox &") # Restart Firefox
return
# Function to block sites by modifying the hosts file
def block(answer):
while True:
with open(host_file, 'r+') as hostentry:
hosts = hostentry.read() #reading the host entries to confirm if the required URLs are present or not
#adding the urls from DB in hosts file and adding redirection to localhost to make them inaccessible
for url in sites_to_be_blocked:
if url not in hosts:
hostentry.write(redirect+' '+url+'\n')
try:
time.sleep(50)
except KeyboardInterrupt:
#catching the ctrl + c (quit) from user, asking for confirmation
quitting = input("Do you really want to disable content blocking?[Y/y or N/n]")
if (quitting == "Y" or quitting == "y"):
#clearing the entries made during blocking phase
with open(host_file, 'r+') as hostentry:
hosts = hostentry.readlines()
hostentry.seek(0)
for host in hosts:
if not any(url in host for url in sites_to_be_blocked):
hostentry.write(host)
hostentry.truncate()
print("Cool!!\nhave a great time!")
sys.exit(1)
else:
continue
if __name__ == '__main__':
answer = input("Do you want to enable content filtering(you need to focus)?[Y/y or N/n]")
if (answer == "Y" or answer == "y"):
#calling the block() function to enable blocking
print("Great\nFetching the blocked URLs from your Database(urllist.txt).")
time.sleep(1)
print("\nDefault blocking has been enabled as per request.")
# Disable Firefox DoH & restart Firefox
disable_firefox_doh()
restart_firefox()
block(answer)