Update server.py

This commit is contained in:
Nova Cat 2025-01-13 15:53:40 -08:00
parent 6d87348b17
commit 23cdd2f7b0

View file

@ -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():
</body>
</html>
"""
@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"<h3>{temp}</h3>\n"
elif i[0:2] == "* ":
temp = i[2:]
code += f"<ul><li>{temp}</li></ul>"
code += f"<ul><li>{temp}</li></ul>\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 = "<img src=\"/external.png\">"
elif prse.hostname != urlparse(fulladdr).hostname:
extra = "<img src=\"/cross-server.png\">"
if temp.split(" ") == 1:
comment = goto
else:
comment = " ".join(temp.split(" ")[1:])
code += f"<a href=\"{goto}\">{comment} {extra}</a>\n"
else:
code += f"<p>{i}</p>\n"
print([i])
if title == "Something went wrong...":
title = "gemini://" + url
except: