Update app.py
Browse files
app.py
CHANGED
|
@@ -34,38 +34,85 @@ async def index():
|
|
| 34 |
|
| 35 |
@app.get("/api/accounts")
|
| 36 |
async def get_accounts():
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
@app.post("/api/accounts")
|
| 40 |
async def add_account(request: Request):
|
| 41 |
-
body
|
| 42 |
-
username = body.get("username","").strip()
|
| 43 |
-
token = body.get("token","").strip()
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
accounts = load_accounts()
|
| 47 |
-
if any(a["username"]==username for a in accounts):
|
| 48 |
-
return JSONResponse({"ok":False,"message":f"⚠️ الحساب {username} موجود مسبقاً"})
|
|
|
|
| 49 |
add_log(f"🔍 جاري التحقق من {username}...")
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if not host:
|
| 52 |
-
add_log(f"❌
|
| 53 |
-
return JSONResponse({"ok":False,"message":"❌ فشل الاتصال. تحقق من البيانات."})
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
save_accounts(accounts)
|
| 57 |
-
add_log(f"✅ تمت إضافة {username} على {host}")
|
| 58 |
-
return JSONResponse({"ok":True,"message":f"✅ تمت إضافة {username} على {host}"})
|
| 59 |
|
| 60 |
@app.delete("/api/accounts/{username}")
|
| 61 |
async def delete_account(username: str):
|
| 62 |
accounts = load_accounts()
|
| 63 |
-
new_list = [a for a in accounts if a["username"]!=username]
|
| 64 |
-
if len(new_list)==len(accounts):
|
| 65 |
-
return JSONResponse({"ok":False,"message":f"⚠️ لم يُعثر على {username}"})
|
| 66 |
save_accounts(new_list)
|
| 67 |
add_log(f"🗑️ تم حذف {username}")
|
| 68 |
-
return JSONResponse({"ok":True,"message":f"🗑️ تم حذف {username}"})
|
| 69 |
|
| 70 |
@app.post("/api/renew")
|
| 71 |
async def renew_all():
|
|
@@ -78,29 +125,29 @@ async def renew_all():
|
|
| 78 |
add_log(f" [{a['username']}] {r['domain']} → {r['status']}")
|
| 79 |
add_log("✅ انتهى التجديد.")
|
| 80 |
threading.Thread(target=do, daemon=True).start()
|
| 81 |
-
return {"ok":True}
|
| 82 |
|
| 83 |
@app.post("/api/renew/{username}")
|
| 84 |
async def renew_one(username: str):
|
| 85 |
accounts = load_accounts()
|
| 86 |
-
account = next((a for a in accounts if a["username"]==username), None)
|
| 87 |
if not account:
|
| 88 |
-
return {"ok":False}
|
| 89 |
account["next_renewal"] = None
|
| 90 |
save_accounts(accounts)
|
| 91 |
def do():
|
| 92 |
add_log(f"🔄 تجديد {username}...")
|
| 93 |
success, results = renew_account(account)
|
| 94 |
accs2 = load_accounts()
|
| 95 |
-
for i,a in enumerate(accs2):
|
| 96 |
-
if a["username"]==username:
|
| 97 |
-
accs2[i]=account
|
| 98 |
save_accounts(accs2)
|
| 99 |
for r in (results if success else []):
|
| 100 |
add_log(f" [{username}] {r['domain']} → {r['status']}")
|
| 101 |
add_log(f"{'✅' if success else '❌'} {'تم' if success else 'فشل'} تجديد {username}")
|
| 102 |
threading.Thread(target=do, daemon=True).start()
|
| 103 |
-
return {"ok":True}
|
| 104 |
|
| 105 |
@app.post("/api/scheduler")
|
| 106 |
async def toggle_scheduler():
|
|
@@ -125,9 +172,9 @@ async def get_log():
|
|
| 125 |
|
| 126 |
@app.get("/health")
|
| 127 |
async def health():
|
| 128 |
-
return {"status":"alive"}
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
| 131 |
start_ping_server(port=8080)
|
| 132 |
add_log("🚀 PythonAnywhere Manager started")
|
| 133 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 34 |
|
| 35 |
@app.get("/api/accounts")
|
| 36 |
async def get_accounts():
|
| 37 |
+
# Never return token or password to the frontend
|
| 38 |
+
safe_keys = {"username", "host", "last_renewed", "next_renewal", "last_results", "auth_method"}
|
| 39 |
+
return [{k: v for k, v in a.items() if k in safe_keys} for a in load_accounts()]
|
| 40 |
|
| 41 |
@app.post("/api/accounts")
|
| 42 |
async def add_account(request: Request):
|
| 43 |
+
body = await request.json()
|
| 44 |
+
username = body.get("username", "").strip()
|
| 45 |
+
token = body.get("token", "").strip()
|
| 46 |
+
password = body.get("password", "").strip()
|
| 47 |
+
|
| 48 |
+
if not username:
|
| 49 |
+
return JSONResponse({"ok": False, "message": "❌ أدخل اسم المستخدم"})
|
| 50 |
+
if not token and not password:
|
| 51 |
+
return JSONResponse({"ok": False, "message": "❌ أدخل API Token أو كلمة المرور"})
|
| 52 |
+
|
| 53 |
accounts = load_accounts()
|
| 54 |
+
if any(a["username"] == username for a in accounts):
|
| 55 |
+
return JSONResponse({"ok": False, "message": f"⚠️ الحساب {username} موجود مسبقاً"})
|
| 56 |
+
|
| 57 |
add_log(f"🔍 جاري التحقق من {username}...")
|
| 58 |
+
|
| 59 |
+
# Detect host
|
| 60 |
+
host = None
|
| 61 |
+
auth_method = ""
|
| 62 |
+
|
| 63 |
+
if token:
|
| 64 |
+
host = get_host(username, token)
|
| 65 |
+
auth_method = "token"
|
| 66 |
+
if not host:
|
| 67 |
+
add_log(f"❌ فشل الاتصال بـ {username} عبر Token")
|
| 68 |
+
if not password:
|
| 69 |
+
return JSONResponse({"ok": False, "message": "❌ فشل الاتصال. تحقق من Token."})
|
| 70 |
+
add_log(f"🔄 سأحاول عبر كلمة المرور...")
|
| 71 |
+
|
| 72 |
+
if not host and password:
|
| 73 |
+
# Try to detect host via simple HTTP check
|
| 74 |
+
import requests as req
|
| 75 |
+
for h in ["www.pythonanywhere.com", "eu.pythonanywhere.com"]:
|
| 76 |
+
try:
|
| 77 |
+
r = req.get(f"https://{h}/login/", timeout=8)
|
| 78 |
+
if r.status_code == 200:
|
| 79 |
+
host = h
|
| 80 |
+
break
|
| 81 |
+
except Exception:
|
| 82 |
+
continue
|
| 83 |
+
auth_method = "password"
|
| 84 |
+
|
| 85 |
if not host:
|
| 86 |
+
add_log(f"❌ تعذّر تحديد الخادم لـ {username}")
|
| 87 |
+
return JSONResponse({"ok": False, "message": "❌ فشل الاتصال. تحقق من البيانات."})
|
| 88 |
+
|
| 89 |
+
new_account = {
|
| 90 |
+
"username": username,
|
| 91 |
+
"host": host,
|
| 92 |
+
"auth_method": auth_method,
|
| 93 |
+
"last_renewed": None,
|
| 94 |
+
"next_renewal": None,
|
| 95 |
+
"last_results": []
|
| 96 |
+
}
|
| 97 |
+
if token:
|
| 98 |
+
new_account["token"] = token
|
| 99 |
+
if password:
|
| 100 |
+
new_account["password"] = password
|
| 101 |
+
|
| 102 |
+
accounts.append(new_account)
|
| 103 |
save_accounts(accounts)
|
| 104 |
+
add_log(f"✅ تمت إضافة {username} على {host} [{auth_method}]")
|
| 105 |
+
return JSONResponse({"ok": True, "message": f"✅ تمت إضافة {username} على {host} [{auth_method}]"})
|
| 106 |
|
| 107 |
@app.delete("/api/accounts/{username}")
|
| 108 |
async def delete_account(username: str):
|
| 109 |
accounts = load_accounts()
|
| 110 |
+
new_list = [a for a in accounts if a["username"] != username]
|
| 111 |
+
if len(new_list) == len(accounts):
|
| 112 |
+
return JSONResponse({"ok": False, "message": f"⚠️ لم يُعثر على {username}"})
|
| 113 |
save_accounts(new_list)
|
| 114 |
add_log(f"🗑️ تم حذف {username}")
|
| 115 |
+
return JSONResponse({"ok": True, "message": f"🗑️ تم حذف {username}"})
|
| 116 |
|
| 117 |
@app.post("/api/renew")
|
| 118 |
async def renew_all():
|
|
|
|
| 125 |
add_log(f" [{a['username']}] {r['domain']} → {r['status']}")
|
| 126 |
add_log("✅ انتهى التجديد.")
|
| 127 |
threading.Thread(target=do, daemon=True).start()
|
| 128 |
+
return {"ok": True}
|
| 129 |
|
| 130 |
@app.post("/api/renew/{username}")
|
| 131 |
async def renew_one(username: str):
|
| 132 |
accounts = load_accounts()
|
| 133 |
+
account = next((a for a in accounts if a["username"] == username), None)
|
| 134 |
if not account:
|
| 135 |
+
return {"ok": False}
|
| 136 |
account["next_renewal"] = None
|
| 137 |
save_accounts(accounts)
|
| 138 |
def do():
|
| 139 |
add_log(f"🔄 تجديد {username}...")
|
| 140 |
success, results = renew_account(account)
|
| 141 |
accs2 = load_accounts()
|
| 142 |
+
for i, a in enumerate(accs2):
|
| 143 |
+
if a["username"] == username:
|
| 144 |
+
accs2[i] = account
|
| 145 |
save_accounts(accs2)
|
| 146 |
for r in (results if success else []):
|
| 147 |
add_log(f" [{username}] {r['domain']} → {r['status']}")
|
| 148 |
add_log(f"{'✅' if success else '❌'} {'تم' if success else 'فشل'} تجديد {username}")
|
| 149 |
threading.Thread(target=do, daemon=True).start()
|
| 150 |
+
return {"ok": True}
|
| 151 |
|
| 152 |
@app.post("/api/scheduler")
|
| 153 |
async def toggle_scheduler():
|
|
|
|
| 172 |
|
| 173 |
@app.get("/health")
|
| 174 |
async def health():
|
| 175 |
+
return {"status": "alive"}
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
start_ping_server(port=8080)
|
| 179 |
add_log("🚀 PythonAnywhere Manager started")
|
| 180 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|