#!/usr/bin/env python3.9

from flask import Flask, render_template_string, request
from subprocess import run

app = Flask(__name__)

@app.route('/')
def index():
    return ("\x3cpre\x3e\x3ccode\x3e%s\x3c/code\x3e\x3c/pre\x3e")%open(__file__).read()

@app.route('/docker')
def docker():
    return ("\x3cpre\x3e\x3ccode\x3e%s\x3c/code\x3e\x3c/pre\x3e")%open("Dockerfile").read()

@app.route('/curl')
def check():
    if "url" not in request.args:
        return '''
            \x3chtml\x3e
                \x3ccenter\x3e
                    \x3ch1\x3eCurl-as-a-Service\x3c/h1\x3e
                    \x3cform action="/curl"\x3e
                        \x3cinput name="url" type="text" rows="1" cols="100"\x3e\x3c/textarea\x3e\x3cbr\x3e
                        \x3cinput type="submit" value="Curl me!"\x3e\x3c/input\x3e
                \x3c/center\x3e
                \x3c/form\x3e
            \x3c/html\x3e
        '''

    url = request.args["url"]
    whitelist = "abcdefghjiklmnopqrstuvwxyz1234567890./:-_"
    if any(i not in whitelist for i in url):
        return "No."
    return run(["curl", "-L", "--max-time", "1", "--max-filesize", "1000",  url], capture_output=True).stdout.decode()

@app.errorhandler(500)
def internal_error(error):
    return f"Oops: {error.original_exception}"

if __name__ == "__main__":
    app.run('0.0.0.0', 4000)