Spaces:
Sleeping
Sleeping
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -1,31 +1,97 @@
|
|
| 1 |
-
# backend.py
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
import base64
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from fastapi
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.post("/api/encode")
|
| 15 |
-
async def
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import io
|
| 3 |
+
import zipfile
|
| 4 |
import base64
|
| 5 |
+
import random
|
| 6 |
+
import tempfile
|
| 7 |
+
from fastapi import FastAPI, Request
|
| 8 |
+
import aiohttp
|
| 9 |
+
import uvicorn
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
# -------------------------------
|
| 14 |
+
# 🔐 Simple Python File Encoder
|
| 15 |
+
# -------------------------------
|
| 16 |
+
|
| 17 |
+
def make_zip_bytes(filename: str, content: bytes) -> bytes:
|
| 18 |
+
"""Compress file bytes into a ZIP archive (in memory)."""
|
| 19 |
+
bio = io.BytesIO()
|
| 20 |
+
with zipfile.ZipFile(bio, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
| 21 |
+
zf.writestr(filename, content)
|
| 22 |
+
return bio.getvalue()
|
| 23 |
+
|
| 24 |
+
def rand_bytes(n):
|
| 25 |
+
"""Generate random bytes."""
|
| 26 |
+
return os.urandom(n)
|
| 27 |
+
|
| 28 |
+
def xor_bytes(data: bytes, key: bytes) -> bytes:
|
| 29 |
+
"""Simple XOR encryption."""
|
| 30 |
+
return bytes([b ^ key[i % len(key)] for i, b in enumerate(data)])
|
| 31 |
+
|
| 32 |
+
def encode_bytes(file_bytes: bytes, filename: str):
|
| 33 |
+
"""Encrypt + wrap into Python file."""
|
| 34 |
+
# Step 1: Zip the file
|
| 35 |
+
zipped = make_zip_bytes(filename, file_bytes)
|
| 36 |
+
|
| 37 |
+
# Step 2: Encrypt with XOR
|
| 38 |
+
key = rand_bytes(16)
|
| 39 |
+
enc = xor_bytes(zipped, key)
|
| 40 |
+
enc_b64 = base64.b64encode(enc).decode()
|
| 41 |
+
key_b64 = base64.b64encode(key).decode()
|
| 42 |
+
|
| 43 |
+
# Step 3: Build output Python file
|
| 44 |
+
wrapper = f"""# Auto Encrypted by JerryCoder Encryptor
|
| 45 |
+
import base64,zlib
|
| 46 |
+
D=base64.b64decode('{enc_b64}')
|
| 47 |
+
K=base64.b64decode('{key_b64}')
|
| 48 |
+
O=bytes([b^K[i%len(K)] for i,b in enumerate(D)])
|
| 49 |
+
import io,zipfile,tempfile,os,runpy
|
| 50 |
+
t=tempfile.mkdtemp()
|
| 51 |
+
zipfile.ZipFile(io.BytesIO(O)).extractall(t)
|
| 52 |
+
p=[f for f in os.listdir(t) if f.endswith('.py')][0]
|
| 53 |
+
runpy.run_path(os.path.join(t,p))
|
| 54 |
+
"""
|
| 55 |
+
output_name = os.path.splitext(filename)[0] + "_enc.py"
|
| 56 |
+
return wrapper.encode(), output_name
|
| 57 |
+
|
| 58 |
+
# -------------------------------
|
| 59 |
+
# 🌐 FastAPI Endpoint
|
| 60 |
+
# -------------------------------
|
| 61 |
+
|
| 62 |
@app.post("/api/encode")
|
| 63 |
+
async def encode_endpoint(request: Request):
|
| 64 |
+
"""Receive file URL from Cloudflare bot, encode, return result."""
|
| 65 |
+
data = await request.json()
|
| 66 |
+
file_url = data.get("url")
|
| 67 |
+
filename = data.get("filename", "file.py")
|
| 68 |
+
|
| 69 |
+
if not file_url:
|
| 70 |
+
return {"ok": False, "error": "No file URL provided"}
|
| 71 |
+
|
| 72 |
+
async with aiohttp.ClientSession() as session:
|
| 73 |
+
async with session.get(file_url) as resp:
|
| 74 |
+
if resp.status != 200:
|
| 75 |
+
return {"ok": False, "error": f"Failed to fetch file ({resp.status})"}
|
| 76 |
+
file_bytes = await resp.read()
|
| 77 |
+
|
| 78 |
+
encoded_bytes, out_name = encode_bytes(file_bytes, filename)
|
| 79 |
+
|
| 80 |
+
out_path = os.path.join(tempfile.gettempdir(), out_name)
|
| 81 |
+
with open(out_path, "wb") as f:
|
| 82 |
+
f.write(encoded_bytes)
|
| 83 |
+
|
| 84 |
+
# ⚠️ NOTE: Hugging Face doesn’t auto-host tmp files publicly.
|
| 85 |
+
# You’ll need to upload to a hosting service (like tmpfiles.org, catbox.moe, etc.)
|
| 86 |
+
# For now, just return ok=True and filename.
|
| 87 |
+
return {
|
| 88 |
+
"ok": True,
|
| 89 |
+
"result_url": f"https://jerrycoder-kuttuxd.hf.space/tmp/{out_name}",
|
| 90 |
+
"filename": out_name
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
# -------------------------------
|
| 94 |
+
# 🏁 Run App
|
| 95 |
+
# -------------------------------
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|