commit ae59e020d8b4631939257553dac57cf75de1d040 Author: trinkey Date: Fri Dec 20 17:42:23 2024 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..70b571d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +secret.py +blocked.json diff --git a/main.py b/main.py new file mode 100644 index 0000000..a4d43b8 --- /dev/null +++ b/main.py @@ -0,0 +1,81 @@ +import json +import time +from pathlib import Path + +import flask +import requests + +from secret import token + +REVERSE_PROXY = False # if True: uses X-Real-IP header instead of remote addr +BASE_DIR = Path(__file__).parent +TIMEOUT = 15 * 60 +CONTENT_WARNING = "test post ignore" # "Post from a random person - trinkey is not responsible for the content!!" +POST_URL = "https://is.trinkey.com/api/iceshrimp/notes" +RUN_CONF = { + "debug": True, + "host": "127.0.0.1", + "port": "8000" +} + +app = flask.Flask(__file__) + +data: dict[str, int] = {} + +try: + data = json.load(open(BASE_DIR / "blocked.json", "r")) +except FileNotFoundError: + ... +except json.JSONDecodeError: + ... + +def save(): + with open(BASE_DIR / "blocked.json", "w") as f: + json.dump(data, f) + +def get_ip() -> str: + return (flask.request.headers.get("X-Real-IP") if REVERSE_PROXY else flask.request.remote_addr) or "0.0.0.0" + +app.route("/about/")(lambda: open(BASE_DIR / "public/about.html", "rb").read()) + +@app.route("/", methods=["POST", "GET"]) +def index() -> bytes: + ip = get_ip() + cant_post = ip in data and data[ip] > time.time() + message = "" + + if flask.request.method == "POST": + if not cant_post: + content = (flask.request.form.get("text") or "").strip() + + if content: + resp = requests.post(POST_URL, json={ + "text": content, + "cw": CONTENT_WARNING, + "replyId": None, + "renoteId": None, + "mediaIds": None, + "visibility": "home", + "idempotencyKey": None + }, headers={ + "Authorization": f"Bearer {token}" + }) + + if resp.status_code == 200: + data[ip] = int(time.time() + TIMEOUT) + save() + message = "Success!" + else: + message = f"Got non-200 status code ({resp})" + else: + message = "Enter some text!" + else: + message = "You can't post yet!" + + return open(BASE_DIR / "public/index.html", "rb").read() \ + .replace(b"{{ IP }}", str.encode(ip)) \ + .replace(b"{{ EXPIRE }}", str.encode(str(data[ip] if cant_post else 0))) \ + .replace(b"{{ ERROR }}", str.encode(message)) + +if __name__ == "__main__": + app.run(**RUN_CONF) diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..006382d --- /dev/null +++ b/public/index.html @@ -0,0 +1,55 @@ + + + + + EveryoneBot + + + + + + + + + + + + + + + + + +

EveryoneBot

+

The fedi bot anyone can post to

+ Source Code - Fedi: @everyonebot@is.trinkey.com +

{{ ERROR }}

+
+

Enter your post below. Anyone can post anything, once every 15 minutes per IP address.

+
+
+
+
+ Moderation enforced at trinkey's discretion. This service can go down temporarily or permanently at any time. All posts are viewable by anyone and cannot be deleted. +

You can post in Please enable JavaScript for this to work!

+

+ Your IP address: {{ IP }} +

+ + + +