From b11e2536cb7574347e2b112985cef28155d19c4c Mon Sep 17 00:00:00 2001 From: C Date: Thu, 29 Dec 2022 16:16:19 +0500 Subject: [PATCH] session destroy fixed --- source/app.js | 8 +-- source/coordinator/config.json | 8 ++- source/coordinator/controller/session.js | 61 +++++++++++-------- .../coordinator/controller/session_server.js | 8 ++- source/coordinator/controller/title.js | 6 +- source/lib/http_client.js | 15 +++++ source/package-lock.json | 11 ++++ source/package.json | 1 + source/session_server/config.json | 21 +++---- source/session_server/controller/session.js | 41 ++++++++----- source/session_server/titles.json | 6 +- source/utils/generate_nginx_proxy_pass.js | 21 +++++++ source/utils/generate_session_server_id.js | 3 + 13 files changed, 146 insertions(+), 64 deletions(-) create mode 100644 source/utils/generate_nginx_proxy_pass.js create mode 100644 source/utils/generate_session_server_id.js diff --git a/source/app.js b/source/app.js index b1bfbc2..65cb98c 100644 --- a/source/app.js +++ b/source/app.js @@ -29,7 +29,7 @@ app.use(function(req, res, next) { // CORS app.options(function(req, res) { //'GET, POST, OPTIONS, PUT, PATCH, DELETE' - res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS') + res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST') res.end() }) @@ -50,7 +50,7 @@ app.use(routes) app.use(function(error, req, res, next) { // errors logger_runtime.error(error) - res.send(error.message) + res.status(400).json({type:'error', content:error.message}) next() }) @@ -60,8 +60,8 @@ app.use(function(req, res, next) { // start listen server try { - app.listen(config.port, config.ip) - logger_init.log(`Listening at http://${config.ip}:${config.port}`) + app.listen(config.port) + logger_init.log(`Listening at http://localhost:${config.port}`) } catch (e) { return logger_init.error(e) } \ No newline at end of file diff --git a/source/coordinator/config.json b/source/coordinator/config.json index c9dc174..a242598 100644 --- a/source/coordinator/config.json +++ b/source/coordinator/config.json @@ -1,10 +1,12 @@ { - "ip" : "192.168.1.115", "port" : "3001", - "mongodb_url" : "mongodb://127.0.0.1:27017", + "mongodb_url" : "mongodb://pixel_admin:!G3432664499@192.168.1.163:27017", "database_name" : "pixel_streaming", "cors":[ "http://192.168.1.171:3000", - "http://localhost:3000" + "http://localhost:3000", + "https://stream.graff.tech/", + "http://212.220.216.185:3005", + "https://stream.graff.tech" ] } \ No newline at end of file diff --git a/source/coordinator/controller/session.js b/source/coordinator/controller/session.js index 9ae8d24..4ad2c1d 100644 --- a/source/coordinator/controller/session.js +++ b/source/coordinator/controller/session.js @@ -4,13 +4,13 @@ const db_connection = require('../database/connection.js') const http_client = require('../../lib/http_client.js') const crypto = require('crypto') -module.exports.plan_session = async (req, res, next) => { +const plan_session = async (req, res, next) => { //var session_sheduled = database.collection('session_sheduled') //var sessions = await session_sheduled.find().toArray() //callback(response) } -module.exports.create_session = async (req, res, next) => { +const create_session = async (req, res, next) => { //create?title=name&next_var=0 try { const db = await db_connection.get_db() @@ -41,27 +41,30 @@ module.exports.create_session = async (req, res, next) => { port: free_server.port, path: `/session/create?title=${req.query.title}&session_id=${session_id}` }, async function(answer) { - answer = JSON.parse(answer) - //console.log(answer.method) - if (!answer.link) { - next(Error('session not created')) - return + try { + answer = JSON.parse(answer) + //console.log(answer.method) + if (!answer.link) + throw new Error('session not created') + + // generate random code for session access + var code = Math.floor(1000 + Math.random() * 9000) + while ((await session_active.find({'code':code}).toArray()).length) { + code = Math.floor(1000 + Math.random() * 9000) + } + + // add session to database + await session_active.insertOne({ + server_id:free_server.server_id, + session_id:session_id, + connection_link:answer.link, + connection_code:code + }) + // SHALL REMOVE CONNECTION CODE BECAUSE IN WEB WE DO NOT NEED IT + res.json({msg:'SESSION_CREATED', link:answer.link, connection_code:code}) + } catch (e) { + next(e) } - // generate random code for session access - var code = Math.floor(1000 + Math.random() * 9000) - while ((await session_active.find({'code':code}).toArray()).length) { - code = Math.floor(1000 + Math.random() * 9000) - } - - // add session to database - await session_active.insertOne({ - server_id:free_server.server_id, - session_id:session_id, - connection_link:answer.link, - connection_code:code - }) - // SHALL REMOVE CONNECTION CODE BECAUSE IN WEB WE DO NOT NEED IT - res.json({msg:'SESSION_CREATED', link:answer.link, connection_code:code}) }, function(err) { next(Error('session server not working')) }) @@ -70,20 +73,28 @@ module.exports.create_session = async (req, res, next) => { } } -module.exports.connect_session = async (req, res, next) => { +const connect_session = async (req, res, next) => { try { const db = await db_connection.get_db() var session = await db.collection('session_active').findOne({connection_code:parseInt(req.query.connection_code)}) if (!session) throw new Error('sessions not found') - res.json({connection_link:session.connection_link}) + + res.json({msg:'SESSION_DATA', link:session.connection_link, connection_code:session.connection_code}) } catch (e) { next (e) } } -module.exports.close_session = async (req, res, next) => { +const close_session = async (req, res, next) => { const db = await db_connection.get_db() await db.collection('session_active').deleteOne({session_id:req.query.session_id}) res.json({msg:"SESSION_CLOSED", session_id:req.session_id}) +} + +module.exports = { + plan_session, + create_session, + connect_session, + close_session } \ No newline at end of file diff --git a/source/coordinator/controller/session_server.js b/source/coordinator/controller/session_server.js index 7dd1ec5..bf8f1e4 100644 --- a/source/coordinator/controller/session_server.js +++ b/source/coordinator/controller/session_server.js @@ -1,10 +1,14 @@ const db_connection = require('../database/connection.js') const crypto = require('crypto') -module.exports.emergency_shutdown = async (req, res, next) => { +const emergency_shutdown = async (req, res, next) => { const db = await db_connection.get_db() - var id = crypto.createHash('md5').update(req.query.ip+req.query.port).digest('hex') + var id = req.query.id var query = {server_id:{$in:[id]}} await db.collection('session_active').deleteMany(query) res.json({msg:'SESSIONS_CLOSED'}) +} + +module.exports = { + emergency_shutdown } \ No newline at end of file diff --git a/source/coordinator/controller/title.js b/source/coordinator/controller/title.js index be63b2e..1095f20 100644 --- a/source/coordinator/controller/title.js +++ b/source/coordinator/controller/title.js @@ -1,7 +1,7 @@ const config = require('../config.json') const db_connection = require('../database/connection.js') -module.exports.get_titles = async (req, res, next) => { +const get_titles = async (req, res, next) => { try { const db = await db_connection.get_db() var titles = await db.collection('title').find().skip(parseInt(req.query.start)).limit(parseInt(req.query.count)).toArray() @@ -9,4 +9,8 @@ module.exports.get_titles = async (req, res, next) => { } catch (e) { next(e) } +} + +module.exports = { + get_titles } \ No newline at end of file diff --git a/source/lib/http_client.js b/source/lib/http_client.js index 1b202b9..570a1a5 100644 --- a/source/lib/http_client.js +++ b/source/lib/http_client.js @@ -1,4 +1,5 @@ const http = require('http') +const https = require('https') module.exports = class http_client { constructor(ip, port) { @@ -63,4 +64,18 @@ module.exports.get = async (options, callback_res, callback_err) => { }).on('error', err => { callback_err(err) }) +} + +module.exports.gets = async (options, callback_res, callback_err) => { + https.get(options, function(res) { + var body_chunks = [] + res.on('data', chunk => { + body_chunks.push(chunk) + }).on('end', () => { + var body = Buffer.concat(body_chunks) + callback_res(body) + }) + }).on('error', err => { + callback_err(err) + }) } \ No newline at end of file diff --git a/source/package-lock.json b/source/package-lock.json index e8979a1..762a8ca 100644 --- a/source/package-lock.json +++ b/source/package-lock.json @@ -13,6 +13,7 @@ "experss": "^0.0.1-security", "express": "^4.18.2", "helmet": "^6.0.1", + "https": "^1.0.0", "mongodb": "^4.12.0", "mongoose": "^6.8.0", "node-datetime": "^2.1.2", @@ -1532,6 +1533,11 @@ "node": ">= 0.8" } }, + "node_modules/https": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz", + "integrity": "sha512-4EC57ddXrkaF0x83Oj8sM6SLQHAWXw90Skqu2M4AEWENZ3F02dFJE/GARA8igO79tcgYqGrD7ae4f5L3um2lgg==" + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -3476,6 +3482,11 @@ "toidentifier": "1.0.1" } }, + "https": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz", + "integrity": "sha512-4EC57ddXrkaF0x83Oj8sM6SLQHAWXw90Skqu2M4AEWENZ3F02dFJE/GARA8igO79tcgYqGrD7ae4f5L3um2lgg==" + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/source/package.json b/source/package.json index e050298..83b9913 100644 --- a/source/package.json +++ b/source/package.json @@ -15,6 +15,7 @@ "experss": "^0.0.1-security", "express": "^4.18.2", "helmet": "^6.0.1", + "https": "^1.0.0", "mongodb": "^4.12.0", "mongoose": "^6.8.0", "node-datetime": "^2.1.2", diff --git a/source/session_server/config.json b/source/session_server/config.json index d96da40..bf76719 100644 --- a/source/session_server/config.json +++ b/source/session_server/config.json @@ -1,19 +1,16 @@ { - "ip" : "192.168.1.115", - "port" : 3002, - "external_ip" : "192.168.1.115", - "external_port" : 3002, + "port":3002, + "external_domain":"a1.session.graff.tech", + "coordinator_domain":"a1.coord.graff.tech", + "server_id":"1acd21dc6b70961f10ad134fb6403985", "session_ports":{ - "count": 50, - "app_begin": 47000, - "http_begin": 47500 - }, - "coordinator":{ - "ip":"192.168.1.115", - "port":3001 + "app_begin":47500, + "http_begin":47000 }, + "start_nginx":115, "cors":[ - "http://192.168.1.115:3001" + "a1.coord.graff.tech", + "http://192.168.1.115" ], "webrtc_args_static":{ "cirrus_path":"D:/shared/Builds/Ivaz_Optimized_2/Samples/PixelStreaming/WebServers/SignallingWebServer/cirrus.js" diff --git a/source/session_server/controller/session.js b/source/session_server/controller/session.js index 13ed8bd..3fc48f8 100644 --- a/source/session_server/controller/session.js +++ b/source/session_server/controller/session.js @@ -7,36 +7,36 @@ const http_client = require('../../lib/http_client.js') const app_port_begin = config.session_ports.app_begin const http_port_begin = config.session_ports.http_begin -const ports_count = config.session_ports.count const app_args_static = config.app_args_static const app_args_ip = config.app_args_runtime.ip -const server_ip = config.external_ip -const server_port = config.external_port +const server_ip = '127.0.0.1'//config.ip +const external_domain = config.external_domain const app_args_port = config.app_args_runtime.port +const start_nginx = config.start_nginx +const server_id = config.server_id const webrtc_cirrus_path = config.webrtc_args_static.cirrus_path -const coordinator_port = config.coordinator.port -const coordinator_ip = config.coordinator.ip +const coordinator_domain = config.coordinator_domain // const http_client = require('../lib/http_client.js') // SHALL FIX IT IN FUTURE BECAUSE IT IS NOT THE REST API +const ports_count = 50 var app_port_alloc = new port_alloc(app_port_begin, ports_count) var http_port_alloc = new port_alloc(http_port_begin, ports_count) // close all own sessions on session server if emergency shutdown -http_client.get({ - host: coordinator_ip, - port: coordinator_port, - path: `/session_server/emergency_shutdown?ip=${server_ip}&port=${server_port}` +http_client.gets({ + host: coordinator_domain, + path: `/session_server/emergency_shutdown?id=${server_id}` }, async function(answer) { }, function(err) { //next(Error('session server not working')) }) -module.exports.create_session = async (req, res, next) => { +const create_session = async (req, res, next) => { //create?title=name&next_var=0 try { var app_info @@ -54,6 +54,7 @@ module.exports.create_session = async (req, res, next) => { app_port = app_port_alloc.get() http_port = http_port_alloc.get() } catch(e) { + //console.log('count error') throw new Error(e) } @@ -85,9 +86,9 @@ module.exports.create_session = async (req, res, next) => { else { app_proc.kill('SIGINT') } - http_client.get({ - host: coordinator_ip, - port: coordinator_port, + + http_client.gets({ + host: coordinator_domain, path: `/session/close?session_id=${req.query.session_id}` }, async function(answer) { }, function(err) { @@ -95,14 +96,22 @@ module.exports.create_session = async (req, res, next) => { }) }) - res.json({msg:'SESSION_CREATED', link:`http://${server_ip+':'+http_port}`}) + setTimeout(() => { + res.json({msg:'SESSION_CREATED', link:`https://${external_domain}/${start_nginx}${http_port}/`}) + }, 1000) } catch (e) { next(e) } } -module.exports.connect_session = async (req, res, next) => { +const connect_session = async (req, res, next) => { } -module.exports.close_session = async (req, res, next) => { +const close_session = async (req, res, next) => { +} + +module.exports = { + create_session, + connect_session, + close_session } \ No newline at end of file diff --git a/source/session_server/titles.json b/source/session_server/titles.json index bf641be..82b5cf4 100644 --- a/source/session_server/titles.json +++ b/source/session_server/titles.json @@ -1,10 +1,14 @@ [ { - "title":"mosharov", + "title":"fortis", "path":"D:/shared/Builds/Fortis_UnStable_64/WindowsNoEditor/FORTIS_Taktika.exe" }, { "title":"ivazowsky", "path":"D:/shared/Builds/Ivaz_Optimized_2/Ivazowsky.exe" + }, + { + "title":"mosharov", + "path":"D:/shared/Builds/mosharForStreaming/Masharovdev.exe" } ] \ No newline at end of file diff --git a/source/utils/generate_nginx_proxy_pass.js b/source/utils/generate_nginx_proxy_pass.js new file mode 100644 index 0000000..5a1b256 --- /dev/null +++ b/source/utils/generate_nginx_proxy_pass.js @@ -0,0 +1,21 @@ +var nginx_start = 115 +var start_port = 47000 + +for (let i = 0; i < 20; i++) { + + // console.log(` location /${nginx_start}${start_port+i} {`) + // console.log(` return 301 http://192.168.1.${nginx_start}:${start_port+i}/;`) + // console.log(` }`) + console.log(` location /${nginx_start}${start_port+i}/ {`) + console.log(` proxy_pass http://192.168.1.${nginx_start}:${start_port+i}/;`) + console.log(' proxy_set_header Upgrade $http_upgrade;') + console.log(` proxy_set_header Connection 'upgrade';`) + console.log(' }') +} +// location = /ombi { +// return 301 https://FQDN/ombi/; +// } + +// setTimeout(() => { +// res.end('Hello World!'); +// }, 10000); \ No newline at end of file diff --git a/source/utils/generate_session_server_id.js b/source/utils/generate_session_server_id.js new file mode 100644 index 0000000..c168a0b --- /dev/null +++ b/source/utils/generate_session_server_id.js @@ -0,0 +1,3 @@ +let host = '5.141.82.116' +let port = '3002' +console.log(require('crypto').createHash('md5').update(`${host}${port}`).digest('hex')) \ No newline at end of file