diff --git a/backend/src/api/controllers/user.ts b/backend/src/api/controllers/user.ts index 946f7455f2fc..ccf60cf6cb4d 100644 --- a/backend/src/api/controllers/user.ts +++ b/backend/src/api/controllers/user.ts @@ -371,6 +371,7 @@ export async function getUser( const agentLog = buildAgentLog(req); Logger.logToDb("user_data_requested", agentLog, uid); + UserDAL.logIpAddress(uid, agentLog.ip, userInfo); let inboxUnreadSize = 0; if (req.ctx.configuration.users.inbox.enabled) { diff --git a/backend/src/dal/user.ts b/backend/src/dal/user.ts index 3309427ff2f5..37c542ff5f85 100644 --- a/backend/src/dal/user.ts +++ b/backend/src/dal/user.ts @@ -1050,3 +1050,21 @@ export async function checkIfUserIsPremium( if (expirationDate === -1) return true; //lifetime return expirationDate > Date.now(); } + +export async function logIpAddress( + uid: string, + ip: string, + userInfoOverride?: MonkeyTypes.User +): Promise { + const user = userInfoOverride ?? (await getUser(uid, "logIpAddress")); + const currentIps = user.ips ?? []; + const ipIndex = currentIps.indexOf(ip); + if (ipIndex !== -1) { + currentIps.splice(ipIndex, 1); + } + currentIps.unshift(ip); + if (currentIps.length > 10) { + currentIps.pop(); + } + await getUsersCollection().updateOne({ uid }, { $set: { ips: currentIps } }); +} diff --git a/backend/src/types/types.d.ts b/backend/src/types/types.d.ts index 02f03883289b..68b14e7187b6 100644 --- a/backend/src/types/types.d.ts +++ b/backend/src/types/types.d.ts @@ -61,6 +61,8 @@ declare namespace MonkeyTypes { rewards: AllRewards[]; } + type UserIpHistory = string[]; + interface User { autoBanTimestamps?: number[]; addedAt: number; @@ -98,6 +100,7 @@ declare namespace MonkeyTypes { lastReultHashes?: string[]; lbOptOut?: boolean; premium?: PremiumInfo; + ips?: UserIpHistory; } interface UserStreak {