Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ LOGIN_URL = "https://www.ivasms.com/login"
|
|
| 9 |
DASHBOARD_URL = "https://www.ivasms.com/portal"
|
| 10 |
SMS_URL = "https://www.ivasms.com/portal/sms/received/getsms"
|
| 11 |
|
| 12 |
-
# Global session (simple & legal)
|
| 13 |
client = httpx.Client(
|
| 14 |
follow_redirects=True,
|
| 15 |
headers={
|
|
@@ -28,7 +27,6 @@ csrf_token = None
|
|
| 28 |
@app.get("/")
|
| 29 |
def root():
|
| 30 |
return {
|
| 31 |
-
"service": "iVasMS API",
|
| 32 |
"login": "/login?email=EMAIL&password=PASSWORD",
|
| 33 |
"status": "/status",
|
| 34 |
"sms": "/sms",
|
|
@@ -38,26 +36,16 @@ def root():
|
|
| 38 |
|
| 39 |
@app.get("/login")
|
| 40 |
def login(email: str, password: str):
|
| 41 |
-
"""
|
| 42 |
-
LOGIN via GET
|
| 43 |
-
Example:
|
| 44 |
-
/login?email=xxx@gmail.com&password=xxxxx
|
| 45 |
-
"""
|
| 46 |
global session_ready, csrf_token
|
| 47 |
|
| 48 |
-
# Step 1: open login page (
|
| 49 |
-
|
| 50 |
-
soup = BeautifulSoup(r.text, "html.parser")
|
| 51 |
-
token_input = soup.find("input", {"name": "_token"})
|
| 52 |
-
if not token_input:
|
| 53 |
-
raise HTTPException(500, "CSRF token not found")
|
| 54 |
|
| 55 |
-
# Step 2: submit login (
|
| 56 |
data = {
|
| 57 |
"email": email,
|
| 58 |
"username": email,
|
| 59 |
-
"password": password
|
| 60 |
-
"_token": token_input["value"]
|
| 61 |
}
|
| 62 |
|
| 63 |
res = client.post(LOGIN_URL, data=data)
|
|
@@ -66,13 +54,14 @@ def login(email: str, password: str):
|
|
| 66 |
session_ready = False
|
| 67 |
raise HTTPException(401, "Login failed")
|
| 68 |
|
| 69 |
-
# Step 3:
|
| 70 |
dash = client.get(DASHBOARD_URL)
|
| 71 |
-
|
| 72 |
-
meta =
|
|
|
|
| 73 |
if not meta:
|
| 74 |
session_ready = False
|
| 75 |
-
raise HTTPException(500, "Dashboard CSRF not found")
|
| 76 |
|
| 77 |
csrf_token = meta["content"]
|
| 78 |
session_ready = True
|
|
@@ -85,17 +74,11 @@ def login(email: str, password: str):
|
|
| 85 |
|
| 86 |
@app.get("/status")
|
| 87 |
def status():
|
| 88 |
-
return {
|
| 89 |
-
"logged_in": session_ready
|
| 90 |
-
}
|
| 91 |
|
| 92 |
|
| 93 |
@app.get("/sms")
|
| 94 |
def get_sms():
|
| 95 |
-
"""
|
| 96 |
-
Ambil SMS (raw HTML)
|
| 97 |
-
Harus login dulu
|
| 98 |
-
"""
|
| 99 |
if not session_ready or not csrf_token:
|
| 100 |
raise HTTPException(401, "Not logged in")
|
| 101 |
|
|
@@ -122,6 +105,4 @@ def logout():
|
|
| 122 |
client.cookies.clear()
|
| 123 |
session_ready = False
|
| 124 |
csrf_token = None
|
| 125 |
-
return {
|
| 126 |
-
"status": "logged_out"
|
| 127 |
-
}
|
|
|
|
| 9 |
DASHBOARD_URL = "https://www.ivasms.com/portal"
|
| 10 |
SMS_URL = "https://www.ivasms.com/portal/sms/received/getsms"
|
| 11 |
|
|
|
|
| 12 |
client = httpx.Client(
|
| 13 |
follow_redirects=True,
|
| 14 |
headers={
|
|
|
|
| 27 |
@app.get("/")
|
| 28 |
def root():
|
| 29 |
return {
|
|
|
|
| 30 |
"login": "/login?email=EMAIL&password=PASSWORD",
|
| 31 |
"status": "/status",
|
| 32 |
"sms": "/sms",
|
|
|
|
| 36 |
|
| 37 |
@app.get("/login")
|
| 38 |
def login(email: str, password: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
global session_ready, csrf_token
|
| 40 |
|
| 41 |
+
# Step 1: open login page (for cookies)
|
| 42 |
+
client.get(LOGIN_URL)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Step 2: submit login (NO CSRF HERE)
|
| 45 |
data = {
|
| 46 |
"email": email,
|
| 47 |
"username": email,
|
| 48 |
+
"password": password
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
res = client.post(LOGIN_URL, data=data)
|
|
|
|
| 54 |
session_ready = False
|
| 55 |
raise HTTPException(401, "Login failed")
|
| 56 |
|
| 57 |
+
# Step 3: open dashboard, get CSRF token
|
| 58 |
dash = client.get(DASHBOARD_URL)
|
| 59 |
+
soup = BeautifulSoup(dash.text, "html.parser")
|
| 60 |
+
meta = soup.find("meta", {"name": "csrf-token"})
|
| 61 |
+
|
| 62 |
if not meta:
|
| 63 |
session_ready = False
|
| 64 |
+
raise HTTPException(500, "Dashboard CSRF token not found")
|
| 65 |
|
| 66 |
csrf_token = meta["content"]
|
| 67 |
session_ready = True
|
|
|
|
| 74 |
|
| 75 |
@app.get("/status")
|
| 76 |
def status():
|
| 77 |
+
return {"logged_in": session_ready}
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
@app.get("/sms")
|
| 81 |
def get_sms():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
if not session_ready or not csrf_token:
|
| 83 |
raise HTTPException(401, "Not logged in")
|
| 84 |
|
|
|
|
| 105 |
client.cookies.clear()
|
| 106 |
session_ready = False
|
| 107 |
csrf_token = None
|
| 108 |
+
return {"status": "logged_out"}
|
|
|
|
|
|