Skip to content

Commit

Permalink
WIP logs
Browse files Browse the repository at this point in the history
  • Loading branch information
AdSegura committed Aug 23, 2019
1 parent 7e73a49 commit 45711cd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 11 additions & 3 deletions src/channels/baseAuthChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
return new Promise<any>((resolve, reject) => {
Expand Down Expand Up @@ -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);
}
});
Expand Down
50 changes: 34 additions & 16 deletions src/echo-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
})
});
}
Expand Down

0 comments on commit 45711cd

Please sign in to comment.