From 8fcb7580ee13a2baeb6e085078f55bd06ab66f6d Mon Sep 17 00:00:00 2001 From: peyman Date: Sat, 17 Jul 2021 14:20:05 +0430 Subject: [PATCH 1/9] http presense with user ips --- src/api/http-api.ts | 23 ++++++++++++++++++----- src/channels/private-channel.ts | 7 +++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/api/http-api.ts b/src/api/http-api.ts index 58477394..dc61d0f0 100644 --- a/src/api/http-api.ts +++ b/src/api/http-api.ts @@ -158,11 +158,24 @@ export class HttpApi { } this.channel.presence.getMembers(channelName).then(members => { - let users = []; - - _.uniqBy(members, 'user_id').forEach((member: any) => { - users.push({ id: member.user_id, user_info: member.user_info }); - }); + const membersDic = members.reduce((dic, member) => { + if (!dic[member.user_id]) { + dic[member.user_id] = { + id: member.user_id, + user_info: member.user_info, + ips: [member.ip], + sessions: 1, + } + } else { + const user_data = dic[member.user_id]; + if (!user_data.ips.includes(member.ip)) { + user_data.ips.push(member.ip) + } + user_data.sessions += 1 + } + }, {}) + + const users = Object.keys(membersDic).map(key => membersDic[key]) res.json({ users: users }); }, error => Log.error(error)); diff --git a/src/channels/private-channel.ts b/src/channels/private-channel.ts index 35c030cc..e22deb57 100644 --- a/src/channels/private-channel.ts +++ b/src/channels/private-channel.ts @@ -110,6 +110,11 @@ export class PrivateChannel { body = response.body } + if (!body.channel_data) { + body.channel_data = {} + } + body.channel_data.ip = socket.request.headers["x-forwarded-for"] || socket.conn.remoteAddress; + resolve(body); } }); @@ -122,6 +127,8 @@ export class PrivateChannel { protected prepareHeaders(socket: any, options: any): any { options.headers['Cookie'] = options.headers['Cookie'] || socket.request.headers.cookie; options.headers['X-Requested-With'] = 'XMLHttpRequest'; + options.headers["User-Agent"] = socket.request.headers["user-agent"]; + options.headers["X-Forwarded-For"] = socket.request.headers["x-forwarded-for"] || socket.conn.remoteAddress; return options.headers; } From 193d1966cea4ae49a1bfb7cb22e0bf7c130c2c49 Mon Sep 17 00:00:00 2001 From: peyman Date: Mon, 19 Jul 2021 01:17:39 +0430 Subject: [PATCH 2/9] testing handshake headers --- src/channels/private-channel.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/channels/private-channel.ts b/src/channels/private-channel.ts index e22deb57..5baa8463 100644 --- a/src/channels/private-channel.ts +++ b/src/channels/private-channel.ts @@ -113,7 +113,9 @@ export class PrivateChannel { if (!body.channel_data) { body.channel_data = {} } - body.channel_data.ip = socket.request.headers["x-forwarded-for"] || socket.conn.remoteAddress; + + Log.info('Handshake: ' + JSON.stringify(socket.request.headers)) + body.channel_data.ip = socket.request.headers["X_FORWARDED_FOR"] || socket.conn.remoteAddress; resolve(body); } From 269454b46f94f72b1f414025022329690fe24f63 Mon Sep 17 00:00:00 2001 From: peyman Date: Mon, 19 Jul 2021 01:18:01 +0430 Subject: [PATCH 3/9] testing handshake headers --- src/channels/private-channel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/channels/private-channel.ts b/src/channels/private-channel.ts index 5baa8463..2de95640 100644 --- a/src/channels/private-channel.ts +++ b/src/channels/private-channel.ts @@ -115,7 +115,7 @@ export class PrivateChannel { } Log.info('Handshake: ' + JSON.stringify(socket.request.headers)) - body.channel_data.ip = socket.request.headers["X_FORWARDED_FOR"] || socket.conn.remoteAddress; + body.channel_data.ip = socket.request.headers["X_FORWARDED_FOR"] || socket.request.headers["X-FORWARDED-FOR"] || socket.conn.remoteAddress; resolve(body); } From 672dc6adca4b1446ba64a934c74180c00a2db350 Mon Sep 17 00:00:00 2001 From: peyman Date: Mon, 19 Jul 2021 04:06:10 +0430 Subject: [PATCH 4/9] fixed bug in reduce --- src/api/http-api.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/http-api.ts b/src/api/http-api.ts index dc61d0f0..f26a48d2 100644 --- a/src/api/http-api.ts +++ b/src/api/http-api.ts @@ -173,6 +173,7 @@ export class HttpApi { } user_data.sessions += 1 } + return dic }, {}) const users = Object.keys(membersDic).map(key => membersDic[key]) From 686a1a149d4add8ddea56394fe12c93b1574c974 Mon Sep 17 00:00:00 2001 From: peyman Date: Mon, 19 Jul 2021 04:13:47 +0430 Subject: [PATCH 5/9] fixed ip for cloudflare --- src/channels/private-channel.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/channels/private-channel.ts b/src/channels/private-channel.ts index 2de95640..d91fb32e 100644 --- a/src/channels/private-channel.ts +++ b/src/channels/private-channel.ts @@ -114,8 +114,7 @@ export class PrivateChannel { body.channel_data = {} } - Log.info('Handshake: ' + JSON.stringify(socket.request.headers)) - body.channel_data.ip = socket.request.headers["X_FORWARDED_FOR"] || socket.request.headers["X-FORWARDED-FOR"] || socket.conn.remoteAddress; + body.channel_data.ip = socket.request.headers["cf-connecting-ip"] || socket.request.headers["x-fORWARDED-fOR"] || socket.conn.remoteAddress; resolve(body); } From 049a4d5e9d3d9ef2bc6f40e0c94175643bc2f249 Mon Sep 17 00:00:00 2001 From: peyman Date: Mon, 19 Jul 2021 04:15:11 +0430 Subject: [PATCH 6/9] fixed ip for cloudflare --- src/channels/private-channel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/channels/private-channel.ts b/src/channels/private-channel.ts index d91fb32e..deb44dd6 100644 --- a/src/channels/private-channel.ts +++ b/src/channels/private-channel.ts @@ -114,7 +114,7 @@ export class PrivateChannel { body.channel_data = {} } - body.channel_data.ip = socket.request.headers["cf-connecting-ip"] || socket.request.headers["x-fORWARDED-fOR"] || socket.conn.remoteAddress; + body.channel_data.ip = socket.request.headers["cf-connecting-ip"] || socket.request.headers["x-forwarded-for"] || socket.conn.remoteAddress; resolve(body); } From 9bc0e61f5756360289df4d0d402f13c7d8d6e758 Mon Sep 17 00:00:00 2001 From: Pem Date: Wed, 12 Apr 2023 13:45:22 +0330 Subject: [PATCH 7/9] added log leveling for Log class --- src/echo-server.ts | 4 +++- src/log.ts | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/echo-server.ts b/src/echo-server.ts index 069335c2..45922b6e 100644 --- a/src/echo-server.ts +++ b/src/echo-server.ts @@ -2,7 +2,7 @@ import { HttpSubscriber, RedisSubscriber, Subscriber } from './subscribers'; import { Channel } from './channels'; import { Server } from './server'; import { HttpApi } from './api'; -import { Log } from './log'; +import { Log, LogLevel } from './log'; import * as fs from 'fs'; const packageFile = require('../package.json'); const { constants } = require('crypto'); @@ -26,6 +26,7 @@ export class EchoServer { } }, devMode: false, + logLevel: LogLevel.Title, host: null, port: 6001, protocol: "http", @@ -100,6 +101,7 @@ export class EchoServer { */ init(io: any): Promise { return new Promise((resolve, reject) => { + Log.setLogLevel(this.options.logLevel); this.channel = new Channel(io, this.options); this.subscribers = []; diff --git a/src/log.ts b/src/log.ts index 4d49fe9b..083f5a6b 100644 --- a/src/log.ts +++ b/src/log.ts @@ -15,7 +15,26 @@ colors.setTheme({ h2: 'yellow' }); +export const LogLevel = { + Info: 1, + Subtitle: 2, + Title: 3, + Success: 4, + Warning: 5, + Error: 6 +} + +var globalMinLogLevel = LogLevel.Title + export class Log { + /** + * + * @param {int} level + */ + static setLogLevel(level): void { + globalMinLogLevel = level + } + /** * Console log heading 1. * @@ -23,7 +42,9 @@ export class Log { * @return {void} */ static title(message: any): void { - console.log(colors.bold(message)); + if (globalMinLogLevel <= LogLevel.Title) { + console.log(colors.bold(message)); + } } /** @@ -33,7 +54,9 @@ export class Log { * @return {void} */ static subtitle(message: any): void { - console.log(colors.h2.bold(message)); + if (globalMinLogLevel <= LogLevel.Subtitle) { + console.log(colors.h2.bold(message)); + } } /** @@ -43,7 +66,9 @@ export class Log { * @return {void} */ static info(message: any): void { - console.log(colors.info(message)); + if (globalMinLogLevel <= LogLevel.Info) { + console.log(colors.info(message)); + } } /** @@ -53,7 +78,9 @@ export class Log { * @return {void} */ static success(message: any): void { - console.log(colors.green('\u2714 '), message); + if (globalMinLogLevel <= LogLevel.Success) { + console.log(colors.green('\u2714 '), message); + } } /** @@ -65,7 +92,9 @@ export class Log { * @return {void} */ static error(message: any): void { - console.log(colors.error(message)); + if (globalMinLogLevel <= LogLevel.Error) { + console.log(colors.error(message)); + } } /** @@ -75,6 +104,8 @@ export class Log { * @return {void} */ static warning(message: any): void { - console.log(colors.warn('\u26A0 ' + message)); + if (globalMinLogLevel <= LogLevel.Warning) { + console.log(colors.warn('\u26A0 ' + message)); + } } } From cf7295ba742f42e7ab44c374322e2ad91ddeb7ea Mon Sep 17 00:00:00 2001 From: Pem Date: Wed, 12 Apr 2023 14:11:38 +0330 Subject: [PATCH 8/9] readme changes for logLevel --- README.md | 2 ++ src/echo-server.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 757a0b51..246a2f5d 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ Edit the default configuration of the server by adding options to your **laravel | `database` | `redis` | Database used to store data that should persist, like presence channel members. Options are currently `redis` and `sqlite` | | `databaseConfig` | `{}` | Configurations for the different database drivers [Example](#database) | | `devMode` | `false` | Adds additional logging for development purposes | +| `logLevel` | `1` | Log level, lower means more logs, enter 6 for errors only, 5 for warnings and errors | | `host` | `null` | The host of the socket.io server ex.`app.dev`. `null` will accept connections on any IP-address | | `port` | `6001` | The port that the socket.io server should run on | | `protocol` | `http` | Must be either `http` or `https` | @@ -102,6 +103,7 @@ file, the following options can be overridden: - `host`: `LARAVEL_ECHO_SERVER_HOST` - `port`: `LARAVEL_ECHO_SERVER_PORT` - `devMode`: `LARAVEL_ECHO_SERVER_DEBUG` +- `devMlogLevelode`: `LARAVEL_ECHO_SERVER_LOGLEVEL` - `databaseConfig.redis.host`: `LARAVEL_ECHO_SERVER_REDIS_HOST` - `databaseConfig.redis.port`: `LARAVEL_ECHO_SERVER_REDIS_PORT` - `databaseConfig.redis.password`: `LARAVEL_ECHO_SERVER_REDIS_PASSWORD` diff --git a/src/echo-server.ts b/src/echo-server.ts index 45922b6e..82ab0b6c 100644 --- a/src/echo-server.ts +++ b/src/echo-server.ts @@ -26,7 +26,7 @@ export class EchoServer { } }, devMode: false, - logLevel: LogLevel.Title, + logLevel: LogLevel.Info, host: null, port: 6001, protocol: "http", From 3fade9eb9e75b57e9ddf9bea091c2178083457ac Mon Sep 17 00:00:00 2001 From: Pem Date: Fri, 7 Jul 2023 23:09:20 +0330 Subject: [PATCH 9/9] minor error check --- src/channels/presence-channel.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/channels/presence-channel.ts b/src/channels/presence-channel.ts index 7b618720..10204a55 100644 --- a/src/channels/presence-channel.ts +++ b/src/channels/presence-channel.ts @@ -122,16 +122,18 @@ export class PresenceChannel { let member = members.find( (member) => member.socketId == socket.id ); - members = members.filter((m) => m.socketId != member.socketId); + if (member) { + members = members.filter((m) => m.socketId != member.socketId); - this.db.set(channel + ":members", members); - - this.isMember(channel, member).then((is_member) => { - if (!is_member) { - delete member.socketId; - this.onLeave(channel, member); - } - }); + this.db.set(channel + ":members", members); + + this.isMember(channel, member).then((is_member) => { + if (!is_member) { + delete member.socketId; + this.onLeave(channel, member); + } + }); + } }, (error) => Log.error(error) );