#!/usr/bin/env python3.9

from flask import Flask, render_template_string, request, make_response
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import subprocess

app = Flask(__name__)
limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=["10000 per hour"]
)

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

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

@limiter.limit("5/second")
@app.route("/curl")
def curl():
    url = request.args["url"].lower()
    if "file" in url or "%" in url:
        return "no"
    p = subprocess.Popen(["curl", "--", url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    return stdout

app.run("0.0.0.0", 1007)