62 lines
3.7 KiB
Python
62 lines
3.7 KiB
Python
"""Explicit one-shot verification command; never starts automatic replies."""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import time
|
|
from app_version import APP_VERSION
|
|
from runtime_paths import application_data_dir
|
|
from functools import partial
|
|
from wecom_native_sender import NativeClient,NativeSender,discover
|
|
|
|
def verification_request_id(profile, account, conversation):
|
|
return (f'manual-verification:build:{profile.sha256}:{profile.adapter_revision}:'
|
|
f'{profile.version}:{account}:{conversation}')
|
|
|
|
|
|
def run_send_test(argv):
|
|
parser=argparse.ArgumentParser(description='向明确指定的现有测试会话发送一条协议测试消息并核对回执')
|
|
parser.add_argument('--protocol-send-test',required=True,metavar='EXACT_CONVERSATION_NAME')
|
|
parser.add_argument('--report',required=True,type=Path)
|
|
parser.add_argument('--expected-client',default='')
|
|
parser.add_argument('--expected-account',default='')
|
|
parser.add_argument('--expected-sha256',default='')
|
|
args=parser.parse_args(argv)
|
|
from reply_database import LiveReplyDatabase
|
|
db=None;sender=None
|
|
report={'appVersion':APP_VERSION,'checkedAt':time.time(),'status':'failed','automaticListenerStarted':False}
|
|
try:
|
|
live=discover()
|
|
report.update(clientVersion=live['clientVersion'],clientSha256=live.get('clientSha256'),nativeAdapter=live.get('nativeAdapter'))
|
|
if args.expected_client and live['clientVersion']!=args.expected_client:
|
|
raise ValueError('企业微信版本与测试目标不一致,未发送')
|
|
if args.expected_account and live['accountId']!=args.expected_account:
|
|
raise ValueError('登录账号在测试准备期间已变化,未发送')
|
|
if args.expected_sha256 and live['clientSha256'] != args.expected_sha256:
|
|
raise ValueError('客户端构建在验证准备期间已变化,未发送')
|
|
db=LiveReplyDatabase._open_database(account=live['accountId'])
|
|
matches=[x for x in db._matching_conversations(args.protocol_send_test) if x[0]==live['accountId']]
|
|
if len(matches)!=1:raise ValueError('测试会话必须在当前原账号中唯一匹配')
|
|
account,conv,name=matches[0]
|
|
from contact_reply_guard import conversation_contact_reason
|
|
def contact_allowed():
|
|
context=db.get_conversation_context_by_id(account,conv,limit=100)
|
|
return bool(context) and not conversation_contact_reason(context)
|
|
if not contact_allowed():
|
|
raise ValueError('测试会话不可读取或已开启联系人验证,跳过发送')
|
|
sender=NativeSender(db,application_data_dir()/'protocol_sends.sqlite3',client_factory=partial(NativeClient,allow_unverified=True,expected_sha256=live['clientSha256']))
|
|
# Preserve deduplication across application releases; distinguish a
|
|
# silently replaced executable even when its product version is unchanged.
|
|
from wecom_native_builds import build_for_hash
|
|
profile = build_for_hash(live['clientSha256'])
|
|
if profile is None:
|
|
raise ValueError('客户端构建未通过核对,未发送')
|
|
request_id=verification_request_id(profile, account, conv)
|
|
receipt=sender.send(request_id,live['pid'],account,conv,'协议通道测试,请忽略',guard=contact_allowed)
|
|
report.update(status='confirmed',target=name,receipt=receipt,requestId=request_id)
|
|
except Exception as exc:report['error']=str(exc)
|
|
finally:
|
|
if sender:sender.close()
|
|
if db:db.close()
|
|
args.report.parent.mkdir(parents=True,exist_ok=True)
|
|
args.report.write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8')
|
|
return 0 if report['status']=='confirmed' else 2 |