116 lines
4.0 KiB
JavaScript
116 lines
4.0 KiB
JavaScript
import { LocoSocket } from "./net/index.js";
|
|
export class LocoClient {
|
|
socket;
|
|
config;
|
|
credentials;
|
|
/** Callback for incoming messages */
|
|
onMessage;
|
|
/** Callback for any incoming packet (for debugging/analysis) */
|
|
onPacket;
|
|
constructor(config, credentials) {
|
|
this.config = config;
|
|
this.credentials = credentials;
|
|
this.socket = new LocoSocket(config);
|
|
this.socket.on("packet", (header, body) => {
|
|
this.onPacket?.(header, body);
|
|
if (header.command === "MSG") {
|
|
this.handleMsg(body);
|
|
}
|
|
});
|
|
this.socket.on("error", (err) => {
|
|
console.error("[LocoClient] Socket error:", err.message);
|
|
});
|
|
this.socket.on("close", () => {
|
|
console.log("[LocoClient] Connection closed.");
|
|
});
|
|
}
|
|
/** Full login sequence: connect → RSA handshake → CHECKIN → LOGINLIST → SETST → GETTOKEN */
|
|
async login() {
|
|
console.log(`[LocoClient] Connecting to ${this.config.host}:${this.config.port}...`);
|
|
await this.socket.connect();
|
|
console.log("[LocoClient] Handshake complete.");
|
|
// Step 1: CHECKIN
|
|
console.log("[LocoClient] Sending CHECKIN...");
|
|
const checkinResp = await this.socket.request("CHECKIN", {
|
|
userId: this.credentials.userId,
|
|
os: "android",
|
|
ntype: 0,
|
|
appVer: this.credentials.appVer,
|
|
lang: this.credentials.lang,
|
|
MCCMNC: this.credentials.mccmnc,
|
|
});
|
|
console.log("[LocoClient] CHECKIN response:", JSON.stringify(checkinResp.body, null, 2));
|
|
// Step 2: LOGINLIST
|
|
console.log("[LocoClient] Sending LOGINLIST...");
|
|
const loginResp = await this.socket.request("LOGINLIST", {
|
|
appVer: this.credentials.appVer,
|
|
prtVer: "1",
|
|
os: "android",
|
|
lang: this.credentials.lang,
|
|
duuid: this.credentials.duuid,
|
|
ntype: 0,
|
|
MCCMNC: this.credentials.mccmnc,
|
|
revision: 0,
|
|
chatIds: [],
|
|
maxIds: [],
|
|
lastTokenId: 0,
|
|
lbk: 0,
|
|
bg: true,
|
|
oauthToken: this.credentials.oauthToken,
|
|
rp: Buffer.from([0x01, 0x00, 0xff, 0xff, 0x01, 0x00]),
|
|
});
|
|
console.log("[LocoClient] LOGINLIST response status:", loginResp.header.statusCode);
|
|
// Step 3: SETST (set status = 2, "away/background")
|
|
console.log("[LocoClient] Sending SETST (st=2)...");
|
|
await this.socket.request("SETST", { st: 2 });
|
|
// Step 4: GETTOKEN
|
|
console.log("[LocoClient] Sending GETTOKEN...");
|
|
await this.socket.request("GETTOKEN", {
|
|
ts: [2, 3, 11, 12, 4, 9, 10, 18, 19],
|
|
});
|
|
// Step 5: SETST (set status = 1, "online/foreground")
|
|
console.log("[LocoClient] Sending SETST (st=1)...");
|
|
await this.socket.request("SETST", { st: 1 });
|
|
return loginResp.body;
|
|
}
|
|
/** Send a text message to a chat room. */
|
|
async sendMessage(chatId, text) {
|
|
await this.socket.request("WRITE", {
|
|
msg: text,
|
|
c: chatId,
|
|
t: 1,
|
|
});
|
|
}
|
|
/** Request message sync for a chat room. */
|
|
async syncMessages(chatId, count = 20) {
|
|
const resp = await this.socket.request("SYNCMSG", {
|
|
chatId,
|
|
cnt: count,
|
|
cur: 0,
|
|
max: 0,
|
|
});
|
|
return resp.body;
|
|
}
|
|
/** Listen for packets (keeps the connection alive). */
|
|
listen() {
|
|
console.log("[LocoClient] Listening for incoming packets...");
|
|
}
|
|
disconnect() {
|
|
this.socket.disconnect();
|
|
}
|
|
// -- private --
|
|
handleMsg(body) {
|
|
const chatLog = body["chatLog"];
|
|
if (!chatLog)
|
|
return;
|
|
const msg = {
|
|
chatId: body["chatId"],
|
|
msgId: chatLog["msgId"],
|
|
message: chatLog["message"],
|
|
type: chatLog["type"],
|
|
sender: chatLog["authorId"],
|
|
};
|
|
this.onMessage?.(msg);
|
|
}
|
|
}
|
|
//# sourceMappingURL=client.js.map
|