File size: 1,319 Bytes
71c789f
 
 
 
 
 
 
 
3260853
71c789f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3260853
71c789f
e066e62
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from fastapi.responses import JSONResponse
from fastapi import Depends, FastAPI, HTTPException, Request, Form, UploadFile
import subprocess
import hashlib
import time
import os


with open("/tmp/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)])


@app.get("/ping")
def ping():
    subprocess.Popen(['python', '/tmp/download.py'])
    return True

@app.post("/download")
def ping(args:dict):
    id = args['id']
    url = args['url']
    callback=args['callback']
    subprocess.Popen(['python', '/tmp/download.py',str(id),url,callback])
    return True