Advertisement
Guest User

port_forward.sh

a guest
Jan 31st, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.14 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. #
  4. #
  5. # If you would prefer not to use a hash of your username & machine os+version for the client ID, please
  6. # uncomment below and type in anything you like, but it MUST BE UNIQUE
  7. #CLIENT_ID="replace with anything you like"
  8. #
  9. # Uncomment below and change to your interface if you don't want the script to try to work it out
  10. #INTERFACE="tun0"
  11. #
  12. # if you have a different url to check port status then place it here, the port will be appended to the end of the URI
  13. #PORTCHECKURI="http://example.xyz/pc.php?p="
  14.  
  15. PROGRAM=`basename $0`
  16. VERSION=1.0
  17. CURL_TIMEOUT=4
  18. USE_IP=0
  19. USE_SUM=0
  20. SILENT=1
  21. TESTPORT=1
  22.  
  23.  
  24. # Check commands we need exist if not found, set alternatives if we have them.
  25. for cmd in awk sed curl ip shasum; do
  26. if ! command -v $cmd > /dev/null; then
  27. case "$cmd" in
  28. 'ip')
  29. if ! command -v ifconfig > /dev/null; then
  30. echo "command 'ip' and 'ifconfig' not found, please check path or install one of them"
  31. exit 1
  32. fi
  33. USE_IP=1
  34. ;;
  35. 'shasum')
  36. if ! command -v md5sum > /dev/null; then
  37. if ! command -v md5 > /dev/null; then
  38. echo "commands 'shasum', 'md5sum' and 'md5' are not found, please check path or install one of them"
  39. exit 1
  40. fi
  41. USE_SUM=2
  42. else
  43. USE_SUM=1
  44. fi
  45. ;;
  46. *)
  47. echo "command '$cmd' not found, please check path or install"
  48. exit 1
  49. ;;
  50. esac
  51. fi
  52. done
  53.  
  54.  
  55. error( )
  56. {
  57. echo "$@" 1>&2
  58. exit 1
  59. }
  60.  
  61. error_and_usage( )
  62. {
  63. echo "$@" 1>&2
  64. usage_and_exit 1
  65. }
  66.  
  67. usage( )
  68. {
  69. echo "Usage: `dirname $0`/$PROGRAM <user> <password> (optional parameters)"
  70. echo " `dirname $0`/$PROGRAM --file <credentials filename> (optional parameters)"
  71. echo ""
  72. echo " <credentials filename> - path to plane text file containing PIA credentials"
  73. echo " 1st line of file is username, 2nd line is passwd"
  74. echo " can use same file you use for openvpn credentials"
  75. echo " optional parameter(s)"
  76. echo " -t | --testport <run an external test on port>"
  77. echo " -s | --silent <silent, print port and nothing else>"
  78. echo ""
  79. }
  80.  
  81. usage_and_exit( )
  82. {
  83. usage
  84. exit $1
  85. }
  86.  
  87. version( )
  88. {
  89. echo "$PROGRAM version $VERSION"
  90. }
  91.  
  92. port_forward_assignment( )
  93. {
  94. if [ $USE_IP -eq 0 ]; then
  95. if [ -z "${INTERFACE}" ];then
  96. INTERFACE=`ip addr show | awk 'BEGIN{FS=":"} /POINTOPOINT/ {gsub(/^[ \t]+/, "", $2); gsub(/[ \t]+$/, "", $2); print $2}'`
  97. if [ -z "${INTERFACE}" ];then
  98. echo "ERROR: Can't find VPN interface, please edit script and hardcode it"
  99. exit 1
  100. fi
  101. fi
  102. IPADDRESS=`ip addr show $INTERFACE | awk '/inet / {print $2}'`
  103. else
  104. if [ -z "${INTERFACE}" ];then
  105. INTERFACE=`ifconfig | awk 'BEGIN {RS="\n\n"} /POINTTOPOINT/ {print $1}'`
  106. if [ -z "${INTERFACE}" ];then
  107. echo "ERROR: Can't find VPN interface, please edit script and hardcode it"
  108. exit 1
  109. fi
  110. fi
  111. IPADDRESS=`ifconfig $INTERFACE | awk '/inet / {print $2}' | awk 'BEGIN { FS = ":" } {print $(NF)}'`
  112. fi
  113.  
  114. if [ -z "${CLIENT_ID}" ];then
  115. CLIENT_ID="$USER `uname -v`"
  116. fi
  117.  
  118. # Encode the string with md5 or sha, then delete every 'f' in the string, this will make a UUID that's constantly
  119. # generated, doesn't have to be stored anywhere, and can't be decrypted.
  120.  
  121. if [ $USE_SUM -eq 0 ]; then
  122. CLIENT_ID=`echo $CLIENT_ID | shasum | awk '{gsub("f","",$1); print $1}'`
  123. else
  124. if [ $USE_SUM -eq 1 ]; then
  125. CLIENT_ID=`echo $CLIENT_ID | md5sum | awk '{gsub("f","",$1); print $1}'`
  126. else
  127. CLIENT_ID=`echo $CLIENT_ID | md5 | awk '{gsub("f","",$1); print $1}'`
  128. fi
  129. fi
  130.  
  131. json=`curl -m $CURL_TIMEOUT --silent --interface $INTERFACE -d "user=$USER&pass=$PASSWORD&client_id=$CLIENT_ID&local_ip=$IPADDRESS" 'https://www.privateinternetaccess.com/vpninfo/port_forward_assignment' | head -1`
  132. }
  133.  
  134.  
  135. print_vpn_information()
  136. {
  137. if [ $SILENT -ne 0 ];then echo "Using VPN connection on interface $INTERFACE..."; fi
  138.  
  139. externalIP=`curl -m $CURL_TIMEOUT --interface $INTERFACE "http://ipinfo.io/ip" --silent --stderr -`
  140. if [ $SILENT -ne 0 ];then echo "VPN Internal IP = $IPADDRESS";fi
  141.  
  142. if port=`echo $json | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}'`; then
  143. if [ ! -z "${port##*[!0-9]*}" ]; then
  144. if [ $SILENT -ne 0 ];then echo "VPN External IP:Port = $externalIP:$port";else echo $port;fi
  145. else
  146. if [ $SILENT -ne 0 ];then echo "VPN External IP = $externalIP (port forwarding is disabled on pia server, or port returned from pia is invalid)";fi
  147. if [ $SILENT -ne 0 ];then echo " *** pia returned invalid port '$port' ***";fi
  148. fi
  149. else
  150. if [ $SILENT -ne 0 ];then echo "Error assigning port forward in pia";fi
  151. echo $json
  152. fi
  153.  
  154. if [ $TESTPORT -eq 0 -a $SILENT -ne 0 ]; then
  155. echo 'Checking incomming connections...'
  156. if [ -z "${PORTCHECKURI}" ];then
  157. echo '*** WARNING Using 3rd pary to check, do not run this often as it will detect a bot and send a capatcha ***'
  158. status=`curl -m $CURL_TIMEOUT "http://ports.yougetsignal.com/check-port.php" -H "Origin: http://www.yougetsignal.com" --data "remoteAddress=$externalIP&portNumber=$port" --compressed --silent --stderr - | sed -e 's/<[^>]*>//g'`
  159. else
  160. status=`curl -s -m $CURL_TIMEOUT "$PORTCHECKURI$port"`
  161. status="Port $port is $status on $externalIP"
  162. fi
  163. echo $status
  164. fi
  165. }
  166.  
  167.  
  168. # By default assume first two parameters are user and passwd.
  169. USER=$1
  170. PASSWORD=$2
  171.  
  172. for prm in "$@"; do
  173. case $prm in
  174. --usage | --help | -h )
  175. usage_and_exit 0
  176. ;;
  177. --version | -v )
  178. version
  179. exit 0
  180. ;;
  181. --file | -f )
  182. if [ -z "${2}" ]; then
  183. usage_and_exit 0
  184. fi
  185. USER=`sed '1q;d' $2`
  186. PASSWORD=`sed '2q;d' $2`
  187. ;;
  188. --testport | -t )
  189. TESTPORT=0
  190. ;;
  191. --silent | -s )
  192. SILENT=0
  193. TESTPORT=1
  194. ;;
  195. esac
  196. done
  197.  
  198. if [ -z "${USER}" ] || [ -z "${PASSWORD}" ]; then
  199. usage_and_exit 0
  200. fi
  201.  
  202. port_forward_assignment
  203. print_vpn_information
  204.  
  205. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement