Adding support for redirecting authenticated media paths

This commit is contained in:
q 2024-09-23 16:20:07 -04:00
parent 6d20d53406
commit 19ee8f0e3b
4 changed files with 49 additions and 10 deletions

View File

@ -16,7 +16,17 @@ class MyServer:
self.blacklist = set(line.strip() for line in open('blacklist.txt')) self.blacklist = set(line.strip() for line in open('blacklist.txt'))
def __iter__(self): 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: if not hs in self.blacklist:
hsu = self.mappings.get(hs) hsu = self.mappings.get(hs)
@ -35,7 +45,7 @@ class MyServer:
else: else:
hsu = self.default_hs 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)]) self.start_response('301 Moved Permanently', [('Location', hsp)])
return iter([]) return iter([])

View File

@ -1,5 +1,5 @@
#!/usr/bin/python3 #!/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 import urllib.request
class MyServer: class MyServer:
@ -8,7 +8,18 @@ class MyServer:
self.start_response = start_response self.start_response = start_response
def __iter__(self): 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)]) self.start_response('301 Moved Permanently', [('Location', hsp)])
return iter([]) return iter([])
@ -30,8 +41,8 @@ if __name__ == "__main__":
options = { options = {
'bind': 'localhost:9999', '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 = GunicornServer(MyServer, options)
server.run() server.run()

View File

@ -15,8 +15,17 @@ class MyServer:
self.default_hs = "https://matrix-client.matrix.org" self.default_hs = "https://matrix-client.matrix.org"
self.whitelist = set(line.strip() for line in open('whitelist.txt')) self.whitelist = set(line.strip() for line in open('whitelist.txt'))
def __iter__(self): 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 hs in self.whitelist: if hs in self.whitelist:
hsu = self.mappings.get(hs) hsu = self.mappings.get(hs)
@ -35,7 +44,7 @@ class MyServer:
else: else:
hsu = self.default_hs 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)]) self.start_response('301 Moved Permanently', [('Location', hsp)])
return iter([]) return iter([])

View File

@ -14,7 +14,16 @@ class MyServer:
self.mappings = self.load_mappings() self.mappings = self.load_mappings()
def __iter__(self): 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) hsu = self.mappings.get(hs)
if not hsu: if not hsu: