User Tools

Site Tools


userspace:block_websites_from_hosts-file

Block websites from hosts-file

Scripts and guide by Soren Birk 23-01-2020

I created this package to block users from accessing certain “unwanted” websites such as Facebook, Netflix, Youtube etc.

The package contains 2 productproperties, one which defines the list of websites to block, and another to define what should be removed on next run-through (both setup and uninstall).

Tested with:

  • OPSI 4.1.1.83
  • Winst 4.12.1.5
  • Windows 10, 64-Bit

Instructions:

  • Creating the blocklists:
    • Add the website you want to block like this: 127.0.0.1 <website>. This will result in a “The page cannot be found” error for the user.

Tree:

CLIENT_DATA
  ├ setup.ins
  ├ uninstall.ins
  ├ delsub.ins
  ├ blockwebsitelogo.png
  └ sitelists
    ├ blocklist1.txt
    ├ blocklist2.txt
    ├ blocklist3.txt    
    .
    .

Setup.ins

; Author Soren Birk
 
[Actions]
requiredWinstVersion >= "4.11.2.6"
 
DefVar $LogDir$
DefVar $ProductId$
DefVar $Uninstall$
DefVar $UninstallFile$
DefVar $ChosenList$
DefVar $SiteFile$
DefstringList $WebsitesList$
DefstringList $WebsitesListFiles$
DefstringList $UninstallAll$
 
Set $LogDir$ = "%SystemDrive%\tmp"
Set $ChosenList$ = GetProductProperty("websiteslist","blocklist1")
Set $SiteFile$ = "%ScriptPath%\sitelists\" + $ChosenList$ + ".txt"
Set $Uninstall$ = GetProductProperty("uninstall","all")
 
; ----------------------------------------------------------------
; - Please edit these values                                     -
; ----------------------------------------------------------------
Set $ProductId$       = "block-websites"
; ----------------------------------------------------------------
comment "Display product logo"
ShowBitmap "%ScriptPath%\blockwebsitelogo.png" "Website Policy"
 
if not ($Uninstall$ = "nothing")
	if FileExists("%ScriptPath%\delsub.ins")
		comment "Start uninstall sub section"
		Sub "%ScriptPath%\delsub.ins"
	endif
endif
 
Message "Applying website policy"
Set $WebsitesList$ = loadTextFile($SiteFile$)
for %website% in $WebsitesList$ do PatchHosts_add "%System%\drivers\etc\hosts"
 
 
[PatchHosts_add]
setAlias %website%

Uninstall.ins

; Author Soren Birk
 
[Actions]
requiredWinstVersion >= "4.11.2.6"
 
DefVar $LogDir$
DefVar $ProductId$
DefVar $Uninstall$
DefVar $UninstallFile$
DefVar $ChosenList$
DefVar $SiteFile$
DefstringList $WebsitesList$
DefstringList $WebsitesListFiles$
DefstringList $UninstallAll$
 
Set $LogDir$ = "%SystemDrive%\tmp"
Set $ChosenList$ = GetProductProperty("websiteslist","blocklist1")
Set $SiteFile$ = "%ScriptPath%\sitelists\" + $ChosenList$ + ".txt"
Set $Uninstall$ = GetProductProperty("uninstall","all")
 
; ----------------------------------------------------------------
; - Please edit these values                                     -
; ----------------------------------------------------------------
Set $ProductId$       = "block-websites"
; ----------------------------------------------------------------
comment "Display product logo"
ShowBitmap "%ScriptPath%\blockwebsitelogo.png" "Website Policy"
 
Message "Removing " + $ProductId$ + " ..."
if not ($Uninstall$ = "nothing")
	if FileExists("%ScriptPath%\delsub.ins")
		comment "Start uninstall sub section"
		Sub "%ScriptPath%\delsub.ins"
	endif
else
	LogError "Remember to define the productproperty for uninstall via Configed."
	isFatalError
endif

Delsub.ins

; Author Soren Birk
 
Message "Removing web filtering"
 
if ($Uninstall$ = "all")
	Message "Removing all host aliases"
	comment "Retrieving blocklists from server"
	Set $WebsitesListFiles$ = getOutStreamFromSection ('DosInAnIcon_sitelists')
	for %websitelistfile% in $WebsitesListFiles$ do Sub_AddFileToList
	comment "Removing websites"
	for %website% in $UninstallAll$ do PatchHosts_remove "%System%\drivers\etc\hosts"
else
	Message "Removing aliases for " + $Uninstall$
	Set $UninstallFile$ = "%ScriptPath%\sitelists\" + $ChosenList$ + ".txt"
	Set $WebsitesList$ = loadTextFile($UninstallFile$)
	for %website% in $WebsitesList$ do PatchHosts_remove "%System%\drivers\etc\hosts"
endif
 
 
[PatchHosts_remove]
delAlias %website%
 
[DosInAnIcon_sitelists]
@echo off
dir "%ScriptPath%\sitelists" /b /a-d
 
[Sub_AddFileToList]
comment "Loading %websitelistfile% and applying to collection."
Set $WebsitesList$ = loadTextFile("%ScriptPath%\sitelists\%websitelistfile%")
Set $UninstallAll$ = addListToList($UninstallAll$,$WebsitesList$)

control-file

[Package]
version: 1
depends: 
 
[Product]
type: localboot
id: block-websites
name: Block Websites
description: Blocks websites using the hosts-file
advice: 
version: 1.0
priority: 0
licenseRequired: False
productClasses: 
setupScript: setup.ins
uninstallScript: uninstall.ins
updateScript: 
alwaysScript: 
onceScript: 
customScript: 
userLoginScript: 
 
[ProductProperty]
type: unicode
name: websiteslist
multivalue: False
editable: False
description: List of websites to block.
values: ["blocklist1", "blocklist2", "blocklist3"]
default: ["blocklist1"]
 
[ProductProperty]
type: unicode
name: uninstall
multivalue: False
editable: False
description: What should be removed during setup/uninstall?
values: ["all", "nothing", "blocklist1", "blocklist2", "blocklist3"]
default: ["all"]

blocklist.txt example

127.0.0.1 www.netflix.com
127.0.0.1 www.facebook.com
127.0.0.1 www.youtube.com
userspace/block_websites_from_hosts-file.txt · Last modified: 2021/08/23 08:37 (external edit)