#!/usr/bin/python3 import asyncio, traceback, socket, ssl from urllib.parse import urlparse, quote from flask import Flask, request, redirect, send_file, Response from hypercorn.config import Config from hypercorn.asyncio import serve app = Flask(__name__) @app.route("/") def root(): return send_file("home.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("/style.css") def style(): return send_file("style.css") @app.route("/gem") def relay(): url = request.args.get('gemini') queries = request.args.get('query') if url == None: return redirect("/") code = "
The file that the Gemini server sent couldn\'t be decoded by Gem2Browser.
' print(mimetype) if num == 1: print(mimetype) for i in bytes(gemraw): try: if i.decode() != "\r": mimetyp += i.decode() else: break except: print(traceback.format_exc()) break gr2 = bytearray() if not mimetype in ["text/gemini", ""]: frline = True for i in gemraw: try: if frline and i.decode() != "\n": pass else: frline = False except: print(traceback.format_exc()) pass if binary: return Response(bytes(gemraw), mimetype="text/gemini" if mimetype=="" else mimetype) received = received.replace("\r", "") firstline = True redirected = False gemtext = True code = "" escaped = False for i in received.split("\n"): i = i.replace("<", "<") if firstline: if i.split(" ")[0][0] == "3": if i.split(" ")[1][0] == "/": return redirect("/gem?gemini=" + urlparse(fulladdr).hostname + quote(i.split(" ")[1], safe='')) return redirect("/gem?gemini=" + quote(i.split(" ")[1][9:], safe='')) elif i.split(" ")[0][0] == "1": return f'\nThe specified Gemini server wants more data:
{i}' elif i.split(" ")[0][0] != "2": return f'\n
The specified Gemini server returned a status of: {i}
' else: firstline = False if i.split(" ")[1].split(";")[0] != "text/gemini": print("Unrecognised type: " + i.split(" ")[1]) return Response(" ".join(received.split("\n")[1:]), mimetype=i.split(" ")[1]) else: if escaped: if i[0:3] == "```": code += "\n" escaped = False else: code += i + "\n" else: if i[0:2] == "# ": if title == "Something went wrong...": title = i[2:] temp = i[2:] code += f"\n" escaped = True elif i[0:4] == "### ": temp = i[4:] code += f"{temp}
\n" elif i[0:2] == "* ": temp = i[2:] code += f"
{i}
\n" else: code += f"{i}
\n" if title == "Something went wrong...": title = "gemini://" + url except: code += "" + traceback.format_exc() + "" return f'\n