From 19ee8f0e3b6587d9ce8a54f7bb6d07fcca7c6d17 Mon Sep 17 00:00:00 2001 From: q Date: Mon, 23 Sep 2024 16:20:07 -0400 Subject: [PATCH] Adding support for redirecting authenticated media paths --- custommedia-blacklist.py | 14 ++++++++++++-- custommedia-morg.py => custommedia-static.py | 19 +++++++++++++++---- custommedia-whitelist.py | 15 ++++++++++++--- custommedia.py | 11 ++++++++++- 4 files changed, 49 insertions(+), 10 deletions(-) rename custommedia-morg.py => custommedia-static.py (58%) diff --git a/custommedia-blacklist.py b/custommedia-blacklist.py index c720597..5a126a4 100755 --- a/custommedia-blacklist.py +++ b/custommedia-blacklist.py @@ -16,7 +16,17 @@ class MyServer: self.blacklist = set(line.strip() for line in open('blacklist.txt')) def __iter__(self): - hs = self.environ['PATH_INFO'].split('/')[5] + + path_info = hs = self.environ['PATH_INFO'].split('/') + + if path_info[2] == 'client': + mediatype = path_info[5] + hs = path_info[6] + mediaid = path_info[7] + else: + mediatype = path_info[4] + hs = path_info[5] + mediaid = path_info[6] if not hs in self.blacklist: hsu = self.mappings.get(hs) @@ -35,7 +45,7 @@ class MyServer: else: hsu = self.default_hs - hsp = hsu + self.environ['PATH_INFO'] + '?' + self.environ['QUERY_STRING'] + hsp = hsu + mediatype + '/' + hs + '/' + mediaid + '?' + self.environ['QUERY_STRING'] self.start_response('301 Moved Permanently', [('Location', hsp)]) return iter([]) diff --git a/custommedia-morg.py b/custommedia-static.py similarity index 58% rename from custommedia-morg.py rename to custommedia-static.py index 0c16de7..5cb3d04 100755 --- a/custommedia-morg.py +++ b/custommedia-static.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -# CustomMedia but all requests are just sent to morg instead of trying to resolve the origin server +# CustomMedia but all requests are just sent to a static server instead of trying to resolve the origin server import urllib.request class MyServer: @@ -8,7 +8,18 @@ class MyServer: self.start_response = start_response def __iter__(self): - hsp = "https://matrix.org" + self.environ['PATH_INFO'] + '?' + self.environ['QUERY_STRING'] + path_info = hs = self.environ['PATH_INFO'].split('/') + + if path_info[2] == 'client': + mediatype = path_info[5] + hs = path_info[6] + mediaid = path_info[7] + else: + mediatype = path_info[4] + hs = path_info[5] + mediaid = path_info[6] + + hsp = 'https://matrix.catgirl.cloud/_matrix/media/v3/' + mediatype + '/' + hs + '/' + mediaid + '?' + self.environ['QUERY_STRING'] self.start_response('301 Moved Permanently', [('Location', hsp)]) return iter([]) @@ -30,8 +41,8 @@ if __name__ == "__main__": options = { 'bind': 'localhost:9999', - 'workers': 32, # Adjust the number of workers based on your system's resources - ChatGPT + 'workers': 16, # Adjust the number of workers based on your system's resources - ChatGPT } server = GunicornServer(MyServer, options) - server.run() \ No newline at end of file + server.run() diff --git a/custommedia-whitelist.py b/custommedia-whitelist.py index cc00a9c..172700b 100755 --- a/custommedia-whitelist.py +++ b/custommedia-whitelist.py @@ -15,8 +15,17 @@ class MyServer: self.default_hs = "https://matrix-client.matrix.org" self.whitelist = set(line.strip() for line in open('whitelist.txt')) - def __iter__(self): - hs = self.environ['PATH_INFO'].split('/')[5] + def __iter__(self): + path_info = hs = self.environ['PATH_INFO'].split('/') + + if path_info[2] == 'client': + mediatype = path_info[5] + hs = path_info[6] + mediaid = path_info[7] + else: + mediatype = path_info[4] + hs = path_info[5] + mediaid = path_info[6] if hs in self.whitelist: hsu = self.mappings.get(hs) @@ -35,7 +44,7 @@ class MyServer: else: hsu = self.default_hs - hsp = hsu + self.environ['PATH_INFO'] + '?' + self.environ['QUERY_STRING'] + hsp = hsu + mediatype + '/' + hs + '/' + mediaid + '?' + self.environ['QUERY_STRING'] self.start_response('301 Moved Permanently', [('Location', hsp)]) return iter([]) diff --git a/custommedia.py b/custommedia.py index 1038ddc..dab0a03 100755 --- a/custommedia.py +++ b/custommedia.py @@ -14,7 +14,16 @@ class MyServer: self.mappings = self.load_mappings() def __iter__(self): - hs = self.environ['PATH_INFO'].split('/')[5] + path_info = hs = self.environ['PATH_INFO'].split('/') + + if path_info[2] == 'client': + mediatype = path_info[5] + hs = path_info[6] + mediaid = path_info[7] + else: + mediatype = path_info[4] + hs = path_info[5] + mediaid = path_info[6] hsu = self.mappings.get(hs) if not hsu: