From 9c8b341ecc57307b777894bee608fe9c7f640a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 29 Sep 2020 19:13:41 +0200 Subject: [PATCH 1/4] [+] Update client IP route bugfix & Many logs --- fivem_script/tokovoip_script/nui/script.js | 4 +++- fivem_script/tokovoip_script/src/c_TokoVoip.lua | 1 + ws_server/index.js | 8 ++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/fivem_script/tokovoip_script/nui/script.js b/fivem_script/tokovoip_script/nui/script.js index e5c6258..b7f7269 100644 --- a/fivem_script/tokovoip_script/nui/script.js +++ b/fivem_script/tokovoip_script/nui/script.js @@ -85,6 +85,7 @@ async function init(address, serverId) { await updateClientIP(endpoint); console.log('TokoVOIP: attempt new connection'); websocket = new WebSocket(`ws://${endpoint}/socket.io/?EIO=3&transport=websocket&from=fivem&serverId=${serverId}`); + console.log('[TokoVOIP] : Websocket object built', websocket) websocket.onopen = () => { updateWsState('FiveM', OK) @@ -122,6 +123,7 @@ async function init(address, serverId) { }; websocket.onerror = (evt) => { + console.log(`[TokoVOIP] : Error on websocket :`, evt) console.error('TokoVOIP: error - ' + evt.data); }; @@ -180,7 +182,7 @@ function receivedClientCall (event) { // Start with a status OK by default, and change the status if issues are encountered voipStatus = OK; - + console.log('[TokoVOIP] Received client call with payload ', payload) if (eventName == 'updateConfig') { updateConfig(payload); diff --git a/fivem_script/tokovoip_script/src/c_TokoVoip.lua b/fivem_script/tokovoip_script/src/c_TokoVoip.lua index 1ad155f..19399e9 100644 --- a/fivem_script/tokovoip_script/src/c_TokoVoip.lua +++ b/fivem_script/tokovoip_script/src/c_TokoVoip.lua @@ -101,6 +101,7 @@ end function TokoVoip.initialize(self) self:updateConfig(); self:updatePlugin("initializeSocket", self.wsServer); + print('[TokoVOIP] Client : NUI event sent to initialize websocket') Citizen.CreateThread(function() while (true) do Citizen.Wait(5); diff --git a/ws_server/index.js b/ws_server/index.js index 2a1f575..b1843d3 100644 --- a/ws_server/index.js +++ b/ws_server/index.js @@ -98,12 +98,12 @@ app.get('/playerbyip', (req, res) => { return res.status(204).send(); }); -app.get('getmyip', (req, res) => { +app.get('/getmyip', (req, res) => { const ip = (lodash.get(req, `headers.['x-forwarded-for']`) || lodash.get(req, `headers.['x-real-ip']`) || lodash.get(req, 'connection.remoteAddress')).replace('::ffff:', ''); res.send(ip); }); -http.on('upgrade', (req, socket) => { +http.on('/upgrade', (req, socket) => { if (!req._query || !req._query.from) return socket.destroy(); if (req._query.from === 'ts3' && !req._query.uuid) return socket.destroy(); }); @@ -112,6 +112,7 @@ io.on('connection', async socket => { socket.from = socket.request._query.from; socket.clientIp = socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress.replace('::ffff:', ''); socket.safeIp = Buffer.from(socket.clientIp).toString('base64'); + console.log(`[TokoVOIP] : IP ${socket.safeIp} successfully joined websocket`); if (socket.clientIp.includes('::1') || socket.clientIp.includes('127.0.0.1') || socket.clientIp.includes('192.168.')) socket.clientIp = hostIP; socket.fivemServerId = socket.request._query.serverId; @@ -125,6 +126,7 @@ io.on('connection', async socket => { socket.uuid = socket.request._query.uuid; if (!handshakes[socket.clientIp]) { + console.log(`[TokoVOIP] : Handshake not found for IP ${socket.clientIp}`) socket.emit('disconnectMessage', 'handshakeNotFound'); socket.disconnect(true); return; @@ -143,6 +145,7 @@ io.on('connection', async socket => { client.ts3.linkedAt = (new Date()).toISOString(); delete handshakes[socket.clientIp]; + console.log(`[TokoVOIP] : Handshake successfull for IP ${socket.clientIp}`) log('log', chalk`{${socket.from === 'ts3' ? 'cyan' : 'yellow'} ${socket.from}} | Handshake {green successful} - ${socket.safeIp}`); socket.on('setTS3Data', data => setTS3Data(socket, data)); @@ -185,6 +188,7 @@ async function registerHandshake(socket) { }, }); } catch (e) { + console.log(`[TokoVOIP] : Cannot register handshake for IP ${socket.clientIp} (Axios error on master)`) console.error(e); throw e; } From 7f21694d1b2b5c0ec95c15a37caa2b3b7540ab49 Mon Sep 17 00:00:00 2001 From: LS Confidential Date: Tue, 29 Sep 2020 13:49:32 -0700 Subject: [PATCH 2/4] [+] Check if nui has been loaded --- fivem_script/tokovoip_script/nui/script.js | 2 +- fivem_script/tokovoip_script/src/c_main.lua | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fivem_script/tokovoip_script/nui/script.js b/fivem_script/tokovoip_script/nui/script.js index b7f7269..49046aa 100644 --- a/fivem_script/tokovoip_script/nui/script.js +++ b/fivem_script/tokovoip_script/nui/script.js @@ -188,8 +188,8 @@ function receivedClientCall (event) { } else if (voip) { if (eventName == 'initializeSocket') { + $.post(`http://${scriptName}/nuiLoaded`) init(payload); - } else if (eventName == 'updateTokovoipInfo') { if (connected) updateTokovoipInfo(payload, 1); diff --git a/fivem_script/tokovoip_script/src/c_main.lua b/fivem_script/tokovoip_script/src/c_main.lua index 550d423..906501c 100644 --- a/fivem_script/tokovoip_script/src/c_main.lua +++ b/fivem_script/tokovoip_script/src/c_main.lua @@ -22,6 +22,7 @@ local animStates = {} local displayingPluginScreen = false; local HeadBone = 0x796e; local radioVolume = 0; +local nuiLoaded = false -------------------------------------------------------------------------------- -- Plugin functions @@ -256,7 +257,10 @@ AddEventHandler("initializeVoip", function() print("TokoVoip: FiveM Server ID is " .. voip.fivemServerId); voip.processFunction = clientProcessing; -- Link the processing function that will be looped - voip:initialize(); -- Initialize the websocket and controls + while not nuiLoaded do + voip:initialize(); -- Initialize the websocket and controls + Citizen.Wait(5000) + end voip:loop(); -- Start TokoVoip's loop end); @@ -406,6 +410,12 @@ AddEventHandler("updateVoipTargetPed", function(newTargetPed, useLocal) useLocalPed = useLocal end) +-- Used to prevent bad nui loading +RegisterNUICallback("nuiLoaded", function(data, cb) + nuiLoaded = true + cb("ok") +end) + -- Make exports available on first tick exports("addPlayerToRadio", addPlayerToRadio); exports("removePlayerFromRadio", removePlayerFromRadio); From f45eda8e468821f65a2e851174726d76503b9e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 29 Sep 2020 23:39:21 +0200 Subject: [PATCH 3/4] [~] Code cleaning --- fivem_script/tokovoip_script/nui/script.js | 3 --- ws_server/index.js | 4 ---- 2 files changed, 7 deletions(-) diff --git a/fivem_script/tokovoip_script/nui/script.js b/fivem_script/tokovoip_script/nui/script.js index 49046aa..d06c760 100644 --- a/fivem_script/tokovoip_script/nui/script.js +++ b/fivem_script/tokovoip_script/nui/script.js @@ -85,7 +85,6 @@ async function init(address, serverId) { await updateClientIP(endpoint); console.log('TokoVOIP: attempt new connection'); websocket = new WebSocket(`ws://${endpoint}/socket.io/?EIO=3&transport=websocket&from=fivem&serverId=${serverId}`); - console.log('[TokoVOIP] : Websocket object built', websocket) websocket.onopen = () => { updateWsState('FiveM', OK) @@ -123,7 +122,6 @@ async function init(address, serverId) { }; websocket.onerror = (evt) => { - console.log(`[TokoVOIP] : Error on websocket :`, evt) console.error('TokoVOIP: error - ' + evt.data); }; @@ -182,7 +180,6 @@ function receivedClientCall (event) { // Start with a status OK by default, and change the status if issues are encountered voipStatus = OK; - console.log('[TokoVOIP] Received client call with payload ', payload) if (eventName == 'updateConfig') { updateConfig(payload); diff --git a/ws_server/index.js b/ws_server/index.js index b1843d3..8e408fd 100644 --- a/ws_server/index.js +++ b/ws_server/index.js @@ -112,7 +112,6 @@ io.on('connection', async socket => { socket.from = socket.request._query.from; socket.clientIp = socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress.replace('::ffff:', ''); socket.safeIp = Buffer.from(socket.clientIp).toString('base64'); - console.log(`[TokoVOIP] : IP ${socket.safeIp} successfully joined websocket`); if (socket.clientIp.includes('::1') || socket.clientIp.includes('127.0.0.1') || socket.clientIp.includes('192.168.')) socket.clientIp = hostIP; socket.fivemServerId = socket.request._query.serverId; @@ -126,7 +125,6 @@ io.on('connection', async socket => { socket.uuid = socket.request._query.uuid; if (!handshakes[socket.clientIp]) { - console.log(`[TokoVOIP] : Handshake not found for IP ${socket.clientIp}`) socket.emit('disconnectMessage', 'handshakeNotFound'); socket.disconnect(true); return; @@ -145,7 +143,6 @@ io.on('connection', async socket => { client.ts3.linkedAt = (new Date()).toISOString(); delete handshakes[socket.clientIp]; - console.log(`[TokoVOIP] : Handshake successfull for IP ${socket.clientIp}`) log('log', chalk`{${socket.from === 'ts3' ? 'cyan' : 'yellow'} ${socket.from}} | Handshake {green successful} - ${socket.safeIp}`); socket.on('setTS3Data', data => setTS3Data(socket, data)); @@ -188,7 +185,6 @@ async function registerHandshake(socket) { }, }); } catch (e) { - console.log(`[TokoVOIP] : Cannot register handshake for IP ${socket.clientIp} (Axios error on master)`) console.error(e); throw e; } From 844b5cb7b03a9ed12c57f5183d18f214eb18740c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 29 Sep 2020 23:43:10 +0200 Subject: [PATCH 4/4] [~] Log cleaning --- fivem_script/tokovoip_script/src/c_TokoVoip.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/fivem_script/tokovoip_script/src/c_TokoVoip.lua b/fivem_script/tokovoip_script/src/c_TokoVoip.lua index 19399e9..1ad155f 100644 --- a/fivem_script/tokovoip_script/src/c_TokoVoip.lua +++ b/fivem_script/tokovoip_script/src/c_TokoVoip.lua @@ -101,7 +101,6 @@ end function TokoVoip.initialize(self) self:updateConfig(); self:updatePlugin("initializeSocket", self.wsServer); - print('[TokoVOIP] Client : NUI event sent to initialize websocket') Citizen.CreateThread(function() while (true) do Citizen.Wait(5);