Plex with VPN

By | February 11, 2015

FROM: http://www.cuttingcords.com/home/2014/11/8/protecting-your-privacy-with-vpn

Fixing myPlex so you can still access Plex remotely

Fixing myPlex so you can still access Plex remotely

One thing that gets screwed up when running through a VPN is the myPlex integration within Plex. When you’re connected to the VPN, myPlex will be trying to reach your server from the VPN address and not your actual IP. We will need to modify the routes so that Plex bypasses the VPN.

If you do not use myPlex, you can skip this section.

We will be using a batch file which automates looking up the required IPs and putting the correct routes in place. Credit for this script goes to XFlak who posted it to the Plex forums.

First, download the script, either from the Plex forums linked above, or rehosted here. Extract the bat file somewhere, such as your user folder. For example, mine is stored at C:\Users\foxingworth\myplex.vpn.bat.

Start Task Scheduler by going to your Start menu/screen, typing “schedule” and clicking the schedule tasks option. On the right side of the window, click Create Task…. Name the task something such as myPlex VPN Route. Under the Security options section, check the Run with highest privileges checkbox. At the bottom of the window, check Hidden.

Switch to the Triggers tab and click New. Switch theBegin the task dropdown to At startup. Check Repeat task every and make sure it is set for 1 hour. Change the for a duration of to Indefinitely. Click OK.

Now switch to the Actions tab and click New. In the Program/script box, browse to the location of the extracted script. Click OK.

You don’t need to touch the Conditions or Settings tabs, so just click OK.

Now on the left side of the Task Scheduler window, click Task Scheduler Library. In the middle, you should now see the task you just added. Right-click it and choose Run. You should see a command prompt appear for a few seconds and then go away.

Open up a web browser and go to your Plex server settings. Click the Connect section and make sure there is not an error message. If it’s connected properly, you should see this:

If you are having trouble, click Show Advanced and make sure the Manually specify port option is selected.

So how does the network traffic actually look now?

Once everything is in place, the traffic flow will act like this:

  • All local network traffic will work normally. Your other computers and media players on your LAN will still see this server as they always have.
  • All remote traffic will now be routed through the VPN. Anything you download or do will come from the VPN IP address and not yours. There are two exceptions to this:
    • myPlex traffic will route over your standard connection since we manually put those routes in place. myPlex won’t work with the VPN connection because we can’t properly port forward it. Accessing Plex from outside your LAN will route over your ISP and not through the VPN.
    • Requests explicitly made to your IP will still be processed and function normally. This means that if you set up remote access to SABnzbd, sonarr, or other programs like that, you can still get to them remotely with your existing IP.

CODE:

XFlak

  1. @echo off
  2. setlocal
  3. set PATH=%SystemRoot%\system32;%SystemRoot%\system32\wbem;%SystemRoot%
  4. chcp 437>nul
  5.  
  6.  
  7. echo my.plexapp.com VPN Route Exception Builder
  8. echo by XFlak
  9. echo.
  10.  
  11.  
  12. ::get Default Gateway
  13. ipconfig|findstr /I /C:“Default Gateway”|findstr /I /C:“1” >“%temp%\gateway.txt”
  14. set /p gateway= <“%temp%\gateway.txt”
  15. set gateway=%gateway:*: =%
  16. ::echo %gateway%
  17.  
  18. ::set gateway=192.168.2.1
  19.  
  20.  
  21. echo Getting my.plexapp.com‘s current IP address
  22. echo.
  23.  
  24. ping “my.plexapp.com”|findstr /I /C:”my.plexapp.com” >”%temp%\my.plexapp.com.txt”
  25.  
  26. set /p PlexIP= <“%temp%\my.plexapp.com.txt”
  27. set PlexIP=%PlexIP:*[=%
  28. set PlexIP=%PlexIP:~0,-24%
  29. echo %PlexIP%
  30. echo.
  31.  
  32. set zero=%PlexIP:*.=%
  33. set zero=%zero:*.=%
  34. echo set PlexIP=%%PlexIP:%zero%=0.0%%>”%temp%\plex.bat”
  35. call “%temp%\plex.bat”
  36.  
  37. ::echo %PlexIP%
  38.  
  39. if not exist “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt” goto:skipcheck
  40.  
  41. findstr /I /C:”%PlexIP%” “%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt”>nul
  42. IF NOT ERRORLEVEL 1 (echo IP already routed, exiting…) & (goto:fin)
  43. :skipcheck
  44.  
  45. echo route -p add %PlexIP% mask 255.255.0.0 %gateway%
  46. route -p add %PlexIP% mask 255.255.0.0 %gateway%
  47. echo.
  48.  
  49. echo Adding IP to %userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt
  50. echo.
  51. echo %PlexIP% >>”%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt”
  52.  
  53.  
  54.  
  55. :fin
  56.  
  57. ::echo fin
  58. ::pause
  59.  
  60. exit
  61.  

One thought on “Plex with VPN

  1. XFlak

    FYI, I just updated my script, find it here:
    https://forums.plex.tv/index.php/topic/64600-using-plex-server-with-a-vpn/page-3#entry602919

    it’s now more efficient\secure by using nslookup instead of ping

    Also it includes a read me, an uninstaller, and multiple versions of the script for different subnet masks. Try them in the following order until you find one that works for you. Warning, the further you get to the bottom of the list the more IPs will bypass your VPN.

    255.255.255.255
    255.255.255.252
    255.255.255.0
    255.255.0.0
    255.0.0.0

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *