网络与调试配方
小于 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 面板配合调试
let enabled = true;
imgui
.window("runtime-debug")
.title("Runtime Debug")
.size(360, 540)
.text(`pkg=${lpparam.packageName}`)
.text(`proc=${lpparam.processName}`)
.separator()
.checkbox("Enable extra logging", "enable_extra_logging", enabled, value => {
enabled = value;
})
.button("Print Activity Fields", () => {
printFields(activity, "\n");
})
.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 结果暴露给浏览器或局域网工具。
