qutebrowser_configs/configs/interceptor-cloudflare.py
2024-04-30 14:51:36 +00:00

57 lines
2.5 KiB
Python

import re
import sys
try:
import qutebrowser.api
from qutebrowser.api import interceptor
from qutebrowser.extensions.interceptors import Request
from qutebrowser.qt.core import QUrl
from qutebrowser.utils import debug, log
except ImportError:
sys.stderr.write('qutebrowser not imported\n')
else:
# redirect on cloudflare
def filter_cf(thereq: Request) -> None:
"""Block given request if necessary
Called by a signal"""
from qutebrowser.utils import objreg
from qutebrowser.mainwindow import mainwindow
url = thereq.request_url
if url.query().startswith('ray='):
log.network.debug(f"filter_cf thereq.first_party_url={thereq.first_party_url} {repr(thereq)}")
if thereq.is_blocked is False and (
url.query().startswith('__cf_chl_rt_tk=') or \
url.host() == 'challenges.cloudflare.com' or \
url.path().startswith('/cdn-cgi/challenge-platform/h/')):
# b/turnstile/if/ov2/av0/rcv0/0/33izj/
# g/orchestrate/chl_page/v1
url.setPath('/web' + '/*/' +thereq.first_party_url.host()
+thereq.first_party_url.path())
url.setHost('web.archive.org')
# "__cf_chl_rt_tk="[A-Za-z0-9.-]*"
url.setQuery(None)
log.network.info(f"config.filter_cf REDIRECTING to {url}")
thereq.request_url = url
thereq._redirected = True
thereq.is_blocked = True
if False:
# doesn't work at least on PyQt5 linux
thereq.redirect(url)
elif True:
#
thereq.block()
from qutebrowser.config import config
# expanded open_desktopservices_url
target = objreg.last_opened_window()
#? target = config.val.new_instance_open_target
window = mainwindow.get_window(via_ipc=False, target=target)
tab = window.tabbed_browser._current_tab()
window.tabbed_browser.tabopen(url,
background=False, related=True)
# NO window.maybe_raise()
# crimeflare tabs keep refreshing infinitely
window.tabbed_browser.close_tab(tab)
interceptor.register(filter_cf)
log.init.debug(f"loaded filter_cf interceptor")