2019-08-21 03:17:58 +02:00
|
|
|
"""
|
|
|
|
This is an example of setting up a Gemini server to proxy requests to other
|
|
|
|
protocols. This application will accept HTTP URLs, download and render them
|
|
|
|
locally using the `w3m` tool, and render the output to the client as plain text.
|
|
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import jetforce
|
2019-08-23 00:53:02 +02:00
|
|
|
from jetforce import Response, Status
|
2019-08-21 03:17:58 +02:00
|
|
|
|
2019-08-23 00:53:02 +02:00
|
|
|
app = jetforce.JetforceApplication()
|
2019-08-21 03:17:58 +02:00
|
|
|
|
|
|
|
|
2019-08-23 00:53:02 +02:00
|
|
|
@app.route(scheme="https", strict_hostname=False)
|
|
|
|
@app.route(scheme="http", strict_hostname=False)
|
|
|
|
def proxy_request(request):
|
|
|
|
command = [b"w3m", b"-dump", request.url.encode()]
|
|
|
|
try:
|
|
|
|
out = subprocess.run(command, stdout=subprocess.PIPE)
|
|
|
|
out.check_returncode()
|
|
|
|
except Exception:
|
|
|
|
return Response(Status.CGI_ERROR, "Failed to load URL")
|
|
|
|
else:
|
|
|
|
return Response(Status.SUCCESS, "text/plain", out.stdout)
|
2019-08-21 03:17:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-08-23 00:53:02 +02:00
|
|
|
args = jetforce.command_line_parser().parse_args()
|
2019-08-29 04:33:58 +02:00
|
|
|
ssl_context = jetforce.make_ssl_context(
|
|
|
|
args.hostname, args.certfile, args.keyfile, args.cafile, args.capath
|
|
|
|
)
|
2019-08-21 03:17:58 +02:00
|
|
|
server = jetforce.GeminiServer(
|
|
|
|
host=args.host,
|
|
|
|
port=args.port,
|
2019-08-29 04:33:58 +02:00
|
|
|
ssl_context=ssl_context,
|
2019-08-21 03:17:58 +02:00
|
|
|
hostname=args.hostname,
|
|
|
|
app=app,
|
|
|
|
)
|
|
|
|
asyncio.run(server.run())
|