Using Plex Server with a VPN

Anyone got a script for linux to automaticly update the routes

currently added
route add -net 184.72.0.0 netmask 255.255.0.0 gw 192.168.0.1
route add -net 184.169.0.0 netmask 255.255.0.0 gw 192.168.0.1
route add -net 54.241.0.0 netmask 255.255.0.0 gw 192.168.0.1
route add -net 50.18.0.0 netmask 255.255.0.0 gw 192.168.0.1

This allows me to connect to myplex but now i can not connect to http://xxx.xxx.xxx.xxx:32400/ from the web. internal it is working

cheers

andy

The script has nothing to do with why it’s not working. It’s not working because u r using the wrong web address. The correct address is http://xxx.xxx.xxx.xxx:32400/web


U r missing the /web part

@kidamnesiac Continued thanks... not sure what is going on but I can't get it to work.

When I manually add I get this for each IP: "route: writing to routing socket: File exists" which I take to mean it was already in the routing table from AddRoutes

netstat is also confirming that the routes were made

Attempt #1: start VPN, manual add, start PMS, unable to publish

Attempt #2: close VPN and PMS, start PMS, publish successfully, start VPN, unable to access myplex from outside WLAN, un-publish, then unable to re-publish

what i still don't understand is why i can't even get this to work by adding the routes manually... since other PIA users have done it successfully (or by using XFlak's script on Windows) i feel like i might just be misunderstanding a small detail

Try flushing your routing table first using “route -f”, then try again

Try flushing your routing table first using "route -f", then try again

thanks for the help. still not working. what i've tried:

-reboot (bc when i enter "sudo route -n flush" i get "route: write to routing socket: No such process got only -1 for rlen")

-make sure PMS is closed

-connect to PIA (have also tried where i add routes first and then connect to PIA)

-enter "sudo route add MY.PLEX.IP.HERE YOUR.GATEWAY.IP.HERE" where myplex ip's are those you've listed here and for gateway ip i've tried both my router and the local machine running PMS

-start PMS

-cannot access myplex from another machine on a different network

-disconnect from PIA

-can access myplex

if anyone can tell from this where i'm going wrong it would be much appreciated! at this point i'd be happy for a solution that i'd have to do each time i reboot. just anything that will work for when i go out of town and i can leave my VPN running

I think we are talking about different OS’s, I was referring to Windows

Okay. I tried JB90's solution for my RT-N66U running TomatoUSB. No luck there because I was routing Amazon EC2 IP Addresses and apparently the plex.tv website uses Amazon IP Addresses.

I have been trying to achieve Plex not running on the VPN and I think i might have a temporary workaround.

You first have to deactivate your VPN, and make sure Plex is running. 

Now Connect to MyPlex and once that is done, go to http://plex.tv/pms/:/ip and copy down your WAN IP.

Now, here comes the interesting part. You need either Nginx if you're on Linux, UniServer if you're on Windows or MAMP if you're on OS X.

What we wanna do now is that we wanna recreate the WAN IP Request page above and emulate that function and it's really simple since it's only returning an IP Address.

You gotta make the relevant directories inside your http folder serving the index.html. You will need to create the index.html with just your WAN IP in it. Nothing else.

Edit the /etc/hosts file and point plex.tv to your local ip address. Turn on the webserver and your plex media server should continue to work until you shut it down.

You can also turn on the VPN now.

This only works because it seems like the Plex Media Server is storing the IP somewhere locally on the OS itself and once it is stored, it only compares to the stored IP and if the request from plex.tv/pms/:/ip matches then it doesn't care to update your myplex configuration on the main site.

I'll post more detailed instructions for more clarification if this is hard for you to follow. Spend the past 6hours looking at logs and trying to get this to work. Tired.

I'm seeing if there's a better way of doing this, something like rewriting the html response by running a squid transparent proxy or something but I think this is the best workaround for now.

EDIT: I also should mention that this workaround is intended for people who are running their plex media server 24/7. You also wouldn't be able to connect to plex.tv on the server that you edited the hosts file. It's gonna be a pain in the ass to keep redoing these steps every single time you want to start plex media server.

You gotta make the relevant directories inside your http folder serving the index.html. You will need to create the index.html with just your WAN IP in it. Nothing else.

Edit the /etc/hosts file and point plex.tv to your local ip address. 

thanks for an alternative method! i would love a little bit more detail on this part quoted above using MAMP. i think i did the first part correctly (i put an index.html in the htdocs folder) but need help with the last line. thanks!

I wrote a windows batch file to route my.plexapp.com's current IP address (with mask 255.255.0.0) to your machine's default gateway (e.g. 192.168.2.1). I promise the bat file is safe, just google me and my reputation will speak for itself.

The code is below but I also uploaded a zip containing the bat file and an exe version of the bat file (same as the bat file except runs invisibly). For best results, use windows task scheduler to launch the exe (with highest privileges) as often as you'd like.

This will ensure that even if the IP for my.plexapp.com changes, your setup will automatically route it past your VPN. A list of routed IPs will be saved here:

"%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt

I'm using PIA and this works perfectly.

Enjoy!

XFlak

@echo off
setlocal
set PATH=%SystemRoot%\system32;%SystemRoot%\system32\wbem;%SystemRoot%
chcp 437>nul

echo my.plexapp.com VPN Route Exception Builder
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%

::set gateway=192.168.2.1

echo Getting my.plexapp.com’s current IP address
echo.

ping “my.plexapp.com”|findstr /I /C:“my.plexapp.com” >“%temp%\my.plexapp.com.txt”

set /p PlexIP= <“%temp%\my.plexapp.com.txt”
set PlexIP=%PlexIP:*[=%
set PlexIP=%PlexIP:~0,-24%
echo %PlexIP%
echo.

set zero=%PlexIP:.=%
set zero=%zero:
.=%
echo set PlexIP=%%PlexIP:%zero%=0.0%%>“%temp%\plex.bat”
call “%temp%\plex.bat”

::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, exiting…) & (goto:fin)
:skipcheck

echo route -p add %PlexIP% mask 255.255.0.0 %gateway%
route -p add %PlexIP% mask 255.255.0.0 %gateway%
echo.

echo Adding IP to %userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt
echo.
echo %PlexIP% >>“%userprofile%\AppData\Local\Plex Media Server\PermittedPlexIPs.txt”

:fin

::echo fin
::pause

exit

edit: code was missing a ">", old version still worked but log only retained latest IP, the 4 people who downloaded the last version may want to redownload the new one

I've got this setup, and when I run: router print

I can see the routes are successfully added. When I run tracert, on a normal domain, I can see it going through the VPN.  Then on the plex domain, I can see it going through my default gateway.  So far so good.

Next. If go to to a machine on my lcoal network: http://serverip:32400/web works.  Also, using http://externalip:32400/web also works.  

Now the problem.  I got to a machine on the outside of my network, and http://externalip:32400/web doesn't work.  If I disconnect the VPN on the server, it works. (I've setup port forward on the router for 32400). 

So, without the VPN connected on the server, http://externalip:32400/web works fine. Turn on the VPN Client, and http://externalip:32400/web stops working outside the network.  ideas?   

thanks for an alternative method! i would love a little bit more detail on this part quoted above using MAMP. i think i did the first part correctly (i put an index.html in the htdocs folder) but need help with the last line. thanks!

You need to recreate the directories in the htdocs folder. For example since the url is "pms/:/ip" The folder structure should be pms -> : -> ip which is pms/:/ip/index.html if you get what i mean.

However I'm still finding the perfect solution for this as i realise this breaks Plex from updating new metadata since it can't access HTTPS. I'm looking for a solution for that but currently am a little busy to test a proposed solution i have.

 

I've got this setup, and when I run: router print

I can see the routes are successfully added. When I run tracert, on a normal domain, I can see it going through the VPN.  Then on the plex domain, I can see it going through my default gateway.  So far so good.

Next. If go to to a machine on my lcoal network: http://serverip:32400/web works.  Also, using http://externalip:32400/web also works.  

Now the problem.  I got to a machine on the outside of my network, and http://externalip:32400/web doesn't work.  If I disconnect the VPN on the server, it works. (I've setup port forward on the router for 32400). 

So, without the VPN connected on the server, http://externalip:32400/web works fine. Turn on the VPN Client, and http://externalip:32400/web stops working outside the network.  ideas?   

Sounds to me like your traffic is still going out the VPN.

You need to recreate the directories in the htdocs folder. For example since the url is "pms/:/ip" The folder structure should be pms -> : -> ip which is pms/:/ip/index.html if you get what i mean.

thanks. os x won't let me create folder with the name ":" though

also - not sure how to "Edit the /etc/hosts file and point plex.tv to your local ip address. "with MAMP

posting to follow developments

thanks. os x won't let me create folder with the name ":" though

also - not sure how to "Edit the /etc/hosts file and point plex.tv to your local ip address. "with MAMP

You can't create the folder with the name ":" ? Let me try on my mac.

Just tried with my mac, You'll have to use Terminal to create the folder. "mkdir :"

You have to use Terminal. Open up terminal. type "open /etc"

EDIT:

Okay. I've been thinking about this for days but don't have an alternative solution to the one that i had proposed above. It works but I found it to be quite unstable also.

So all in all, i went around trying to find a complicated solution when static routes were the original solution.

I found that adding static routes seemed to fix the issue and i wrote up a shell script whilst borrowing code from JB90 to automatically add the routes.

Here's the script

#!/bin/sh

#Fetch Current Server Address for plex.tv
DNS=“$(dig plex.tv +short) $(dig @8.8.8.8 plex.tv +short) $(dig @209.244.0.3 plex.tv +short)”
UNIQ_IP=$(echo “$DNS” | tr ’ ’ ’
’ | sort -u | tr ’
’ ’ ')

for IP in $UNIQ_IP
do
        #Create localroute variable to see if route exists
        localroute=route | grep $IP
        #Check if route exists
        if [[ -z “$localroute” ]]; then
                #route doesn’t exist, add route
                route add -net “$IP” netmask 255.255.255.255 gw yourgatewayiphere
        else
                #route exists, hurray
                echo “yay”
        fi
done

EDIT 2:

Apparently the script above wasn't enough so I had to use this script below. Thanks to buhman on archlinux channel for helping and Grdnkln from linksysinfo.

You can place the script below in WAN UP or run it as a .sh

#!/bin/sh

This code goes in the WAN UP section of the Tomato GUI.

This code based on the contributions from this thread:

Route only specific ports through VPN (openvpn) | LinksysInfo.org

And from material in these articles:

10.4. Multiple Connections to the Internet

fedorasolved.org - fedorasolved Resources and Information.

This script configures “selective” VPN routing. Normally Tomato will route ALL traffic out

the OpenVPN tunnel. These changes to iptables allow some outbound traffic to use the VPN, and some

traffic to bypass the VPN and use the regular Internet instead.

To list the current rules on the router, issue the command:

iptables -t mangle -L PREROUTING

Flush/reset all the rules to default by issuing the command:

iptables -t mangle -F PREROUTING

First it is necessary to disable Reverse Path Filtering on all

current and future network interfaces:

for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do
echo 0 > $i
done

Delete and table 100 and flush any existing rules if they exist.

ip route flush table 100
ip route del default table 100
ip rule del fwmark 1 table 100
ip route flush cache
iptables -t mangle -F PREROUTING

Copy all non-default and non-VPN related routes from the main table into table 100.

Then configure table 100 to route all traffic out the WAN gateway and assign it mark “1”

NOTE: Here I assume the OpenVPN tunnel is named “tun11”.

ip route show table main | grep -Ev ^default | grep -Ev tun11
| while read ROUTE ; do
ip route add table 100 $ROUTE
done
ip route add default table 100 via $(nvram get wan_gateway)
ip rule add fwmark 1 table 100
ip route flush cache

Define the routing policies for the traffic. The rules will be applied in the order that they

are listed. In the end, packets with MARK set to “0” will pass through the VPN. If MARK is set

to “1” it will bypass the VPN.

EXAMPLES:

All LAN traffic will bypass the VPN (Useful to put this rule first, so all traffic bypasses the VPN and you can configure exceptions afterwards)

iptables -t mangle -A PREROUTING -i br0 -j MARK --set-mark 1

Ports 80 and 443 will bypass the VPN

iptables -t mangle -A PREROUTING -i br0 -p tcp -m multiport --dport 80,443 -j MARK --set-mark 1

All traffic from a particular computer on the LAN will use the VPN

iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range 192.168.1.2 -j MARK --set-mark 0

All traffic to a specific Internet IP address will use the VPN

iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 216.146.38.70 -j MARK --set-mark 0

All UDP and ICMP traffic will bypass the VPN

iptables -t mangle -A PREROUTING -i br0 -p udp -j MARK --set-mark 1

iptables -t mangle -A PREROUTING -i br0 -p icmp -j MARK --set-mark 1

#iptables -t mangle -A PREROUTING -i br0 -p tcp -m multiport --dport 32400 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -i br0 -s 192.168.5.2 -p tcp -m multiport --sport 32400 -j MARK --set-mark 1

I wrote a windows batch file to route my.plexapp.com's current IP address (with mask 255.255.0.0) to your machine's default gateway (e.g. 192.168.2.1). I promise the bat file is safe, just google me and my reputation will speak for itself.

The code is below but I also uploaded a zip containing the bat file and an exe version of the bat file (same as the bat file except runs invisibly). For best results, use windows task scheduler to launch the exe (with highest privileges) as often as you'd like

Extreme thanks XFlak

Works great for me, though I had to run it twice before Plex published (also have to manually specify port 32400 within plex settings, hope that is default behavior)

Now I get to use PIAs client app on my server and still run Plex :) Can't thank you enough

I wrote a windows batch file to route my.plexapp.com's current IP address (with mask 255.255.0.0) to your machine's default gateway (e.g. 192.168.2.1). I promise the bat file is safe, just google me and my reputation will speak for itself.

This is amazing.  Thank you so much for this.  I am going to convert it to exe and schedule it to run every hour but for now everything is working great.   Thanks again.

I now have Private Internet Access but my plex folder is not in the default location. Any one know what I should modify so I can still use Plex remotely.

I have tried XFlak's script for a few days now scheduled every hour, cycling my router on and off, my computer on and off, purging my router tables, starting PIA first then plex and vice versa, but I get the same problem every time. Port 32400 has been manually specified in Plex and forwarded in my router.  I launch the exe or bat with admin privileges.

As soon as I turn on PIA, Plex becomes inaccessible.  Anyone else having this problem?  Am I doing something wrong?  Any help greatly appreciated!!

Note that my plex folder is not in the default location because my OS SSD was too full and I moved it to another HD.

I don't think the script will work for us  since we are not using the default location. Im waiting to find out what lines need to be changed to point it to my actual location.

So I think I found a fix for me and it had nothing to do with the location of my library.  I modified the script to use a subnet mask of 255.255.255.0 and instead of zeroing the last 2 numbers, I kept them as they were.  Now everything appears to be working.  Never understood why they were zeroed in the first place - anyone know?

EDIT: Here's another thread for people with this problem: https://forums.plex.tv/topic/84556-myplex-with-vpn/

1 Like

XFlak,

Thank you for the script.  I've run it and tested external and internal communication to plex through the clients and everything appears to be working.  The only thing that is odd is that the server connection setup under settings states that I'm signed into plex but plex cannot reach me externally. Picture below:

![post-117360-0-97951000-1402368549.jpg|690x388](upload://fS8OZUU6jXZPpwHkS8cN0JfJrwI.jpg)

Edit:  Eventually Plex stops communicating externally, within about 15 minutes.  Plex must do some level of caching of the library.