-
Notifications
You must be signed in to change notification settings - Fork 61
/
tun2socks.sh
executable file
·167 lines (156 loc) · 5.27 KB
/
tun2socks.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
#!/bin/bash
# Tun2socks Wrapper
# by Lutfa Ilham
# v1.0.0
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
SYSTEM_CONFIG="${LIBERNET_DIR}/system/config.json"
TUN2SOCKS_MODE="$(grep 'legacy":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g')"
TUN_DEV="$(grep 'dev":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g')"
TUN_ADDRESS="$(grep 'address":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g')"
TUN_NETMASK="$(grep 'netmask":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g')"
TUN_GATEWAY="$(grep 'gateway":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g')"
TUN_MTU="$(grep 'mtu":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g')"
SOCKS_IP="$(grep 'ip":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g' | sed -n '1p')"
SOCKS_PORT="$(grep 'port":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g' | sed -n '1p')"
SOCKS_SERVER="${SOCKS_IP}:${SOCKS_PORT}"
UDPGW_IP="$(grep 'ip":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g' | sed -n '2p')"
UDPGW_PORT="$(grep 'port":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g' | sed -n '2p')"
UDPGW="${UDPGW_IP}:${UDPGW_PORT}"
GATEWAY="$(ip route | grep -v tun | awk '/default/ { print $3 }')"
SERVER_IP="$(grep 'server":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g' | sed -n '1p')"
CDN_IP="$(grep 'cdn_server":' ${SYSTEM_CONFIG} | awk '{print $2}' | sed 's/,//g; s/"//g' | sed -n '1p')"
readarray -t PROXY_IPS < <(jq -r '.proxy_servers[]' < ${SYSTEM_CONFIG})
readarray -t DNS_IPS < <(jq -r '.dns_servers[]' < ${SYSTEM_CONFIG})
ROUTE_LOG="${LIBERNET_DIR}/log/route.log"
DEFAULT_ROUTE="$(ip route show | grep default)"
function init_tun_dev {
# write to service log
"${LIBERNET_DIR}/bin/log.sh" -w "Tun2socks: initializing tun device"
# remove tun dev if already exist
if ifconfig "${TUN_DEV}" > /dev/null 2>&1; then
ifconfig ${TUN_DEV} down
ip tuntap del dev ${TUN_DEV} mode tun
fi
# finally init tun dev
ip tuntap add dev ${TUN_DEV} mode tun
ifconfig ${TUN_DEV} mtu ${TUN_MTU}
echo -e "Tun device initialized!"
}
function destroy_tun_dev {
# write to service log
"${LIBERNET_DIR}/bin/log.sh" -w "Tun2socks: removing tun device"
ifconfig ${TUN_DEV} down
ip tuntap del dev ${TUN_DEV} mode tun
echo -e "Tun device removed!"
}
function start_tun2socks {
# write to service log
"${LIBERNET_DIR}/bin/log.sh" -w "Starting tun2socks service"
ifconfig ${TUN_DEV} ${TUN_GATEWAY} netmask ${TUN_NETMASK} up
if [[ $TUN2SOCKS_MODE == "false" ]]; then
screen -AmdS go-tun2socks go-tun2socks -loglevel none -proxyServer "${SOCKS_SERVER}" -proxyType socks -tunName "${TUN_DEV}" -tunAddr "${TUN_ADDRESS}" -tunGw "${TUN_GATEWAY}" -tunMask "${TUN_NETMASK}"
else
screen -AmdS badvpn-tun2socks badvpn-tun2socks --loglevel 0 --tundev ${TUN_DEV} --netif-ipaddr ${TUN_ADDRESS} --netif-netmask ${TUN_NETMASK} --socks-server-addr ${SOCKS_SERVER} --udpgw-remote-server-addr "${UDPGW}"
fi
# removing default route
echo ${DEFAULT_ROUTE} > ${ROUTE_LOG} \
&& ip route del ${DEFAULT_ROUTE}
# add default route to tun2socks
ip route add default via ${TUN_ADDRESS} metric 6
echo -e "Tun2socks started!"
# write connected time
"${LIBERNET_DIR}/bin/log.sh" -c "$(date +"%s")"
}
function stop_tun2socks {
# write to service log
"${LIBERNET_DIR}/bin/log.sh" -w "Stopping tun2socks service"
if [[ $TUN2SOCKS_MODE == "false" ]]; then
kill $(screen -list | grep go-tun2socks | awk -F '[.]' {'print $1'})
else
kill $(screen -list | grep badvpn-tun2socks | awk -F '[.]' {'print $1'})
fi
# recover default route
ip route add $(cat "${ROUTE_LOG}") \
&& rm -rf "${ROUTE_LOG}"
# remove default route to tun2socks
ip route del default via ${TUN_ADDRESS} metric 6
echo -e "Tun2socks stopped!"
}
function route_add_ip {
# write to service log
"${LIBERNET_DIR}/bin/log.sh" -w "Tun2socks: routing server, proxy and DNS IPs"
ip route add ${SERVER_IP} via ${GATEWAY} metric 4 &
ip route add ${CDN_IP} via ${GATEWAY} metric 4 &
for IP in "${PROXY_IPS[@]}"; do
ip route add ${IP} via ${GATEWAY} metric 4 &
done
for IP in "${DNS_IPS[@]}"; do
ip route add ${IP} via ${GATEWAY} metric 4 &
done
echo -e "Routes initialized!"
}
function route_del_ip {
# write to service log
"${LIBERNET_DIR}/bin/log.sh" -w "Tun2socks: removing routes"
for IP in "${DNS_IPS[@]}"; do
ip route del ${IP} &
done
for IP in "${PROXY_IPS[@]}"; do
ip route del ${IP} &
done
ip route del ${CDN_IP} &
ip route del ${SERVER_IP} &
echo -e "Routes removed!"
}
function usage() {
cat <<EOF
Usage:
-i Initialize tun device
-d Destroy tun device
-y Route server, proxy & dns
-z Remove route server, proxy & dns
-r Run tun2socks
-s Stop tun2socks
EOF
}
case "${1}" in
-v)
# start tun2socks service
init_tun_dev
route_add_ip
start_tun2socks
;;
-w)
# stop tun2socks service
echo -e "Stopping Tun2socks service ..."
stop_tun2socks
echo -e "Removing routes ..."
route_del_ip
echo -e "Removing tun device ..."
destroy_tun_dev
;;
-i)
init_tun_dev
;;
-d)
destroy_tun_dev
;;
-r)
start_tun2socks
;;
-s)
stop_tun2socks
;;
-y)
route_add_ip
;;
-z)
route_del_ip
;;
*)
usage
;;
esac