Advertisement
Nordenkult

Untitled

Aug 8th, 2016
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.47 KB | None | 0 0
  1. SCRIPT 1 -- Connexion au meilleur serveur mais query du login pass et TCP / UDP
  2.  
  3.  
  4.  
  5. #!/bin/bash
  6.  
  7. scriptversion="0.23"
  8. serverlist="/tmp/serverlist.txt"
  9.  
  10. # Save prompt settings, OpenVPN tends to brick the prompt
  11. sttysettings=$(stty -g)
  12.  
  13. trap ctrl_c INT
  14.  
  15. # In case ctrl+c is pressed, restore terminal color and prompt settings, then exit
  16. function ctrl_c()
  17. {
  18. echo
  19. echo "Exiting script..."
  20. echo
  21. tput sgr0
  22. stty $sttysettings
  23. exit 0
  24. }
  25.  
  26. # Function to show usage (available switches)
  27. function showhelp()
  28. {
  29. echo "- Available switches:"
  30. echo " $0 -h = Show help"
  31. echo " $0 -d = Daemonize OpenVPN"
  32. echo " $0 -f = tests for and connects you to the fastest server"
  33. echo " $0 -t = tests for and shows top 10 fastest servers"
  34. echo " $0 -s = Shows OpenVPN status & IP"
  35. echo " $0 -u = Updates this script"
  36. echo
  37. tput bold
  38. echo "- This script requires the following packages:"
  39. echo " curl, openvpn, wget, dialog"
  40. echo
  41. tput sgr0
  42. }
  43.  
  44. # Function to execute server latency test
  45. function pingtest
  46. {
  47. rm /tmp/pingtest* 2> /dev/null
  48. echo
  49. # If not fping package avail., advise to install and exit
  50. if [[ $(which fping) == "" ]] ; then
  51. echo "'fping' package not found!"
  52. echo "Please install (apt-get install fping)"
  53. echo
  54. exit
  55. fi
  56.  
  57. echo -n "Testing all servers for latency using fping"
  58. # Download serverlist
  59. curl -s -k https://www.hidemyass.com/vpn-config/l2tp/ > $serverlist
  60.  
  61. # How many servers do we have? (line count of serverlist)
  62. servercount=$(wc -l $serverlist | awk '{print $1}')
  63. i=1
  64.  
  65. # Extract server IPs and names from serverlist
  66. while read line; do
  67. serverip=$(echo $line | awk '{print $1}')
  68. servername=$(echo $line | awk '{$1="";print $0}')
  69. # Test and save average latency for each server IP
  70. avg=$(fping -B 1.0 -t 300 -i 1 -r 0 -e -c 1 -q $serverip 2>&1 | awk -F'/' '{print $8}')
  71. # Save servername and latency to temp. result file
  72. echo "$servername = $avg" >> /tmp/pingtest.txt
  73.  
  74. # Calculate percentage of server testing process
  75. percentage=$((($i*100)/$servercount));
  76. echo -ne "Testing all servers for latency using fping ($i \ $servercount) $percentage % \033[0K\r"
  77. i=$((i+1))
  78. done < $serverlist
  79.  
  80. # Re-order and sort latency test results, save to new temp result file
  81. cat /tmp/pingtest.txt | awk -F[=] '{ t=$1;$1=$2;$2=t;print; }' | sort -n > /tmp/pingtest.txt.2
  82.  
  83. # Save all ping result file lines to final result file as long as they start with a ping value
  84. while read line; do
  85. firstcol=$(echo $line | awk '{print $1}')
  86. re='^[0-9]+([.][0-9]+)?$'
  87.  
  88. if [[ $firstcol =~ $re ]] ; then
  89. echo $line >> /tmp/pingtest.best.txt
  90. fi
  91. done < /tmp/pingtest.txt.2
  92.  
  93. echo
  94. # If we're not supposed to connect to VPN, just print top 10 servers
  95. if [[ ! "$1" == "connect" ]] ; then
  96. echo -e "\nTop 10 Servers by latency (ping)"
  97. echo "================================"
  98. cat /tmp/pingtest.best.txt | sort -n | head -10
  99. echo
  100. exit
  101. else
  102. # If we're supposed to connect to VPN, save fastest server for later
  103. fastestserver=$(cat /tmp/pingtest.best.txt | head -1 | awk '{$1="";print}' | sed -e 's/^[[:space:]]*//')
  104. echo -e "Fastest server: $fastestserver\n"
  105. sleep 3
  106. fi
  107. }
  108.  
  109. # Function to check external IP
  110. function checkip()
  111. {
  112. ip=""
  113. attempt=0
  114. while [ "$ip" = "" ]; do
  115. attempt=$(($attempt+1))
  116. ip=`curl http://geoip.hmageo.com/ip/ 2>/dev/null`
  117. if [ "$ip" != "" ]; then
  118. if [ ! "$1" == "silent" ] ; then echo "- Current IP: $ip" ; fi
  119. fi
  120. if [ $attempt -gt 3 ]; then
  121. if [ ! "$1" == "silent" ] ; then echo "- Failed to check current IP address." ; fi
  122. exit
  123. fi
  124. done
  125. }
  126.  
  127. function updatenow
  128. {
  129. echo -e "\n[ HMA! OpenVPN Script v$scriptversion - http://hmastuff.com/linux-cli ]\n\nChecking for new version..."
  130. rm /tmp/hma-openvpn.sh 2> /dev/null
  131. # Download hosted script version to temp file
  132. curl -s -k https://hmastuff.com/linux/hma-openvpn.sh > /tmp/hma-openvpn.sh
  133. if [[ -f "/tmp/hma-openvpn.sh" ]] ; then
  134. # Extract script version from top of downloaded script
  135. updateversion=$(grep -m 1 'scriptversion=' /tmp/hma-openvpn.sh | awk -F'\042' '$0=$2')
  136. # If extracting script version failed, download of script must have failed. Advise and exit
  137. if [[ "$updateversion" = "" ]] ; then
  138. echo -e "Unable to check for new version.\nPlease check your internet connectivity or try again later.\n"
  139. exit 1
  140. fi
  141.  
  142. # If hosted script version is newer than this script's version, replace this script
  143. if [[ $scriptversion < $updateversion ]] ; then
  144. echo "Updating v$scriptversion to v$updateversion ... "
  145. chmod +x /tmp/hma-openvpn.sh && mv /tmp/hma-openvpn.sh .
  146. echo "Done!"
  147. else
  148. echo -e "Already latest version. (v$scriptversion)\n"
  149. fi
  150.  
  151. fi
  152. exit 0
  153. }
  154.  
  155.  
  156. # If no su privileges available, try to get them
  157. if [[ ! "$(whoami)" == "root" ]] ; then
  158. echo -e "\nHMA! OpenVPN Script v$scriptversion ]"
  159.  
  160. # No sudo available? Then we can't get su privs. Advise and exit
  161. if [[ $(which sudo) == "" ]] ; then
  162. echo "'sudo' package missing! Please install."
  163. echo "e.g.: apt-get install sudo"
  164. exit 1
  165. fi
  166.  
  167. echo "Requesting su permissions..."
  168. # Run this script with sudo privs
  169. sudo $0 $*
  170. # If running this script with su privs failed, advise to do so manually and exit
  171. if [[ $? > 0 ]] ; then
  172. echo
  173. echo "Acquiring su permission failed!"
  174. echo "Please run this script with sudo permissions!"
  175. echo "(e.g. 'sudo $0' or 'sudo bash $0')"
  176. echo
  177. exit 1
  178. fi
  179. exit 0
  180. fi
  181.  
  182. # Check for which parameters this script was run with, act accordingly
  183. while getopts "tfdhsu" parm
  184. do
  185. case $parm in
  186. f) pingtest connect
  187. ;;
  188.  
  189. t) pingtest
  190. ;;
  191.  
  192. d) daemonize=1
  193. ;;
  194. s) if [ -z "$(pidof openvpn)" ] ; then
  195. echo -e "\n- OpenVPN is not running!"
  196. else
  197. echo -e "\n- OpenVPN is running."
  198. fi
  199. checkip
  200. echo
  201. exit 0
  202. ;;
  203. u) updatenow
  204. ;;
  205. ?) echo -e "\nHMA! OpenVPN Script v$scriptversion"
  206. echo -e "==================\n"
  207. showhelp
  208. exit 0
  209. ;;
  210. esac
  211. done
  212.  
  213.  
  214. showtitle() {
  215. clear
  216. green="\e[36;5;82m"
  217. stdcol="\e[0m"
  218. echo -e "\n$green =============================="
  219. echo " | __ ____ ______ __ |"
  220. echo " | / / / / |/ / | / / |"
  221. echo " | / /_/ / /|_/ / /| | / / |"
  222. echo " | / __ / / / / ___ |/_/ |"
  223. echo " | /_/ /_/_/ /_/_/ |_(_) |"
  224. echo " | |"
  225. echo " | HMA! OpenVPN Script v$scriptversion |"
  226. echo -e " ==============================\n"
  227. echo "-> https://hidemyass.com/vpn"
  228. echo -e "-> https://support.hidemyass.com$stdcol\n"
  229. }
  230.  
  231. showtitle
  232. showhelp
  233.  
  234. # Check what package managers are available, yum or apt-get. If both, use apt-get
  235. pkgmgr=""
  236. if [[ ! $(which yum) == "" ]] ; then
  237. pkgmgr="yum install"
  238. fi
  239. if [[ ! $(which pacman) == "" ]] ; then
  240. pkgmgr="pacman"
  241. fi
  242. if [[ ! $(which apt-get) == "" ]] ; then
  243. pkgmgr="apt-get install"
  244. fi
  245.  
  246. # Function to check for and install needed packages
  247. function checkpkg
  248. {
  249. if [[ $(which $1) == "" ]] ; then
  250. echo -n "Package '$1' not found! Attempt installation? (y/n) "
  251. read -n1 answer
  252. echo
  253. case $answer in
  254. y) $pkgmgr $1
  255. ;;
  256. n) echo -n "Proceed anyway? (y/n) "
  257. read -n1 answer2
  258. echo
  259. if [[ "$answer2" == "n" ]] ; then exit
  260. fi
  261. ;;
  262. esac
  263. fi
  264. }
  265.  
  266. # Check for all needed packages
  267. checkpkg curl
  268. checkpkg wget
  269. checkpkg openvpn
  270. checkpkg dialog
  271. checkpkg fping
  272.  
  273. sleep 4
  274. echo "- Getting HMA! Pro VPN serverlist..."
  275. sleep 1
  276.  
  277. # Download serverlist to temp file
  278. curl -k -s https://www.hidemyass.com/vpn-config/l2tp/ > $serverlist
  279.  
  280. # If no fastest server was specified, ask user to select a server via dialog
  281. if [[ "$fastestserver" == "" ]] ; then
  282.  
  283. LINES=$(cat $serverlist | awk -F'\t' '{ print $2,"\t",$1 }')
  284.  
  285. IFS=$'\n\t'
  286. dialog --backtitle "HMA! OpenVPN Script" \
  287. --title "Select a server" \
  288. --menu "Select a server" 17 90 15 $LINES 2>/tmp/server
  289.  
  290. response=$?
  291. if [ $response == 255 ] || [ $response = 1 ]; then
  292. ctrl_c
  293. fi
  294.  
  295. unset IFS
  296.  
  297. clear
  298. # Set chosen server as connection target
  299. hmaservername=$(cat /tmp/server | sed 's/ *$//')
  300. hmaserverip=$(grep "$hmaservername" $serverlist | awk '{ print $1 }')
  301.  
  302. else
  303. # If a fastest server was specified, use that as connection target
  304. hmaservername=$fastestserver
  305. hmaserverip=$(grep "$fastestserver" $serverlist | awk '{ print $1 }')
  306.  
  307. fi
  308.  
  309. sleep 1
  310. clear
  311.  
  312. # Ask user if he wants to connect via OpenVPN-TCP or OpenVPN-UDP
  313. dialog --backtitle "HMA! OpenVPN Script" \
  314. --title "Select OpenVPN protocol to use" \
  315. --yes-label "UDP" --no-label "TCP" --yesno "Which protocol do you want to use?" 6 40
  316. response=$?
  317. case $response in
  318. 0) echo "udp" > /tmp/hma-proto ;;
  319. 1) echo "tcp" > /tmp/hma-proto ;;
  320. 255) ctrl_c ;;
  321. esac
  322.  
  323. sleep 1
  324. clear
  325.  
  326. showtitle
  327. hmaproto=`cat /tmp/hma-proto | tr '[:upper:]' '[:lower:]'`
  328. echo "- Getting .ovpn template..."
  329. echo
  330. sleep 1
  331. # Download *.ovpn template, then add chosen protocol and server IP to it
  332. wget -O /tmp/hma-template.ovpn http://zdcdn.hidemyass.com/other/hma-template.ovpn >/dev/null 2>&1
  333.  
  334. echo "proto $hmaproto" >> /tmp/hma-template.ovpn
  335.  
  336. if [ "$hmaproto" == "udp" ]; then
  337. echo "remote $hmaserverip 53" >> /tmp/hma-template.ovpn
  338. hmaport=53
  339. fi
  340. if [ "$hmaproto" == "tcp" ]; then
  341. echo "remote $hmaserverip 443" >> /tmp/hma-template.ovpn
  342. hmaport=443
  343. fi
  344.  
  345. checkip
  346. sleep 1
  347.  
  348. echo -e "\n- Starting OpenVPN connection to:"
  349. echo " $hmaservername - $hmaserverip : $hmaport ($hmaproto) ..."
  350. echo -e " (Please enter your HMA! Pro VPN account username and password when asked)\n"
  351.  
  352. sleep 1
  353.  
  354. # If we're supposed to run as daemon, run OpenVPN in daemon mode as well
  355. if [ "$daemonize" == "1" ]; then
  356. openvpn --daemon --script-security 3 --config /tmp/hma-template.ovpn
  357. echo -n -e "\n - Waiting for connection process to complete.."
  358. sleep 5
  359.  
  360. oldip=$ip
  361. ipattempt=0
  362. while [ "$ipattempt" -lt "5" ]; do
  363. ipattempt=$(($ipattempt+1))
  364. echo -n "."
  365. checkip silent
  366. if [ ! "$ip" == "$oldip" ] ; then
  367. echo -e "\n - IP has changed ($oldip -> $ip)"
  368. echo " Connection successful."
  369. ipattempt=5
  370. fi
  371. sleep 5
  372. done
  373.  
  374. if [ "$ip" == "$oldip" ] ; then
  375. echo -e "\nIP has not changed! Please check for possible network problems."
  376. killall openvpn 2>/dev/null
  377. ctrl_c
  378. fi
  379.  
  380. echo -e "\nDisconnect via: 'sudo killall openvpn'\n"
  381. else
  382.  
  383. # If we're not supposed to run as daemon, run OpenVPN the normal way
  384. openvpn --script-security 3 --config /tmp/hma-template.ovpn
  385. fi
  386.  
  387. # Exit script
  388. ctrl_c
  389.  
  390.  
  391.  
  392.  
  393.  
  394. SCRIPT 2 -- Pas de login/pass mais connexion au hasard
  395.  
  396. #!/bin/bash
  397.  
  398. cd `dirname $0`
  399.  
  400. curl=`which curl`
  401. if [ "$curl" == "" ]; then
  402. curl=`which wget`
  403. if [ "$curl" == "" ]; then
  404. echo <<EOF
  405. Error: Please install curl or wget for this script to work.
  406. You can try any of the following commands:
  407. apt-get install wget
  408. yum install wget
  409. apt-get install curl
  410. yum install curl
  411. EOF
  412. exit 1
  413. else
  414. curl="$curl -T 5 -O - "
  415. fi
  416. else
  417. curl="$curl --connect-timeout 5 -s"
  418. fi
  419.  
  420. openvpn=`which openvpn`
  421. if [ "$openvpn" == "" ] ; then
  422. cat <<EOF
  423. Error: Please install openvpn for this script to work.
  424. You can try any of the following commands:
  425. apt-get install openvpn
  426. yum install openvpn
  427. EOF
  428. exit 1
  429. fi
  430.  
  431. proto=
  432. list=0
  433. while getopts "lp:" parm
  434. do
  435. case $parm in
  436. l)
  437. list=1
  438. ;;
  439. p)
  440. proto="$OPTARG"
  441. ;;
  442. ?) echo "unknown $parm / $OPTARG"
  443. esac
  444. done
  445.  
  446. shift $(( $OPTIND - 1 ))
  447. grep="$*"
  448. names=( )
  449. ips=( )
  450. tcps=( )
  451. udps=( )
  452.  
  453. count=0
  454.  
  455. echo "Obtaining list of servers..."
  456. $curl https://www.securenetconnection.com/vpnconfig/servers-cli.php 2>/dev/null| grep -i -e "$grep" | grep -i -e "$proto" > /tmp/hma-servers
  457. exec < /tmp/hma-servers
  458. rm /tmp/hma-servers
  459. while read server
  460. do
  461. : $(( count++ ))
  462. ips[$count]=`echo "$server"|cut -d '|' -f 1`
  463. udps[$count]=`echo "$server"|cut -d '|' -f 5`
  464. tcps[$count]=`echo "$server"|cut -d '|' -f 4`
  465. names[$count]=`echo "$server"|cut -d '|' -f 2`
  466.  
  467. done
  468.  
  469. if [ "$count" -lt 1 ] ; then
  470. echo "No matching servers to connect: $grep"
  471. exit
  472. else
  473. echo "$count servers matched"
  474. fi
  475.  
  476. if [ $list -eq 1 ]; then
  477. for i in `seq 1 $count`; do
  478. echo -e "${names[$i]}\t${ips[$i]}\t${tcps[$i]}\t${udps[$i]}"
  479. done
  480. exit
  481. fi
  482.  
  483.  
  484. i=$(( $RANDOM%$count + 1 ))
  485. echo "Connecting to:"
  486. echo -e "${names[$i]}\t${ips[$i]}"
  487. if [ "$proto" == "" ]; then
  488. if [ "$udps[$i]" != "" ]; then
  489. proto=udp
  490. else
  491. proto=tcp
  492. fi
  493. fi
  494.  
  495. if [ "$proto" == "tcp" ]; then
  496. port=443
  497. else
  498. port=53
  499. fi
  500.  
  501. echo "Loading configuration..."
  502. $curl "https://securenetconnection.com/vpnconfig/openvpn-template.ovpn" > /tmp/hma-config.cfg 2>/dev/null
  503.  
  504. echo "remote ${ips[$i]} $port" >> /tmp/hma-config.cfg
  505. echo "proto $proto" >> /tmp/hma-config.cfg
  506.  
  507. #sudo $openvpn --config /tmp/hma-config.cfg
  508. #rm /tmp/hma-config.cfg
  509. cat <<EOF > /tmp/hma-routeup.sh
  510. #!/bin/sh
  511. cat <<EOEO >> /tmp/hma-ipcheck.txt
  512. *******************************************
  513. * *
  514. * You are now connected to HMA Pro! VPN *
  515. * *
  516. *******************************************
  517.  
  518. Checking new IP address...
  519. EOEO
  520. nohup /tmp/hma-ipcheck.sh >/dev/null 2>&1 &
  521. rm /tmp/hma-routeup.sh
  522. EOF
  523.  
  524. cat <<EOF > /tmp/hma-ipcheck.sh
  525. #!/bin/sh
  526. ip=""
  527. attempt=0
  528. while [ "\$ip" = "" ]; do
  529. attempt=\$((\$attempt+1))
  530. ip="\`$curl http://geoip.hidemyass.com/ip/ 2>/dev/null\`"
  531. if [ "\$ip" != "" ]; then
  532. echo "Your IP is \$ip" >> /tmp/hma-ipcheck.txt
  533. fi
  534. if [ \$attempt -gt 3 ]; then
  535. echo "Failed to load IP address." >> /tmp/hma-ipcheck.txt
  536. exit
  537. fi
  538. done
  539.  
  540. EOF
  541. echo "" > /tmp/hma-ipcheck.txt
  542. tail -f /tmp/hma-ipcheck.txt &
  543. chmod 755 /tmp/hma-ipcheck.sh
  544. chmod 755 /tmp/hma-routeup.sh
  545. /tmp/hma-ipcheck.sh
  546. sleep 1
  547.  
  548.  
  549. # MODIFICATION BY PETE
  550.  
  551. #!/bin/bash
  552.  
  553. pwfile="password.txt"
  554.  
  555. if [ -f "$pwfile" ]
  556. then
  557. echo "Login details found in password.txt"
  558. else
  559. echo "No Login details found. Please enter:"
  560.  
  561. read -p "Username: " vpnuser < /dev/tty
  562. echo $vpnuser > password.txt
  563.  
  564. read -p "Password: " vpnpass < /dev/tty
  565. echo $vpnpass >> password.txt
  566.  
  567. fi
  568.  
  569. sleep 1
  570.  
  571. # adding to config file that password.txt should be used
  572. sed -i 's/auth-user-pass/auth-user-pass password.txt/g' /tmp/hma-config.cfg
  573.  
  574. # END OF MODIFICATIONS
  575.  
  576.  
  577.  
  578.  
  579.  
  580. sudo $openvpn --script-security 3 --route-up /tmp/hma-routeup.sh --verb 2 --config /tmp/hma-config.cfg
  581. rm /tmp/hma-config.cfg
  582. rm /tmp/hma-ipcheck.sh
  583. rm /tmp/hma-routeup.sh 2>/dev/null
  584. rm /tmp/hma-ipcheck.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement