diff --git a/README.md b/README.md index 085bf62..4b86ab0 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,20 @@ w3b13locker # w3bsite 13locker -`beta build v0.2` +`beta build v0.3` ### web blocker | w3b13locker | website blocker -> Block inapropriate websites and ads by their domain +> Block inappropriate websites and ads by their domain > CommandLine application > Build with python 3.7 -> Beta Build v0.2 +> Beta Build v0.3 -[![Build Status](https://img.shields.io/badge/version-v0.2-green?style=flat-square)](https://github.com/yunghog/w3bsite13locker) +[![Build Status](https://img.shields.io/badge/version-v0.3-green?style=flat-square)](https://github.com/yunghog/w3bsite13locker) [![Python](https://img.shields.io/badge/python-3.7-orange?style=flat-square&logo=python)](https://github.com/yunghog/w3bsite13locker) -# Table of Contents +## Table of Contents - [Requirements](#requirements) - [Installation](#installation) - [Features](#features) @@ -34,7 +34,7 @@ ```shell git clone https://github.com/yunghog/w3bsite13locker.git ``` -### Setting up +### Setting up - open a command prompt/terminal with admin rights (*run as administrator) or a python console inside the cloned directory - run setup.py using command ```shell @@ -45,19 +45,20 @@ python setup.py - Working with hosts file - Compatible on Windows and Linux machines - Lightweight python script +- Log file to keep track of hosts - Easy to handle ## Usage - To block any domain use command (*without '[' ']' around domain name) ```shell python main.py -b [domain] -python main.py -block [domain] +python main.py --block [domain] ``` - To unblock any domain use command (*without '[' ']' around domain name) ```shell python main.py -ub [domain] -python main.py -unblock [domain] +python main.py --unblock [domain] ``` > For full usage refer usage.txt - `usage.txt` @@ -76,6 +77,7 @@ python main.py -unblock [domain] | -ub/--unblock | domain name | unblocking | | -l/--list | none | list already blocked domains | | -s/--search | domain name | find out if the domain is blocked | +| -h/--help | none | list all possible commands | +----------------+------------------+-------------------------------------+ # launch the python console in administrator mode @@ -111,6 +113,9 @@ python main.py -unblock [domain] * bugs fixed - works on linux os - hosts file with domains separated by '\t' problem resolved +# w3b13locker beta v0.3 : + - compatible on mac os + - working log file @ data/log.ini which holds all logs ``` ## Snapshot @@ -128,6 +133,3 @@ python main.py -unblock [domain] - 🍴 Fork this repo! - 👯 Clone this repo to your local machine using `https://github.com/yunghog/w3bsite13locker.git` - Pull requests - - - diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc index 61852ad..13bd122 100644 Binary files a/__pycache__/config.cpython-37.pyc and b/__pycache__/config.cpython-37.pyc differ diff --git a/__pycache__/func.cpython-37.pyc b/__pycache__/func.cpython-37.pyc deleted file mode 100644 index 6f6510c..0000000 Binary files a/__pycache__/func.cpython-37.pyc and /dev/null differ diff --git a/__pycache__/logger.cpython-37.pyc b/__pycache__/logger.cpython-37.pyc new file mode 100644 index 0000000..e8ec466 Binary files /dev/null and b/__pycache__/logger.cpython-37.pyc differ diff --git a/changeLog.txt b/changeLog.txt index 33844af..80e6802 100644 --- a/changeLog.txt +++ b/changeLog.txt @@ -20,3 +20,7 @@ * bugs fixed - works on linux os - hosts file with domains separated by '\t' problem resolved + +# w3b13locker beta v0.3 : + - compatible on mac os + - working log file @ data/log.ini which holds all logs diff --git a/config.py b/config.py index f0cb242..4987410 100644 --- a/config.py +++ b/config.py @@ -7,7 +7,7 @@ def config() : if myos=='Windows': windir=os.environ['windir'] hostPath=windir+r'\system32\drivers\etc\hosts' - if myos=='Linux': + if myos=='Linux' or myos=='Darwin': hostPath=r'/etc/hosts' pathFile=open(r'data/path.txt','w') pathFile.write('operatingSystem|'+myos+'\n'+'hosts|'+hostPath+'\n') diff --git a/data/hostsOrginal.txt b/data/hostsOrginal.txt index 1f0770a..e69de29 100644 --- a/data/hostsOrginal.txt +++ b/data/hostsOrginal.txt @@ -1,41 +0,0 @@ -# Copyright (c) 1993-2009 Microsoft Corp. -# -# This is a sample HOSTS file used by Microsoft TCP/IP for Windows. -# -# This file contains the mappings of IP addresses to host names. Each -# entry should be kept on an individual line. The IP address should -# be placed in the first column followed by the corresponding host name. -# The IP address and the host name should be separated by at least one -# space. -# -# Additionally, comments (such as these) may be inserted on individual -# lines or following the machine name denoted by a '#' symbol. -# -# For example: -# -# 102.54.94.97 rhino.acme.com # source server -# 38.25.63.10 x.acme.com # x client host -# localhost name resolution is handled within DNS itself. -# 127.0.0.1 localhost -# ::1 localhost -127.0.0.1 www.r2rdownload.com -127.0.0.1 www.elephantafiles.com -127.0.0.1 localhost -::1 localhost -127.0.0.1 musico -::1 musico -127.0.0.1 sc -::1 sc -127.0.0.1 mixtape -::1 mixtape -127.0.0.1 yunghogbeats -::1 yunghogbeats -127.0.0.1 ekathvatheme -::1 ekathvatheme -127.0.0.1 kalasa -::1 kalasa - - - - - diff --git a/data/log.ini b/data/log.ini new file mode 100644 index 0000000..e69de29 diff --git a/data/path.txt b/data/path.txt index 28c1199..e69de29 100644 --- a/data/path.txt +++ b/data/path.txt @@ -1,2 +0,0 @@ -operatingSystem|Windows -hosts|C:\Windows\system32\drivers\etc\hosts diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..78dbd26 --- /dev/null +++ b/logger.py @@ -0,0 +1,9 @@ +#write log.ini +#author : Samartha +import datetime as dt +def log(logDetails): + logFile=open(r'data/log.ini','a+') + now = dt.datetime.now() + now = now.strftime("%Y-%m-%d_%H:%M:%S") + logFile.write(now+'_'+logDetails+'\n') + logFile.close() diff --git a/main.py b/main.py index e7e61a8..8ff136b 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ #w3b13locker by Samartha import sys +import os import config as c +import logger as l #hostsFile=r'hosts.txt' hostsFile=c.config() def searchDomain(dom): @@ -21,7 +23,6 @@ def searchDomain(dom): return index else: return -1 -# rHost=open(r'hosts.txt','r') argc = len(sys.argv) if argc>1 and argc<=4 : option = sys.argv[1] @@ -34,13 +35,32 @@ def searchDomain(dom): print('Invalid set of arguments\nUse -h/--help to display valid options') if argc==2 : if option=='-l' or option=='--list' : + os.system('cls') + print('------------hosts-------------') rHost=open(hostsFile,'r') for line in rHost : if line[0] != '#' : print(line, end='') rHost.close(); elif option=='-h' or option=='--help' : - print('all the possible option combinations !!') + os.system('cls') + help='''Usage: python main.py [option] [arguments] + python main.py [-b/-ub/-s/-h/l/--block/--unblock/--search/--list/--help] *[-f/--file] *[domain/domain list]\n ++----------------+-----------------------------------------------+ +| option | description | ++----------------+-----------------------------------------------+ +| -b/--block | blocks the domain. takes domain as argument | +| -ub/--unblock | unblocks the domain. takes doamin as argument | +| -s/--search | search for a domain. takes domain as argument | +| -l/--list | list blocked domain | +| -f/--file | used along with -b/--block or -ub/--unblock. | +| | takes domain list | ++----------------+-----------------------------------------------+ + ----------w3b13locker---------- + ---stable build v1.0--- + --by Samartha-- +''' + print(help) else: print('Invalid argument\nUse -h/--help to display valid options') if argc==3 : @@ -48,26 +68,36 @@ def searchDomain(dom): if searchDomain(domain)>=0: print(domain, ' is already blocked') else: - wHost=open(hostsFile,'a') - wHost.write('\n') - block='127.0.0.1 ' + domain - wHost.write(block) - wHost.close() + try: + wHost=open(hostsFile,'a') + wHost.write('\n') + block='127.0.0.1 ' + domain + wHost.write(block) + wHost.close() + l.log('blocked_'+domain) + except PermissionError : + l.log('failed_to_block_'+domain+'_Error:Access denied') + print('Failed! Access denied! Open console with admin rights') elif option=='-ub' or option=='--unblock' : index=searchDomain(domain) if index>=0: - rHost=open(hostsFile,'r') - dList=rHost.read().split('\n') - rHost.close() - wHost=open(hostsFile,'w') - for dom in dList: - if '#' in dom : - wHost.write(dom+'\n') - if '#' not in dom and dom!='\n' and dom!='': - xDom=dom.split(' ') - if domain!=xDom[1]: - wHost.write(xDom[0]+' '+xDom[1]+'\n') - wHost.close() + try: + rHost=open(hostsFile,'r') + dList=rHost.read().split('\n') + rHost.close() + wHost=open(hostsFile,'w') + for dom in dList: + if '#' in dom : + wHost.write(dom+'\n') + if '#' not in dom and dom!='\n' and dom!='': + xDom=dom.split(' ') + if domain!=xDom[1]: + wHost.write(xDom[0]+' '+xDom[1]+'\n') + wHost.close() + l.log('unblocked_'+domain) + except PermissionError : + l.log('failed_to_unblock_'+domain+'_Error:Access denied') + print('Failed! Access denied! Open console with admin rights') else: print(domain , ' domain is not in the hosts') elif option=='-s' or option=='--search' :