Files
pinhaofan/AI代码/小程序mtgsig.py
T
2026-11-11 22:25:40 +08:00

123 lines
6.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 美团小程序 mtgsig 出签客户端(本地补环境,不依赖浏览器/远端签名服务)
# 依赖常驻服务 mtgsig_js/serve.mjs,先跑 启动出签服务.py
import json
import urllib.parse
import requests
BASE_URL = 'http://127.0.0.1:18092'
SIGN_TIMEOUT = 60
PROXY = {'http': None, 'https': None}
def get_Datatext(dataq):
data = urllib.parse.urlencode(dataq)
return data
def check_health():
r = requests.get(BASE_URL + '/health', timeout=5, proxies=PROXY)
r.raise_for_status()
return r.json()
def derive_from_mtgsig(mtgsig):
"""从一份 mtgsig(抓包里的旧签名即可)解出会话身份:appid / dfpid / localId / filetime"""
r = requests.post(BASE_URL + '/derive', json={'mtgsig': mtgsig}, timeout=SIGN_TIMEOUT, proxies=PROXY)
r.raise_for_status()
return r.json()
def get_mtgsig(url, data, mtgsig, open_id, method, mode=None, sys=None, ext=None, r=None):
"""按 url + body 出 mtgsig(url/data/mtgsig/open_id 必传)。
mtgsig 传抓包里的旧签名,appid/dfpid/localId/filetime 会从它的 a6 里自动解出;
open_id 传请求头里的 openId(a6 里要带账号身份,缺了签名不对)。
method 默认 POST;请求体按 x-www-form-urlencoded 处理(美团小程序绝大多数接口如此)。
mode: 不传即走**纯算**(无预热、毫秒级);传 'bundle' 才用补环境跑 jsguard(每个新身份首次约 20s)。
sys/ext: 按字段名覆盖 a6 里的 system(46 项)/ext(5 项),只传要改的那几个,不传就原样。
sys 常用字段: brand/model/system/SDKVersion/version/screenWidth/screenHeight/pixelRatio/
brightness/fontSizeSetting/language/networkType/wifiEnabled/hasSystemProxy/
locationEnabled/bluetoothEnabled/deviceOrientation/LaunchOptionsSync 等(共 46 个,
完整清单见 serve.mjs 的 SYSTEM_FIELDS)
ext 字段: NativeClient / addEventListener / getPublicLibVersion / __WeixinJSBridge / NFC
仅纯算生效;字段名写错服务端会直接报错(并回可用字段名)。
同一个小程序下换任意接口都通用,只换 url + data 即可。
data 传字典时按 x-www-form-urlencoded 自动拼成 k=v&k=v;传字符串则原样使用。
"""
# if isinstance(data, dict):
# import traceback
# traceback.print_exc()
body = {'url': url, 'data': data, 'mtgsig': mtgsig, 'openId': open_id, 'method': method}
if mode:
body['mode'] = mode
if sys is not None:
body['sys'] = sys
if ext is not None:
body['ext'] = ext
if r is not None:
body['r'] = r
try:
resp = requests.post(BASE_URL + '/sign', json=body, timeout=SIGN_TIMEOUT, proxies=PROXY)
except requests.exceptions.ConnectionError:
raise RuntimeError('出签服务没启:先跑 `python 启动出签服务.py`(地址 %s)' % BASE_URL)
if resp.status_code != 200:
raise RuntimeError('出签失败 %s: %s' % (resp.status_code, resp.text[:200]))
return resp.json()['mtgsig']
def get_h5_sign(url, data, method, mode, profile, cookie='', data_type='json', type=None):
"""H5 出签(走服务里的 H5guard 补环境,非小程序 jsguard),返回整个结果 dict:
{mtgsig, url, headers, mode, a3, a6pfx}
url / data / method / mode / profile 必传(profile 缺字段服务端直接报错)。
profile = 设备/请求环境,全部来自抓包请求(没有内置默认身份):
ua, referrer, origin, chBrands(sec-ch-ua 原串), chPlatform(sec-ch-ua-platform),
chMobile(sec-ch-ua-mobile), language, languages(数组), platform, hardwareConcurrency,
deviceMemory, maxTouchPoints, screenWidth, screenHeight, availHeight, colorDepth,
devicePixelRatio, innerWidth, innerHeight, glVendor, glRenderer, appid, domains(数组)
可选: title / model / architecture / bitness / platformVersion
mode='sign':走手动 sign(),a6 前缀 h1.9 —— 外卖/买药 H5 页面自身 jquery 加签走这条。
mode='xhr':走 SDK 自己的 XHR 自动加签,a6 前缀 hs1.6 —— maf.meituan.com 这类接口要这个;
GET -> 用返回的 `url`(mtgsig 已拼进 query);POST -> 也用返回的 `url`。
cookie 可选(dfpid 走 mtgsig.a3)。服务按 profile 缓存环境,每个新 profile 首次等指纹预热。
"""
body = {'url': url, 'data': data, 'method': method, 'mode': mode, 'profile': profile,
'cookie': cookie, 'dataType': data_type, 'type': type or method}
try:
resp = requests.post(BASE_URL + '/sign_h5', json=body, timeout=SIGN_TIMEOUT, proxies=PROXY)
except requests.exceptions.ConnectionError:
raise RuntimeError('出签服务没启:先跑 `python 启动出签服务.py`(地址 %s)' % BASE_URL)
if resp.status_code != 200:
raise RuntimeError('H5 出签失败 %s: %s' % (resp.status_code, resp.text[:200]))
return resp.json()
def get_h5_mtgsig(url, data, method, mode, profile, cookie='', data_type='json', type=None):
"""只要 mtgsig 值本身时用这个(= get_h5_sign(...)['mtgsig'])。"""
return get_h5_sign(url=url, data=data, method=method, mode=mode, profile=profile, cookie=cookie, data_type=data_type, type=type)['mtgsig']
def get_h5_mtgsig_pure(url, data, mtgsig, method='POST', origin_url=None, origin_data=None, cookie=''):
"""外卖 H5(h1.9) 出签—— 走服务里的 /sign_h5_pure,服务调 H5guard.sign 后返回 mtgsig。
url/data/method 是要出签的请求。data 是 form body 串,必须和实际 POST 的字节一致。
cookie 传抓包 Cookie,让 a3 跟 WEBDFPID 对齐。mtgsig 仍要传,服务用来认这是 H5 请求。
"""
body = {'url': url, 'data': data, 'mtgsig': mtgsig, 'method': method}
if origin_url is not None:
body['origin_url'] = origin_url
if origin_data is not None:
body['origin_data'] = origin_data
if cookie:
body['cookie'] = cookie
try:
resp = requests.post(BASE_URL + '/sign_h5_pure', json=body, timeout=SIGN_TIMEOUT, proxies=PROXY)
except requests.exceptions.ConnectionError:
raise RuntimeError('出签服务没启:先跑 `python 启动出签服务.py`(地址 %s)' % BASE_URL)
if resp.status_code != 200:
raise RuntimeError('H5 纯算出签失败 %s: %s' % (resp.status_code, resp.text[:200]))
return resp.json()['mtgsig']
if __name__ == '__main__':
print(json.dumps(check_health(), ensure_ascii=False))