Mr-Help commited on
Commit
1b65d09
ยท
verified ยท
1 Parent(s): f07d22f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +40 -9
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
- URL = "https://odoo-demo.binrushd.care/api/auth/token"
 
 
11
 
12
- headers = {
 
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
- resp = requests.get(URL, headers=headers, timeout=20)
 
28
 
29
- # ---- Logging ู…ูุตู„ ููŠ ุงู„ุณูŠุฑูุฑ ----
 
 
 
 
 
 
 
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("[ODDO TOKEN RESPONSE]", json.dumps(log_obj, ensure_ascii=False), file=sys.stderr)
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("[ODDO TOKEN ERROR]", err_txt, file=sys.stderr)
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")