Initial fork
This commit is contained in:
commit
86083a9f86
7
LICENSE
Normal file
7
LICENSE
Normal file
@ -0,0 +1,7 @@
|
||||
Copyright 2022 +NIGGER License <https://plusnigger.org/>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice, this permission notice and the word "NIGGER" shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
44
README.md
Normal file
44
README.md
Normal file
@ -0,0 +1,44 @@
|
||||
# CustomMedia
|
||||
|
||||
A fork of [Plan9's CustomMedia project](https://gitea.plan9.rocks/cat/CustomMedia) that adds a whitelist-based version for choosing specific homeservers to pull media from directly. Conserve your matrix homeserver's precious storage space and bandwidth with this one simple webserver.
|
||||
|
||||
# Requirements
|
||||
- Python 3
|
||||
- gunicorn - `pip3 install gunicorn`
|
||||
|
||||
# Nginx configuration
|
||||
Add this inside of the block that matches for requests to /_matrix, replacing `server\.org` with your own homeserver
|
||||
```
|
||||
location ~ ^/_matrix/media/(?<folder>[^/]+)/((download|thumbnail)/(?!(server\.org)/))(?<file>.*)$ {
|
||||
proxy_pass http://localhost:9999;
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_read_timeout 3600s;
|
||||
add_header 'Access-Control-Allow-Origin' '*';
|
||||
add_header 'Access-Control-Allow-Credentials' 'true';
|
||||
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
|
||||
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
|
||||
}
|
||||
```
|
||||
|
||||
# systemd Service
|
||||
Clone this repository into /opt, check that custommedia.py has permission to execute, then use this systemd service (replacing custommedia.py with one of the other ones if desired):
|
||||
```
|
||||
[Unit]
|
||||
Description=Custom media
|
||||
After=network.target
|
||||
StartLimitIntervalSec=0
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
User=root
|
||||
WorkingDirectory=/opt/CustomMedia
|
||||
ExecStart=/opt/CustomMedia/custommedia.py
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
# Mappings
|
||||
CustomMedia uses a mappings.json file to cache which domain a homeserver's media is on (unless using the -morg.py version). This file will automatically be created in the working directory if one does not exist, but one has been provided here with some common homeservers.
|
37
custommedia-morg.py
Normal file
37
custommedia-morg.py
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/python3
|
||||
# CustomMedia but all requests are just sent to morg instead of trying to resolve the origin server
|
||||
import urllib.request
|
||||
|
||||
class MyServer:
|
||||
def __init__(self, environ, start_response):
|
||||
self.environ = environ
|
||||
self.start_response = start_response
|
||||
|
||||
def __iter__(self):
|
||||
hsp = "https://matrix.org" + self.environ['PATH_INFO'] + '?' + self.environ['QUERY_STRING']
|
||||
self.start_response('301 Moved Permanently', [('Location', hsp)])
|
||||
return iter([])
|
||||
|
||||
if __name__ == "__main__":
|
||||
from gunicorn.app.base import BaseApplication
|
||||
|
||||
class GunicornServer(BaseApplication):
|
||||
def __init__(self, app, options=None):
|
||||
self.options = options or {}
|
||||
self.application = app
|
||||
super().__init__()
|
||||
|
||||
def load_config(self):
|
||||
for key, value in self.options.items():
|
||||
self.cfg.set(key, value)
|
||||
|
||||
def load(self):
|
||||
return self.application
|
||||
|
||||
options = {
|
||||
'bind': 'localhost:9999',
|
||||
'workers': 32, # Adjust the number of workers based on your system's resources - ChatGPT
|
||||
}
|
||||
|
||||
server = GunicornServer(MyServer, options)
|
||||
server.run()
|
75
custommedia-whitelist.py
Executable file
75
custommedia-whitelist.py
Executable file
@ -0,0 +1,75 @@
|
||||
#!/usr/bin/python3
|
||||
import urllib.request
|
||||
import json
|
||||
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
|
||||
}
|
||||
|
||||
class MyServer:
|
||||
def __init__(self, environ, start_response):
|
||||
self.environ = environ
|
||||
self.start_response = start_response
|
||||
self.mapping_file = 'mapping.json'
|
||||
self.mappings = self.load_mappings()
|
||||
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]
|
||||
|
||||
if hs in self.whitelist:
|
||||
hsu = self.mappings.get(hs)
|
||||
|
||||
if not hsu:
|
||||
wellknown = "https://" + hs + "/.well-known/matrix/client"
|
||||
req = urllib.request.Request(wellknown, headers=headers)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=2) as cont:
|
||||
hsu = json.load(cont)
|
||||
hsu = hsu["m.homeserver"]["base_url"].rstrip('/')
|
||||
self.mappings[hs] = hsu
|
||||
self.save_mappings()
|
||||
except Exception as e: # I don't care to fix this properly, not my problem
|
||||
hsu = self.default_hs
|
||||
else:
|
||||
hsu = self.default_hs
|
||||
|
||||
hsp = hsu + self.environ['PATH_INFO'] + '?' + self.environ['QUERY_STRING']
|
||||
self.start_response('301 Moved Permanently', [('Location', hsp)])
|
||||
return iter([])
|
||||
|
||||
def load_mappings(self):
|
||||
try:
|
||||
with open(self.mapping_file, 'r') as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
def save_mappings(self):
|
||||
with open(self.mapping_file, 'w') as f:
|
||||
json.dump(self.mappings, f)
|
||||
|
||||
if __name__ == "__main__":
|
||||
from gunicorn.app.base import BaseApplication
|
||||
|
||||
class GunicornServer(BaseApplication):
|
||||
def __init__(self, app, options=None):
|
||||
self.options = options or {}
|
||||
self.application = app
|
||||
super().__init__()
|
||||
|
||||
def load_config(self):
|
||||
for key, value in self.options.items():
|
||||
self.cfg.set(key, value)
|
||||
|
||||
def load(self):
|
||||
return self.application
|
||||
|
||||
options = {
|
||||
'bind': 'localhost:9999',
|
||||
'workers': 48, # Adjust the number of workers based on your system's resources - ChatGPT
|
||||
}
|
||||
|
||||
server = GunicornServer(MyServer, options)
|
||||
server.run()
|
69
custommedia.py
Normal file
69
custommedia.py
Normal file
@ -0,0 +1,69 @@
|
||||
#!/usr/bin/python3
|
||||
import urllib.request
|
||||
import json
|
||||
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
|
||||
}
|
||||
|
||||
class MyServer:
|
||||
def __init__(self, environ, start_response):
|
||||
self.environ = environ
|
||||
self.start_response = start_response
|
||||
self.mapping_file = 'mapping.json'
|
||||
self.mappings = self.load_mappings()
|
||||
|
||||
def __iter__(self):
|
||||
hs = self.environ['PATH_INFO'].split('/')[5]
|
||||
hsu = self.mappings.get(hs)
|
||||
|
||||
if not hsu:
|
||||
wellknown = "https://" + hs + "/.well-known/matrix/client"
|
||||
req = urllib.request.Request(wellknown, headers=headers)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=2) as cont:
|
||||
hsu = json.load(cont)
|
||||
hsu = hsu["m.homeserver"]["base_url"].rstrip('/')
|
||||
self.mappings[hs] = hsu
|
||||
self.save_mappings()
|
||||
except Exception as e: # I don't care to fix this properly, not my problem
|
||||
hsu = "https://matrix-client.matrix.org"
|
||||
|
||||
hsp = hsu + self.environ['PATH_INFO'] + '?' + self.environ['QUERY_STRING']
|
||||
self.start_response('301 Moved Permanently', [('Location', hsp)])
|
||||
return iter([])
|
||||
|
||||
def load_mappings(self):
|
||||
try:
|
||||
with open(self.mapping_file, 'r') as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
def save_mappings(self):
|
||||
with open(self.mapping_file, 'w') as f:
|
||||
json.dump(self.mappings, f)
|
||||
|
||||
if __name__ == "__main__":
|
||||
from gunicorn.app.base import BaseApplication
|
||||
|
||||
class GunicornServer(BaseApplication):
|
||||
def __init__(self, app, options=None):
|
||||
self.options = options or {}
|
||||
self.application = app
|
||||
super().__init__()
|
||||
|
||||
def load_config(self):
|
||||
for key, value in self.options.items():
|
||||
self.cfg.set(key, value)
|
||||
|
||||
def load(self):
|
||||
return self.application
|
||||
|
||||
options = {
|
||||
'bind': 'localhost:9999',
|
||||
'workers': 32, # Adjust the number of workers based on your system's resources - ChatGPT
|
||||
}
|
||||
|
||||
server = GunicornServer(MyServer, options)
|
||||
server.run()
|
1
mapping.json
Normal file
1
mapping.json
Normal file
@ -0,0 +1 @@
|
||||
{"catgirl.cloud": "https://matrix.catgirl.cloud/", "midov.pl": "https://midov.pl", "denshi.org": "https://denshi.org", "yuri.im": "https://yuri.im:8448/", "maunium.net": "https://api.mau.chat", "miau.chat": "https://miau.chat/", "cutefunny.art": "https://matrix.cutefunny.art", "poa.st": "https://matrix.poast.org:8448", "glowers.club": "https://glowers.club", "cia.govt.hu": "https://cia.govt.hu", "matrix.abuser.eu": "https://matrix.abuser.eu:8448", "matrix.im": "https://matrix.im", "matrix.juggler.jp": "https://matrix.juggler.jp/", "linuxdelta.com": "https://matrix.linuxdelta.com", "nerdsin.space": "https://nerdsin.space/", "perthchat.org": "https://matrix.perthchat.org", "waifuhunter.club": "https://matrix.waifuhunter.club", "eientei.org": "https://matrix.eientei.org/", "g33k.se": "https://g33k.se", "nitro.chat": "https://nitro.chat", "chat.mistli.net": "https://chat.mistli.net/", "matrix.org": "https://matrix-client.matrix.org", "lolifan.club": "https://matrix.lolifan.club", "matrix.fedibird.com": "https://matrix.fedibird.com/", "agdersam.no": "https://agdersam.no", "kit.edu": "https://matrix.scc.kit.edu", "t2bot.io": "https://t2bot.io", "pettan.cc": "https://matrix.pettan.cc", "matrix.thisisjoes.site": "https://matrix-client.matrix.org", "unredacted.org": "https://matrix.unredacted.org", "matrix.bottomservices.club": "https://matrix.bottomservices.club", "m.wfr.moe": "https://m.wfr.moe", "kde.org": "https://kde.modular.im", "nibbana.jp": "https://nibbana.jp", "matrixim.cc": "https://matrixim.cc", "asra.gr": "https://asra.gr", "gnu.moe": "https://matrix.gnu.moe", "crossbach.de": "https://matrix.crossbach.de", "tchncs.de": "https://matrix.tchncs.de", "cat.casa": "https://matrix.cat.casa", "synapse.travnewmatic.com": "https://synapse.travnewmatic.com/", "halogen.city": "https://halogen.city", "interlaced-insanity.com": "https://interlaced-insanity.com", "lolisho.chat": "https://lolisho.chat", "tedomum.net": "https://matrix.tedomum.net", "monero.social": "https://matrix.monero.social", "animegirls.win": "https://animegirls.win", "fairydust.space": "https://matrix.fairydust.space", "hot-chilli.im": "https://hot-chilli.im", "yesmap.net": "https://matrix.yesmap.net", "basket-weaving.kyrgyzstan.kg": "https://basket-weaving.kyrgyzstan.kg", "noevil.pl": "https://matrix.noevil.pl", "zspn.me": "https://matrix.zspn.me"}
|
3
whitelist.txt
Normal file
3
whitelist.txt
Normal file
@ -0,0 +1,3 @@
|
||||
nerdsin.space
|
||||
midov.pl
|
||||
ihazurinter.net
|
Loading…
Reference in New Issue
Block a user