upload
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
||||
# 美团小程序 mtgsig 出签客户端(本地补环境,不依赖浏览器/远端签名服务)
|
||||
# 依赖常驻服务 mtgsig_js/serve.mjs,先跑 启动出签服务.py
|
||||
import os
|
||||
import json
|
||||
import urllib.parse
|
||||
import requests
|
||||
|
||||
BASE_URL = os.environ.get('MTGSIG_BASE_URL', 'http://192.168.5.134: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):
|
||||
"""按 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
|
||||
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):
|
||||
"""外卖 H5(h1.9) **纯算**出签(不跑 SDK,毫秒级)—— 走服务里的 /sign_h5_pure。
|
||||
|
||||
mtgsig 传一份抓包里的 H5 mtgsig(a6 前缀 h1.9),url/data 传要出签的请求(data 是 form body 串);
|
||||
origin_url/origin_data 是那份抓包签名对应的 url/data —— 只有要和出签请求不一样时才传(比如改了 body),
|
||||
不传就按同一份包复算出签。换接口只换 url + data 即可(url/data/mtgsig/method 必传)。
|
||||
"""
|
||||
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
|
||||
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))
|
||||
Reference in New Issue
Block a user