From 1b02eb1bce69ca3f59304718cdc2a31fe8d26735 Mon Sep 17 00:00:00 2001 From: abdussamedulutas Date: Thu, 26 Feb 2026 08:12:40 +0300 Subject: [PATCH] Revision --- Source/Client.js | 37 ++--- Source/HTTPServer.js | 4 - Source/IPC.js | 246 ---------------------------------- Source/Services/Auth.js | 22 +-- Source/Services/IPPressure.js | 60 --------- Source/Services/Room.js | 16 --- Source/WebSocket.js | 35 +---- Source/stats.js | 61 --------- incoming-features.md | 5 - index.js | 139 ++----------------- 10 files changed, 28 insertions(+), 597 deletions(-) delete mode 100644 Source/IPC.js delete mode 100644 Source/stats.js delete mode 100644 incoming-features.md diff --git a/Source/Client.js b/Source/Client.js index 9b3fe76..1b6d08c 100644 --- a/Source/Client.js +++ b/Source/Client.js @@ -1,5 +1,3 @@ -const { CLIENT_SEND_MESSAGE, CLIENT_UPDATE_PROP } = require("./IPC"); -const stats = require("./stats"); function Client() { /** @@ -36,16 +34,6 @@ function Client() this.APNumber = 0; this.APShortCode = 0; this.APIPAddress = 0; - this.isProxy = false; - this.proxyProcess = null; - - this.sync = function(...args){ - process.nextTick(()=>{ - for (const name of args) { - CLIENT_UPDATE_PROP(this.id, name, this[name]); - } - }) - }; }; /** * @type {Map} @@ -59,7 +47,6 @@ Client.prototype.peerRequest = function(client){ let info = {}; this.info.forEach((value, name) => info[name] = value); this.pairs.add(client.id); - this.sync('pairs'); client.send([ { from: this.id }, 'request/pair' @@ -165,7 +152,6 @@ Client.prototype.getSucureClients = function() */ Client.prototype.acceptPeerRequest = function(client){ this.pairs.add(client.id); - this.sync('pairs'); client.send([{ from: this.id },'accepted/pair']); @@ -176,7 +162,6 @@ Client.prototype.getSucureClients = function() Client.prototype.rejectPeerRequest = function(client){ this.pairs.delete(client.id); client.pairs.delete(this.id); - this.sync('pairs'); client.send([{ from: this.id },'end/pair']); @@ -200,21 +185,15 @@ Client.prototype.pairList = function(){ }; Client.prototype.send = function(obj){ - if(this.isProxy) - { - CLIENT_SEND_MESSAGE(this.id, obj, this.proxyProcess) + if(this.socket.connected){ + this.socket.sendUTF(JSON.stringify(obj),err => { + if(err && this.socket) + { + console.error("I/O: Hatalı yazma işlemi yapıldı",err.message) + } + }); }else{ - stats.ws_sended_packs++; - if(this.socket.connected){ - this.socket.sendUTF(JSON.stringify(obj),err => { - if(err && this.socket) - { - console.error("I/O: Hatalı yazma işlemi yapıldı",err.message) - } - }); - }else{ - console.error("Bağlantısı kopmuş yazma işlemi") - } + console.error("Bağlantısı kopmuş yazma işlemi") } }; diff --git a/Source/HTTPServer.js b/Source/HTTPServer.js index 787c588..f5cef50 100644 --- a/Source/HTTPServer.js +++ b/Source/HTTPServer.js @@ -8,7 +8,6 @@ let auth = require("express-basic-auth"); const { termoutput } = require("./config"); let server = http.createServer(); -const stats = require("./stats"); let app = express(); server.addListener("request", app); app.use(compression({ @@ -38,9 +37,6 @@ app.get("/stream",(request, response)=>{ app.get("/",(request, response)=>{ response.sendFile(resolve("./script/index.html")) }); -app.post("/stats",(request, response)=>{ - response.json(stats.others); -}); app.get("/console",(request, response)=>{ response.sendFile(resolve("./console/index.html")) }); diff --git a/Source/IPC.js b/Source/IPC.js deleted file mode 100644 index 48a15a6..0000000 --- a/Source/IPC.js +++ /dev/null @@ -1,246 +0,0 @@ - -process.on('message',data => { - const { Client } = require("./Client.js"); - const { Room } = require("./Services/Room"); - switch(data.type) - { - case "CLIENT_CREATED":{ - //slog("CLIENT_CREATED"); - let client = new Client(); - client.isProxy = true; - client.proxyProcess = data.pid; - client.id = data.uuid; - if(Client.clients.has(client.id)) - { - console.error("IPC: Zaten var olan kullanıcı oluşturuluyor") - }else{ - Client.clients.set(client.id, client); - } - break; - } - case "CLIENT_UPDATE_PROP":{ - - data.value = transformDeserialization(data.value, data.typing); - - //slog("CLIENT_UPDATE_PROP"); - let client = Client.clients.get(data.uuid); - client[data.name] = data.value; - break; - } - case "CLIENT_SEND_MESSAGE":{ - //slog("CLIENT_SEND_MESSAGE"); - let client = Client.clients.get(data.uuid); - if(client) - { - if(client.isProxy != true) - { - client.send(data.message) - }else{ - console.error("IPC: Proxy olmayan bir client için IPC mesajı alındı") - } - }else{ - console.error("IPC: Olmayan bir kullanıcı için mesaj gönderiliyor") - } - break; - } - case "CLIENT_DESTROY":{ - //slog("CLIENT_DESTROY"); - if(Client.clients.has(data.uuid)) - { - Client.clients.delete(data.uuid); - }else{ - console.error("IPC: Olmayan bir kullanıcı için silme gerçekleştiriliyor") - } - break; - } - case "ROOM_CREATED":{ - //slog("ROOM_CREATED"); - let room = Room.fromJSON(data.value); - Room.rooms.set(room.id, room); - break; - } - case "ROOM_UPDATE_PROP":{ - - data.value = transformDeserialization(data.value, data.typing); - - - //slog("ROOM_UPDATE_PROP"); - let room = Room.rooms.get(data.uuid); - room[data.name] = data.value; - break; - } - case "ROOM_JOIN_CLIENT":{ - //slog("ROOM_JOIN_CLIENT"); - let room = Room.rooms.get(data.uuid); - let client = Client.clients.get(data.client); - if(room && client) - { - client.rooms.add(room.id); - room.clients.set(client.id, client); - } - break; - } - case "ROOM_EJECT_CLIENT":{ - //slog("ROOM_EJECT_CLIENT"); - let room = Room.rooms.get(data.uuid); - let client = Client.clients.get(data.client); - if(room && client) - { - client.rooms.delete(room.id); - room.clients.delete(client.id, client); - } - break; - } - case "ROOM_DESTROY":{ - //slog("ROOM_DESTROY"); - Room.rooms.delete(data.value); - break; - } - } -}); - -function CLIENT_CREATED(uuid) -{ - mlog("CLIENT_CREATED"); - process.send({ - type:'CLIENT_CREATED', - uuid: uuid - }) -}; -function CLIENT_UPDATE_PROP(uuid, name, value) -{ - mlog("CLIENT_UPDATE_PROP"); - - let typing = value.__proto__.constructor.name; - value = transformSerialization(value); - - process.send({ - type:'CLIENT_UPDATE_PROP', - uuid: uuid, - name, - value, - typing - }) -}; -function CLIENT_SEND_MESSAGE(uuid, message, clusterPid) -{ - mlog("CLIENT_SEND_MESSAGE"); - process.send({ - type:'CLIENT_SEND_MESSAGE', - uuid: uuid, - message, - process:clusterPid - }) -}; -function CLIENT_DESTROY(uuid) -{ - mlog("CLIENT_DESTROY"); - process.send({ - type:'CLIENT_DESTROY', - uuid: uuid - }) -}; - - -function ROOM_CREATED(room) -{ - mlog("ROOM_CREATED"); - let raw = room.toJSON(true); - process.send({ - type:'ROOM_CREATED', - value: raw - }) -}; - -function ROOM_UPDATE_PROP(uuid, name, value) -{ - mlog("ROOM_UPDATE_PROP"); - - let typing = value.__proto__.constructor.name; - value = transformSerialization(value); - - process.send({ - type:'ROOM_UPDATE_PROP', - uuid: uuid, - name, - value, - typing - }) -}; - -function ROOM_JOIN_CLIENT(uuid, client) -{ - mlog("ROOM_JOIN_CLIENT"); - process.send({ - type:'ROOM_JOIN_CLIENT', - uuid: uuid, - client - }) -}; -function ROOM_EJECT_CLIENT(uuid, client) -{ - mlog("ROOM_EJECT_CLIENT"); - process.send({ - type:'ROOM_EJECT_CLIENT', - uuid: uuid, - client - }) -}; - -function ROOM_DESTROY(room) -{ - mlog("ROOM_DESTROY"); - process.send({ - type:'ROOM_DESTROY', - value: room.id - }) -}; - -function mlog(command) -{ - return; - console.log("M",process.pid, command) -} -function slog(command) -{ - return; - console.log("S",process.pid, command) -} - -function transformSerialization(value) -{ - switch(value.__proto__.constructor.name) - { - case "Map":{ - return [...value]; - } - case "Set":{ - return [...value]; - } - } -} -function transformDeserialization(value,type) -{ - switch(type) - { - case "Map":{ - return new Map(value); - } - case "Set":{ - return new Set(value) - } - } -} - - -exports.CLIENT_CREATED = CLIENT_CREATED; -exports.CLIENT_UPDATE_PROP = CLIENT_UPDATE_PROP; -exports.CLIENT_DESTROY = CLIENT_DESTROY; -exports.CLIENT_SEND_MESSAGE = CLIENT_SEND_MESSAGE; -exports.ROOM_CREATED = ROOM_CREATED; -exports.ROOM_UPDATE_PROP = ROOM_UPDATE_PROP; -exports.ROOM_JOIN_CLIENT = ROOM_JOIN_CLIENT; -exports.ROOM_EJECT_CLIENT = ROOM_EJECT_CLIENT; -exports.ROOM_DESTROY = ROOM_DESTROY; -exports.mlog = mlog; -exports.slog = slog; \ No newline at end of file diff --git a/Source/Services/Auth.js b/Source/Services/Auth.js index 4b52f17..162d6ac 100644 --- a/Source/Services/Auth.js +++ b/Source/Services/Auth.js @@ -204,12 +204,6 @@ addService(({ value },"pair/info"]); }; - process.send({ - type: 'AUTH/INFO', - uuid: client.id, - name, - value - }); return end({ status: 'success' }); @@ -235,18 +229,4 @@ addService(({ next(); } } -}); - -process.on('message',({type, uuid, value, name}) => { - switch(type) - { - case "AUTH/INFO":{ - let client = Client.clients.get(uuid); - if(client) - { - client.info.set(name, value); - } - break; - } - } -}) \ No newline at end of file +}); \ No newline at end of file diff --git a/Source/Services/IPPressure.js b/Source/Services/IPPressure.js index 1de3b25..4db2bb9 100644 --- a/Source/Services/IPPressure.js +++ b/Source/Services/IPPressure.js @@ -378,66 +378,6 @@ addService(({ } }) -process.on('message',({type, uuid, value}) => { - switch(type) - { - case "AP_NUMBER/LOCK":{ - let client = Client.clients.get(uuid); - APNumber.busyNumbers.set(value, client); - if(client) - { - client.APNumber = value; - } - break; - } - case "AP_NUMBER/RELEASE":{ - APNumber.busyNumbers.delete(value); - let client = Client.clients.get(uuid); - if(client) - { - client.APNumber = void 0; - } - break; - } - case "AP_SHORTCODE/LOCK":{ - let client = Client.clients.get(uuid); - APShortCode.busyCodes.set(value, client); - if(client) - { - client.APShortCode = value; - } - break; - } - case "AP_SHORTCODE/RELEASE":{ - APShortCode.busyCodes.delete(value); - let client = Client.clients.get(uuid); - if(client) - { - client.APShortCode = void 0; - } - break; - } - case "AP_IPADDRESS/LOCK":{ - let client = Client.clients.get(uuid); - APIPAddress.busyIP.set(value, client); - if(client) - { - client.APIPAddress = value; - } - break; - } - case "AP_IPADDRESS/RELEASE":{ - APIPAddress.busyIP.delete(value); - let client = Client.clients.get(uuid); - if(client) - { - client.APIPAddress = void 0; - } - break; - } - } -}) - addListener('disconnect',(global, client)=>{ if(client.APIPAddress != 0) { diff --git a/Source/Services/Room.js b/Source/Services/Room.js index 208b9db..314a66e 100644 --- a/Source/Services/Room.js +++ b/Source/Services/Room.js @@ -3,9 +3,7 @@ let {randomUUID,createHash} = require("crypto"); const joi = require("joi"); let {addService,addListener} = require("../WebSocket.js"); const { termoutput } = require("../config.js"); -const { ROOM_CREATED, ROOM_DESTROY, ROOM_UPDATE_PROP, ROOM_JOIN_CLIENT, ROOM_EJECT_CLIENT } = require("../IPC.js"); let term = require("terminal-kit").terminal; -const stats = require("../stats"); function Sha256(update) { @@ -71,22 +69,12 @@ function Room() * @type {Map} */ this.info = new Map(); - - this.sync = function(...args){ - process.nextTick(()=>{ - for (const name of args) { - ROOM_UPDATE_PROP(this.id, name, this[name]); - } - }) - }; } /** * @param {Room} room */ Room.prototype.publish = function(){ - stats.mwse_rooms++; Room.rooms.set(this.id, this); - ROOM_CREATED(this); termoutput && term.green("Room Published ").white(this.name," in ").yellow(this.clients.size).white(" clients")('\n'); }; /** @@ -199,7 +187,6 @@ Room.prototype.join = function(client){ }; client.rooms.add(this.id); this.clients.set(client.id, client); - ROOM_JOIN_CLIENT(this.id, client.id); termoutput && term.green("Client Room joined ").white(this.name," in ").yellow(this.clients.size + "").white(" clients")('\n'); }; Room.prototype.down = function(){ @@ -209,8 +196,6 @@ Room.prototype.down = function(){ ownerid: this.owner.id },'room/closed']); Room.rooms.delete(this.id); - ROOM_DESTROY(this) - stats.mwse_rooms--; }; /** * @param {Client} client @@ -233,7 +218,6 @@ Room.prototype.eject = function(client){ } client.rooms.delete(this.id); this.clients.delete(client.id); - ROOM_EJECT_CLIENT(this.id, client.id); if(this.clients.size == 0) { diff --git a/Source/WebSocket.js b/Source/WebSocket.js index cd3aa77..fe750dd 100644 --- a/Source/WebSocket.js +++ b/Source/WebSocket.js @@ -5,8 +5,6 @@ let {http} = require("./HTTPServer"); let {randomUUID} = require("crypto"); const { Client } = require("./Client.js"); const { termoutput } = require("./config"); -const { CLIENT_CREATED, CLIENT_DESTROY } = require("./IPC"); -const stats = require("./stats"); termoutput && console.log("Web Socket Protocol is ready"); http.addListener("upgrade",() => { @@ -42,32 +40,8 @@ wsServer.addListener("connect",(socket) => { Client.clients.set(id, xClient); clients.set(id, xClient); - stats.mwse_clients++; - - CLIENT_CREATED(id); - emit("connect", global, xClient); - let oldw = 0, oldr = 0; - let timer = setInterval(()=>{ - let writed = socket.socket.bytesRead - oldr; - let readed = socket.socket.bytesWritten - oldw; - stats.ws_total_bytes += (writed + readed); - stats.ws_readed_bytes += readed; - stats.ws_writed_bytes += writed; - oldr = socket.socket.bytesRead; - oldw = socket.socket.bytesWritten; - }, 1000) - - socket.addListener("close",()=>{ - stats.mwse_clients--; - emit("disconnect", global, xClient); - CLIENT_DESTROY(id); - Client.clients.delete(id); - clearInterval(timer); - clearInterval(pingTimer); - }); - let pingTimer = setInterval(()=> socket.ping('saQut') , 10_000); socket.addListener("pong",validationText => { @@ -76,9 +50,6 @@ wsServer.addListener("connect",(socket) => { } }) socket.addListener("message",({type,utf8Data}) => { - stats.ws_recaived_packs++; - stats.ws_total_packs++; - if(type == "utf8") { let json; @@ -97,6 +68,12 @@ wsServer.addListener("connect",(socket) => { } } }); + + socket.addListener("close",()=>{ + emit("disconnect", global, xClient); + Client.clients.delete(id); + clearInterval(pingTimer); + }); }); /** diff --git a/Source/stats.js b/Source/stats.js deleted file mode 100644 index 4d21ca1..0000000 --- a/Source/stats.js +++ /dev/null @@ -1,61 +0,0 @@ -const { mlog } = require("./IPC"); - -exports.ws_writed_bytes = 0; -exports.ws_readed_bytes = 0; -exports.ws_total_bytes = 0; -exports.ws_sended_packs = 0; -exports.ws_recaived_packs = 0; -exports.ws_total_packs = 0; -exports.mwse_rooms = 0; -exports.mwse_clients = 0; - - -process.send({ - core: "writestat", - ws_writed_bytes: exports.ws_writed_bytes, - ws_readed_bytes: exports.ws_readed_bytes, - ws_total_bytes: exports.ws_total_bytes, - ws_sended_packs: exports.ws_sended_packs, - ws_recaived_packs: exports.ws_recaived_packs, - ws_total_packs: exports.ws_total_packs, - mwse_rooms: exports.mwse_rooms, - mwse_clients: exports.mwse_clients -}); -setInterval(()=>{ - process.send({ - core: "writestat", - ws_writed_bytes: exports.ws_writed_bytes, - ws_readed_bytes: exports.ws_readed_bytes, - ws_total_bytes: exports.ws_total_bytes, - ws_sended_packs: exports.ws_sended_packs, - ws_recaived_packs: exports.ws_recaived_packs, - ws_total_packs: exports.ws_total_packs, - mwse_rooms: exports.mwse_rooms, - mwse_clients: exports.mwse_clients - }) - mlog(`writed ${exports.ws_writed_bytes} bytes, readed ${exports.ws_readed_bytes} bytes`); - exports.ws_writed_bytes = 0; - exports.ws_readed_bytes = 0; - exports.ws_total_bytes = 0; - exports.ws_sended_packs = 0; - exports.ws_recaived_packs = 0; - exports.ws_total_packs = 0; - process.send({ - core: "readstat", - ws_writed_bytes: exports.ws_writed_bytes, - ws_readed_bytes: exports.ws_readed_bytes, - ws_total_bytes: exports.ws_total_bytes, - ws_sended_packs: exports.ws_sended_packs, - ws_recaived_packs: exports.ws_recaived_packs, - ws_total_packs: exports.ws_total_packs, - mwse_rooms: exports.mwse_rooms, - mwse_clients: exports.mwse_clients - }) -}, 3000) - -process.on('message', stat => { - if(stat.type == ':stats:') - { - exports.others = stat.data; - } -}) \ No newline at end of file diff --git a/incoming-features.md b/incoming-features.md deleted file mode 100644 index efdac40..0000000 --- a/incoming-features.md +++ /dev/null @@ -1,5 +0,0 @@ -/ Odaların kendi verilerinin bulunması -> HTTP ile kişilere mesaj (request) mesaj iletimi -> HTTP ile odalar ve kişiler hakkında veri alınabilmesi -> Websoket ile http proxy uygulama (WSAuth) -> Session WSAuth \ No newline at end of file diff --git a/index.js b/index.js index 3bfe2d6..95d25f6 100644 --- a/index.js +++ b/index.js @@ -1,127 +1,14 @@ -/** @type {import('node:cluster').Cluster} */ -const cluster = require("cluster"); -const os = require("os"); -let {randomUUID} = require("crypto"); +require("./Source/index"); -/** - * Use Round Robin algorithm for cluster process load balancer - */ -// cluster.schedulingPolicy = cluster.SCHED_RR; - -async function main() -{ - if(cluster.isPrimary == false) - { - console.log("Slave Process PID:", process.pid); - // This process is a worker / slave - // Compile source code and run - require("./Source/index"); - // stay here - return; - }; - - // This process is a primary / master - console.log("Master Process PID:", process.pid); - - // Worker process list - const master = new Map(); - - const coreCount = 1 //os.cpus().length; - for(let index = 0; index < coreCount; index++) - { - // Open slave process - let worker = await generateFlow(); - // Save process with id - master.set(worker.id, worker); - // Listen process for commands - worker.message( - // This process want to send payload to sibling process with IPC - (workerId, payload) =>{ - // Check Target worker - if(payload.core) - { - switch(payload.core) - { - case "writestat":{ - master.get(workerId).setStats(payload) - break; - } - case "readstat":{ - master.get(workerId).send({ - type: ':stats:', - data: [ - ...master.entries() - ].map(( - [, master] - ) => { - let e = master.getStats(); - return { - core: master.uuid, - ws_writed_bytes:e.ws_writed_bytes, - ws_readed_bytes:e.ws_readed_bytes, - ws_total_bytes:e.ws_total_bytes, - ws_sended_packs:e.ws_sended_packs, - ws_recaived_packs:e.ws_recaived_packs, - ws_total_packs:e.ws_total_packs, - mwse_rooms: e.mwse_rooms, - mwse_clients: e.mwse_clients - } - }) - }) - break; - } - } - }else if(payload.process) - { - master.get(payload.process).send({ - ...payload, - pid: worker.id - }) - }else for (const [siblingWorkerId,{send}] of master) { - // No sending to itself - if(workerId !== siblingWorkerId) - { - - // Send command to sibling with IPC - send({ - ...payload, - pid: worker.id - }) - } - } - } - ) - } -} - - - - -async function generateFlow() -{ - // Mirror this process with for (low-level os multitasking) - const worker = cluster.fork(); - // Wait process is online - await new Promise(ok => { - worker.addListener("online",()=> { - ok() - }) - }); - // Get process pid on the os - let id = worker.process.pid; - - let stats = {}; - - // Simplification wrapping send and get events with IPC's event functions - return { - id, - uuid: randomUUID(), - send: message => worker.send(message), - message: (callback) => worker.addListener("message", e => callback(id,e)), - getStats: () => stats, - setStats: e => Object.assign(stats, e) - } -} - -// Run immediately -process.nextTick(main); \ No newline at end of file +process.on('unhandledRejection',(reason, promise)=>{ + console.log("Process unhandledRejection",{reason, promise}) +}); +process.on('rejectionHandled',(promise)=>{ + console.log("Process rejectionHandled",{promise}) +}); +process.on('multipleResolves',(type, promise, value)=>{ + console.log("Process multipleResolves",{type, promise, value}) +}); +process.on('warning',(err)=>{ + console.log("Process warning", err) +});