This repository was archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinuxcontrol.sh
executable file
·199 lines (160 loc) · 5.38 KB
/
linuxcontrol.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
#!/bin/bash
# This script is designed to mimic the function of SelfControl.app, a macOS app
# for blocking destracting websites for an arbitrary period of time.
#
# Todo:
# - Automatically remove expired block (Is this just a deamon in bash?)
# - Add ability to add to distracting url list
# - Update distracting url list if change made while block is in place
# - Add support for swiching between lists or merging lists
# - Consistent function names (this would be halfway to a rewrite)
#
# Possibly move to function:
# - Wiping block end time
# - Finding block end time
# - Finding block end time but pretty (this is a real mess and the source of all
# shellcheck violations at the moment)
# - Move block end time storage to a non-userspace location
#
# Current obvious security problems:
# - List of urls is not sanitized or validated
configDirectory="$HOME/.config/diwesser"
statusFile="$configDirectory/linux_control_status"
endBlockTimeFile="$configDirectory/linux_control_block_end"
originalHostsBackup="/etc/hosts.linux_control_backup"
# Set /etc/hosts to blocked state.
set_blocked_hosts () {
if check_if_blocked ; then
echo "Block is already in place"
else
# Backup current /etc/hosts
cp /etc/hosts "$originalHostsBackup"
# Append blocked url rules to /etc/hosts
convert_urls_to_hosts >> /etc/hosts
set_block_status blocked
echo "Block set"
fi
}
# Restore /etc/hosts to original unblocked state.
restore_original_hosts () {
# Copy backup of original hosts file to /etc/hosts
cp "$originalHostsBackup" "/etc/hosts"
set_block_status unblocked
echo "Block removed"
}
# Takes plain list of urls and outputs them as host rules.
# Output of this function is intended to be piped to /etc/hosts to start
# blocked state.
convert_urls_to_hosts () {
while read -r i; do
echo "0.0.0.0 $i" # IPv4 rule
echo "::0 $i" # IPv6 rule
done <"$configDirectory/distracting_urls"
}
# Checks if block is enabled.
# Returns true of false boolean
check_if_blocked () {
if grep -q -x "blocked" "$statusFile"; then
true
elif grep -q -x "unblocked" "$statusFile"; then
false
fi
}
# Sets blocked status
# Takes input as either "blocked" or "unblocked"
set_block_status () {
if [[ "$1" == "blocked" ]] ; then
echo "blocked" > "$statusFile"
elif [[ "$1" == "unblocked" ]] ; then
echo "unblocked" > "$statusFile"
fi
}
# Takes a number of minutes and adds them to current epoc as seconds
# Returns epoc.
epoc_now_plus_min () {
local minutes="$1"
local seconds="$(echo "$minutes * 60" | bc -ql)"
local now="$(date +'%s')"
echo -n "$(echo "$now + $seconds" | bc -ql)"
}
# Check if block has expired.
# Returns true if it has expired, false if not
block_has_expired () {
local endTime="$(tail -n 1 "$endBlockTimeFile")"
local now="$(date +'%s')"
if [[ "$now" < "$endTime" ]] ; then
false
else
true
fi
}
# Writes block expiration in epoc time
# Assumes block starts now
# Takes block time in minutes
set_block_expiration () {
epoc_now_plus_min "$1" > "$endBlockTimeFile"
}
# Checks for block expiration and remove if expired
remove_block_if_expired () {
if [[ $(block_has_expired) == true ]] ; then
restore_original_hosts
echo "" > "$endBlockTimeFile" # Wipe block time
echo "Expired block removed"
else
echo "Block has not expired"
fi
}
# Starts block and sets time for it to expire
# Takes block length in minutes
start_block_with_expiration () {
if [[ $(date +'%s') < $(tail -n1 $endBlockTimeFile) && check_if_blocked ]] ; then
echo "Block is in place and has not expired"
else
set_block_expiration "$1"
set_blocked_hosts
fi
echo "Block will expire at $(date -d @$(tail -n1 $endBlockTimeFile) \
+'%A at %I:%M %P')"
# Date formatted as "[day of week] at [HH]:[MM] [am/pm]"
}
main () {
local functionName="main"
if [[ "$1" == "start" ]] ; then
if [[ -z "$2" ]] ; then
echo "Block time in minutes is needed."
echo "For an indefinite block, use 'endless'."
else
echo "Setting block"
start_block_with_expiration "$2"
fi
elif [[ "$1" == "endless" ]] ; then
echo "Setting block"
set_blocked_hosts
echo "" > "$endBlockTimeFile" # Wipe block time
elif [[ "$1" == "end" ]] ; then
remove_block_if_expired
elif [[ "$1" == "purge" ]] ; then
echo "Removing block"
restore_original_hosts
echo "" > "$endBlockTimeFile" # Wipe block time
echo "Block time wiped"
elif [[ "$1" == "status" ]] ; then
if check_if_blocked ; then
echo "Block is enabled and will expire at $(date -d \
@$(tail -n1 $endBlockTimeFile) \
+'%A at %I:%M %P')"
else
echo "Block is not enabled"
fi
elif [[ "$1" == "help" ]] ; then
echo "The following commands are accepted:"
echo " start --- Starts block. Requires duration in minutes."
echo " endless - Starts block without end"
echo " end ----- Ends expired blocks."
echo " purge --- Ends all blocks. Overrides timer."
echo " status -- Prints block status."
else
echo "Error from '$functionName': Unrecognized command."
fi
}
main "$@"