From 23cdd2f7b0a4f6a78401d7e34861a1534703c7be Mon Sep 17 00:00:00 2001 From: swee Date: Mon, 13 Jan 2025 15:53:40 -0800 Subject: [PATCH] Update server.py --- server.py | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/server.py b/server.py index 773957e..b5c0704 100644 --- a/server.py +++ b/server.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 import asyncio, traceback, socket, ssl from urllib.parse import urlparse -from flask import Flask, request, redirect +from flask import Flask, request, redirect, send_file from hypercorn.config import Config from hypercorn.asyncio import serve app = Flask(__name__) @@ -26,6 +26,12 @@ def root(): """ +@app.route("/external.png") +def external(): + return send_file("external.png") +@app.route("/cross-server.png") +def crosserver(): + return send_file("cross-server.png") @app.route("/gem") def relay(): url = request.args.get('gemini') @@ -37,19 +43,15 @@ def relay(): gsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) fulladdr = "gemini://" + url gemsocket = ssl._create_unverified_context().wrap_socket(gsocket, server_hostname=urlparse(fulladdr).hostname) - print(fulladdr) - print(urlparse(fulladdr).hostname) gemsocket.connect((urlparse(fulladdr).hostname, 1965)) gemsocket.send(bytes("gemini://" + url + "\r\n", "UTF-8")) received = "" while True: gemresponse = gemsocket.recv(2048) - print(gemresponse) if gemresponse.decode() != "": received += gemresponse.decode() else: break - print(received) received = received.replace("\r", "") firstline = True redirected = False @@ -79,10 +81,30 @@ def relay(): code += f"

{temp}

\n" elif i[0:2] == "* ": temp = i[2:] - code += f"" + code += f"\n" + elif i[0:2] == "=>": + temp = i[2:].strip() + goto = temp.split(" ")[0] + prse = urlparse(goto) + extra = "" + if prse.netloc == "" and prse.scheme == "": + isdir = url[len(url) - 1] == "/" + if isdir: + tempurl = url + goto + else: + tempurl = "/".join(url.split("/")[:-1]) + "/" + goto + goto = f"/gem?gemini={tempurl}" + elif prse.scheme != "gemini": + extra = "" + elif prse.hostname != urlparse(fulladdr).hostname: + extra = "" + if temp.split(" ") == 1: + comment = goto + else: + comment = " ".join(temp.split(" ")[1:]) + code += f"{comment} {extra}\n" else: code += f"

{i}

\n" - print([i]) if title == "Something went wrong...": title = "gemini://" + url except: