pantry/system/public/connect.js

75 lines
1.9 KiB
JavaScript

"use strict";
const { SodiumPlus, X25519SecretKey } = require("sodium-plus");
const fs = require("node:fs");
(async function () {
const sodium = await SodiumPlus.auto();
fs.readFile("../settings/secret.txt", "utf8", async (err, data) => {
let key;
if (err || data.length === 0) {
const keys = await sodium.crypto_box_keypair();
const secret = await sodium.crypto_box_secretkey(keys);
key = await sodium.sodium_bin2hex(
(await sodium.crypto_box_publickey(keys)).getBuffer(),
);
fs.writeFile(
"../settings/secret.txt",
await sodium.sodium_bin2hex(secret.getBuffer()),
() => {},
);
} else {
key = await sodium.sodium_bin2hex(
(await sodium.crypto_box_publickey_from_secretkey(
await X25519SecretKey.from(data, "hex"),
)).getBuffer(),
);
}
fetch(
`https://notchat.mirzaev.sexy/servers/connect/MIRZAEV`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
description: "",
location: "Russia",
host: "https://pantry.bebra.team",
accounts: [],
settings: {
chats: {
max: 50,
private: false,
languages: [],
},
},
key,
}),
},
)
.then((response) =>
Number(response.headers.get("content-length")) === 0
? null
: response.json()
)
.then((data) => {
if (
data !== null &&
typeof data !== "undefined" &&
data.errors !== null &&
typeof data.errors === "object" &&
data.errors.length > 0
) {
console.log("Response from server:\n");
for (const error of data.errors) console.log(error);
}
});
});
})();