-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.sh
209 lines (152 loc) · 3.38 KB
/
block.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
set -e
SELF="$0"
# CHECK ROOT
if [ "$(whoami)" != "root" ]; then
echo "You are not root"
if [ ! -z $@ ]; then
echo "Add sudo before command: sudo $0 $@"
else
echo "Add sudo before command: sudo $0"
fi
exit 1
fi
# PARSE ARGUMENTS
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--RKN|--rkn)
rkn=true
shift
;;
--self)
self=true
shift
;;
-h|--help)
need_help=true
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
if [ ! -z "$need_help" ]; then
echo "This software raises the Tor socks proxy and enables" \
"proxying in settings to Wi-Fi network"
echo ""
echo "If no flags was specified this software will trigger current state"
echo ""
echo "Available flags:"
echo "\t--RKN | --rkn: Raise Tor socks proxy and enable proxying in settings"
echo "\t--self: Disable Tor socks proxy and removing it from settings"
echo "\t-h | --help: Display this help"
exit 0
fi
function raiseTor {
if pgrep -x "tor" > /dev/null; then
echo "Looks like Tor is already raised - skipping"
return 0
fi
torRaising=$(su $(logname) -c "tor RunAsDaemon 1")
if [[ "$?" == "0" ]]; then
echo "Raised"
return 0
fi
echo "Tor raising failed - exit"
echo "Tor logs:"
echo "$torRaising"
exit 2
}
function killTor {
killall tor
if [[ "$?" == "0" ]]; then
echo "Killed"
return 0
fi
echo "WTF, Tor is a God - we can't kill him"
}
function enableProxyInSettings {
networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 9050
networksetup -setsocksfirewallproxystate Wi-Fi on
echo "Enabled"
}
function disableProxyInSettings {
networksetup -setsocksfirewallproxystate Wi-Fi off
echo "Disabled"
}
if [ ! -z "$rkn" ]; then
echo "Blocking RKN"
echo -n "Raising Tor... "
raiseTor
echo -n "Enabling proxy in settings... "
enableProxyInSettings
exit 0
fi
if [ ! -z "$self" ]; then
echo "Blocking self "
echo -n "Killing Tor... "
killTor
echo -n "Disabling proxy in settings... "
disableProxyInSettings
exit 0
fi
echo "No flags provided - detecting current state"
echo
proxyingSettingsCorrect=false
echo "Network settings:"
networkSettings=$(networksetup -getsocksfirewallproxy Wi-Fi)
echo -n -e "\tProxying enabled: "
proxyingState=$(echo "$networkSettings" | head -n 1)
if [[ $proxyingState = *"Yes"* ]]; then
proxyingEnabled=true
echo "true"
else
proxyingEnabled=false
echo "false"
fi
echo -n -e "\tProxy server IP correct: "
server=$(echo "$networkSettings" | head -n 2 | tail -n 1)
if [[ $server = *"127.0.0.1"* ]]; then
ipCorrect=true
echo "true"
else
ipCorrect=false
echo "false"
fi
echo -n -e "\tProxy server port correct: "
serverPort=$(echo "$networkSettings" | head -n 3 | tail -n 1)
if [[ $serverPort = *" 9050"* ]]; then
echo "true"
portCorrect=true
else
echo "false"
portCorrect=false
fi
if [[ $ipCorrect == false || $portCorrect == false ]]; then
echo "Network settings corrupted - launch this software with --rkn flag"
exit 2
fi
torRaised=false
if pgrep -x "tor" > /dev/null; then
torRaised=true
fi
echo "Tor raised: $torRaised"
echo
if [[ $torRaised == true && $proxyingEnabled == true ]]; then
echo "RKN is blocked - unblocking"
echo
$SELF --self
exit 0
fi
if [[ $torRaised == false && $proxyingEnabled == false ]]; then
echo "Self is blocked - unblocking"
echo
$SELF --rkn
exit 0
fi
echo "Unknow state - please launch this software with --rkn or --self flag"