-
Notifications
You must be signed in to change notification settings - Fork 149
/
torproxy.sh
executable file
·159 lines (146 loc) · 5.46 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
#!/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
}
### 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
}
### newnym: setup new circuits
# Arguments:
# N/A)
# Return: New circuits for tor connections
newnym() { local file=/etc/tor/run/control.authcookie
echo -e 'AUTHENTICATE "'"$(cat $file)"'"\nSIGNAL NEWNYM\nQUIT' |
nc 127.0.0.1 9051
if ps -ef | egrep -v 'grep|torproxy.sh' | grep -q tor; then exit 0; fi
}
### password: setup a hashed password
# Arguments:
# passwd) passwd to set
# Return: Updated configuration file
password() { local passwd="$1" file=/etc/tor/torrc
sed -i '/^HashedControlPassword/d' $file
sed -i '/^ControlPort/s/ 9051/ 0.0.0.0:9051/' $file
echo "HashedControlPassword $(su - tor -s/bin/bash -c \
"tor --hash-password '$passwd' |tail -n 1")" >>$file 2>/dev/null
}
### 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
-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
-n Generate new circuits now
-p \"<password>\" Configure tor HashedControlPassword for control port
-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
The 'command' (if provided and valid) will be run instead of torproxy
" >&2
exit $RC
}
while getopts ":hb:el:np:s:" opt; do
case "$opt" in
h) usage ;;
b) bandwidth "$OPTARG" ;;
e) exitnode ;;
l) exitnode_country "$OPTARG" ;;
n) newnym ;;
p) password "$OPTARG" ;;
s) eval hidden_service $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $OPTARG) ;;
"?") echo "Unknown option: -$OPTARG"; usage 1 ;;
":") echo "No argument value for option: -$OPTARG"; usage 2 ;;
esac
done
shift $(( OPTIND - 1 ))
[[ "${BW:-""}" ]] && bandwidth "$BW"
[[ "${EXITNODE:-""}" ]] && exitnode
[[ "${LOCATION:-""}" ]] && exitnode_country "$LOCATION"
[[ "${PASSWORD:-""}" ]] && password "$PASSWORD"
[[ "${SERVICE:-""}" ]] && eval hidden_service \
$(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $SERVICE)
[[ "${USERID:-""}" =~ ^[0-9]+$ ]] && usermod -u $USERID -o tor
[[ "${GROUPID:-""}" =~ ^[0-9]+$ ]] && groupmod -g $GROUPID -o tor
for env in $(printenv | grep '^TOR_'); do
name="$(cut -c5- <<< ${env%%=*})"
val="\"${env##*=}\""
[[ "$name" =~ _ ]] && continue
[[ "$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 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