Update main.py
Browse files
main.py
CHANGED
|
@@ -2,20 +2,40 @@ from fastapi import FastAPI, Response
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import sys
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@app.get("/get-token")
|
| 9 |
def get_token():
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
"CF-Access-Client-Id": "0491b36d7dcabce5b04f1a53f347bb4e.access",
|
| 14 |
"CF-Access-Client-Secret": "22152cb41b62393e159daaff7dce433006c3744c5850e6adc15fa3544bb5eb09",
|
|
|
|
| 15 |
"login": "binrushd.automation@gmail.com",
|
| 16 |
"password": "BR2025",
|
| 17 |
"db": "Live_August_25",
|
| 18 |
-
|
|
|
|
| 19 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 20 |
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 21 |
"Accept": "application/json, text/plain, */*",
|
|
@@ -23,23 +43,35 @@ def get_token():
|
|
| 23 |
"Connection": "keep-alive",
|
| 24 |
}
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
body_preview = resp.text[:1000]
|
| 32 |
except Exception:
|
| 33 |
body_preview = "<unreadable>"
|
|
|
|
| 34 |
log_obj = {
|
| 35 |
"status_code": resp.status_code,
|
| 36 |
"reason": resp.reason,
|
| 37 |
"headers": dict(resp.headers),
|
| 38 |
"body_preview": body_preview
|
| 39 |
}
|
| 40 |
-
print("[
|
| 41 |
|
| 42 |
-
#
|
| 43 |
content_type = resp.headers.get("Content-Type", "application/json")
|
| 44 |
return Response(
|
| 45 |
content=resp.text,
|
|
@@ -48,7 +80,6 @@ def get_token():
|
|
| 48 |
)
|
| 49 |
|
| 50 |
except requests.RequestException as e:
|
| 51 |
-
# ุฃุฎุทุงุก ุงูุดุจูุฉ/timeout
|
| 52 |
err_txt = f"Network error: {type(e).__name__} - {str(e)}"
|
| 53 |
-
print("[
|
| 54 |
return Response(content=err_txt, status_code=502, media_type="text/plain")
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import sys
|
| 5 |
+
from http.cookiejar import Cookie
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
def cookies_to_log_list(cookiejar) -> list:
|
| 10 |
+
"""ุญููู ุงูููููุฒ ููุงุฆู
ุฉ ุณููุฉ ุงููุฑุงุกุฉ ููููุฌ."""
|
| 11 |
+
out = []
|
| 12 |
+
for c in cookiejar: # type: Cookie
|
| 13 |
+
out.append({
|
| 14 |
+
"name": c.name,
|
| 15 |
+
"value": c.value,
|
| 16 |
+
"domain": c.domain,
|
| 17 |
+
"path": c.path,
|
| 18 |
+
"secure": c.secure,
|
| 19 |
+
"expires": c.expires,
|
| 20 |
+
})
|
| 21 |
+
return out
|
| 22 |
+
|
| 23 |
@app.get("/get-token")
|
| 24 |
def get_token():
|
| 25 |
+
BASE = "https://odoo-demo.binrushd.care"
|
| 26 |
+
PREFLIGHT_URL = f"{BASE}/" # ูุฌูุจ ู
ูู ุงูููููุฒ (ุฌูุณุฉ/ุชูููุถ CF)
|
| 27 |
+
TOKEN_URL = f"{BASE}/api/auth/token"
|
| 28 |
|
| 29 |
+
# ููุฏุฑุฒ ู
ูุญุฏุฉ ููุฌูุณุฉ (CF Access + UA)
|
| 30 |
+
common_headers = {
|
| 31 |
"CF-Access-Client-Id": "0491b36d7dcabce5b04f1a53f347bb4e.access",
|
| 32 |
"CF-Access-Client-Secret": "22152cb41b62393e159daaff7dce433006c3744c5850e6adc15fa3544bb5eb09",
|
| 33 |
+
# ูู ุงูู API ูุนูุงู ุจุชูุฑุฃ login/password/db ู
ู ุงูููุฏุฑุฒ ุฎูููููููู
ู
ูุฌูุฏูู:
|
| 34 |
"login": "binrushd.automation@gmail.com",
|
| 35 |
"password": "BR2025",
|
| 36 |
"db": "Live_August_25",
|
| 37 |
+
|
| 38 |
+
# ุชูููุฏ ู
ุชุตูุญ ูุชูููู ุดุจูุฉ ุงูุจูุช
|
| 39 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 40 |
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 41 |
"Accept": "application/json, text/plain, */*",
|
|
|
|
| 43 |
"Connection": "keep-alive",
|
| 44 |
}
|
| 45 |
|
| 46 |
+
s = requests.Session()
|
| 47 |
+
s.headers.update(common_headers)
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
+
# (1) ุทูุจ ุชู
ููุฏู ูุงุณุชุฎุฑุงุฌ ุงูููููุฒ ู
ู Cloudflare/Access
|
| 51 |
+
pre = s.get(PREFLIGHT_URL, timeout=20, allow_redirects=True)
|
| 52 |
|
| 53 |
+
# ููุฌ ููููููุฒ ุจุนุฏ ุงูุฎุทูุฉ ุงูุฃููู
|
| 54 |
+
pre_cookies_log = cookies_to_log_list(s.cookies)
|
| 55 |
+
print("[PRE-FLIGHT COOKIES]", json.dumps(pre_cookies_log, ensure_ascii=False), file=sys.stderr)
|
| 56 |
+
|
| 57 |
+
# (2) ุงูุทูุจ ุงูุฃุณุงุณู ุจุงุณุชุฎุฏุงู
ููุณ ุงูุฌูุณุฉ (ุงูููุฏุฑุฒ + ุงูููููุฒ ูุชุจุนุชูุง ุชููุงุฆูุงู)
|
| 58 |
+
resp = s.get(TOKEN_URL, timeout=20)
|
| 59 |
+
|
| 60 |
+
# ููุฌ ู
ูุตู ููุชูุฌุฉ ุทูุจ ุงูุชููู
|
| 61 |
try:
|
| 62 |
body_preview = resp.text[:1000]
|
| 63 |
except Exception:
|
| 64 |
body_preview = "<unreadable>"
|
| 65 |
+
|
| 66 |
log_obj = {
|
| 67 |
"status_code": resp.status_code,
|
| 68 |
"reason": resp.reason,
|
| 69 |
"headers": dict(resp.headers),
|
| 70 |
"body_preview": body_preview
|
| 71 |
}
|
| 72 |
+
print("[TOKEN RESPONSE]", json.dumps(log_obj, ensure_ascii=False), file=sys.stderr)
|
| 73 |
|
| 74 |
+
# ุฑุฌูุน ููุณ ุงูุญุงูุฉ ูุงูู
ุญุชูู
|
| 75 |
content_type = resp.headers.get("Content-Type", "application/json")
|
| 76 |
return Response(
|
| 77 |
content=resp.text,
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
except requests.RequestException as e:
|
|
|
|
| 83 |
err_txt = f"Network error: {type(e).__name__} - {str(e)}"
|
| 84 |
+
print("[TOKEN ERROR]", err_txt, file=sys.stderr)
|
| 85 |
return Response(content=err_txt, status_code=502, media_type="text/plain")
|