Split Tunnel / Plex / Torrents

Like many I have a media server set up with bittorrent and it also has my Plex server on it.  I wanted to be able to access my Plex from anywhere but I had no luck getting PIA to properly exclude Plex itself.  I only use this box to do media server stuff, so I went the other way.  I told PIA to allow all but specified apps to bypass the VPN and gave it the executable locations for Transmission and the other programs I use to make this work.

This came to an issue where if the VPN goes down, everything just defaults back to going over the Internet like normal which is, not ok.

I wrote a script in PowerShell (Requires PowerShell 7) to do a few things, the main thing that is required is to install Chocolaty and install curl, then make sure PIA is set to run curl through the VPN.  The script uses curl and PowerShell to get the public IP through the VPN and through normal means, then compares them.  If that comparison checks out (i.e. they are different) then the system defaults to asking PIA if it is connected or not.  This is just in case one of the IP methods returns an IPv4 and the other an IPv6 which could make it appear the VPN is running when it's not.

Because I want to KNOW when this happens I have it set to send me out a Telegram message when it does, if that's not necessary it can be removed and of course it needs to get any changes to executable names, it's not polished at all.

Telegram Messages require the PoshGram module:  https://techthoughts.info/poshgram-powershell-module-for-telegram/
# Checks to see if the VPN is online, if not kills downloaders.

$botToken = "your bot token here"
$chatID = "your chat ID here"

$vpnCommand = "C:\Program Files\Private Internet Access\piactl"
$vpnParams = "get", "connectionstate"
$vpnState = & $vpnCommand $vpnParams

$services = "transmission", "nzbdrone", "radarr", "jackett"

$vpnUp = "VPN Status`nThe VPN is online, all downloaders have been started."
$vpnDown = "VPN Status`nThe VPN is offline, all downloaders have been stopped."

$publicVPN = C:\ProgramData\chocolatey\lib\curl\tools\curl-7.72.0-win64-mingw\bin\curl.exe -s ipecho.net/plain
$publicISP = (Invoke-WebRequest -URI http://ipecho.net/plain).Content

function stopDownloaders($services) {
	$needSend = $false
	ForEach ($s in $services) {
		if ((Get-Service $s).Status -eq "running") {
			Stop-Service -WarningAction Ignore $s
			$needSend = $true
		}
	}
	if ($needSend -eq $true) {
		sendMessage $vpnDown
	}
}

function startSownloaders($services) {
	$needSend = $false
	ForEach ($s in $services) {
		if ((Get-Service $s).Status -ne "running") {
			Start-Service -WarningAction Ignore $s
		}
	}
	if ($needSend -eq $true) {
		sendMessage $vpnUp
	}
}

function sendMessage($msg) {
	Send-TelegramTextMessage -BotToken $botToken -ChatID $chatID -Message $msg 2>&1 | out-null
}

if ($publicVPN -eq $publicISP) {
	stopDownloaders $services
} else {
	if ($vpnState -eq "connected") {
		startSownloaders $services
	} else {
		stopDownloaders $services
	}
}

Comments

  • Slight edit...  I forgot to put a line in:
    function startSownloaders($services) {
    	$needSend = $false
    	ForEach ($s in $services) {
    		if ((Get-Service $s).Status -ne "running") {
    			Start-Service -WarningAction Ignore $s
    			$needSend = $true
    		}
    	}
    	if ($needSend -eq $true) {
    		sendMessage $vpnUp
    	}
    }
    
    Otherwise the Telegram message fails to send when the VPN connection goes back up.
Sign In or Register to comment.