Advertisement
Guest User

pia-port.sh

a guest
Jul 18th, 2016
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #!/bin/sh
  2. export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
  3.  
  4. # Private Internet Access Advanced Port Forward Script for pfSense
  5. # v1.0 (21st January 2014)
  6. # v2.0 Code for pfSense 2.3.1_1 (30th May 2016) - by Grehund
  7.  
  8. # Pre-requisites for this version of the script:
  9. # pfSense v2.3.1
  10. # curl - pkg install curl
  11. # xmlstarlet - pkg install xmlstarlet
  12.  
  13. # Add your PIA username and password
  14. USERNAME="username"
  15. PASSWORD="password"
  16. PIACLIENTID=/cf/conf/pia_client_id
  17. CONFFILE=/cf/conf/config.xml
  18.  
  19. # Check to see if we have a valid PIA Client ID file.
  20. # If not, create one. Linux is included for illustration only.
  21. if [ ! -e $PIACLIENTID ]; then
  22. # OSX/FreeBSD (pfSense)
  23. head -n 100 /dev/urandom | md5 > $PIACLIENTID
  24.  
  25. # Linux
  26. #head -n 100 /dev/urandom | md5sum | tr -d " -" > $PIACLIENTID
  27.  
  28. logger "pia-port: Created new PIA Client ID."
  29. fi
  30.  
  31. # Find out the tunnelling device for your VPN and get your IP address.
  32. # There are several options presented here. Personally, I prefer to use
  33. # the interface which I know relates to my VPN tunnel for forwarding.
  34.  
  35. #DEVICE=`ifconfig | grep -o "tun[0-9]"`
  36. #LOCAL_IP=`ifconfig $DEVICE | grep -Po "(?<=addr.)[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`
  37. LOCAL_IP=`ifconfig ovpnc2 | grep inet | awk 'NR>1 {print $2}'`
  38.  
  39. # Get the port number for the forwarded port
  40. PORT=`curl -d "user=$USERNAME&pass=$PASSWORD&client_id=$(cat $PIACLIENTID)&local_ip=$LOCAL_IP" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment`
  41.  
  42. PORTNUM=`echo $PORT | grep -oE "[0-9]+"`
  43.  
  44. # Some error detection. If PORTNUM is longer than 5 characters, we know that
  45. # an error has been returned. We log it to syslog, and exit.
  46. len=`expr $PORTNUM : '.*'`
  47. echo $len
  48.  
  49. if [ $len -gt 5 ]; then
  50. logger "pia-port: $PORTNUM"
  51. exit 0
  52. fi
  53.  
  54. logger "pia-port: Port number acquired: $PORTNUM"
  55.  
  56. # Get current NAT port number using xmlstarlet to parse the config file.
  57. CURPORT=`xml sel -t -v '//rule[descr="NAT Torrent"]/destination/port' $CONFFILE`
  58.  
  59. logger "pia-port: Current port forward: $CURPORT"
  60.  
  61. # The port mapping doesn't always change.
  62. # We don't want to force pfSense to re-read it's config if we don't need to.
  63. if [ "$CURPORT" = "$PORTNUM" ]; then
  64. logger "pia-port: Port not changed. Exiting."
  65. exit 0
  66. fi
  67.  
  68. # Port forward has changed, so we update the rules in the config file.
  69. xml ed -u '//rule[descr="Torrent"]/destination/port' -v $PORTNUM -u '//rule[descr="Torrent"]/local-port' -v $PORTNUM -u '//rule[descr="NAT Torrent"]/destination/port' -v $PORTNUM $CONFFILE > /tmp/config.pia
  70.  
  71. # Put the config file in the correct location.
  72. cp /tmp/config.pia $CONFFILE
  73.  
  74. # Create a file in the pfSense web server root that contains the current port.
  75. # This can then be read by other hosts in order to update the open port in
  76. # whatever torrent client is in use.
  77. echo $PORTNUM > /usr/local/www/pia_port.txt
  78.  
  79. # Force pfSense to re-read it's config
  80. rm /tmp/config.cache
  81.  
  82. logger "pia-port: New port number ($PORTNUM) inserted into config file."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement