| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- # -*- coding: utf-8 -*-
- """爱云医 spu/page 探测脚本(联调用)。
- 用法:
- python aicds_probe_spu.py [手机号]
- 不带手机号: 仅做「无 token 直连」探测,验证 spu/page 是否强制要求登录。
- 带手机号: 走完整 identify 登录 → spu/page → 输出商品清单。
- 凭证从 /tmp/aiyunyi_creds.txt 读取(不入库、不打印)。
- """
- import re, sys, os, json, time, base64, subprocess, tempfile
- TMPDIR = tempfile.gettempdir() # Windows 下 = C:\Users\...\AppData\Local\Temp(Python与openssl同路径)
- CRED_PATH = os.environ.get("AICDS_CRED",
- r"C:\Users\liaoxg.DESKTOP-DGLN79N\AppData\Local\Temp\aiyunyi_creds.txt")
- BASE = "https://devadmin.aicds.com.cn/open-api"
- def load_cred():
- txt = open(CRED_PATH, encoding="utf-8", errors="ignore").read()
- appkey = re.search(r"API Key:\s*(\S+)", txt).group(1)
- priv = re.search(r"RSA \S+:\s*([A-Za-z0-9+/=]{100,})", txt).group(1)
- return appkey, priv
- APPKEY, PRIV = load_cred()
- PEM = os.path.join(TMPDIR, "aicds_probe_key.pem")
- def sign_headers():
- ts = str(int(time.time() * 1000))
- sign_str = "appKey=%s×tamp=%s" % (APPKEY, ts)
- pem = ("-----BEGIN PRIVATE KEY-----\n"
- + "\n".join(PRIV[i:i+64] for i in range(0, len(PRIV), 64))
- + "\n-----END PRIVATE KEY-----\n")
- with open(PEM, "w") as f:
- f.write(pem)
- os.chmod(PEM, 0o600)
- raw = subprocess.run(["openssl", "dgst", "-sha256", "-sign", PEM],
- input=sign_str.encode(), capture_output=True)
- if raw.returncode != 0:
- print("签名失败:", raw.stderr.decode()); sys.exit(2)
- return ts, base64.b64encode(raw.stdout).decode()
- def call(method, path, token=None, uid_enc=None, body=None):
- ts, sign = sign_headers()
- cmd = ["curl", "-sS", "-m", "30", "-w", "\n__HTTP__%{http_code}",
- "-X", method, BASE + path,
- "-H", "X-App-Key: " + APPKEY,
- "-H", "X-Timestamp: " + ts,
- "-H", "X-Api-Sign: " + sign]
- if token:
- cmd += ["-H", "Authorization: Bearer " + token]
- if uid_enc:
- cmd += ["-H", "X-User-Id-Encrypt: " + uid_enc]
- cmd += ["-H", "Content-Type: application/json"]
- if body is not None:
- cmd += ["--data", json.dumps(body)]
- r = subprocess.run(cmd, capture_output=True)
- return r.stdout.decode(errors="ignore") + r.stderr.decode(errors="ignore")
- def encrypt_uid(user_id):
- """用租户 RSA 公钥 PKCS#1 加密 userId → Base64(X-User-Id-Encrypt)"""
- pub_b64 = re.search(r"RSA \S+:\s*([A-Za-z0-9+/=]{100,})",
- open(CRED_PATH, encoding="utf-8", errors="ignore").read()).group(1)
- pubpem = os.path.join(TMPDIR, "aicds_probe_pub.pem")
- with open(pubpem, "w") as f:
- f.write("-----BEGIN PUBLIC KEY-----\n"
- + "\n".join(pub_b64[i:i+64] for i in range(0, len(pub_b64), 64))
- + "\n-----END PUBLIC KEY-----\n")
- raw = subprocess.run(["openssl", "pkeyutl", "-encrypt", "-pubin",
- "-inkey", pubpem, "-pkeyopt", "rsa_padding_mode:pkcs1"],
- input=str(user_id).encode(), capture_output=True)
- if raw.returncode != 0:
- print("userId 加密失败:", raw.stderr.decode()); sys.exit(2)
- return base64.b64encode(raw.stdout).decode()
- if __name__ == "__main__":
- mode = sys.argv[1] if len(sys.argv) > 1 else "no-token"
- if mode == "no-token":
- print("=== 探测1:无 token 直连 GET /product/spu/page ===")
- print(call("GET", "/product/spu/page?pageNo=1&pageSize=20"))
- print("=== (若返回未登录/401,即证明必须先 identify 登录;带手机号重跑可查商品) ===")
- sys.exit(0)
- # 带手机号:完整链路
- mobile = mode
- print("=== 探测2:identify 登录(mobile 脱敏 %s****%s)===" % (mobile[:3], mobile[-4:]))
- login_raw = call("POST", "/member/user/identify", body={"mobile": mobile})
- print(login_raw)
- try:
- login = json.loads(login_raw.split("\n__HTTP__")[0])
- d = login.get("data") or {}
- token, uid = d.get("accessToken"), d.get("userId")
- except Exception as e:
- print("登录解析失败:", e); sys.exit(3)
- if not token:
- print("未拿到 accessToken,停止。"); sys.exit(3)
- print("\n=== 探测3:GET /product/spu/page(登录后)===")
- uid_enc = encrypt_uid(uid)
- spu_raw = call("GET", "/product/spu/page?pageNo=1&pageSize=50",
- token=token, uid_enc=uid_enc)
- print(spu_raw)
- try:
- spu = json.loads(spu_raw.split("\n__HTTP__")[0])
- lst = (spu.get("data") or {}).get("list") or []
- print("\n=== 商品清单汇总:共 %d 条 ===" % len(lst))
- for it in lst:
- print(" spuId=%s name=%s price=%s status=%s" % (
- it.get("id"), it.get("name"),
- (it.get("price") if isinstance(it.get("price"), str) else json.dumps(it.get("price"), ensure_ascii=False)),
- it.get("status")))
- except Exception as e:
- print("spu 解析失败:", e)
|