diff --git a/cw/package.json b/cw/package.json index 8cd3726f7a2c9f54306988fd15b78949ee0e329b..25a16108b816722e7898acb03ed20f1dbd1bcbab 100644 --- a/cw/package.json +++ b/cw/package.json @@ -7,7 +7,7 @@ "@types/koa-compose": "^3.2.5", "@types/node": "^17.0.25", "ts-node": "^10.7.0", - "typescript": "^4.6.3" + "typescript": "^4.6.4" }, "dependencies": { "@noble/secp256k1": "^1.5.5", diff --git a/cw/recv.data b/cw/recv.data new file mode 100644 index 0000000000000000000000000000000000000000..c839beb4cd384f565aa7200ea52dc4689ad80a99 Binary files /dev/null and b/cw/recv.data differ diff --git a/cw/src/client.ts b/cw/src/client.ts index 191378e2f1a8cdbc5d803fd2a0497f4c9d2a88a1..e1e06ba03249141426c61476c2358ca9eaf55efc 100644 --- a/cw/src/client.ts +++ b/cw/src/client.ts @@ -1,6 +1,6 @@ import { Socket } from "net"; import { createConnection } from "net" -import { waitForDataUB, writeUB, destroyUB, setMessageID, waitForEncDataUB, writeEncUB, msgListenerUB, } from "./utils"; +import { waitForDataUB, writeUB, destroyUB, setMessageID, waitForEncDataUB, writeEncUB, msgListenerUB, registerRecvBuffer, } from "./utils"; import { genIntro, parseIntro, receiveCapabilities, receiveEncCheck, receivePubKey, receiveSymKey, sendCapabilities, sendEncCheck, sendMsg, sendMsgReq, sendPubKey } from "./messages"; import { getPublicKey, utils } from "@noble/secp256k1" import { createCipheriv, createDecipheriv } from "crypto"; @@ -28,6 +28,7 @@ export const capabilities = [ async function main() { const conn = await connect(); + registerRecvBuffer(conn) conn.setKeepAlive() conn.setNoDelay(true) diff --git a/cw/src/server.ts b/cw/src/server.ts index d29c55b27e5e7f3be0aa5cb7cf443661d1a261df..cfe1d294079542d189730b141efd1f66a3fffec5 100644 --- a/cw/src/server.ts +++ b/cw/src/server.ts @@ -1,6 +1,6 @@ import { genAndSendSymKey, genIntro, parseIntro, receiveCapabilities, receiveEncCheck, receivePubKey, sendCapabilities, sendEncCheck, sendPubKey } from "./messages" import { createServer, Socket } from "net" -import { destroyUB, getMessageID, waitForDataUB, waitForEncDataUB, writeEncUB, writeUB, msgListenerUB } from "./utils" +import { destroyUB, getMessageID, waitForDataUB, waitForEncDataUB, writeEncUB, writeUB, msgListenerUB, registerRecvBuffer } from "./utils" import { getPublicKey, utils } from "@noble/secp256k1" @@ -22,6 +22,7 @@ export const capabilities = [ export async function handleConnection(conn: Socket) { + registerRecvBuffer(conn) conn.on("data", (d) => { console.log(d) }) diff --git a/cw/src/utils.ts b/cw/src/utils.ts index 67cff81c4adf75e862a4ee01cff74f9df953f73f..e9441b20ff1911d4829c3511054e730c5dc6ea5a 100644 --- a/cw/src/utils.ts +++ b/cw/src/utils.ts @@ -32,6 +32,12 @@ export const writeUB = async (conn: Socket, d: any) => { }) } + +let readBuffer: Buffer[] = [] + +export function registerRecvBuffer(conn: Socket) { + conn.on("data", d => readBuffer.push(d)) +} // let writes = 0; // let reads = 0; @@ -41,8 +47,14 @@ export const writeEncUB = async (conn: Socket, cipher: Cipher, d: any) => { } export async function waitForDataUB(s: Socket): Promise<Buffer> { + if (readBuffer.length > 0) { + return readBuffer.shift() as Buffer + } return new Promise(res => - s.once("data", res) + s.once("data", d => { + readBuffer.shift() + res(d) + }) ) } diff --git a/cw/yarn.lock b/cw/yarn.lock index d2ce43c74ec45d00c05554a579dfad4f674992f7..9f75eec6179e34d46bdeaa0980575adc5b67cecf 100644 --- a/cw/yarn.lock +++ b/cw/yarn.lock @@ -330,10 +330,10 @@ ts-node@^10.7.0: v8-compile-cache-lib "^3.0.0" yn "3.1.1" -typescript@^4.6.3: - version "4.6.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.3.tgz#eefeafa6afdd31d725584c67a0eaba80f6fc6c6c" - integrity sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw== +typescript@^4.6.4: + version "4.6.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.4.tgz#caa78bbc3a59e6a5c510d35703f6a09877ce45e9" + integrity sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg== v8-compile-cache-lib@^3.0.0: version "3.0.1" diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity new file mode 100644 index 0000000000000000000000000000000000000000..3f7172cd8a6fece40bf078ee36028c681be35fc4 --- /dev/null +++ b/node_modules/.yarn-integrity @@ -0,0 +1,10 @@ +{ + "systemParams": "linux-x64-93", + "modulesFolders": [], + "flags": [], + "linkedModules": [], + "topLevelPatterns": [], + "lockfileEntries": {}, + "files": [], + "artifacts": {} +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000000000000000000000000000000000000..fb57ccd13afbd082ad82051c2ffebef4840661ec --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + +