-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfireblock.sh
executable file
·47 lines (40 loc) · 1.45 KB
/
fireblock.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
#!/bin/bash
POLICY_FILE="/usr/lib/firefox/distribution/policies.json"
# POLICY_FILE="/etc/firefox/policies/policies.json" # System-wide setting
# Ensure the policy file exists
mkdir -p "$(dirname "$POLICY_FILE")"
if [[ ! -f "$POLICY_FILE" ]]; then
echo '{ "policies": { "WebsiteFilter": { "Block": [], "Exceptions": [] } } }' > "$POLICY_FILE"
fi
# Read the existing policies
BLOCKED_SITES=$(jq -r '.policies.WebsiteFilter.Block // []' "$POLICY_FILE")
EXCEPTIONS=$(jq -r '.policies.WebsiteFilter.Exceptions // []' "$POLICY_FILE")
# Parse arguments (check for flags)
ADD_EXCEPTIONS=false
SITES=()
while [[ "$#" -gt 0 ]]; do
case "$1" in
-e)
ADD_EXCEPTIONS=true
;;
*)
SITES+=("$1")
;;
esac
shift
done
# Add sites to the appropriate list, avoiding duplicates
for SITE in "${SITES[@]}"; do
if [ "$ADD_EXCEPTIONS" = true ]; then
if ! echo "$EXCEPTIONS" | grep -q "$SITE"; then
EXCEPTIONS=$(echo "$EXCEPTIONS" | jq ". + [\"$SITE\"]")
fi
else
if ! echo "$BLOCKED_SITES" | grep -q "$SITE"; then
BLOCKED_SITES=$(echo "$BLOCKED_SITES" | jq ". + [\"$SITE\"]")
fi
fi
done
# Write updated policies back
jq ".policies.WebsiteFilter.Block = $BLOCKED_SITES | .policies.WebsiteFilter.Exceptions = $EXCEPTIONS" "$POLICY_FILE" | sudo tee "${POLICY_FILE}.tmp" && sudo mv "${POLICY_FILE}.tmp" "$POLICY_FILE"
echo "Updated policies in $POLICY_FILE"