完整签名校验 + 心跳保活,生产级实现
import requests import hashlib import time import threading BASE_URL = "https://www.keyt.cn/kami/你的用户名/check.php" APP_NAME = "你的应用名" SIGN_KEY = "你的签名密钥" # 从后台「修改签名密钥」获取 TIMESTAMP_MAX_DIFF = 120 def md5(data): return hashlib.md5(data.encode()).hexdigest() def verify_response(raw): """校验签名 + 时间戳,返回业务内容或 None""" if "|sign=" not in raw: return None body, sign = raw.split("|sign=", 1) local_sign = md5(body + SIGN_KEY) if local_sign != sign: return None last_pipe = body.rfind("|") if last_pipe == -1: return None ts_str = body[last_pipe + 1:] if not ts_str.isdigit(): return None ts = int(ts_str) now_ts = int(time.time()) if abs(now_ts - ts) > TIMESTAMP_MAX_DIFF: return None return body[:last_pipe] def get_card_switch(): for _ in range(3): url = f"{BASE_URL}?act=get_switch&app={APP_NAME}&t={int(time.time())}" try: resp = requests.get(url, timeout=5) biz = verify_response(resp.text) if biz and "CARD_ON" in biz: return "CARD_ON" elif biz and "CARD_OFF" in biz: return "CARD_OFF" except: pass time.sleep(0.5) return "CARD_ON" def verify_card(card, mac): url = f"{BASE_URL}?card={card}&mac={mac}&app={APP_NAME}&heart=1&t={int(time.time())}" resp = requests.get(url, timeout=5) biz = verify_response(resp.text) return biz def heartbeat(card, mac): fail_cnt = 0 while True: biz = verify_card(card, mac) if biz and biz.startswith("ok|"): fail_cnt = 0 else: fail_cnt += 1 if fail_cnt >= 5: print("心跳失败超过5次,退出") break time.sleep(50) def main(): mac = "你的机器码" card_switch = get_card_switch() if card_switch == "CARD_OFF": print("验证已关闭,直接进入") threading.Thread(target=heartbeat, args=("88888888", mac)).start() # 你的主逻辑 return card = input("请输入卡密: ") biz = verify_card(card, mac) if biz and biz.startswith("ok|"): print("验证成功") threading.Thread(target=heartbeat, args=(card, mac)).start() # 你的主逻辑 else: print("验证失败") if __name__ == "__main__": main()
md5(body + SIGN_KEY) 确保响应未被篡改。get_card_switch() 判断是否开启验证,支持动态关闭。 提示: 使用前请将 你的用户名、你的应用名、你的签名密钥 替换为后台实际值。完整对接流程可参考 文档首页。