systemquery/src/subcommands/test-client/test-client.mjs

36 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
import net from 'net';
import l from 'log';
import settings from '../../settings.mjs';
import sleep from '../../lib/async/sleep.mjs';
import Connection from '../../lib/transport/Connection.mjs';
import { encrypt_bytes, decrypt_bytes } from '../../lib/crypto/secretbox.mjs';
export default async function() {
const test_key = "H7xKSxvJFoZoNjCKAfxn4E3qUzY3Y/4bjY+qIzxg+78=";
const test_key_bytes = Buffer.from(test_key, "base64");
const test_data = "hello, world";
l.notice(`TEST_DATA`, test_data);
const encrypted = encrypt_bytes(test_key_bytes, Buffer.from(test_data, "utf-8"));
l.notice(`ENCRYPTED`, encrypted);
const decrypted = decrypt_bytes(test_key_bytes, encrypted);
l.notice(`DECRYPTED`, decrypted);
l.notice(`DECRYPTED_TEXT`, decrypted.toString("utf-8"));
const socket = await Connection.Create(test_key, "::1", settings.cli.port);
2021-10-03 11:14:57 +00:00
socket.on("message", (_event, _msg) => {
// noop
// l.notice(`<<< ${event}: ${JSON.stringify(msg)}`);
});
for(let i = 0; i < 100; i++) {
await sleep(1000);
socket.send(`test-client`, `hello world ${i}\n`);
}
}