From 45711cdfd0f1ea7c5138cf9f48ab86243a9c17a5 Mon Sep 17 00:00:00 2001 From: Adolfo Segura Hall Date: Fri, 23 Aug 2019 02:40:33 +0200 Subject: [PATCH] WIP logs --- README.md | 29 +++++++++++++++---- package.json | 2 +- src/channels/baseAuthChannel.ts | 14 +++++++-- src/echo-server.ts | 50 ++++++++++++++++++++++----------- 4 files changed, 70 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index b6fbaf83..5fd0d4ef 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,30 @@ If Echo is behind a proxy will get the client's Ip from X-forward headers. * true * false +## Tests +```sh +$> npm run build && npm run test + +Laravel Echo Auth Methods + ✓ should Fail Authenticate with Bearer Token Header and should be disconnected (98ms) + ✓ should Authenticate Correctly with Bearer Token Header + ✓ should Fail Authenticate with Query Token + ✓ should Authenticate Correctly with Query Token + ✓ should Fail Authenticate with Cookie Token + ✓ should Authenticate Correctly with Cookie Token + + 6 passing (280ms) + + Laravel Echo Multiple Sockets not Allowed + ✓ Second Connection to Echo Server is not allowed with multiple_sockets = false (3024ms) + + Laravel Echo Multiple Sockets Allowed + ✓ Second Connection to Echo Server is allowed with multiple_sockets = true (3011ms) + + 2 passing (6s) +``` + + # Laravel backend ##### routes/channels.php ```php @@ -218,11 +242,6 @@ template(name="bunyan" type="string" node_modules/bunyan/bin/bunyan /var/log/myapp.log ``` -## Tests -```sh -$> npm run build && npm run test -``` - ### Laravel Echo Auth and Message Flow ![Auth Flow, Message Flow](laravelEcho.png) diff --git a/package.json b/package.json index 25a13ba5..aefc6159 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "build": "tsc", "dev": "tsc -w", "prepublish": "npm run build", - "test": "node_modules/.bin/_mocha --exit dist/test/auth/index.js" + "test": "node_modules/.bin/_mocha --exit dist/test/auth/index.js && node_modules/.bin/_mocha --exit dist/test/multiple_sockets/index.js" }, "dependencies": { "bunyan": "^1.8.12", diff --git a/src/channels/baseAuthChannel.ts b/src/channels/baseAuthChannel.ts index f28a2945..e36a2247 100644 --- a/src/channels/baseAuthChannel.ts +++ b/src/channels/baseAuthChannel.ts @@ -65,6 +65,8 @@ export class BaseAuthChannel { /** * Send a request to the server. + * + * { channel_data: { user_id: 2, user_info: 2 } } */ protected serverRequest(socket: any, options: any): Promise { return new Promise((resolve, reject) => { @@ -94,15 +96,21 @@ export class BaseAuthChannel { }); } else { - Log.info(`[${new Date().toLocaleTimeString()}] - ${socket.id} authenticated for: ${options.form.channel_name}`); - this.log.info(`${socket.id} authenticated for: ${options.form.channel_name}`); - try { body = JSON.parse(response.body); } catch (e) { body = response.body } + const msg = [ + `Auth: user:${body.channel_data.user_id},`, + `socket_id:${socket.id},`, + `channel:${options.form.channel_name}` + ].join(' '); + + Log.info(`[${new Date().toLocaleTimeString()}] - ` + msg); + this.log.info(msg); + resolve(body); } }); diff --git a/src/echo-server.ts b/src/echo-server.ts index 86d1c83a..372c7fcd 100644 --- a/src/echo-server.ts +++ b/src/echo-server.ts @@ -211,21 +211,32 @@ export class EchoServer { this.server.io.on('connection', socket => { this.channel.joinRoot(socket) //Auth Root Channel '/' .then(auth => { - if(auth === false) - return IoUtils.disconnect( - socket, - this.log, - 'Laravel Auth Failed for user id:' + auth.channel_data.user_id, - ); + if(auth === false) { + const msg = `Auth: Failed for user:${auth.channel_data.user_id}, channel:root`; + return IoUtils.disconnect(socket, this.log, msg); + } + + if(! auth.hasOwnProperty('channel_data') ){ + let msg_err = 'Error: on Connect Echo-Server response'; + msg_err += ' do not have channel_data property'; + this.log.error(msg_err); + Log.error(msg_err); + + return IoUtils.disconnect(socket, this.log, msg_err); + } else if(! auth.channel_data.hasOwnProperty('user_id') ){ + let msg_err = 'Error: on Connect Echo-Server response'; + msg_err += ' do not have channel_data.user_id property'; + this.log.error(msg_err); + Log.error(msg_err); + + return IoUtils.disconnect(socket, this.log, msg_err); + } - //console.log(socket.adapter.nsp.sockets) socket.user_id = auth.channel_data.user_id; const ip = IoUtils.getIp(socket, this.options); Log.success(`LOG Success on Server: ${this.server.getServerId()}`); - //IoUtils.setActiveUserOnServer(this.server.getServerId(), - // {user_id: auth.channel_data.user_id, socket_id: socket.id}) - // collection echo_users, {user_id:1, socket_id:2ff, server_id: foo1 }) + this.db.setUserInServer('echo_users', { user_id: auth.channel_data.user_id, socket_id: socket.id, @@ -250,17 +261,24 @@ export class EchoServer { IoUtils.getAllActiveSocketsInThisIoServer(this.server.io) ); - Log.success(`AUTH Success ON NSP / User Id:${socket.user_id} SocketID: ${socket.id} with IP:${ip}`); - this.log.info(`Auth Success ON NSP / User Id:${socket.user_id} with Socket:${socket.id} with IP:${ip}`); + const msg_sucess = [ + `Auth Success ON NSP /`, + `User Id:${socket.user_id}`, + `with Socket:${socket.id} with IP:${ip}` + ].join(' '); + + Log.success(msg_sucess); + this.log.info(msg_sucess); + return this.startSubscribers(socket); }) .catch(e => { - Log.error(`Socket:${socket.id} join Root Auth Error, reason:${e.reason}`); - this.log - .error(`Socket:${socket.id} join Root Auth Error, reason:${e.reason}`); + const msg_error = `Auth: Socket:${socket.id} join Root Auth Error, reason:${e.reason}` + Log.error(msg_error); + this.log.error(msg_error); - IoUtils.disconnect(socket, this.log, e.reason) + IoUtils.disconnect(socket, this.log, msg_error) }) }); }