User Tools

Site Tools


userspace:block_websites_from_hosts-file

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

userspace:block_websites_from_hosts-file [2020/01/23 10:20]
bobzbobz [Setup.ins]
userspace:block_websites_from_hosts-file [2021/08/23 08:37]
Line 1: Line 1:
-====== 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:** 
-<code> 
-CLIENT_DATA 
-  ├ setup.ins 
-  ├ uninstall.ins 
-  ├ delsub.ins 
-  ├ blockwebsitelogo.png 
-  └ sitelists 
-    ├ blocklist1.txt 
-    ├ blocklist2.txt 
-    ├ blocklist3.txt     
-    . 
-    . 
-</code> 
- 
-==== Setup.ins ==== 
-<code winst> 
-; 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% 
-</code> 
-==== Uninstall.ins ==== 
-<code winst> 
-; 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") 
- 
-; ---------------------------------------------------------------- 
-; - Rediger venligst punkterne herunder                          - 
-; ---------------------------------------------------------------- 
-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 
-</code> 
-==== Delsub.ins ==== 
-<code winst> 
-; 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$) 
-</code> 
-==== control-file ==== 
-<code winst> 
-[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"] 
-</code> 
-==== blocklist.txt example ==== 
-<code winst> 
-127.0.0.1 www.netflix.com 
-127.0.0.1 www.facebook.com 
-127.0.0.1 www.youtube.com 
-</code> 
userspace/block_websites_from_hosts-file.txt · Last modified: 2021/08/23 08:37 (external edit)