网络与调试配方
约 312 字大约 1 分钟
网络与调试配方
这一页把最常一起使用的“联网 + 调试 + 可视化”能力放在一起。
HTTP 客户端探活
http.setTimeout({
connectTimeout: 5000,
readTimeout: 10000,
followRedirects: true
});
const response = http.get("https://httpbin.org/get");
log(`status=${response.statusCode}`);
log(response.body.string());
本地 HTTP 调试服务
httpServer.start({
port: 18765,
allowLan: false,
token: "debug-token"
});
httpServer.get("/debug/state", req => {
return {
status: 200,
mimeType: "application/json; charset=utf-8",
body: JSON.stringify({
packageName: lpparam.packageName,
processName: lpparam.processName,
activity: String(activity)
})
};
});
带查询参数的接口
httpServer.get("/debug/echo", req => {
const name = req.param("name") || "guest";
return {
status: 200,
mimeType: "application/json; charset=utf-8",
body: JSON.stringify({
hello: name
})
};
});
MCP Tool 暴露运行时信息
mcpServer.start({
host: "127.0.0.1",
port: 5698,
endpoint: "/mcp",
token: "secret-token",
name: "jsxhook-runtime"
});
mcpServer.tool("runtime_state", {
title: "Runtime State",
description: "Return package, process and current activity",
inputSchema: {
type: "object",
properties: {}
}
}, () => {
return {
content: [
{
type: "text",
text: JSON.stringify({
packageName: lpparam.packageName,
processName: lpparam.processName,
activity: String(activity)
}, null, 2)
}
]
};
});
ImGui 面板配合调试
const state = imgui.reactive({ enabled: false });
const panel = imgui.window("runtime-debug", {
title: "Runtime Debug",
width: 360,
height: 540,
displayMode: "in_app",
resizable: true,
collapsible: true
});
panel.text("包名: " + lpparam.packageName);
panel.text("进程: " + lpparam.processName);
panel.checkbox("启用额外日志", imgui.bind(state, "enabled"));
panel.button("打印 Activity 字段", function () {
printFields(activity, "\n");
});
panel.show();
Hook + HTTP 联动
const events = [];
hook({
class: "com.example.target.LoginManager",
classloader: lpparam.classLoader,
method: "login",
params: ["java.lang.String", "java.lang.String"],
before(it) {
events.push({
time: Date.now(),
args: Array.from(it.args)
});
}
});
httpServer.get("/debug/login-events", () => {
return {
status: 200,
mimeType: "application/json; charset=utf-8",
body: JSON.stringify(events)
};
});
这种方式很适合在不方便频繁切日志页时,临时把命中的 Hook 结果暴露给浏览器或局域网工具。
