Advertisement
winstonSmith84

hma.sh

Feb 18th, 2014
2,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.76 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ $# -lt 1 ]; then
  4.    cat <<EOF
  5.    __ ____  ______   __  ___             _   _____  _  __
  6.   / // /  |/  / _ | / / / _ \_______    | | / / _ \/ |/ /
  7.  / _  / /|_/ / __ |/_/ / ___/ __/ _ \   | |/ / ___/    /
  8. /_//_/_/  /_/_/ |_(_) /_/  /_/  \___/   |___/_/  /_/|_/
  9.  
  10. Usage:
  11.     $0 [-l] [-p tcp|udp] [server name]
  12.  
  13. Parameters:
  14.     [server name] - this is grep pattern by which the script will filter server list and connect to random of any servers which matched the pattern
  15.     -l            - if specified, instead of connecting, the script will output server list matched with the grep pattern above
  16.     -p tcp|udp    - sets preferred protocol, default is OpenVPN UDP
  17.  
  18. Examples:
  19.     $0 -l "New York"  - lists all servers in New York
  20.     $0 -l -p udp      - lists all servers with OpenVPN UDP support
  21.     $0 -p tcp Texas   - connects to a random Texas server using OpenVPN TCP protocol
  22. EOF
  23.  
  24.    exit -2
  25. fi
  26.  
  27. cd `dirname $0`
  28.  
  29. curl=`which curl`
  30. if [ "$curl" == "" ]; then
  31.   curl=`which wget`
  32.   if [ "$curl" == "" ]; then
  33.     echo <<EOF
  34. Error: Please install curl or wget for this script to work.
  35. You can try any of the following commands:
  36. apt-get install wget
  37. yum install wget
  38. apt-get install curl
  39. yum install curl
  40. EOF
  41.     exit 1
  42.   else
  43.     curl="$curl -T 5 -O - "
  44.   fi
  45. else
  46.   curl="$curl --connect-timeout 5 -s"
  47. fi
  48.  
  49. openvpn=`which openvpn`
  50. if [ "$openvpn" == "" ] ; then
  51.     cat <<EOF
  52. Error: Please install openvpn for this script to work.
  53. You can try any of the following commands:
  54. apt-get install openvpn
  55. yum install openvpn
  56. EOF
  57.     exit 1
  58. fi
  59.  
  60. proto=
  61. list=0
  62. while getopts "lp:" parm
  63. do
  64.     case $parm in
  65.     l)
  66.         list=1
  67.         ;;
  68.     p)
  69.         proto="$OPTARG"
  70.         ;;
  71.     ?)  echo "unknown $parm / $OPTARG"
  72.     esac
  73. done
  74.  
  75. shift $(( $OPTIND - 1 ))
  76. grep="$*"
  77. names=( )
  78. ips=( )
  79. tcps=( )
  80. udps=( )
  81.  
  82. count=0
  83.  
  84. echo "Obtaining list of servers..."
  85. $curl https://securenetconnection.com/vpnconfig/servers-cli.php 2>/dev/null| grep -i -e "$grep" | grep -i -e "$proto" > /tmp/hma-servers
  86. exec < /tmp/hma-servers
  87. rm /tmp/hma-servers
  88.     while read server
  89.     do
  90.         : $(( count++ ))
  91.         ips[$count]=`echo "$server"|cut -d '|' -f 1`
  92.         udps[$count]=`echo "$server"|cut -d '|' -f 5`
  93.         tcps[$count]=`echo "$server"|cut -d '|' -f 4`
  94.         names[$count]=`echo "$server"|cut -d '|' -f 2`
  95.  
  96.     done
  97.  
  98. if [ "$count" -lt 1 ] ; then
  99.     echo "No matching servers to connect: $grep"
  100.     exit
  101. else
  102.     echo "$count servers matched"
  103. fi
  104.  
  105. if [ $list -eq 1 ]; then
  106.     for i in `seq 1 $count`; do
  107.         echo -e "${names[$i]}\t${ips[$i]}\t${tcps[$i]}\t${udps[$i]}"
  108.     done
  109.     exit
  110. fi
  111.  
  112.  
  113. i=$(( $RANDOM%$count + 1 ))
  114. echo "Connecting to:"
  115. echo -e "${names[$i]}\t${ips[$i]}"
  116. if [ "$proto" == "" ]; then
  117.     if [ "$udps[$i]" != "" ]; then
  118.         proto=udp
  119.     else
  120.         proto=tcp
  121.     fi
  122. fi
  123.  
  124. if [ "$proto" == "tcp" ]; then
  125.     port=443
  126. else
  127.     port=53
  128. fi
  129.  
  130. echo "Loading configuration..."
  131. $curl "https://securenetconnection.com/vpnconfig/openvpn-template.ovpn" > /tmp/hma-config.cfg 2>/dev/null
  132.  
  133. echo "remote ${ips[$i]} $port" >> /tmp/hma-config.cfg
  134. echo "proto $proto" >> /tmp/hma-config.cfg
  135.  
  136. #sudo $openvpn --config /tmp/hma-config.cfg
  137. #rm /tmp/hma-config.cfg
  138. cat <<EOF > /tmp/hma-routeup.sh
  139. #!/bin/sh
  140. cat <<EOEO >> /tmp/hma-ipcheck.txt
  141.  *******************************************
  142. *                                           *
  143. *   You are now connected to HMA Pro! VPN   *
  144. *                                           *
  145.  *******************************************
  146.  
  147. Checking new IP address...
  148. EOEO
  149. nohup /tmp/hma-ipcheck.sh >/dev/null 2>&1 &
  150. rm /tmp/hma-routeup.sh
  151. EOF
  152.  
  153. cat <<EOF > /tmp/hma-ipcheck.sh
  154. #!/bin/sh
  155. ip=""
  156. attempt=0
  157. while [ "\$ip" = "" ]; do
  158.     attempt=\$((\$attempt+1))
  159.     ip="\`$curl http://geoip.hidemyass.com/ip/ 2>/dev/null\`"
  160.     if [ "\$ip" != "" ]; then
  161.             echo "Your IP is \$ip" >> /tmp/hma-ipcheck.txt
  162.     fi
  163.     if [ \$attempt -gt 3 ]; then
  164.         echo "Failed to load IP address." >> /tmp/hma-ipcheck.txt
  165.         exit
  166.     fi
  167. done
  168.  
  169. EOF
  170. echo "" > /tmp/hma-ipcheck.txt
  171. tail -f /tmp/hma-ipcheck.txt &
  172. chmod 755 /tmp/hma-ipcheck.sh
  173. chmod 755 /tmp/hma-routeup.sh
  174. /tmp/hma-ipcheck.sh
  175. sleep 1
  176.  
  177.  
  178. # MODIFICATION BY PETE
  179.  
  180. #!/bin/bash
  181.  
  182. pwfile="password.txt"
  183.  
  184. if [ -f "$pwfile" ]
  185. then
  186. echo "Login details found in password.txt"
  187. else
  188. echo "No Login details found. Please enter:"
  189.  
  190. read -p "Username: " vpnuser < /dev/tty
  191. echo $vpnuser > password.txt
  192.  
  193. read -p "Password: " vpnpass < /dev/tty
  194. echo $vpnpass >> password.txt
  195.  
  196. fi
  197.  
  198. sleep 1
  199.  
  200. # adding to config file that password.txt should be used
  201. sed -i 's/auth-user-pass/auth-user-pass password.txt/g' /tmp/hma-config.cfg
  202.  
  203. # END OF MODIFICATIONS
  204.  
  205.  
  206.  
  207.  
  208.  
  209. sudo $openvpn --script-security 3 --route-up /tmp/hma-routeup.sh --verb 2 --config /tmp/hma-config.cfg
  210. rm /tmp/hma-config.cfg
  211. rm /tmp/hma-ipcheck.sh
  212. rm /tmp/hma-routeup.sh 2>/dev/null
  213. rm /tmp/hma-ipcheck.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement