File size: 2,483 Bytes
af0cc64
 
 
 
 
cb0d637
af0cc64
 
 
 
 
 
 
 
cb0d637
 
af0cc64
cb0d637
 
 
af0cc64
 
 
 
 
cb0d637
 
 
 
 
 
 
 
 
 
 
 
af0cc64
 
 
3304cbb
 
af0cc64
3304cbb
af0cc64
 
 
3304cbb
af0cc64
 
 
 
 
cb0d637
af0cc64
 
3304cbb
af0cc64
3304cbb
 
 
cb0d637
 
3304cbb
af0cc64
 
 
 
cb0d637
 
 
 
 
af0cc64
 
 
3304cbb
cb0d637
af0cc64
 
 
cb0d637
af0cc64
 
 
 
 
 
 
 
 
 
 
 
 
cb0d637
 
 
 
 
 
 
af0cc64
cb0d637
af0cc64
 
cb0d637
3304cbb
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from fastapi import FastAPI, HTTPException
import httpx
from bs4 import BeautifulSoup
from datetime import datetime, timedelta

app = FastAPI(title="iVasMS API (GET only)")

LOGIN_URL = "https://www.ivasms.com/login"
DASHBOARD_URL = "https://www.ivasms.com/portal"
SMS_URL = "https://www.ivasms.com/portal/sms/received/getsms"

client = httpx.Client(
    follow_redirects=True,
    headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.9",
        "Connection": "keep-alive",
    },
    timeout=30.0
)

session_ready = False
csrf_token = None


@app.get("/")
def root():
    return {
        "login": "/login?email=EMAIL&password=PASSWORD",
        "status": "/status",
        "sms": "/sms",
        "logout": "/logout"
    }


@app.get("/login")
def login(email: str, password: str):
    global session_ready, csrf_token

    # Step 1: open login page (for cookies)
    client.get(LOGIN_URL)

    # Step 2: submit login (NO CSRF HERE)
    data = {
        "email": email,
        "username": email,
        "password": password
    }

    res = client.post(LOGIN_URL, data=data)

    if "login" in str(res.url):
        session_ready = False
        raise HTTPException(401, "Login failed")

    # Step 3: open dashboard, get CSRF token
    dash = client.get(DASHBOARD_URL)
    soup = BeautifulSoup(dash.text, "html.parser")
    meta = soup.find("meta", {"name": "csrf-token"})

    if not meta:
        session_ready = False
        raise HTTPException(500, "Dashboard CSRF token not found")

    csrf_token = meta["content"]
    session_ready = True

    return {
        "status": "ok",
        "logged_in": True
    }


@app.get("/status")
def status():
    return {"logged_in": session_ready}


@app.get("/sms")
def get_sms():
    if not session_ready or not csrf_token:
        raise HTTPException(401, "Not logged in")

    today = datetime.utcnow()
    start = today - timedelta(days=1)

    payload = {
        "from": start.strftime("%m/%d/%Y"),
        "to": today.strftime("%m/%d/%Y"),
        "_token": csrf_token
    }

    r = client.post(SMS_URL, data=payload)

    return {
        "success": True,
        "raw_html": r.text
    }


@app.get("/logout")
def logout():
    global session_ready, csrf_token
    client.cookies.clear()
    session_ready = False
    csrf_token = None
    return {"status": "logged_out"}