176 lines
4.4 KiB
JavaScript
176 lines
4.4 KiB
JavaScript
"use strict";
|
|
|
|
const { Client } = require("../Client.js");
|
|
const { on, emit, register } = require("../WebSocket");
|
|
|
|
on('disconnect', (xclient) => {
|
|
const { intersection, pairs } = xclient.getSucureClients();
|
|
|
|
for (const [clientid, client] of intersection) {
|
|
client?.send([{ id: xclient.id }, "peer/disconnect"]);
|
|
}
|
|
|
|
for (const [id, peer] of pairs) {
|
|
peer?.pairs.delete(xclient.id);
|
|
xclient.pairs.delete(id);
|
|
}
|
|
});
|
|
|
|
register('auth/pair-system', (client, msg) => {
|
|
if (msg.value == 'everybody') {
|
|
client.requiredPair = true;
|
|
return { status: 'success' };
|
|
}
|
|
if (msg.value == 'disable') {
|
|
client.requiredPair = false;
|
|
return { status: 'success' };
|
|
}
|
|
return { status: 'fail', message: 'INVALID_VALUE' };
|
|
});
|
|
|
|
register('my/socketid', (client, msg) => {
|
|
return client.id;
|
|
});
|
|
|
|
register('auth/public', (client, msg) => {
|
|
client.requiredPair = false;
|
|
return { value: 'success', mode: 'public' };
|
|
});
|
|
|
|
register('auth/private', (client, msg) => {
|
|
client.requiredPair = true;
|
|
return { value: 'success', mode: 'private' };
|
|
});
|
|
|
|
register('request/pair', (client, msg) => {
|
|
const { to } = msg;
|
|
|
|
if (!Client.clients.has(to)) {
|
|
return { status: 'fail', message: 'CLIENT_NOT_FOUND' };
|
|
}
|
|
|
|
const pairclient = Client.clients.get(to);
|
|
|
|
if (pairclient.pairs.has(client.id)) {
|
|
return { status: 'success', message: 'ALREADY-PAIRED' };
|
|
}
|
|
|
|
if (client.pairs.has(to)) {
|
|
return { status: 'fail', message: 'ALREADY-REQUESTED' };
|
|
}
|
|
|
|
client.peerRequest(pairclient);
|
|
return { status: 'success', message: 'REQUESTED' };
|
|
});
|
|
|
|
register('accept/pair', (client, msg) => {
|
|
const { to } = msg;
|
|
|
|
if (!Client.clients.has(to)) {
|
|
return { status: 'fail', message: 'CLIENT_NOT_FOUND' };
|
|
}
|
|
|
|
const pairclient = Client.clients.get(to);
|
|
|
|
if (pairclient.pairs.has(client.id)) {
|
|
return { status: 'success', message: 'ALREADY-PAIRED' };
|
|
}
|
|
|
|
if (!client.pairs.has(to)) {
|
|
return { status: 'fail', message: 'NOT_REQUESTED_PAIR' };
|
|
}
|
|
|
|
client.acceptPeerRequest(pairclient);
|
|
return { status: 'success' };
|
|
});
|
|
|
|
register('reject/pair', (client, msg) => {
|
|
const { to } = msg;
|
|
|
|
if (!Client.clients.has(to)) {
|
|
return { status: 'fail', message: 'CLIENT_NOT_FOUND' };
|
|
}
|
|
|
|
const pairclient = Client.clients.get(to);
|
|
|
|
if (pairclient.pairs.has(client.id)) {
|
|
return { status: 'success', message: 'ALREADY-PAIRED' };
|
|
}
|
|
|
|
if (!client.pairs.has(to)) {
|
|
return { status: 'fail', message: 'NOT_REQUESTED_PAIR' };
|
|
}
|
|
|
|
client.rejectPeerRequest(pairclient);
|
|
return { status: 'success' };
|
|
});
|
|
|
|
register('end/pair', (client, msg) => {
|
|
const { to } = msg;
|
|
|
|
if (!Client.clients.has(to)) {
|
|
return { status: 'fail', message: 'CLIENT_NOT_FOUND' };
|
|
}
|
|
|
|
const pairclient = Client.clients.get(to);
|
|
|
|
if (!pairclient.pairs.has(client.id)) {
|
|
return { status: 'success', message: 'NOT_PAIRED' };
|
|
}
|
|
|
|
client.rejectPeerRequest(pairclient);
|
|
return { status: 'success' };
|
|
});
|
|
|
|
register('pair/list', (client, msg) => {
|
|
return { type: 'pair/list', value: client.pairList() };
|
|
});
|
|
|
|
register('is/reachable', (client, msg) => {
|
|
const { to } = msg;
|
|
|
|
if (!Client.clients.has(to)) {
|
|
return false;
|
|
}
|
|
|
|
const otherPeer = Client.clients.get(to);
|
|
|
|
if (otherPeer.requiredPair && !otherPeer.pairs.has(to)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
register('auth/info', (client, msg) => {
|
|
const { name, value } = msg;
|
|
|
|
client.info.set(name, value);
|
|
|
|
const clients = client.getSucureClients();
|
|
|
|
for (const [, spair] of clients.pairs) {
|
|
spair.send([{ from: client.id, name, value }, "pair/info"]);
|
|
}
|
|
|
|
for (const [, spair] of clients.roompairs) {
|
|
spair.send([{ from: client.id, name, value }, "pair/info"]);
|
|
}
|
|
|
|
return { status: 'success' };
|
|
});
|
|
|
|
register('peer/info', (client, msg) => {
|
|
const { peer } = msg;
|
|
|
|
if (!client.isSecure(peer)) {
|
|
return { status: "fail", message: "unaccessible user" };
|
|
}
|
|
|
|
const peerClient = Client.clients.get(peer);
|
|
const info = {};
|
|
peerClient.info.forEach((value, name) => { info[name] = value; });
|
|
|
|
return { status: "success", info };
|
|
});
|