Skip to content

Added block/unblock functionality in Firefox #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions websiteblocker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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():
Expand All @@ -9,21 +11,61 @@ def fetchurls():
urllist = [line.rstrip() for line in file]
return urllist

#fetching the URLs, defining the redirection to localhost 127.0.0.1
# 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 that will block or remove blocking based on user request
# 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:
Expand Down Expand Up @@ -51,6 +93,7 @@ def block(answer):
else:
continue


if __name__ == '__main__':
answer = input("Do you want to enable content filtering(you need to focus)?[Y/y or N/n]")

Expand All @@ -59,4 +102,7 @@ def block(answer):
print("Great\nFetching the blocked URLs from your Database(urllist.txt).")
time.sleep(1)
print("\nDefault blocking has been enabled as per request.")
block(answer)
# Disable Firefox DoH & restart Firefox
disable_firefox_doh()
restart_firefox()
block(answer)