#!/usr/bin/env python #Author: Shriphani Palakodety #Mail: spalakod@purdue.edu #An attempt to replicate Steve Lambert's self-control application for windows. import sys import time import os machine_hosts_location = "" if os.name == "nt": machine_hosts_location = "C:\Windows\System32\drivers\etc\hosts" elif os.name == "posix": machine_hosts_location = "/etc/hosts" else: sys.exit(1) #else, this app cannot really help you class App: def __init__(self, urlList=[], path="list.txt", hosts=machine_hosts_location): self.UrlList = urlList self.Path = path self.HostsFile = hosts self.StartTime = time.time() #get no. of seconds since epoch self.StartMarker = "# Inserted by WinSelfControl! DO NOT EDIT OR CHANGE CONTENTS" self.EndMarker = "# End of WinSelfControl Section ! Edit away after this line" def confirm(self, duration): '''Prints out a list of domains to be blocked''' print "The following domains will be blocked for the next %s minutes", duration f = open(self.path, "r") for new_line in f: print f f.close() def addNewTLD(self, tldname): '''Take new TLD and block it''' pass #this won't come in use till I attach a GUI to it. def beginControl(self, duration): '''add blacklisted dudes to hosts file for user specified time''' #read in blacklisted domains f = open(self.Path, "r") for new_line in f: self.UrlList.append(new_line) f.close() #write to hosts file hosts = open(self.HostsFile, "a") #first insert markers so I can detect where to remove from hosts.write(self.StartMarker + "\n\n\n") #next put in the entries for domain in self.UrlList: hosts.write("127.0.0.1\t"+domain+"\n") #next close our section hosts.write("\n\n\n"+self.EndMarker) hosts.close() #call timeOut self.timeOut(duration) #this is currently super necessary. I will write this tomorrow def endControl(self): '''remove blacklisted sites from hosts file''' #first open file to see where the lines added by this app are. hostsInStream = open(self.HostsFile, "r") #read in text till our blacklisted section content = [] #hold content that was present beforehand self_content = False for new_line in hostsInStream: if new_line.find(self.StartMarker) == -1 and not self_content: content.append(new_line) else: self_content = True if self_content and new_line.find(self.EndMarker) > -1: self_content = False hostsInStream.close() #write back original file content hostsOutStream = open(self.HostsFile, "w") for line in content: hostsOutStream.write(line) hostsOutStream.close() def timeOut(self, duration): '''count down for minutes''' curTime = time.time() passed = (curTime - self.StartTime)/60.0 while passed < duration: curTime = time.time() passed = (curTime - self.StartTime)/60.0 #print passed #pass #now, kick out the entries from the hosts file. self.endControl() def usage(self): '''tell the user how to use this app''' print "Usage: selfcontrol " if __name__ == "__main__": #initalize the App object appInstance = App() #start with a blank urllist #perform sanity checks and complain etc. if len(sys.argv) < 2: appInstance.usage() else: duration = float(sys.argv[1]) appInstance.beginControl(duration) #need to get time to work