forked from ph00lt0/blocklist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-exclusions.sh
More file actions
executable file
·345 lines (272 loc) · 13 KB
/
apply-exclusions.sh
File metadata and controls
executable file
·345 lines (272 loc) · 13 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
set -e
EXCLUSIONS_FILE="my-exclusions.txt"
INCLUSIONS_FILE="my-inclusions.txt"
# ─────────────────────────────────────────────
# 1. RESTORE FORK-SPECIFIC FILES
# Ensures HTML install files always point to
# this repo, not the upstream original.
# ─────────────────────────────────────────────
echo "Restoring fork-specific HTML files..."
cat > install.html << 'EOF'
<!DOCTYPE html>
<html class="js" lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ph00lt0 - blocklist installation</title>
</head>
<body>
<a target="_blank" href="abp:subscribe?location=https%3A%2F%2Fraw.githubusercontent.com%2FCaptainCodeAU%2Flittlesnitch_blocklist%2Fmaster%2Fblocklist.txt&title=ph00lt0%20-%20blocklist" title="ph00lt0 - blocklist">
Click here to install the blocklist
</a>, else follow manual instructions
</body>
</html>
EOF
cat > little-snitch-install.html << 'EOF'
<!DOCTYPE html>
<html class="js" lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ph00lt0 - blocklist installation for Little Snitch</title>
<script>
window.location.href="x-littlesnitch:subscribe-rules?url=https://raw.githubusercontent.com/CaptainCodeAU/littlesnitch_blocklist/master/little-snitch-blocklist.lsrules"
</script>
</head>
<body>
<a target="_blank" href="x-littlesnitch:subscribe-rules?url=https://raw.githubusercontent.com/CaptainCodeAU/littlesnitch_blocklist/master/little-snitch-blocklist.lsrules">
Click here to install the blocklist
</a>, else follow manual instructions
</body>
</html>
EOF
echo "HTML files restored."
# ─────────────────────────────────────────────
# 2. FIX README URLS
# Replaces upstream URLs with fork-specific
# URLs in README.md after every sync.
# ─────────────────────────────────────────────
echo "Fixing README URLs..."
# Replace raw githubusercontent URLs
sed -i.bak 's|https://raw.githubusercontent.com/ph00lt0/blocklist/master/|https://raw.githubusercontent.com/CaptainCodeAU/littlesnitch_blocklist/master/|g' README.md
# Replace GitHub Pages URLs
sed -i.bak 's|https://ph00lt0.github.io/blocklist/|https://captaincodeau.github.io/littlesnitch_blocklist/|g' README.md
# Replace abp: protocol links
sed -i.bak 's|abp:subscribe?location=https%3A%2F%2Fraw.githubusercontent.com%2Fph00lt0%2Fblocklist%2Fmaster%2Fblocklist.txt&title=ph00lt0%20-%20blocklist|https://captaincodeau.github.io/littlesnitch_blocklist/install.html|g' README.md
# Replace x-littlesnitch: protocol links
sed -i.bak 's|x-littlesnitch:subscribe-rules?url=https://raw.githubusercontent.com/ph00lt0/blocklist/master/little-snitch-blocklist.lsrules|https://captaincodeau.github.io/littlesnitch_blocklist/little-snitch-install.html|g' README.md
# Replace GitHub issue links
sed -i.bak 's|https://github.com/ph00lt0/blocklist/issues|https://github.com/CaptainCodeAU/littlesnitch_blocklist/issues|g' README.md
rm -f README.md.bak
echo "README URLs fixed."
# ─────────────────────────────────────────────
# 3. FIX LITTLE SNITCH METADATA
# Updates name and description in .lsrules
# to point to this fork.
# ─────────────────────────────────────────────
echo "Fixing Little Snitch metadata..."
python3 - << 'PYEOF'
import json
with open('little-snitch-blocklist.lsrules', 'r') as f:
data = json.load(f)
data['name'] = 'CaptainCodeAU - blocklist'
data['description'] = 'https://github.com/CaptainCodeAU/littlesnitch_blocklist'
with open('little-snitch-blocklist.lsrules', 'w') as f:
json.dump(data, f, indent=4)
print("Little Snitch metadata updated.")
PYEOF
# ─────────────────────────────────────────────
# 4. APPLY DOMAIN EXCLUSIONS
# Strips excluded domains from all blocklist
# files across all supported formats.
# ─────────────────────────────────────────────
if [ ! -f "$EXCLUSIONS_FILE" ]; then
echo "No exclusions file found, skipping."
else
while IFS= read -r domain || [ -n "$domain" ]; do
[[ -z "$domain" || "$domain" == \#* ]] && continue
echo "Removing entries matching: $domain"
# blocklist.txt — ||domain^
sed -i.bak "/||${domain}^/d" blocklist.txt
# wildcard-blocklist.txt — *.domain
sed -i.bak "/\*\.${domain}/d" wildcard-blocklist.txt
# unbound-blocklist.txt — local-zone: "domain." always_null
sed -i.bak "/local-zone: \"${domain}\.\" always_null/d" unbound-blocklist.txt
# rpz-blocklist.txt — domain CNAME .
sed -i.bak "/^${domain} CNAME \./d" rpz-blocklist.txt
# domains.txt — plain domain
sed -i.bak "/^${domain}$/d" domains.txt
# pihole-blocklist.txt and hosts-blocklist.txt — 0.0.0.0 domain
sed -i.bak "/^0\.0\.0\.0 ${domain}$/d" pihole-blocklist.txt
sed -i.bak "/^0\.0\.0\.0 ${domain}$/d" hosts-blocklist.txt
# little-snitch-blocklist.lsrules — via Python (JSON-safe)
python3 - << PYEOF
import json
with open('little-snitch-blocklist.lsrules', 'r') as f:
data = json.load(f)
domain = "${domain}"
data['denied-remote-domains'] = [d for d in data.get('denied-remote-domains', []) if d != domain]
with open('little-snitch-blocklist.lsrules', 'w') as f:
json.dump(data, f, indent=4)
PYEOF
# Clean up .bak files
rm -f blocklist.txt.bak wildcard-blocklist.txt.bak unbound-blocklist.txt.bak \
rpz-blocklist.txt.bak domains.txt.bak pihole-blocklist.txt.bak \
hosts-blocklist.txt.bak
done < "$EXCLUSIONS_FILE"
echo "Done. All exclusions applied."
fi
# ─────────────────────────────────────────────
# 5. APPLY SURGICAL INCLUSIONS
# Removes exact bare domain matches only.
# Never removes wildcards or subdomains.
# ─────────────────────────────────────────────
if [ ! -f "$INCLUSIONS_FILE" ]; then
echo "No inclusions file found, skipping."
exit 0
fi
while IFS= read -r domain || [ -n "$domain" ]; do
[[ -z "$domain" || "$domain" == \#* ]] && continue
echo "Unblocking exact domain: $domain"
# blocklist.txt — exact ||domain^ only
sed -i.bak "/^||${domain}\^$/d" blocklist.txt
# domains.txt — exact domain only
sed -i.bak "/^${domain}$/d" domains.txt
# pihole-blocklist.txt and hosts-blocklist.txt — exact 0.0.0.0 domain only
sed -i.bak "/^0\.0\.0\.0 ${domain}$/d" pihole-blocklist.txt
sed -i.bak "/^0\.0\.0\.0 ${domain}$/d" hosts-blocklist.txt
# unbound-blocklist.txt — exact local-zone: "domain." always_null only
sed -i.bak "/^local-zone: \"${domain}\.\" always_null$/d" unbound-blocklist.txt
# rpz-blocklist.txt — exact domain CNAME . only
sed -i.bak "/^${domain} CNAME \.$/d" rpz-blocklist.txt
# little-snitch-blocklist.lsrules — via Python (JSON-safe, exact match only)
python3 - << PYEOF
import json
with open('little-snitch-blocklist.lsrules', 'r') as f:
data = json.load(f)
domain = "${domain}"
data['denied-remote-domains'] = [d for d in data.get('denied-remote-domains', []) if d != domain]
with open('little-snitch-blocklist.lsrules', 'w') as f:
json.dump(data, f, indent=4)
PYEOF
# Clean up .bak files
rm -f blocklist.txt.bak domains.txt.bak pihole-blocklist.txt.bak \
hosts-blocklist.txt.bak unbound-blocklist.txt.bak rpz-blocklist.txt.bak
done < "$INCLUSIONS_FILE"
echo "Done. All inclusions applied."
# ─────────────────────────────────────────────
# 6. APPLY DOMAIN ADDITIONS
# Adds domains from my-additions.txt to all
# blocklist formats (if not already present).
# ─────────────────────────────────────────────
ADDITIONS_FILE="my-additions.txt"
if [ ! -f "$ADDITIONS_FILE" ]; then
echo "No additions file found, skipping."
else
while IFS= read -r domain || [ -n "$domain" ]; do
[[ -z "$domain" || "$domain" == \#* ]] && continue
echo "Adding domain: $domain"
# blocklist.txt — ||domain^
grep -qxF "||${domain}^" blocklist.txt 2>/dev/null || echo "||${domain}^" >> blocklist.txt
# domains.txt — plain domain
grep -qxF "${domain}" domains.txt 2>/dev/null || echo "${domain}" >> domains.txt
# wildcard-blocklist.txt — *.domain
grep -qxF "*.${domain}" wildcard-blocklist.txt 2>/dev/null || echo "*.${domain}" >> wildcard-blocklist.txt
# unbound-blocklist.txt — local-zone: "domain." always_null
grep -qxF "local-zone: \"${domain}.\" always_null" unbound-blocklist.txt 2>/dev/null || echo "local-zone: \"${domain}.\" always_null" >> unbound-blocklist.txt
# rpz-blocklist.txt — domain CNAME .
grep -qxF "${domain} CNAME ." rpz-blocklist.txt 2>/dev/null || echo "${domain} CNAME ." >> rpz-blocklist.txt
# pihole-blocklist.txt — 0.0.0.0 domain
grep -qxF "0.0.0.0 ${domain}" pihole-blocklist.txt 2>/dev/null || echo "0.0.0.0 ${domain}" >> pihole-blocklist.txt
# hosts-blocklist.txt — 0.0.0.0 domain
grep -qxF "0.0.0.0 ${domain}" hosts-blocklist.txt 2>/dev/null || echo "0.0.0.0 ${domain}" >> hosts-blocklist.txt
# little-snitch-blocklist.lsrules — add to denied-remote-domains JSON array
python3 - << PYEOF
import json
with open('little-snitch-blocklist.lsrules', 'r') as f:
data = json.load(f)
domain = "${domain}"
if domain not in data.get('denied-remote-domains', []):
data.setdefault('denied-remote-domains', []).append(domain)
with open('little-snitch-blocklist.lsrules', 'w') as f:
json.dump(data, f, indent=4)
PYEOF
done < "$ADDITIONS_FILE"
# Re-sort text-based blocklist files
for f in blocklist.txt domains.txt wildcard-blocklist.txt unbound-blocklist.txt \
rpz-blocklist.txt pihole-blocklist.txt hosts-blocklist.txt; do
sort -uf "$f" -o "$f"
done
echo "Done. All additions applied."
fi
# ─────────────────────────────────────────────
# 7. LOCAL RUN CLEANUP
# When running locally (not in GitHub Actions),
# restore all generated blocklist files to their
# git-committed state. This prevents accidentally
# committing blocklist changes that should only
# be committed by the GitHub Actions workflow.
# ─────────────────────────────────────────────
if [ "$GITHUB_ACTIONS" != "true" ]; then
echo ""
echo "═══════════════════════════════════════════"
echo " LOCAL RUN DETECTED"
echo "═══════════════════════════════════════════"
echo ""
echo "Verifying inclusions were removed from lsrules..."
INCLUSION_ERRORS=0
if [ -f "$INCLUSIONS_FILE" ]; then
while IFS= read -r domain || [ -n "$domain" ]; do
[[ -z "$domain" || "$domain" == \#* ]] && continue
if grep -q "\"${domain}\"" little-snitch-blocklist.lsrules 2>/dev/null; then
echo " ✗ STILL PRESENT: $domain"
INCLUSION_ERRORS=1
else
echo " ✓ Removed: $domain"
fi
done < "$INCLUSIONS_FILE"
fi
echo ""
echo "Verifying additions are present in lsrules..."
ADDITION_ERRORS=0
if [ -f "$ADDITIONS_FILE" ]; then
while IFS= read -r domain || [ -n "$domain" ]; do
[[ -z "$domain" || "$domain" == \#* ]] && continue
if grep -q "\"${domain}\"" little-snitch-blocklist.lsrules 2>/dev/null; then
echo " ✓ Present: $domain"
else
echo " ✗ MISSING: $domain"
ADDITION_ERRORS=1
fi
done < "$ADDITIONS_FILE"
fi
echo ""
if [ "$INCLUSION_ERRORS" -eq 1 ] || [ "$ADDITION_ERRORS" -eq 1 ]; then
echo "⚠ Verification found issues — review above."
else
echo "All verifications passed."
fi
echo ""
echo "Restoring blocklist files to git state..."
GENERATED_FILES=(
blocklist.txt
domains.txt
wildcard-blocklist.txt
unbound-blocklist.txt
rpz-blocklist.txt
pihole-blocklist.txt
hosts-blocklist.txt
little-snitch-blocklist.lsrules
install.html
little-snitch-install.html
README.md
)
git checkout -- "${GENERATED_FILES[@]}"
echo "Done. Blocklist files restored — only my-*.txt changes remain."
echo ""
echo "DO NOT commit blocklist files manually."
echo "The GitHub Actions workflow handles that on sync."
fi