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