forked from sensepost/wiresocks
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathentrypoint.sh
More file actions
103 lines (80 loc) · 2.12 KB
/
Copy pathentrypoint.sh
File metadata and controls
103 lines (80 loc) · 2.12 KB
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
#!/bin/sh
# Modified version of
# https://github.com/xjasonlyu/tun2socks/blob/main/docker/entrypoint.sh
LOGLEVEL="${LOGLEVEL:-info}"
TUN_IDX=0 # TUN starts with 0
IP_TABLE_IDX=100 # IP Tables start with 100
PEER_IDX=2 # Peer Starts with .2/32; sequential loop will
PROXY_PEERS=${PROXY_PEERS:-3}
create_tun() {
tun_name=$1
tun_range_ip=$2
# Setup TUN
ip tuntap add mode tun dev "$tun_name"
ip addr add "$tun_range_ip/15" dev "$tun_name"
ip link set dev "$tun_name" up
}
config_route() {
tun_name=$1
tun_range_ip=$2
ip_table_name=$3
# Proxy 1
ip route add default via "$tun_range_ip" dev "$tun_name" table "$ip_table_name"
# For each peer
i=0
while [ $i -lt "$PROXY_PEERS" ]
do
last_part=$((PEER_IDX + i))
ip rule add from "10.13.13.$last_part" table "$ip_table_name"
true $(( i++ ))
done
# inc index
PEER_IDX=$((PEER_IDX + PROXY_PEERS))
}
setup() {
default_args=$1
for proxy in $(echo "$PROXIES" | tr ',' '\n'); do
# calculations
ip_range=$((TUN_IDX * 2))
# determine vars
tun_name="tun$TUN_IDX"
tun_range_ip="198.$ip_range.0.1"
# setup
create_tun $tun_name $tun_range_ip
config_route $tun_name $tun_range_ip $IP_TABLE_IDX
# Start tun2socks
exec tun2socks --loglevel "$LOGLEVEL" --device "$tun_name" --proxy "$proxy" $default_args &
# inc indexes
TUN_IDX=$((TUN_IDX + 1))
IP_TABLE_IDX=$((IP_TABLE_IDX + 1))
done
}
run() {
# apply extra commands
if [ -n "$EXTRA_COMMANDS" ]; then
sh -c "$EXTRA_COMMANDS"
fi
if [ -n "$MTU" ]; then
ARGS="--mtu $MTU"
fi
if [ -n "$RESTAPI" ]; then
ARGS="$ARGS --restapi $RESTAPI"
fi
if [ -n "$UDP_TIMEOUT" ]; then
ARGS="$ARGS --udp-timeout $UDP_TIMEOUT"
fi
if [ -n "$TCP_SNDBUF" ]; then
ARGS="$ARGS --tcp-sndbuf $TCP_SNDBUF"
fi
if [ -n "$TCP_RCVBUF" ]; then
ARGS="$ARGS --tcp-rcvbuf $TCP_RCVBUF"
fi
if [ "$TCP_AUTO_TUNING" = 1 ]; then
ARGS="$ARGS --tcp-auto-tuning"
fi
# Set up the routing
setup "$ARGS"
# Wait for processes to finish (they should usually not finish except they're breaking)
wait
}
run || exit 1