#!/usr/bin/env python3
"""快速探测Frida方法名格式 - 带超时保护"""
import frida, time, json, signal, sys

def timeout_handler(signum, frame):
    print("TIMEOUT!")
    sys.exit(1)

signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(20)  # 20秒超时

print("[1] Connecting...")
d = frida.get_device_manager().add_remote_device("192.168.0.12:27042")
print("[2] Attaching...")
s = d.attach(7239)
print("[3] Loading script...")

# 用一个极简脚本测试方法名格式
MINI_SCRIPT = """
rpc.exports = {
    ping: function() { return "pong"; },
    getProcessInfo: function() { return {pid: Process.id, arch: Process.arch}; },
    getConnectionStatus: function() { return {mode: "tcp", ts: Date.now()}; },
    takeScreenshot: function(p) { return {success: true, test: true}; },
};
"""

sc = s.create_script(MINI_SCRIPT)
sc.on("message", lambda m, d: None)
sc.load()
time.sleep(1)

e = sc.exports_sync
methods = [x for x in dir(e) if not x.startswith("_")]
print(f"[4] Methods ({len(methods)}): {methods}")

# 测试各种格式
print("\n[5] Testing method name formats:")
tests = [
    ("ping", None),
    ("getProcessInfo", None),
    ("getprocessinfo", None),
    ("get_process_info", None),
    ("getConnectionStatus", None),
    ("getconnectionstatus", None),
    ("takeScreenshot", {"path": "/tmp/test.png"}),
    ("takescreenshot", {"path": "/tmp/test.png"}),
]

for name, params in tests:
    fn = getattr(e, name, None)
    if fn is None:
        print(f"  {name}: NOT IN DIR")
        continue
    try:
        r = fn(params) if params else fn()
        print(f"  {name}: OK -> {json.dumps(r)[:60]}")
    except Exception as ex:
        print(f"  {name}: ERROR -> {ex}")

sc.unload()
s.detach()
print("\nDone!")
