2023-01-16 16:36:14 +00:00
|
|
|
import random
|
|
|
|
import re
|
2024-02-05 11:09:23 +00:00
|
|
|
from typing import Any, Callable
|
|
|
|
from urllib import parse
|
|
|
|
|
2023-01-16 16:36:14 +00:00
|
|
|
from qutebrowser.api import interceptor
|
2023-08-29 20:45:09 +00:00
|
|
|
from qutebrowser.extensions.interceptors import QUrl, RedirectException
|
2023-05-23 13:42:03 +00:00
|
|
|
from qutebrowser.utils import message
|
|
|
|
|
2023-08-29 20:45:09 +00:00
|
|
|
|
|
|
|
def fixScribePath(url: QUrl):
|
|
|
|
"""Fix external medium blog to scribe translation.
|
2023-05-23 13:42:03 +00:00
|
|
|
Some paths from medium will go through a 'global identity'
|
|
|
|
path which messes up the actual url path we want to go
|
|
|
|
to and puts it in queries. This puts it back on the path.
|
|
|
|
"""
|
2023-08-29 20:45:09 +00:00
|
|
|
# double unquoting necessary!
|
|
|
|
# I suppose we double-wrap it earlier somewhere?
|
|
|
|
# unquoted = parse.unquote(
|
|
|
|
# url.path(options=QUrl.ComponentFormattingOption.FullyEncoded)
|
|
|
|
# )
|
2023-10-03 11:43:00 +00:00
|
|
|
path = parse.unquote(f"{url.path()}{url.query()}", encoding="ascii")
|
2023-08-29 20:45:09 +00:00
|
|
|
url.setQuery(None)
|
|
|
|
new_path = re.sub(r"m/global-identity-2redirectUrl=", "", path)
|
|
|
|
url.setPath(
|
|
|
|
parse.quote(new_path),
|
|
|
|
mode=QUrl.ParsingMode.StrictMode,
|
|
|
|
)
|
|
|
|
return url
|
|
|
|
|
2023-01-16 16:36:14 +00:00
|
|
|
|
|
|
|
redirects = {
|
|
|
|
"youtube": {
|
|
|
|
"source": ["youtube.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["invidious"],
|
|
|
|
},
|
|
|
|
"stackoverflow": {
|
|
|
|
"source": ["stackoverflow.com"],
|
|
|
|
"farside": ["anonymousoverflow"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"lbry": {
|
|
|
|
"source": ["odysee.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["librarian"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"reddit": {
|
|
|
|
"source": ["reddit.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["redlib"],
|
|
|
|
},
|
|
|
|
"instagram": {
|
|
|
|
"source": ["instagram.com"],
|
|
|
|
"farside": ["proxigram"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"twitter": {
|
|
|
|
"source": ["twitter.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["nitter"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"imdb": {
|
|
|
|
"source": ["imdb.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["libremdb"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"translate": {
|
|
|
|
"source": ["translate.google.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["lingva"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"tiktok": {
|
|
|
|
"source": ["tiktok.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["proxitok"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"imgur": {
|
|
|
|
"source": ["imgur.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["rimgo"],
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"medium": {
|
|
|
|
"source": ["medium.com"],
|
2024-02-05 11:09:23 +00:00
|
|
|
"farside": ["scribe"],
|
|
|
|
# "postprocess": fixScribePath
|
|
|
|
},
|
|
|
|
"fandom": {
|
|
|
|
"source": ["fandom.com"],
|
|
|
|
"farside": ["breezewiki"],
|
|
|
|
},
|
|
|
|
"quora": {
|
|
|
|
"source": ["quora.com"],
|
|
|
|
"farside": ["quetre"],
|
|
|
|
# "postprocess": lambda url: message.info(f"CALLING QUORA WITH {url}")
|
2023-01-16 16:36:14 +00:00
|
|
|
},
|
|
|
|
"google": {
|
|
|
|
"source": ["google.com"],
|
|
|
|
"target": [
|
2024-02-05 11:09:23 +00:00
|
|
|
"search.albony.xyz",
|
|
|
|
"search.garudalinux.org",
|
|
|
|
"search.dr460nf1r3.org",
|
|
|
|
"s.tokhmi.xyz",
|
|
|
|
"search.sethforprivacy.com",
|
2023-01-16 16:36:14 +00:00
|
|
|
"whoogle.dcs0.hu",
|
2024-02-05 11:09:23 +00:00
|
|
|
"gowogle.voring.me",
|
|
|
|
"whoogle.privacydev.net",
|
|
|
|
"wg.vern.cc",
|
|
|
|
"whoogle.hxvy0.gq",
|
|
|
|
"whoogle.hostux.net",
|
|
|
|
"whoogle.lunar.icu",
|
|
|
|
"wgl.frail.duckdns.org",
|
|
|
|
"whoogle.no-logs.com",
|
|
|
|
"whoogle.ftw.lol",
|
|
|
|
"whoogle-search--replitcomreside.repl.co",
|
|
|
|
"search.notrustverify.ch",
|
|
|
|
"whoogle.datura.network",
|
|
|
|
"whoogle.yepserver.xyz",
|
|
|
|
"search.nezumi.party",
|
2023-01-16 16:36:14 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:24:27 +00:00
|
|
|
|
2023-10-03 11:43:00 +00:00
|
|
|
def rewrite(request: interceptor.Request) -> None:
|
|
|
|
# if config.get(name="content.oss_redirects") is False:
|
|
|
|
# return
|
2023-03-22 10:48:57 +00:00
|
|
|
|
2023-01-16 16:36:14 +00:00
|
|
|
if (
|
|
|
|
request.resource_type != interceptor.ResourceType.main_frame
|
|
|
|
or request.request_url.scheme() in {"data", "blob"}
|
|
|
|
):
|
|
|
|
return
|
|
|
|
|
|
|
|
url = request.request_url
|
|
|
|
|
2023-08-29 20:45:09 +00:00
|
|
|
if service := _should_be_redirected(url.host()):
|
2024-02-05 11:09:23 +00:00
|
|
|
if "farside" in service:
|
|
|
|
url = _farside_redirect(url, _pick_random(service["farside"]))
|
|
|
|
else:
|
|
|
|
srv = _pick_random(service["target"])
|
|
|
|
url = _target_redirect(url, srv)
|
|
|
|
try:
|
|
|
|
request.redirect(url)
|
|
|
|
except RedirectException as e:
|
|
|
|
message.error(str(e))
|
|
|
|
|
|
|
|
if "postprocess" in service and isinstance(service["postprocess"], Callable):
|
|
|
|
url = service["postprocess"](url)
|
|
|
|
|
|
|
|
|
|
|
|
def _farside_redirect(url: QUrl, service: str) -> QUrl:
|
|
|
|
try:
|
|
|
|
url.setHost("farside.link")
|
|
|
|
url.setPath(f"/{service}{url.path()}")
|
|
|
|
except RedirectException as e:
|
|
|
|
message.error(str(e))
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
def _target_redirect(url: QUrl, target: str) -> QUrl:
|
|
|
|
if target is not None and url.setHost(target) is not False:
|
|
|
|
return url
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
def _pick_random(choices: list) -> Any:
|
|
|
|
return choices[random.randint(0, len(choices) - 1)]
|
|
|
|
|
|
|
|
|
|
|
|
def _should_be_redirected(
|
|
|
|
host: str, redirects: dict = redirects
|
|
|
|
) -> dict[str, list] | None:
|
2023-01-16 16:36:14 +00:00
|
|
|
for service in redirects.values():
|
|
|
|
for source in service["source"]:
|
2023-08-29 20:45:09 +00:00
|
|
|
if re.search(source, host):
|
|
|
|
return service
|
|
|
|
return None
|
2023-01-16 16:36:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
interceptor.register(rewrite)
|