first commit

This commit is contained in:
dahyeon committed 2026-02-02 19:14:07 -08:00
commit b6256abbfb
397 files changed
+537630

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
import { connect as tlsConnect } from "node:tls";
import { connect as netConnect } from "node:net";
import { EventEmitter } from "node:events";
import { generateAESKey } from "../crypto/index.js";
import { buildHandshakePacket, buildEncryptedPacket, buildPlainPacket, buildLocoPacket, PacketReader, PlainPacketReader, } from "../protocol/index.js";
export class LocoSocket extends EventEmitter {
socket = null;
aesKey;
encryptedReader;
plainReader;
packetId = 1000;
config;
plaintext;
/** Map of pending request id -> resolve callback */
pending = new Map();
constructor(config, options) {
super();
this.config = config;
this.plaintext = options?.plaintext ?? false;
this.aesKey = generateAESKey();
this.encryptedReader = new PacketReader(this.aesKey);
this.plainReader = new PlainPacketReader();
}
/** Connect to the LOCO server. Performs RSA handshake unless in plaintext mode. */
async connect() {
return new Promise((resolve, reject) => {
const onConnect = () => {
if (this.plaintext) {
// No handshake needed — TLS provides encryption
this.emit("connected");
resolve();
return;
}
// Send handshake: RSA-encrypted AES key
const handshake = buildHandshakePacket(this.aesKey, this.config.serverPublicKey);
if (process.env["LOCO_DEBUG"]) {
console.log(`[Socket] handshake ${handshake.length} bytes: ${handshake.subarray(0, Math.min(64, handshake.length)).toString("hex")}`);
console.log(`[Socket] AES key: ${this.aesKey.toString("hex")}`);
}
this.socket.write(handshake, (err) => {
if (err)
return reject(err);
this.emit("connected");
resolve();
});
};
if (this.config.useTLS) {
this.socket = tlsConnect({
host: this.config.host,
port: this.config.port,
rejectUnauthorized: true,
}, onConnect);
}
else {
this.socket = netConnect({ host: this.config.host, port: this.config.port }, onConnect);
}
this.socket.on("data", (chunk) => {
if (process.env["LOCO_DEBUG"]) {
console.log(`[Socket] recv ${chunk.length} bytes: ${chunk.subarray(0, Math.min(64, chunk.length)).toString("hex")}`);
}
const reader = this.plaintext ? this.plainReader : this.encryptedReader;
const packets = reader.feed(chunk);
for (const pkt of packets) {
// Resolve pending request if matched
const resolver = this.pending.get(pkt.header.id);
if (resolver) {
this.pending.delete(pkt.header.id);
resolver(pkt);
}
this.emit("packet", pkt.header, pkt.body);
}
});
this.socket.on("error", (err) => {
this.emit("error", err);
reject(err);
});
this.socket.on("close", () => {
this.emit("close");
});
});
}
/** Send a LOCO command and return a promise that resolves with the server's response. */
async request(command, body) {
const id = ++this.packetId;
const inner = buildLocoPacket(id, command, 0, body);
const packet = this.plaintext
? buildPlainPacket(inner)
: buildEncryptedPacket(inner, this.aesKey);
return new Promise((resolve, reject) => {
if (process.env["LOCO_DEBUG"]) {
console.log(`[Socket] send ${command} id=${id} ${packet.length} bytes: ${packet.subarray(0, Math.min(64, packet.length)).toString("hex")}`);
}
this.pending.set(id, resolve);
this.socket.write(packet, (err) => {
if (err) {
this.pending.delete(id);
reject(err);
}
});
});
}
/** Send a LOCO command without waiting for a specific response. */
send(command, body) {
const id = ++this.packetId;
const inner = buildLocoPacket(id, command, 0, body);
const packet = this.plaintext
? buildPlainPacket(inner)
: buildEncryptedPacket(inner, this.aesKey);
this.socket.write(packet);
return id;
}
disconnect() {
this.socket?.destroy();
this.socket = null;
}
get sessionKey() {
return this.aesKey;
}
}
//# sourceMappingURL=socket.js.map