67 lines
2.7 KiB
PowerShell
67 lines
2.7 KiB
PowerShell
[int] $schedule = $ENV:SCHEDULE
|
|
[string] $gluetunForwardedPortFile = $ENV:GLUETUNFORWARDEDPORTFILE
|
|
[string] $qbtHost = $ENV:QBTHOST
|
|
[string] $qbtPort = $ENV:QBTPORT
|
|
[string] $qbtUser = $ENV:QBTUSER
|
|
[string] $qbtPassword = $ENV:QBTPASSWORD
|
|
[string] $notificationWebhookURL = $ENV:NOTIFICATIONWEBHOOKURL
|
|
[string] $discordWebhookURL = $ENV:DISCORDWEBHOOKURL
|
|
[string] $appriseWebhookURL = $ENV:APPRISEWEBHOOKURL
|
|
[string] $appriseWebhookTag = $ENV:APPRISEWEBHOOKTAG
|
|
|
|
while ($true)
|
|
{
|
|
$gluetunForwardedPort = Get-Content $gluetunForwardedPortFile
|
|
Write-Host "Current forwarded port: $gluetunForwardedPort"
|
|
|
|
$qbtHeaders = @{
|
|
Referer = "http://$($qbtHost):$($qbtPort)"
|
|
}
|
|
$qbtBody = @{
|
|
username = $qbtUser
|
|
password = $qbtPassword
|
|
}
|
|
$qbtSession = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
|
|
$qbtResponse = Invoke-WebRequest -Uri "http://$($qbtHost):$($qbtPort)/api/v2/auth/login" -WebSession $qbtSession -Method POST -Headers $qbtHeaders -Body $qbtBody
|
|
|
|
$qbtQueryResponse = Invoke-WebRequest -Uri "http://$($qbtHost):$($qbtPort)/api/v2/app/preferences" -WebSession $qbtSession -Method GET
|
|
$qbtListenPort = (ConvertFrom-Json $qbtQueryResponse.Content).listen_port
|
|
Write-Host "Current QBT Listening port: $qbtListenPort"
|
|
|
|
if ($gluetunForwardedPort -eq $qbtListenPort)
|
|
{
|
|
Write-Host "Ports match!"
|
|
}
|
|
else
|
|
{
|
|
$qbtPortChangeBody = 'json={"listen_port":' + $gluetunForwardedPort + '}'
|
|
$qbtPortChangeResponse = Invoke-WebRequest -Uri "http://$($qbtHost):$($qbtPort)/api/v2/app/setPreferences" -WebSession $qbtSession -Method POST -Headers $qbtHeaders -Body $qbtPortChangeBody
|
|
if ($notificationWebhookURL -or $discordWebhookURL)
|
|
{
|
|
if ($discordWebhookURL) { $notificationWebhookURL = $discordWebhookURL }
|
|
$headers = @{
|
|
"Content-Type"="application/json"
|
|
}
|
|
$body = @{
|
|
content="QBitTorrent Listening Port updated to $gluetunForwardedPort"
|
|
}
|
|
$json = $body | ConvertTo-Json
|
|
Invoke-WebRequest -URI $notificationWebhookURL -Method post -Body $json -Headers $headers
|
|
}
|
|
elseif ($appriseWebhookURL)
|
|
{
|
|
$headers = @{
|
|
"Content-Type"="application/json"
|
|
}
|
|
$body = @{
|
|
title="QBitTorrent Port Manager"
|
|
body="QBitTorrent Listening Port updated to $gluetunForwardedPort"
|
|
tags="$appriseWebhookTag"
|
|
}
|
|
$json = $body | ConvertTo-Json
|
|
Invoke-WebRequest -URI $appriseWebhookURL -Method post -Body $json -Headers $headers
|
|
}
|
|
}
|
|
Write-Host "Sleeping for $schedule minutes..."
|
|
Start-Sleep ($schedule * 60)
|
|
} |