|
| 1 | +import win32com.shell.shell as shell |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import ctypes |
| 5 | +import time |
| 6 | +from datetime import datetime as dt |
| 7 | +from tkinter import * |
| 8 | +from tkinter import messagebox |
| 9 | + |
| 10 | +#Create tkinter root/GUI |
| 11 | +root = Tk() |
| 12 | +root.title("Website Blocker by Rutuj Runwal") |
| 13 | +root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id())) |
| 14 | +root.geometry("600x300") |
| 15 | +ASADMIN = 'asadmin' |
| 16 | +# Host's Path - All the website info that is to be blocked is saved here |
| 17 | +hosts_path = r"C:\Windows\System32\drivers\etc\hosts" |
| 18 | +# localhost's IP |
| 19 | +redirect = "127.0.0.1" |
| 20 | +website_list = [] |
| 21 | + |
| 22 | + |
| 23 | +def AddWebsite(): |
| 24 | + site = Input.get() # Get the site-input given by the user and check if it is |
| 25 | + print("You Entered: "+site) |
| 26 | + if("www" not in site or "." not in site): |
| 27 | + messagebox.showinfo( |
| 28 | + "Invalid Website", "Make sure the site is of this format 'www.google.com' `") |
| 29 | + else: |
| 30 | + website_list.append(site) |
| 31 | + print(website_list) |
| 32 | + |
| 33 | +# Function to check for admin access |
| 34 | +def is_admin(): |
| 35 | + try: |
| 36 | + return ctypes.windll.shell32.IsUserAnAdmin() |
| 37 | + except: |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +def Block(): |
| 42 | + if is_admin(): |
| 43 | + while True: |
| 44 | + # Time of your work |
| 45 | + if dt(dt.now().year, dt.now().month, dt.now().day, 8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 16): |
| 46 | + print("Access Denied") |
| 47 | + with open(hosts_path, 'r+') as file: |
| 48 | + content = file.read() |
| 49 | + for website in website_list: |
| 50 | + if website in content: |
| 51 | + pass |
| 52 | + else: |
| 53 | + # Mapping hostnames to your localhost IP address |
| 54 | + file.write(redirect + " " + website + "\n") |
| 55 | + else: |
| 56 | + with open(hosts_path, 'r+') as file: |
| 57 | + content = file.readlines() |
| 58 | + file.seek(0) |
| 59 | + for line in content: |
| 60 | + if not any(website in line for website in website_list): |
| 61 | + file.write(line) |
| 62 | + # Removing hostnames from host file |
| 63 | + file.truncate() |
| 64 | + |
| 65 | + print("Access Granted") |
| 66 | + time.sleep(5) |
| 67 | + else: |
| 68 | + # Retry getting admin access to modify hosts file |
| 69 | + ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1) |
| 70 | + if is_admin(): |
| 71 | + while True: |
| 72 | + # Time of your work |
| 73 | + if dt(dt.now().year, dt.now().month, dt.now().day, 8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 16): |
| 74 | + print("Access Denied") |
| 75 | + with open(hosts_path, 'r+') as file: |
| 76 | + content = file.read() |
| 77 | + for website in website_list: |
| 78 | + if website in content: |
| 79 | + pass |
| 80 | + else: |
| 81 | + # mapping hostnames to your localhost IP address |
| 82 | + file.write(redirect + " " + website + "\n") |
| 83 | + else: |
| 84 | + with open(hosts_path, 'r+') as file: |
| 85 | + content = file.readlines() |
| 86 | + file.seek(0) |
| 87 | + for line in content: |
| 88 | + if not any(website in line for website in website_list): |
| 89 | + file.write(line) |
| 90 | + # removing hostnames from host file |
| 91 | + file.truncate() |
| 92 | + |
| 93 | + print("Access Granted") |
| 94 | + time.sleep(5) |
| 95 | + |
| 96 | +# The BlockSites function is used to handle no. of sites to block |
| 97 | +def BlockSites(): |
| 98 | + if(len(website_list) == 1): |
| 99 | + Val = messagebox.askquestion("", "You are about to block 1 website.Are you sure you dont want to add more?") |
| 100 | + if Val == "yes": #If user says "yes" , block the site provided |
| 101 | + print("Blocking One Site...") |
| 102 | + Block() |
| 103 | + else: |
| 104 | + messagebox.showinfo("", "Add more sites and then press Block-Sites when you are ready") |
| 105 | + if(len(website_list) == 0): |
| 106 | + #If the website_list is zero it means the user has not added any site to block |
| 107 | + messagebox.showinfo("No sites to block","Please add sites to block first!") |
| 108 | + else: |
| 109 | + #If the sites are >1 block them directly |
| 110 | + print("Ready to block "+str(len(website_list)) + " sites") |
| 111 | + Block() |
| 112 | + |
| 113 | + |
| 114 | +if sys.argv[-1] != ASADMIN: |
| 115 | + script = os.path.abspath(sys.argv[0]) |
| 116 | + params = ' '.join([script] + sys.argv[1:] + [ASADMIN]) |
| 117 | + try: |
| 118 | + # Try running the code with admin access |
| 119 | + shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params) |
| 120 | + messagebox.showinfo("Welcome", "Welcome to Website Blocker v1.0.Add Websites to block,Once you are done click on Block-Sites to get started!") |
| 121 | + MyLabel = Label(root, text="Enter Website to Block:") |
| 122 | + MyLabel.grid(row=0, column=0) |
| 123 | + Input = Entry(root, width=60, borderwidth=3) |
| 124 | + Input.insert(0, "Please REMOVE this text and enter website address.") |
| 125 | + Input.grid(row=0, column=2) |
| 126 | + MyBtn = Button(root, text="Add Website",command=AddWebsite).grid(row=1, column=2) |
| 127 | + MyBtn2 = Button(root, text="Block-Sites",command=BlockSites).grid(row=3, column=2) |
| 128 | + root.mainloop() |
| 129 | + except(Exception): |
| 130 | + # Show an error when admin access is denied |
| 131 | + messagebox.showerror( |
| 132 | + "ERR:Admin Denied", "Admin rights are required for the script to work") |
0 commit comments