-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_connect.sh
executable file
·79 lines (61 loc) · 2.2 KB
/
proxy_connect.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
#!/bin/bash
# go in directory of script
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR" || exit
if [ ! -f "config.sh" ]; then
cp default_config.sh config.sh
echo "Copied config file. Please edit it, then re-run this script."
exit
fi
# read config
# shellcheck source=/dev/null
. config.sh
# will get extracted out of route command later
GATEWAY=
# decode password
PROXY_PASS=$(echo "$PROXY_PASS" | base64 --decode)
# undo all changes this script did
cleanup() {
# param to tell if the badvpn service should also get stopped
_stopBadvpn="${1:-true}";
echo -e "\nCleanup...";
# kill the processes
if [ "$_stopBadvpn" = "true" ];
then
pkill -x badvpn-tun2sock;
fi;
pkill -x gost;
# remove the created routes
route del $PROXY_DOMAIN gw ${GATEWAY:-localhost} metric 5;
route del default gw 10.20.0.2 metric 6;
}
# called when the script receives an exit signal
onExit() {
cleanup;
exit;
}
# register a listener for when this script ends or the user pressed CTRL+C
trap onExit 1 2 3 15
# create the virtual tunnel device
openvpn --mktun --dev tun0
# and configure its ip
ifconfig tun0 10.20.0.1 netmask 255.255.255.0
# use badvpn to route the traffic coming from the SOCKS Proxy, that GOST will start, to the tun0 device (TCP only)
(./badvpn-tun2socks --tundev tun0 --netif-ipaddr 10.20.0.2 --netif-netmask 255.255.255.0 --socks-server-addr 127.0.0.1:1080 &) > /dev/null
sleep 1
# this part of the code is in a loop to allow automatic reconnects when the internet connection drops for whatever reason
while true
do
# first do a cleanup without stopping badvpn
cleanup false
# get the gateway from the route command
GATEWAY=$(route -n | grep 'UG[ \t]' | awk '{print $2}')
# make all traffic to the proxy server pass through the detected gateway
route add $PROXY_DOMAIN gw $GATEWAY metric 5
# the remaining traffic will go through the badvpn gateway and therefore through the proxy
route add default gw 10.20.0.2 metric 6
echo "Starting GOST..."
gost -L socks5://:1080 -F http://$PROXY_USER:$PROXY_PASS@$PROXY_DOMAIN:$PROXY_PORT
# repeat all that after 5 seconds
sleep 5
done