diff --git a/Source/Client.js b/Source/Client.js index 3ef4999..4246c3a 100644 --- a/Source/Client.js +++ b/Source/Client.js @@ -15,12 +15,61 @@ function Client() this.store = new Map(); this.rooms = new Set(); + this.pairs = new Set(); + this.requiredPair = false; }; /** * @type {Map} */ Client.clients = new Map(); +/** + * @param {Client} client + */ +Client.prototype.peerRequest = function(client){ + let info = {}; + this.store.forEach((value, name) => info[name] = value); + this.pairs.add(client.id); + client.send([{ + from: this.id, + info + },'request/pair']); +}; + +/** + * @param {Client} client + */ + Client.prototype.acceptPeerRequest = function(client){ + this.pairs.add(client.id); + client.send([{ + from: this.id + },'accepted/pair']); +}; +/** + * @param {Client} client + */ +Client.prototype.rejectPeerRequest = function(client){ + this.pairs.delete(client.id); + client.pairs.delete(this.id); + client.send([{ + from: this.id + },'rejected/pair']); +}; +/** + * @param {Client|string} client + * @returns {Boolean} + */ +Client.prototype.isPaired = function(client){ + if(typeof client == "string") + { + return Client.clients.get(client)?.pairs.has(this.id) && this.pairs.has(client) + } + return client.pairs.has(this.id) && this.pairs.has(client.id); +}; +Client.prototype.pairList = function(){ + return [...this.pairs.values()].filter(e => this.isPaired(e)); +}; + Client.prototype.send = function(obj){ this.socket.sendUTF(JSON.stringify(obj)); }; diff --git a/Source/Services/Auth.js b/Source/Services/Auth.js index da3adf4..49eb97a 100644 --- a/Source/Services/Auth.js +++ b/Source/Services/Auth.js @@ -1,4 +1,11 @@ -let {addService} = require("../WebSocket.js"); +const { Client } = require("../Client.js"); +let {addService, addListener} = require("../WebSocket.js"); + +addListener('disconnect',(global, xClient)=>{ + for (const pair of xClient.pairs) { + + } +}) addService(({ client, @@ -6,41 +13,139 @@ addService(({ end, next })=>{ - let {type,username,password} = message; - if(type === 'auth/check') + let {type,username,password,to} = message; + switch(type) { - let auth = client.store.has('user'); - return end({ - value: auth - }) - }; - if(type === 'auth/login') - { - if(username == 'admin' && password == '123456Kc') - { + case 'auth/public':{ + client.requiredPair = false; return end({ - status: 'success' - }) - }else{ - return end({ - status: 'fail' + value: 'success', + mode: 'public' }) } - }; - if(type === 'auth/logout') - { - let auth = client.store.has('user'); - if(auth) - { - client.store.delete('user'); + case 'auth/private':{ + client.requiredPair = true; return end({ - status: 'success' - }) - }else{ - return end({ - status: 'fail' + value: 'success', + mode: 'private' }) } - }; - next(); + case 'request/pair':{ + if(Client.clients.has(to)){ + return end({ + status: 'fail', + message: 'CLIENT-NOT-FOUND' + }) + }; + let pairclient = Client.clients.get(to); + if(pairclient.pairs.has(client.id)) + { + return end({ + status: 'success', + message: 'ALREADY-PAIRED' + }) + } + if(client.pairs.add(to)) + { + return end({ + status: 'fail', + message: 'ALREADY-REQUESTED' + }) + } + client.peerRequest(pairclient); + break; + } + case 'accept/pair':{ + if(Client.clients.has(to)){ + return end({ + status: 'fail', + message: 'CLIENT-NOT-FOUND' + }) + }; + let pairclient = Client.clients.get(to); + if(pairclient.pairs.has(client.id)) + { + return end({ + status: 'success', + message: 'ALREADY-PAIRED' + }) + } + if(!client.pairs.has(to)) + { + return end({ + status: 'fail', + message: 'NOT-REQUESTED-PAIR' + }) + } + client.acceptPeerRequest(pairclient); + break; + } + case 'reject/pair':{ + if(Client.clients.has(to)){ + return end({ + status: 'fail', + message: 'CLIENT-NOT-FOUND' + }) + }; + let pairclient = Client.clients.get(to); + if(pairclient.pairs.has(client.id)) + { + return end({ + status: 'success', + message: 'ALREADY-PAIRED' + }) + } + if(!client.pairs.has(to)) + { + return end({ + status: 'fail', + message: 'NOT-REQUESTED-PAIR' + }) + } + client.rejectPeerRequest(pairclient); + break; + } + case 'pair/list':{ + end({ + type:'pair/list', + value: pairList + }) + break; + } + case 'auth/check':{ + let auth = client.store.has('user'); + return end({ + value: auth + }) + } + case 'auth/login':{ + if(username == '*' && password == '*') + { + return end({ + status: 'success' + }) + }else{ + return end({ + status: 'fail' + }) + } + } + case 'auth/logout':{ + let auth = client.store.has('user'); + if(auth) + { + client.store.delete('user'); + return end({ + status: 'success' + }) + }else{ + return end({ + status: 'fail' + }) + } + } + default:{ + next(); + } + } }); \ No newline at end of file diff --git a/Source/Services/DataTransfer.js b/Source/Services/DataTransfer.js index 138854c..6ef7388 100644 --- a/Source/Services/DataTransfer.js +++ b/Source/Services/DataTransfer.js @@ -29,7 +29,8 @@ addService(({ let {to,pack,handshake} = message; if(Client.clients.has(to)) { - Client.clients.get(to).send([{ + let otherPeer = Client.clients.get(to); + otherPeer.send([{ from: client.id, pack: pack }, 'pack']); diff --git a/script/wsjs.js b/script/wsjs.js index 7d00469..3e41c97 100644 --- a/script/wsjs.js +++ b/script/wsjs.js @@ -116,7 +116,14 @@ WSJS.prototype.sendRaw = function(obj){ { this.ws.send(JSON.stringify(obj)) }; -} +}; + + + + + + + WSJS.prototype.authWith = async function(username, password){ await this.request({ type: 'auth/login', diff --git a/test.html b/test.html index d3defee..595ed83 100644 --- a/test.html +++ b/test.html @@ -9,6 +9,65 @@