forked from dperson/torproxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtorproxy.sh
executable file
·193 lines (178 loc) · 7.33 KB
/
torproxy.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
192
193
#!/usr/bin/env bash
#===============================================================================
# FILE: torproxy.sh
#
# USAGE: ./torproxy.sh
#
# DESCRIPTION: Entrypoint for torproxy docker container
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: David Personette ([email protected]),
# ORGANIZATION:
# CREATED: 09/28/2014 12:11
# REVISION: 1.0
#===============================================================================
set -o nounset # Treat unset variables as an error
### bandwidth: set the BW available for relaying
# Arguments:
# KiB/s) KiB/s of data that can be relayed
# Return: Updated configuration file
bandwidth() { local kbs="${1:-10}" file=/etc/tor/torrc
sed -i '/^RelayBandwidth/d' $file
echo "RelayBandwidthRate $kbs KB" >>$file
echo "RelayBandwidthBurst $(( kbs * 2 )) KB" >>$file
}
### socks_listen_address: set the socks listen address
# Arguments:
# IP address to listen on
# Return: Updated configuration file
socks_listen_address() { local socks_listen_addresses="$1" file=/etc/tor/torrc
sed -i '/^SocksListenAddress/d' $file
for socks_listen_address_or_name in $socks_listen_addresses; do
socks_listen_ipv4_addresses="$(getent ahostsv4 ""$socks_listen_address_or_name"" | grep STREAM | cut -d' ' -f1)"
socks_listen_ipv6_addresses="$(getent ahostsv6 ""$socks_listen_address_or_name"" | grep STREAM | cut -d' ' -f1 | grep -v '\.' | sed -e 's/^\(.\+\)$/[\1]/g')"
for socks_listen_address in $socks_listen_ipv4_addresses $socks_listen_ipv6_addresses; do
if [ -n "$socks_listen_address" ]; then
echo "SocksListenAddress $socks_listen_address" >>$file
fi
done
done
}
### outbound_bind_address: set the outbound bind address
# Arguments:
# IP address to bind to
# Return: Updated configuration file
outbound_bind_address() { local outbound_bind_addresses="$1" file=/etc/tor/torrc
sed -i '/^OutboundBindAddress/d' $file
for outbound_bind_address_or_name in $outbound_bind_addresses; do
outbound_bind_ipv4_addresses="$(getent ahostsv4 ""$outbound_bind_address_or_name"" | grep STREAM | cut -d' ' -f1)"
outbound_bind_ipv6_addresses="$(getent ahostsv6 ""$outbound_bind_address_or_name"" | grep STREAM | cut -d' ' -f1 | grep -v '\.' | sed -e 's/^\(.\+\)$/[\1]/g')"
for outbound_bind_address in $outbound_bind_ipv4_addresses $outbound_bind_ipv6_addresses; do
if [ -n "$outbound_bind_address" ]; then
echo "OutboundBindAddress $outbound_bind_address" >>$file
fi
done
done
}
### exitnode: Allow exit traffic
# Arguments:
# N/A)
# Return: Updated configuration file
exitnode() { local file=/etc/tor/torrc
sed -i '/^ExitPolicy/d' $file
}
### exitnode_country: Only allow traffic to exit in a specified country
# Arguments:
# country) country where we want to exit
# Return: Updated configuration file
exitnode_country() { local country="$1" file=/etc/tor/torrc
sed -i '/^StrictNodes/d; /^ExitNodes/d' $file
echo "StrictNodes 1" >>$file
echo "ExitNodes {$country}" >>$file
}
### hidden_service: setup a hidden service
# Arguments:
# port) port to connect to service
# host) host:port where service is running
# Return: Updated configuration file
hidden_service() { local port="$1" host="$2" file=/etc/tor/torrc
sed -i '/^HiddenServicePort '"$port"' /d' $file
grep -q '^HiddenServiceDir' $file ||
echo "HiddenServiceDir /var/lib/tor/hidden_service" >>$file
echo "HiddenServicePort $port $host" >>$file
}
### timezone: Set the timezone for the container
# Arguments:
# timezone) for example EST5EDT
# Return: the correct zoneinfo file will be symlinked into place
timezone() { local timezone="${1:-EST5EDT}"
[[ -e /usr/share/zoneinfo/$timezone ]] || {
echo "ERROR: invalid timezone specified: $timezone" >&2
return
}
if [[ -w /etc/timezone && $(cat /etc/timezone) != $timezone ]]; then
echo "$timezone" >/etc/timezone
ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
}
### usage: Help
# Arguments:
# none)
# Return: Help text
usage() { local RC=${1:-0}
echo "Usage: ${0##*/} [-opt] [command]
Options (fields in '[]' are optional, '<>' are required):
-h This help
-a \"<address>\" Configure socks listen address
-o \"<address>\" Configure outbound bind address
-b \"\" Configure tor relaying bandwidth in KB/s
possible arg: \"[number]\" - # of KB/s to allow
-e Allow this to be an exit node for tor traffic
-l \"<country>\" Configure tor to only use exit nodes in specified country
required args: \"<country>\" (IE, "US" or "DE")
<country> - country traffic should exit in
-s \"<port>;<host:port>\" Configure tor hidden service
required args: \"<port>;<host:port>\"
<port> - port for .onion service to listen on
<host:port> - destination for service request
-t \"\" Configure timezone
possible arg: \"[timezone]\" - zoneinfo timezone for container
The 'command' (if provided and valid) will be run instead of torproxy
" >&2
exit $RC
}
while getopts ":ho:a:b:el:s:t:" opt; do
case "$opt" in
h) usage ;;
o) outbound_bind_address "$OPTARG" ;;
a) socks_listen_address "$OPTARG" ;;
b) bandwidth "$OPTARG" ;;
e) exitnode ;;
l) exitnode_country "$OPTARG" ;;
s) eval hidden_service $(sed 's/^\|$/"/g; s/;/" "/g' <<< $OPTARG) ;;
t) timezone "$OPTARG" ;;
"?") echo "Unknown option: -$OPTARG"; usage 1 ;;
":") echo "No argument value for option: -$OPTARG"; usage 2 ;;
esac
done
shift $(( OPTIND - 1 ))
[[ "${SOCKS_LISTEN_ADDRESS:-""}" ]] && socks_listen_address "$SOCKS_LISTEN_ADDRESS"
[[ "${OUTBOUND_BIND_ADDRESS:-""}" ]] && outbound_bind_address "$OUTBOUND_BIND_ADDRESS"
[[ "${BW:-""}" ]] && bandwidth "$BW"
[[ "${EXITNODE:-""}" ]] && exitnode
[[ "${LOCATION:-""}" ]] && exitnode_country "$LOCATION"
[[ "${SERVICE:-""}" ]] && eval hidden_service \
$(sed 's/^\|$/"/g; s/;/" "/g' <<< $SERVICE)
[[ "${TZ:-""}" ]] && timezone "$TZ"
[[ "${USERID:-""}" =~ ^[0-9]+$ ]] && usermod -u $USERID -o debian-tor
[[ "${GROUPID:-""}" =~ ^[0-9]+$ ]] && groupmod -g $GROUPID -o debian-tor
for env in $(printenv | grep '^TOR_'); do
name=$(cut -c5- <<< ${env%%=*})
val="\"${env##*=}\""
[[ "$val" =~ ^\"([0-9]+|false|true)\"$ ]] && val=$(sed 's|"||g' <<<$val)
if grep -q "^$name" /etc/tor/torrc; then
sed -i "/^$name/s| .*| $val|" /etc/tor/torrc
else
echo "$name $val" >>/etc/tor/torrc
fi
done
chown -Rh debian-tor. /etc/tor /var/lib/tor /var/log/tor 2>&1 |
grep -iv 'Read-only' || :
if [[ $# -ge 1 && -x $(which $1 2>&-) ]]; then
exec "$@"
elif [[ $# -ge 1 ]]; then
echo "ERROR: command not found: $1"
exit 13
elif ps -ef | egrep -v 'grep|torproxy.sh' | grep -q tor; then
echo "Service already running, please restart container to apply changes"
else
[[ -e /srv/tor/hidden_service/hostname ]] && {
echo -en "\nHidden service hostname: "
cat /srv/tor/hidden_service/hostname; echo; }
/usr/sbin/privoxy --user privoxy /etc/privoxy/config
exec /usr/bin/tor
fi