Plex Media Server is the popular choice for making your media collection available to an array of devices. If you use torrents for acquiring new content then you of course use a VPN as well for safety. Plex routes traffic through plex.tv which goes through your VPN connection, meaning remotely accessing your media collection would require port forwarding through the VPN. Luckily XFlak has created a batch script to reroute Plex traffic through your normal network connection, bypassing the VPN and restoring Plex Media Server remote access behind the VPN. This tutorial is relatively easy to set up on Windows 7, 8 and 10. It should work on Windows Server 2008, 2012 and 2016 too.
Private Internet Access and PureVPN are popular VPN providers which you may want to access your Plex media collection while using.
VPN Service | ||||
---|---|---|---|---|
Private Internet Access | ($3.33 / month) | |||
Pure VPN | ($4.91 / month) | |||
IPVanish | ($6.41 / month) |
Remote Access Plex Media Server on Windows Behind VPN
All credit for this script goes to XFlak (his site) on the Plex forum. The original post by XFlak is here and may have a newer version of the Windows batch script than here.
Create an empty file somewhere on your machine, I put it in C:\HTPCGuides\PlexVPN.bat
Paste the batch script written by XFlak for allowing Plex to map its port back without the VPN by correcting the routes for Plex
@echo off
setlocal
set PATH=%SystemRoot%\system32;%SystemRoot%\system32\wbem;%SystemRoot%
chcp 437>nul
echo VPN Bypass for Plex Media Server
echo by XFlak
echo.
::get Default Gateway
ipconfig|findstr /I /C:"Default Gateway"|findstr /I /C:"1" >"%temp%\gateway.txt"
set /p gateway= <"%temp%\gateway.txt" set gateway=%gateway:*: =% ::echo %gateway% ::If gateway is detected incorrectly, override it by uncommenting the below like (delete : and input your correct gateway ::set gateway=192.168.2.1 echo Getting plex.tv current IP addresses... echo. echo Note: Log of plex.tv's routed IP's saved here: echo %userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt echo. nslookup "plex.tv"|findstr /I /V "Server: Address: Name: timeout" >"%temp%\temp.txt"
findstr /I /C:" " "%temp%\temp.txt" >"%temp%\plex.tv.txt"
echo.
cd /d "%temp%"
for /F "tokens=*" %%A in (plex.tv.txt) do call :list %%A
goto:donelist
:list
set PlexIP=%*
set PlexIP=%PlexIP:* =%
echo %PlexIP%
if not exist "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt" goto:skipcheck
findstr /I /C:"%PlexIP%" "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt">nul
IF NOT ERRORLEVEL 1 (echo IP already routed, skipping...) & (goto:EOF)
:skipcheck
echo route -p add %PlexIP% mask 255.255.255.255 %gateway%
route -p add %PlexIP% mask 255.255.255.255 %gateway%
echo.
echo %PlexIP% >>"%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt"
goto:EOF
:donelist
::clean no longer used IPs
echo.
echo Removing routed IPs no longer used by plex.tv
echo.
if exist "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt" del "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt">nul
if not exist "%userprofile%\AppData\Local\Plex Media Server" goto:doneclean
if not exist "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt" goto:doneclean
cd /d "%userprofile%\AppData\Local\Plex Media Server"
for /F "tokens=*" %%A in (PermittedPlexIPs.txt) do call :clean %%A
goto:doneclean
:clean
set PlexIP=%*
findstr /I /C:"%PlexIP%" "%temp%\plex.tv.txt" >nul
IF ERRORLEVEL 1 goto:remove
echo IP still used: %PlexIP%
echo %PlexIP% >>"%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt"
goto:EOF
:remove
echo IP no longer used: route delete %PlexIP%
route delete %PlexIP%
goto:EOF
:doneclean
if exist "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt" del "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt">nul
if exist "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt" move /y "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs2.txt" "%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt">nul
echo.
echo Finished, exiting...
@ping 127.0.0.1 -n 3 -w 1000> nul
exit
You can create a task for the Plex VPN Windows script. Personally I have never needed it and just run it as a batch file on boot, however XFlak and this site recommend it in case Plex IPs change.
Load up the Windows Run menu with Windows+R
Taskschd.msc
Click Create Task..
Enter a Name for the Task
Select Run whether user is logged on or not
Check Run with highest privileges
Click the Triggers tab and click New
For Begin the task: choose At startup
Check Repeat task every: and choose 1 hour to make sure the route is always up to date for a duration of Indefinitely
Check Enabled and click OK
Now click the Action tab and click New
Click Browse and choose your PlexVPN.bat file
Click OK
Now just click OK and your task is created.
Now you will be able to access Plex Media Server behind a VPN on Windows. If you found this useful consider donating to XFlak so he continues to update this valuable script.