-
Notifications
You must be signed in to change notification settings - Fork 2
/
shfokus.sh
284 lines (221 loc) · 6.36 KB
/
shfokus.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
#
# @author Rio Astamal <[email protected]>
# @desc shFokus is Bash script to make you focus and stay productive by
# blocking distracting websites suchs Facebook, Youtube etc.
# @url https://github.com/rioastamal/shfokus
readonly FOKUS_SCRIPT_NAME=$(basename $0)
FOKUS_VERSION="2020-09-17"
FOKUS_DRY_RUN="no"
FOKUS_MINUTES="0"
[[ -z "$FOKUS_FILE" ]] && FOKUS_FILE="~/.shfokus"
[[ -z "$HOSTS_FILE" ]] && HOSTS_FILE="/etc/hosts"
shfokus_help()
{
echo "\
Usage: $0 [OPTIONS]
Where OPTIONS:
-a ACTION Specify action name using ACTION.
-f FILE List of blocked websites from FILE. Default is ~/.shfokus. Will
be created if not exists.
-h Print this help and exit.
-r Dry run mode. Print the content that will be written to hosts
file.
-t MINUTES Specify how long in MINUTES sites should be blocked/unblocked.
Default is '0' which means blocked/unblocked forever. Make sure
there is no other shfokus instance running.
-v Display shFokus version.
List of available ACTION:
- block -> block website
- unblock -> release block
Example of ~/.shfokus (one domain per line):
youtube.com
www.youtube.com
facebook.com
www.facebook.com
Example 1 - Block distracting websites:
$ sudo bash ./shfokus.sh -a block
Example 2 - Block distracting websites in 30 minutes:
$ sudo bash ./shfokus.sh -a block -t 30
Example 3 - Unblock distracting websites.
$ sudo bash ./shfokus.sh -a unblock
Example 4 - Unblock distracting websites in 10 minutes.
$ sudo bash ./shfokus.sh -a unblock -t 10
-------------------------------- About shFokus --------------------------------
shFokus is Bash script to make you focus and stay productive by blocking
distracting websites such as Facebook, Youtube etc.
shFokus is free software licensed under MIT. Visit the project homepage
at https://github.com/rioastamal/shfokus."
}
shfokus_see_help()
{
echo "Try '$FOKUS_SCRIPT_NAME -h' for more information."
}
shfokus_ctrl_c()
{
echo -e "\n"
echo "----- Interrupted by CTRL+C -----"
# Reset FOKUS_MINUTES so it will not spawn another timer
FOKUS_MINUTES=0
# Execute the 1st arg as it act as a function name
$1
exit 0
}
shfokus_block_in_minutes()
{
# Catch CTRL+C and call unblock function
trap 'shfokus_ctrl_c shfokus_unblock' INT
# convert minutes to seconds
local SECONDS_LEFT=$(( $FOKUS_MINUTES * 60 ))
while (( $SECONDS_LEFT >= 0 ))
do
# clear line
echo -ne "\033[K"
echo -ne "\rBlock will be released in ${SECONDS_LEFT} sec... CTRL+C to unblock"
sleep 1
SECONDS_LEFT=$(( $SECONDS_LEFT - 1 ))
done
echo -e "\n"
# Reset FOKUS_MINUTES so it will not spawn another timer
FOKUS_MINUTES=0
shfokus_unblock
}
shfokus_unblock_in_minutes()
{
# Catch CTRL+C and call block function
trap 'shfokus_ctrl_c shfokus_block' INT
# convert minutes to seconds
local SECONDS_LEFT=$(( $FOKUS_MINUTES * 60 ))
while (( $SECONDS_LEFT >= 0 ))
do
# clear line
echo -ne "\033[K"
echo -ne "\rWebsites will be blocked in ${SECONDS_LEFT} sec... CTRL+C to block"
sleep 1
SECONDS_LEFT=$(( $SECONDS_LEFT - 1 ))
done
echo -e "\n"
# Reset FOKUS_MINUTES so it will not spawn another timer
FOKUS_MINUTES=0
shfokus_block
}
shfokus_write_hosts_file()
{
local MODE="block"
[[ ! -z "$1" ]] && MODE="$1"
# Content to write to HOSTS file
local FOKUS_CONTENT=
# Used for dry-run mode
local DRY_RUN_FILE=$HOSTS_FILE
read -r -d '' FOKUS_CONTENT << EOF
# shfokus_begin - DO NOT TOUCH
$( echo 0.0.0.0 $( <$FOKUS_FILE ) )
# shfokus_end
EOF
[[ "$FOKUS_DRY_RUN" == "yes" ]] && DRY_RUN_FILE="/dev/stdout"
[[ ! -w "$HOSTS_FILE" && "$FOKUS_DRY_RUN" != "yes" ]] && {
echo "Error: Can not write to HOSTS file $DRY_RUN_FILE." >&2
echo "Make sure it is writeable by running as root." >&2
exit 403
}
# Output the content inside sub-shell because we are
# reading and writing the same file
echo "$(
echo "$( <$HOSTS_FILE )" | \
sed "/^# shfokus_begin/,/^# shfokus_end/d"
)" > $DRY_RUN_FILE
[[ "$MODE" == "unblock" ]] && {
echo "Distracting websites has been unblocked -- Enjoy your day :)"
[[ "$FOKUS_MINUTES" != "0" ]] && shfokus_unblock_in_minutes
return 0
}
# Append our blocked site config at the end of Hosts file
echo "${FOKUS_CONTENT}" >> $DRY_RUN_FILE
echo "Stay focus and productive -- Have a great day :)"
[[ "$FOKUS_MINUTES" != "0" ]] && shfokus_block_in_minutes
}
shfokus_block()
{
shfokus_write_hosts_file "block"
}
shfokus_unblock()
{
shfokus_write_hosts_file "unblock"
}
shfokus_init()
{
# Issue for expanding "~" because treated as string literal
# Simple hack is replacing "~" with the value of $HOME
FOKUS_FILE=$( echo "$FOKUS_FILE" | sed "s#~#$HOME#" )
local FOKUS_DIR="$( dirname $FOKUS_FILE )"
[[ ! -w "$FOKUS_DIR" ]] && {
echo "Error: Directory $FOKUS_DIR is not writeable. Make sure it is writeable by running as root." >&2
exit 403
}
# Write shfokus file if it not exists
[[ ! -f "$FOKUS_FILE" ]] && {
cat <<EOF > $FOKUS_FILE
www.youtube.com
youtube.com
www.facebook.com
facebook.com
www.instagram.com
instagram.com
twitter.com
www.twitter.com
twitch.tv
www.twitch.tv
netflix.com
www.netflix.com
EOF
}
[[ ! -w "$FOKUS_FILE" ]] && {
echo "Error: File $FOKUS_FILE is not writeable. Make sure it is writeable by running as root." >&2
exit 403
}
}
# Parse the arguments
while getopts a:f:hrt:v FOKUS_OPT;
do
case $FOKUS_OPT in
a)
FOKUS_ACTION="$OPTARG"
;;
f)
FOKUS_FILE="$OPTARG"
;;
h)
shfokus_help
exit 0
;;
r)
FOKUS_DRY_RUN="yes"
;;
t)
FOKUS_MINUTES="$OPTARG"
;;
v)
echo "shFokus version $FOKUS_VERSION"
exit 0
;;
\?)
shfokus_see_help
exit 400
;;
esac
done
shfokus_init
case $FOKUS_ACTION in
block)
shfokus_block
;;
unblock)
shfokus_unblock
;;
*)
echo -e "Unrecognized action.\n" >&2
shfokus_see_help
exit 400
;;
esac
exit 0