Last active
July 2, 2024 01:24
-
-
Save milnak/9ceb89522dc6ef246112b24f2d646d07 to your computer and use it in GitHub Desktop.
qBittorrent IP Filter updater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
qBittorrent IP Filter updater. | |
.DESCRIPTION | |
Downloads IP Filter list. | |
.NOTES | |
To enable in qBittorrent: | |
Settings - Connection - IP Filtering - Filter path - %APPDATA%\qBittorrent\ipfilter.dat | |
.LINK | |
Uses i-blocklist level 1 list. | |
https://www.iblocklist.com/list?list=ydxerpxkpcfqjaybcssw | |
#> | |
param( | |
# URI to download block list from. | |
[uri]$BlockListUri = 'http://list.iblocklist.com/?list=ydxerpxkpcfqjaybcssw&fileformat=dat&archiveformat=gz', | |
# Path to store ipfilter DAT file into. | |
# For scoop, use '~\scoop\apps\qbittorrent\current\profile\qBittorrent' | |
[string]$BlockListPath = "$env:AppData\qBittorrent", | |
# If set, then update local filter file, even if not out of date. | |
[switch]$Force | |
) | |
function Expand-GzipFile { | |
param( | |
$InFile, | |
$OutFile = ($InFile -replace '\.gz$','') | |
) | |
$instr = New-Object IO.FileStream $InFile,([IO.FileMode]::Open),([IO.FileAccess]::Read),([IO.FileShare]::Read) | |
$outstr = New-Object IO.FileStream $OutFile,([IO.FileMode]::Create),([IO.FileAccess]::Write),([IO.FileShare]::None) | |
$gzstr = New-Object IO.Compression.GzipStream $instr,([IO.Compression.CompressionMode]::Decompress) | |
$buffer = New-Object byte[] (1024) | |
while ($true) { | |
$read = $gzstr.Read($buffer,0,1024) | |
if ($read -le 0) { break } | |
$outstr.Write($buffer,0,$read) | |
} | |
$gzstr.Close() | |
$outstr.Close() | |
$instr.Close() | |
} | |
$BlockListPathResolved = Resolve-Path $BlockListPath -ErrorAction SilentlyContinue | |
if (!$BlockListPathResolved) { | |
'Block list path doesnt exist. Use -BlockListPath to specify location.' | |
return | |
} | |
if (-not (Test-Path $BlockListPathResolved -PathType Container)) { | |
Write-Warning "Path '$BlockListPathResolved' doesnt exist." | |
return | |
} | |
$ipfilterFilename = 'ipfilter.dat' | |
$ipfilterPath = Join-Path $BlockListPathResolved $ipfilterFilename | |
$lmFilename = "$ipfilterFilename.lastmodified" | |
$lmPath = Join-Path $BlockListPathResolved $lmFilename | |
if ($Force) { | |
Remove-Item $lmPath -ErrorAction SilentlyContinue | |
} | |
$response = Invoke-WebRequest -Uri $BlockListUri -Method Head -UserAgent 'curl/8.7.1' | |
$lm = $response.Headers['Last-Modified'] | |
$lmLocal = Get-Content $lmPath -ErrorAction SilentlyContinue | |
"Last-Modified: Server='$lm', Local= '$lmLocal'" | |
if ($lmLocal -eq $lm) { | |
'File is up to date.' | |
return | |
} | |
"Downloading new ipfilter.dat to '$ipfilterPath'" | |
$tempGZipPath = Join-Path $BlockListPathResolved 'ipfilter.dat.gz' | |
$response = Invoke-WebRequest -Uri $BlockListUri -UserAgent 'curl/8.7.1' -OutFile $tempGZipPath | |
Expand-GzipFile -InFile $tempGZipPath | |
Remove-Item $tempGZipPath | |
"Updating '$lmPath'." | |
$lm | Out-File -LiteralPath $lmPath | |
'' | |
'To install IP Filter file in qBitTorrent:' | |
'* Options - Connection - IP Filtering,' | |
"* Set P2P file to: '$ipfilterPath'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment