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 #!/usr/bin/python3
import asyncio, traceback, socket, ssl import asyncio, traceback, socket, ssl
from urllib.parse import urlparse 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.config import Config
from hypercorn.asyncio import serve from hypercorn.asyncio import serve
app = Flask(__name__) app = Flask(__name__)
@ -26,6 +26,12 @@ def root():
</body> </body>
</html> </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") @app.route("/gem")
def relay(): def relay():
url = request.args.get('gemini') url = request.args.get('gemini')
@ -37,19 +43,15 @@ def relay():
gsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) gsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
fulladdr = "gemini://" + url fulladdr = "gemini://" + url
gemsocket = ssl._create_unverified_context().wrap_socket(gsocket, server_hostname=urlparse(fulladdr).hostname) 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.connect((urlparse(fulladdr).hostname, 1965))
gemsocket.send(bytes("gemini://" + url + "\r\n", "UTF-8")) gemsocket.send(bytes("gemini://" + url + "\r\n", "UTF-8"))
received = "" received = ""
while True: while True:
gemresponse = gemsocket.recv(2048) gemresponse = gemsocket.recv(2048)
print(gemresponse)
if gemresponse.decode() != "": if gemresponse.decode() != "":
received += gemresponse.decode() received += gemresponse.decode()
else: else:
break break
print(received)
received = received.replace("\r", "") received = received.replace("\r", "")
firstline = True firstline = True
redirected = False redirected = False
@ -79,10 +81,30 @@ def relay():
code += f"<h3>{temp}</h3>\n" code += f"<h3>{temp}</h3>\n"
elif i[0:2] == "* ": elif i[0:2] == "* ":
temp = i[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: else:
code += f"<p>{i}</p>\n" code += f"<p>{i}</p>\n"
print([i])
if title == "Something went wrong...": if title == "Something went wrong...":
title = "gemini://" + url title = "gemini://" + url
except: except: