-
Notifications
You must be signed in to change notification settings - Fork 0
/
hma-vpn.sh
executable file
·191 lines (163 loc) · 4.41 KB
/
hma-vpn.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
if [ $# -lt 1 ]; then
cat <<EOF
__ ____ ______ __ ___ _ _____ _ __
/ // / |/ / _ | / / / _ \_______ | | / / _ \/ |/ /
/ _ / /|_/ / __ |/_/ / ___/ __/ _ \ | |/ / ___/ /
/_//_/_/ /_/_/ |_(_) /_/ /_/ \___/ |___/_/ /_/|_/
Slightly modified for docker. :)
Environment variables:
HMA_USERNAME - Hide my ass username
HMA_PASSWORD - Hide my ass password
Usage:
$0 [-l] [-p tcp|udp] [server name]
Parameters:
[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
-l - if specified, instead of connecting, the script will output server list matched with the grep pattern above
-p tcp|udp - sets preferred protocol, default is OpenVPN UDP
Examples:
$0 -l "New York" - lists all servers in New York
$0 -l -p udp - lists all servers with OpenVPN UDP support
$0 -p tcp Texas - connects to a random Texas server using OpenVPN TCP protocol
EOF
exit -2
fi
: ${HMA_USERNAME:?"Need to set HMA_USERNAME to a valid hide my ass user."}
: ${HMA_PASSWORD:?"Need to set HMA_PASSWORD to a valid hide my ass pass."}
cd `dirname $0`
curl=`which curl`
if [ "$curl" == "" ]; then
curl=`which wget`
if [ "$curl" == "" ]; then
echo <<EOF
Error: Please install curl or wget for this script to work.
You can try any of the following commands:
apt-get install wget
yum install wget
apt-get install curl
yum install curl
EOF
exit 1
else
curl="$curl -T 5 -O - "
fi
else
curl="$curl --connect-timeout 5 -s"
fi
openvpn=`which openvpn`
if [ "$openvpn" == "" ] ; then
cat <<EOF
Error: Please install openvpn for this script to work.
You can try any of the following commands:
apt-get install openvpn
yum install openvpn
EOF
exit 1
fi
proto=
list=0
while getopts "lp:" parm
do
case $parm in
l)
list=1
;;
p)
proto="$OPTARG"
;;
?) echo "unknown $parm / $OPTARG"
esac
done
shift $(( $OPTIND - 1 ))
grep="$*"
names=( )
ips=( )
tcps=( )
udps=( )
count=0
echo "Obtaining list of servers..."
$curl https://securenetconnection.com/vpnconfig/servers-cli.php 2>/dev/null| grep -i -e "$grep" | grep -i -e "$proto" > /tmp/hma-servers
exec < /tmp/hma-servers
rm /tmp/hma-servers
while read server
do
: $(( count++ ))
ips[$count]=`echo "$server"|cut -d '|' -f 1`
udps[$count]=`echo "$server"|cut -d '|' -f 5`
tcps[$count]=`echo "$server"|cut -d '|' -f 4`
names[$count]=`echo "$server"|cut -d '|' -f 2`
done
if [ "$count" -lt 1 ] ; then
echo "No matching servers to connect: $grep"
exit
else
echo "$count servers matched"
fi
if [ $list -eq 1 ]; then
for i in `seq 1 $count`; do
echo -e "${names[$i]}\t${ips[$i]}\t${tcps[$i]}\t${udps[$i]}"
done
exit
fi
i=$(( $RANDOM%$count + 1 ))
echo "Connecting to:"
echo -e "${names[$i]}\t${ips[$i]}"
if [ "$proto" == "" ]; then
if [ "$udps[$i]" != "" ]; then
proto=udp
else
proto=tcp
fi
fi
if [ "$proto" == "tcp" ]; then
port=443
else
port=53
fi
echo "Loading configuration..."
$curl "https://securenetconnection.com/vpnconfig/openvpn-template.ovpn" > /tmp/hma-config.cfg 2>/dev/null
echo "${HMA_USERNAME}" > /tmp/hma-auth
echo "${HMA_PASSWORD}" >> /tmp/hma-auth
echo "auth-user-pass /tmp/hma-auth" >> /tmp/hma-config.cfg
echo "remote ${ips[$i]} $port" >> /tmp/hma-config.cfg
echo "proto $proto" >> /tmp/hma-config.cfg
cat <<EOF > /tmp/hma-routeup.sh
#!/bin/sh
cat <<EOEO >> /tmp/hma-ipcheck.txt
*******************************************
* *
* You are now connected to HMA Pro! VPN *
* *
*******************************************
Checking new IP address...
EOEO
nohup /tmp/hma-ipcheck.sh >/dev/null 2>&1 &
rm /tmp/hma-routeup.sh
EOF
cat <<EOF > /tmp/hma-ipcheck.sh
#!/bin/sh
ip=""
attempt=0
while [ "\$ip" = "" ]; do
attempt=\$((\$attempt+1))
ip="\`$curl http://geoip.hidemyass.com/ip/ 2>/dev/null\`"
if [ "\$ip" != "" ]; then
echo "Your IP is \$ip" >> /tmp/hma-ipcheck.txt
fi
if [ \$attempt -gt 3 ]; then
echo "Failed to load IP address." >> /tmp/hma-ipcheck.txt
exit
fi
done
EOF
echo "" > /tmp/hma-ipcheck.txt
tail -f /tmp/hma-ipcheck.txt &
chmod 755 /tmp/hma-ipcheck.sh
chmod 755 /tmp/hma-routeup.sh
/tmp/hma-ipcheck.sh
sleep 1
sudo $openvpn --script-security 3 --route-up /tmp/hma-routeup.sh --verb 2 --config /tmp/hma-config.cfg
rm /tmp/hma-config.cfg
rm /tmp/hma-ipcheck.sh
rm /tmp/hma-routeup.sh 2>/dev/null
rm /tmp/hma-ipcheck.txt