From 603397132dc9713b1ba993674c9814cd3b27e843 Mon Sep 17 00:00:00 2001 From: Abdussamed Date: Wed, 31 Jan 2024 12:34:15 +0300 Subject: [PATCH] =?UTF-8?q?IP=20Bas=C4=B1n=C3=A7l=C4=B1=20ba=C4=9Flant?= =?UTF-8?q?=C4=B1lar=C4=B1=20optimize=20edildi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/Services/Client.js | 4 +- Source/Services/IPPressure.js | 2 +- Source/config.js | 7 +- Source/index.js | 1 + Source/module/IPAllocation.js | 224 ++++++++++ database/ipallocation.db | Bin 0 -> 16384 bytes package-lock.json | 9 + package.json | 1 + script/test.html | 4 +- yarn.lock | 757 ++++++++++++++++++---------------- 10 files changed, 641 insertions(+), 368 deletions(-) create mode 100644 Source/module/IPAllocation.js create mode 100644 database/ipallocation.db diff --git a/Source/Services/Client.js b/Source/Services/Client.js index f5aa137..d59a55d 100644 --- a/Source/Services/Client.js +++ b/Source/Services/Client.js @@ -1,5 +1,5 @@ -const { CLIENT_SEND_MESSAGE, CLIENT_UPDATE_PROP } = require("./IPC"); -const stats = require("./stats"); +const { CLIENT_SEND_MESSAGE, CLIENT_UPDATE_PROP } = require("../IPC"); +const stats = require("../stats"); function Client() { /** diff --git a/Source/Services/IPPressure.js b/Source/Services/IPPressure.js index 1de3b25..58f2f36 100644 --- a/Source/Services/IPPressure.js +++ b/Source/Services/IPPressure.js @@ -1,4 +1,4 @@ -const { Client } = require("../Client"); +const { Client } = require("./Client"); let {addService, addListener} = require("../WebSocket.js"); class APNumber{ diff --git a/Source/config.js b/Source/config.js index c03498f..655faf6 100644 --- a/Source/config.js +++ b/Source/config.js @@ -1,3 +1,5 @@ +const {resolve} = require("node:path"); + exports.termoutput = false; exports.HTTP_PORT = 7707; @@ -7,4 +9,7 @@ exports.HTTP_AUTH = true; exports.HTTP_AUTH_USERNAME = 'saqut'; exports.HTTP_AUTH_USERPASSWORD = 'yum81633'; -exports.LOADBALANCE_CPU = 2; // ---> Cpu number (2,3,4...) for load balancer process or false for single thread (no load balancer and multiprocess) \ No newline at end of file +exports.LOADBALANCE_CPU = false; // ---> Cpu number (2,3,4...) for load balancer process or false for single thread (no load balancer and multiprocess) + +exports.IPALLOCATION_DB = resolve(__dirname,"../database/ipallocation.db"); +exports.IPALLOCATION_STORE_TYPE = "memory" // or file \ No newline at end of file diff --git a/Source/index.js b/Source/index.js index f2ca768..d529aa2 100644 --- a/Source/index.js +++ b/Source/index.js @@ -6,6 +6,7 @@ require("./Services/Auth.js"); require("./Services/Room.js"); require("./Services/DataTransfer.js"); require("./Services/IPPressure.js"); +require("./module/IPAllocation.js"); process.on('unhandledRejection',()=>{ diff --git a/Source/module/IPAllocation.js b/Source/module/IPAllocation.js new file mode 100644 index 0000000..6bd94ad --- /dev/null +++ b/Source/module/IPAllocation.js @@ -0,0 +1,224 @@ +const knex = require("knex"); +const { IPALLOCATION_DB } = require("../config"); +const crypto = require("node:crypto"); +const moment = require("moment"); + +let IPDB = knex({ + client: "sqlite3", + connection: exports.IPALLOCATION_STORE_TYPE == "memory" ? ":memory:" : IPALLOCATION_DB, + useNullAsDefault: true +}); + +let flovitNumber = [ + [ + 8, + 12, + 24, + 48, + 52, + 56 + ], + [ + 7, + 10, + 13, + 16, + 19, + 22, + 25, + 28, + 31, + 34, + 37, + 40, + 43, + 46, + 49, + 52, + 55, + 58, + 61, + 64, + 67, + 70, + 73, + 76, + 79, + 82 + ] +]; + +async function read(filter) +{ + return await IPDB.table("iptable").where(filter).first(); +} +async function has(filter) +{ + return ( + await IPDB.table("iptable").where(filter).count('* as key').first() + ).key != 0; +} +async function flushAll() +{ + await IPDB.table("iptable").delete(); +} +async function write(value,filter) +{ + await IPDB.table("iptable").where(filter).update(value); +} +async function create(value) +{ + await IPDB.table("iptable").insert(value); +} +async function remove(...filterArgs) +{ + await IPDB.table("iptable").where(...filterArgs).delete(); +} + +async function migrate() +{ + if(!await IPDB.schema.hasTable("iptable")) + { + await IPDB.schema.createTable("iptable",function(table){ + table.bigInteger("ipaddress").unsigned().primary(); + table.text("owner").unique(); + table.string("type", 50).defaultTo("client"); + table.dateTime("created").defaultTo(IPDB.fn.now()); + }); + } +}; + + +function iptobin(a,b,c,d) +{ + const bin = + ((a | 0) << 24) | + ((b | 0) << 16) | + ((c | 0) << 8) | + (d | 0); + return bin; +} +function bintoip(bin) +{ + if(typeof bin == "string") + { + bin = bin | 0; + } + const octet1 = (bin >>> 24) & 0xff; + const octet2 = (bin >>> 16) & 0xff; + const octet3 = (bin >>> 8) & 0xff; + const octet4 = bin & 0xff; + return [octet1,octet2,octet3,octet4]; +} +function randomMaskINIT() +{ + if(typeof bin == "string") + { + bin = bin.split('.').map(e => e | 0); + } + let globalmask = crypto.randomInt(flovitNumber[0].length); + let macromask = crypto.randomInt(flovitNumber[1].length); + + return [ + flovitNumber[0][globalmask], + flovitNumber[1][macromask], + 0, + 0 + ] +} +async function init() +{ + await migrate(); + await flushAll(); + await create({ + ipaddress: 0, // globalmask & macromask + owner: randomMaskINIT().join('.'), + type: "system", + created: moment().format() + }); + hook() +} +init.inited = false; + +async function getip(owner, type) +{ + let call = await read({ + owner, + type + }); + return !call ? false : { + ipaddress: call.ipaddress, + date: call.created + }; +} +async function setip(owner, type, ip) +{ + await getip(owner, type) ? + await write({ + ipaddress: ip + },{ + owner, + type + }) + : + await create({ + ipaddress: ip, + owner, + type + }); +} + +async function allocateIP(owner, type, changeOverlord) +{ + let [a,b,c,d] = [ ,,, ]; + if(changeOverlord) + { + [a,b,c,d] = randomMaskINIT(); + }else{ + let ipinfo = await read({ + ipaddress: 0, + type: "system" + }); + [a,b,c,d] = ipinfo.owner.split('.').map(e => e | 0); + } + + while(1) + { + if(d != 255) + { + d++; + }else if(c != 255){ + c++; + }else if(b != 255){ + b++; + }else if(a != 255){ + a++; + }else{ + [a,b,c,d] = randomMaskINIT(); + } + let query = { + ipaddress: iptobin(a,b,c,d) + }; + let result = await has(query); + if(result == false) + { + await create({ + ipaddress: iptobin(a,b,c,d), + owner, + type + }); + await write({ + owner: [a,b,c,d].join('.') + },{ + type: "system", + ipaddress: 0 + }); + break; + }else{ + console.log("Checking",[a,b,c,d].join('.')) + } + } + return[a,b,c,d].join('.') +} + +process.nextTick(init); \ No newline at end of file diff --git a/database/ipallocation.db b/database/ipallocation.db new file mode 100644 index 0000000000000000000000000000000000000000..e8c1a975addc93a28a08062fffe52d6ecc701e11 GIT binary patch literal 16384 zcmeI3Ply{;9LHxelVp<2%y0jNWm%-JUfQLzVgBUrUhK98U2Io(n*~uyGTqGTU^cr= zCi;RP)QjLn!Bek#P!M_&q=z27h@Qklu~(rKw1*yw;K75F_}**=I=y<>--BVkzszS| zCZAsjPcvMSyJzu)z!e;l|sdS3hJ zgT6OtkNW=2k;fjBFYh)tTTQaJy|uH~BwO1rH{T?kXPwX?NB!tXhn(+BXX~upx+G?o zS69W4-=01RJC*HHZ2H#br7O+Y+5WNH?GC)*utRS6@A&<|AZ$hwcyv+)<72Nw-ggK4 z@4AEY6?2Vry#sgD3&^?sp6~U8b0M@p@Z7-bc1Sne1^%HIL6`S-cbnU-c5CZu^IB{3 z>Kg_*9{7juU`*cg#^Lp!T*2DfdMdNLxhe`%UBjEb@LPS`9R)|zy3KW1EJ&efWg=S( zx1w50Y4_nP7{M#BqCeK}>7VJY?&xXwRr^{SYp-c9s87@%)Gt&|txe9tfdl+I|kLb2`OoE0MMg zjnkqTjRfCTV_i%uXI5USR7;jwGE0r&co=wxCyi;@_M|apFnkOA+g#_q3KwE1ZscPpipvmeW!1X(gG?a60NeEho{Ak>+&Nd+H?9 zGN+^7Q#*;a4T;lH@2QnUTSki0V$_|acZy{hDyIt(Z6?iKH}m0#RuNtm;2(Gjf5PwZ z0DgfV;U0Vk-@=!08$O3m;G=M5pa=W#7VLy54m5xU&;S}h184vZpaC?12G9T+Km(`N zfFjA_WHqeFg0jRyc9Dh50t@MR7UVe=Bw!(>vmk1d5H83RR2K3D7IJwOlpG6Lg@sI( zg>;4mIn9D3vyhTl5L1$*|hi}4l zfKTDWa1G!(JUq26#uttT&;S}h184vZpaC?12G9T+Km-4W0bM#9{TyDW#o4k(muAZ< zU7Rf|bYZqE)A`xbp>wmPO<}gQs6Hv9J1|Ub_9!(~RXUT4Zo9Bz2OEW0S#RgRyyfeY0I(Fl42#D{Ec5L*Gpw*a%;%Hv*HIS literal 0 HcmV?d00001 diff --git a/package-lock.json b/package-lock.json index 515501d..5f781c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "fflate": "^0.8.1", "joi": "^17.11.0", "knex": "^3.0.1", + "moment": "^2.29.4", "sqlite3": "^5.1.6", "systemjs": "^6.14.2", "terminal": "^0.1.4", @@ -2619,6 +2620,14 @@ "node": ">=10" } }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", diff --git a/package.json b/package.json index 52f55e1..c234975 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "fflate": "^0.8.1", "joi": "^17.11.0", "knex": "^3.0.1", + "moment": "^2.29.4", "sqlite3": "^5.1.6", "systemjs": "^6.14.2", "terminal": "^0.1.4", diff --git a/script/test.html b/script/test.html index 632ddc6..1ea956f 100644 --- a/script/test.html +++ b/script/test.html @@ -9,11 +9,11 @@

- +