#===========================================================
#
# HOSTS 3.1, 2026-07-13
#
# Author:   IPWorX.com
# Purpose:  The HOSTS project creates DNS black holes on
#           local machines. This script for compiling HOST
#           file entries from online sources. This script
#           should be run as a scheduled job. This script 
#           should not be edited outside the configuration
#           section.
#
# This script and more to help manage your network found, 
# managed and happily shared with documentation on usage at
# the Information Processing Work eXchange.
#                   https://www.IPWORX.com
#
#===========================================================

# =================== CONFIGURATION ===================

 $NewHOST = "C:\PUBLIC\NewHOST.txt"

# =================== CONFIGURATION ===================

$CacheDir  = "$((Get-Location).Path)\CACHE"
$ConfigDir = "$((Get-Location).Path)\CONFIG"

New-Item -ItemType Directory -Force -Path $CacheDir | Out-Null
New-Item -ItemType Directory -Force -Path $ConfigDir | Out-Null
Remove-Item -Path "$CacheDir\*.txt" -Force -ErrorAction SilentlyContinue

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Write-Host "Downloading mvps.org hosts file..."
Invoke-WebRequest -Uri "http://winhelp2002.mvps.org/hosts.txt" -OutFile "$CacheDir\mvps.txt" -UseBasicParsing

Write-Host "Downloading adware/malware domain hosts file..."
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" -OutFile "$CacheDir\malware.txt" -UseBasicParsing

$CacheHOST   = "$CacheDir\NewHOST.txt"

Remove-Item -Path $CacheHOST -Force -ErrorAction SilentlyContinue

# Use a case-insensitive HashSet for ultra-fast de-duplication and deletion
$hostList = New-Object System.Collections.Generic.HashSet[string] ([System.StringComparer]::OrdinalIgnoreCase)

$txtFiles  = Get-ChildItem -Path "$CacheDir" -Filter "*.txt" -File
$txtFiles += Get-ChildItem -Path "$ConfigDir\ADDBLOCK.dat"

foreach ($file in $txtFiles) {
    write-host "Importing $($file.FullName)..."

    $lines = Get-Content -Path $file.FullName |
      ForEach-Object { $_ -replace '^\s+', '' } |
      Select-String -NotMatch "^#|^$|::|255.255.255.255|127.0.0.1 local"
    
    foreach ($line in $lines) {
        $strTmp = $line
            
        $strTmp = $strTmp -replace "255\.255\.255\.255", ""
        $strTmp = $strTmp -replace "0\.0\.0\.0", ""
        $strTmp = $strTmp -replace "127\.0\.0\.1", ""
        $strTmp = $strTmp -replace "\t", ""
        $strTmp = $strTmp -replace " ", ""
        $strTmp = $strTmp.Trim()

        If($strTmp -gt ""){$hostList.Add($strTmp.ToLower()) | Out-Null}
    }
}

if (Test-Path "$ConfigDir\DONTBLOCK.dat") {
    write-host "Processing DONTBLOCK.dat..."
    $dontBlockLines = Get-Content -Path "$ConfigDir\DONTBLOCK.dat" |
      Select-String -NotMatch "^#|^$"
    
    foreach ($line in $dontBlockLines) {
        $hostList.Remove($strTmp.ToLower()) | Out-Null
    }
}

$sortedHosts = $hostList | Sort

write-host "Writing NewHOST..."
"`r`n`r`n`r`n#BEGIN-IPWORX-HOSTS`r`n" | Out-File -FilePath $CacheHOST -Encoding ASCII

foreach ($shost in $sortedHosts) {
    "0.0.0.0  $shost" | Out-File -FilePath $CacheHOST -Encoding ASCII -Append
}

"`r`n#END-IPWORX-HOSTS`r`n`r`n" | Out-File -FilePath $CacheHOST -Encoding ASCII -Append

if (Test-Path "$CacheDir\NewHOST.txt") {
    Copy-Item -Path $CacheHOST -Destination $NewHOST -Force
} else {
    Write-Warning "NewHOST.txt was not generated by CleanHOSTS.ps1. Copy step skipped."
}

Write-Host "Done."