-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathredsocks
More file actions
99 lines (81 loc) · 2.27 KB
/
Copy pathredsocks
File metadata and controls
99 lines (81 loc) · 2.27 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
#!/usr/bin/env bash
iptables_rules() {
# private ip ranges are not intercepted
iptables -t nat -$1 PREROUTING -i docker0 -d 10.0.0.0/8 -j RETURN
iptables -t nat -$1 PREROUTING -i docker0 -d 127.0.0.0/8 -j RETURN
iptables -t nat -$1 PREROUTING -i docker0 -d 169.254.0.0/16 -j RETURN
iptables -t nat -$1 PREROUTING -i docker0 -d 172.16.0.0/12 -j RETURN
iptables -t nat -$1 PREROUTING -i docker0 -d 192.168.0.0/16 -j RETURN
iptables -t nat -$1 PREROUTING -i docker0 -d 224.0.0.0/4 -j RETURN
iptables -t nat -$1 PREROUTING -i docker0 -d 240.0.0.0/4 -j RETURN
iptables -t nat -$1 PREROUTING -p tcp --dport 80 -i docker0 -j REDIRECT --to 12345 2>/dev/null
iptables -t nat -$1 PREROUTING -p tcp --dport 8080 -i docker0 -j REDIRECT --to 12345 2>/dev/null
iptables -t nat -$1 PREROUTING -p tcp -i docker0 -j REDIRECT --to 12346
}
append_redsocks_conf() {
local type=$1
local ip=$2
local port=$3
local local_port=$4
if [ -z "$type" -o -z "$ip" -o -z "$port" -o -z "$local_port" ] ; then
echo missing required parameter >&2
exit 1
fi
(cat <<EOF
redsocks {
type = $type;
ip = $ip;
port = $port;
local_ip = 0.0.0.0;
local_port = $local_port;
}
EOF
) >> /tmp/redsocks.conf
}
parse_ip() {
echo $1 | sed -nE "s/^(http(s)?:\/\/)?(.+):([0-9]+)\/?$/\3/p"
}
parse_port() {
echo $1 | sed -nE "s/^(http(s)?:\/\/)?(.+):([0-9]+)\/?$/\4/p"
}
stop() {
echo "Cleaning iptables"
iptables_rules D
pkill -9 redsocks
}
interrupted () {
echo 'Interrupted, cleaning up...'
trap - INT
stop
kill -INT $$
}
run() {
trap interrupted INT
trap terminated TERM
if [ -z "$http_proxy" ]; then
echo "No http_proxy set. Exiting"
exit 1
fi
ip=$(parse_ip $http_proxy)
port=$(parse_port $http_proxy)
append_redsocks_conf "http-relay" $ip $port "12345"
if [ -z "$https_proxy" ]; then
https_proxy="$http_proxy"
fi
ip=$(parse_ip $https_proxy)
port=$(parse_port $https_proxy)
append_redsocks_conf "http-connect" $ip $port "12346"
iptables_rules A
/usr/sbin/redsocks -c /tmp/redsocks.conf &
tail -f /dev/null
}
terminated () {
echo 'Terminated, cleaning up...'
trap - TERM
stop
kill -TERM $$
}
case "$1" in
stop ) stop ;;
* ) run ;;
esac