Spaces:
Sleeping
Sleeping
from fastapi.responses import JSONResponse | |
from fastapi import Depends, FastAPI, HTTPException, Request, Form, UploadFile | |
import subprocess | |
import hashlib | |
import time | |
import os | |
with open("download.py", "w") as file: | |
file.write(os.environ.get('DOWNALOD_SCRIPT')) | |
API_AUTH_KEY = os.environ.get('AUTH_KEY') | |
async def verify_internal_token(request: Request): | |
auth_key = request.query_params.get("auth_key") | |
if not auth_key: | |
raise HTTPException(status_code=403, detail="Forbidden") | |
timestamp = int(time.time()) | |
timestampStr, rand, uid, hashStr = auth_key.split('-', 3) | |
path = request.url.path | |
if timestamp > int(timestampStr): | |
raise HTTPException(status_code=403, detail="Forbidden") | |
md5hash = hashlib.md5( | |
f"{path}-{timestampStr}-{rand}-{uid}-{API_AUTH_KEY}".encode()) | |
md5 = md5hash.hexdigest() | |
if md5 != hashStr: | |
raise HTTPException(status_code=403, detail="Forbidden") | |
app = FastAPI(dependencies=[Depends(verify_internal_token)]) | |
def ping(): | |
subprocess.Popen(['python', '/app/download.py']) | |
return True | |