#!/usr/bin/env python3
"""测试Frida RPC调用方式 - 确定正确的方法名格式"""
import frida
import json

DEVICE_IP = "192.168.0.12"
FRIDA_PORT = 27042
WECHAT_PID = 7239

mgr = frida.get_device_manager()
device = mgr.add_remote_device(f"{DEVICE_IP}:{FRIDA_PORT}")
print(f"Device: {device.name}")

# 用PID attach
print(f"Attaching to PID {WECHAT_PID}...")
session = device.attach(WECHAT_PID)
print("Attached!")

# 加载最小测试脚本
test_script = """
rpc.exports = {
    ping: function() { return 'pong_test'; },
    getProfile: function() { return {success: true, nickname: 'test'}; },
    sendMessage: function(params) { return {success: true, params: params}; },
};
"""

script = session.create_script(test_script)
script.load()

# 检查Python端看到的方法名
exports = script.exports_sync
available = [x for x in dir(exports) if not x.startswith('_')]
print(f"\nAvailable exports: {available}")

# 测试调用
print(f"\nping(): {exports.ping()}")

# Frida Python 会把 camelCase 转成 snake_case
# getProfile -> get_profile
try:
    result = exports.get_profile()
    print(f"get_profile(): {result}")
except Exception as e:
    print(f"get_profile() failed: {e}")

# 或者直接用原始名
try:
    result = exports.getProfile()
    print(f"getProfile(): {result}")
except Exception as e:
    print(f"getProfile() failed: {e}")

# 测试带参数
try:
    result = exports.send_message({"to_id": "test", "content": "hello"})
    print(f"send_message(params): {result}")
except Exception as e:
    print(f"send_message(params) failed: {e}")

script.unload()
session.detach()
print("\nDone!")
