From 1d1080af7d347df4afe7c4cb7090eeac93a89003 Mon Sep 17 00:00:00 2001 From: MishraSanskriti Date: Wed, 9 Apr 2025 19:14:43 +0530 Subject: [PATCH] Added block/unblock functionality in Firefox --- websiteblocker.py | 52 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/websiteblocker.py b/websiteblocker.py index 95a7a45..28819ee 100644 --- a/websiteblocker.py +++ b/websiteblocker.py @@ -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(): @@ -9,7 +11,7 @@ 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" @@ -17,13 +19,53 @@ def fetchurls(): #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: @@ -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]") @@ -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) \ No newline at end of file