1
1
mirror of https://gitlab.archlinux.org/archlinux/infrastructure.git synced 2026-03-05 19:51:30 +01:00
infrastructure/roles/archwiki/files/nginx-cache-purge
Kristian Klausen edb35bd433
Add opt-in Anubis for the wiki to combat scraping
The wiki has been brought down by scraping and Anubis has proven
effective at blocking this type of attack. The old challenging is
removed as it is mostly ineffective.

If is still undecided if we want Anubis permanently enabled, so for now
it is disabled by default but can quickly be enabled if needed.
2025-08-09 17:50:22 +02:00

31 lines
1.0 KiB
Python

#!/usr/bin/env python
import hashlib
import http.server
import pathlib
import socketserver
import urllib.parse
socketserver.ThreadingTCPServer.allow_reuse_address = True
class Handler(http.server.BaseHTTPRequestHandler):
def do_PURGE(self):
self.send_response(http.HTTPStatus.OK)
self.end_headers()
o = urllib.parse.urlparse(self.path)
for method in ["GET", "HEAD"]:
# Please keep in sync with "fastcgi_cache_key" in nginx.d.conf.j2
if o.query:
cache_key = f"http{method}{o.netloc}{o.path}?{o.query}"
else:
cache_key = f"http{method}{o.netloc}{o.path}"
hash = hashlib.md5(cache_key.encode("utf-8")).hexdigest()
# Please keep in sync with "fastcgi_cache_path" in nginx.d.conf.j2
pathlib.Path(
f"/var/lib/nginx/cache/{hash[-1]}/{hash[-3:-1]}/{hash}"
).unlink(missing_ok=True)
httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 1080), Handler)
httpd.serve_forever()