Skip to content

Commit

Permalink
Improved handling of incorrect hosts file
Browse files Browse the repository at this point in the history
  • Loading branch information
amd64fox committed May 2, 2024
1 parent 8970cae commit 167678c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -592,20 +592,24 @@ $hostsFilePath = Join-Path $Env:windir 'System32\Drivers\Etc\hosts'
$hostsBackupFilePath = Join-Path $Env:windir 'System32\Drivers\Etc\hosts.bak'

if (Test-Path -Path $hostsFilePath) {
$hosts = Get-Content -Path $hostsFilePath

if ($hosts -match '^[^\#|].+scdn.+|^[^\#|].+spotify.+') {
$hosts = [System.IO.File]::ReadAllLines($hostsFilePath)
$regex = "^(?!#|\|)((?:.*?(?:download|upgrade)\.scdn\.co|.*?spotify).*)"

if ($hosts -match $regex) {

Write-Host ($lang).HostInfo
Write-Host ($lang).HostBak

Copy-Item -Path $hostsFilePath -Destination $hostsBackupFilePath -ErrorAction SilentlyContinue

if ($?) {

Write-Host ($lang).HostDel

try {
$hosts = $hosts -replace '^[^\#|].+scdn.+|^[^\#|].+spotify.+', ''
$hosts = $hosts | Where-Object { $_.trim() -ne "" }
Set-Content -Path $hostsFilePath -Value $hosts -Force
$hosts = $hosts | Where-Object { $_ -notmatch $regex }
[System.IO.File]::WriteAllLines($hostsFilePath, $hosts)
}
catch {
Write-Host ($lang).HostError -ForegroundColor Red
Expand Down

0 comments on commit 167678c

Please sign in to comment.