Skip to content

Commit

Permalink
impr: keep a list of ips that accessed the account
Browse files Browse the repository at this point in the history
part of #4490
  • Loading branch information
Miodec committed Nov 30, 2023
1 parent 25b1ecb commit 6c3cfe0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/src/api/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions backend/src/dal/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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 } });
}
3 changes: 3 additions & 0 deletions backend/src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ declare namespace MonkeyTypes {
rewards: AllRewards[];
}

type UserIpHistory = string[];

interface User {
autoBanTimestamps?: number[];
addedAt: number;
Expand Down Expand Up @@ -98,6 +100,7 @@ declare namespace MonkeyTypes {
lastReultHashes?: string[];
lbOptOut?: boolean;
premium?: PremiumInfo;
ips?: UserIpHistory;
}

interface UserStreak {
Expand Down

0 comments on commit 6c3cfe0

Please sign in to comment.