Skip to content

Commit

Permalink
track traffic for api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
InventivetalentDev committed Jun 8, 2024
1 parent dd80aae commit 4e40007
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
17 changes: 14 additions & 3 deletions src/database/schemas/Traffic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const TrafficSchema: Schema<ITrafficDocument, ITrafficModel> = new Schema
type: String,
index: true
},
key: {
type: String,
index: true
},
lastRequest: {
type: Date,
expires: 3600
Expand All @@ -18,11 +22,18 @@ export const TrafficSchema: Schema<ITrafficDocument, ITrafficModel> = new Schema
});

TrafficSchema.statics.findForIp = function (this: ITrafficModel, ip: string): Promise<LeanDocumentOrArray<ITrafficDocument | null>> {
return this.findOne({ ip: ip }).lean().exec();
return this.findOne({ip: ip}, null, {
sort: {lastRequest: -1}
}).lean().exec();
};

TrafficSchema.statics.updateRequestTime = function (this: ITrafficModel, ip: string, time: Date = new Date()): Promise<any> {
return this.updateOne({ip: ip},{lastRequest: time}, {upsert: true, maxTimeMS: 5000}).exec();
TrafficSchema.statics.updateRequestTime = function (this: ITrafficModel, ip: string, key: string | null, time: Date = new Date()): Promise<any> {
return this.updateOne({
ip: ip,
key: key
}, {
lastRequest: time
}, {upsert: true, maxTimeMS: 5000}).exec();
};

export const Traffic: ITrafficModel = model<ITrafficDocument, ITrafficModel>("Traffic", TrafficSchema);
15 changes: 11 additions & 4 deletions src/generator/Caching.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { MOJANG_API, MOJANG_SESSION, Requests } from "./Requests";
import { AsyncLoadingCache, Caches, CacheStats, ICacheBase, LoadingCache, SimpleCache } from "@inventivetalent/loading-cache";
import {
AsyncLoadingCache,
Caches,
CacheStats,
ICacheBase,
LoadingCache,
SimpleCache
} from "@inventivetalent/loading-cache";
import * as Sentry from "@sentry/node";
import { Severity } from "@sentry/node";
import { Maybe, sha512, stripUuid } from "../util";
Expand Down Expand Up @@ -285,9 +292,9 @@ export class Caching {
return this.trafficByIpCache.get(ip);
}

public static async updateTrafficRequestTime(ip: string, time: Date): Promise<any> {
public static async updateTrafficRequestTime(ip: string, key: string | null, time: Date): Promise<any> {
this.trafficByIpCache.put(ip, time);
return await Traffic.updateRequestTime(ip, time);
return await Traffic.updateRequestTime(ip, key, time);
}

public static getSkinById(id: number): Promise<Maybe<ISkinDocument>> {
Expand Down Expand Up @@ -316,7 +323,7 @@ export class Caching {
return this.pendingDiscordLinkByStateCache.getIfPresent(state) as T;
}

public static storePendingMicrosoftLink(state: string, link: MojangAccountLink): void{
public static storePendingMicrosoftLink(state: string, link: MojangAccountLink): void {
this.pendingMicrosoftLinkByStateCache.put(state, link);
}

Expand Down
8 changes: 4 additions & 4 deletions src/routes/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const register = (app: Application) => {
console.log(debug(`${ options.breadcrumb } URL: ${ url }`));

if (!options.checkOnly || !client.apiKey) {
await updateTraffic(req);
await updateTraffic(client);
}

const skin = await Generator.generateFromUrlAndSave(url, options, client);
Expand Down Expand Up @@ -109,7 +109,7 @@ export const register = (app: Application) => {
console.log(debug(`${ options.breadcrumb } FILE: "${ file.name }" ${ file.md5 }`))

if (!options.checkOnly || !client.apiKey) {
await updateTraffic(req);
await updateTraffic(client);
}

const skin = await Generator.generateFromUploadAndSave(file, options, client);
Expand All @@ -133,7 +133,7 @@ export const register = (app: Application) => {
if (!requestAllowed) {
return;
}
await updateTraffic(req);
await updateTraffic(client);
Sentry.setTag("generate_type", GenerateType.USER);

const uuids = longAndShortUuid(uuidStr);
Expand Down Expand Up @@ -173,7 +173,7 @@ export const register = (app: Application) => {
if (!requestAllowed) {
return;
}
await updateTraffic(req);
await updateTraffic(client);
Sentry.setTag("generate_type", GenerateType.USER);

const uuids = longAndShortUuid(uuidStr);
Expand Down
2 changes: 1 addition & 1 deletion src/typings/db/ITrafficDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export interface ITrafficDocument extends Document {
export interface ITrafficModel extends Model<ITrafficDocument> {
findForIp(ip: string): Promise<Maybe<ITrafficDocument>>;

updateRequestTime(ip: string, time?: Date): Promise<any>;
updateRequestTime(ip: string, key: string | null, time?: Date): Promise<any>;
}
7 changes: 4 additions & 3 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export async function updateTraffic(req: Request | ClientInfo, time: Date = new
op: "generate_updateTraffic"
})
const ip = req.ip ?? getIp(req as Request);
await Caching.updateTrafficRequestTime(ip, time);
const key = 'apiKeyId' in req ? req.apiKeyId : null;
await Caching.updateTrafficRequestTime(ip, key || null, time);
span?.finish();
}

Expand Down Expand Up @@ -316,7 +317,7 @@ export function timeout<U>(promise: Promise<U>, t: number, tag?: Maybe<string>):
promise
.then(v => {
if (timedOut) {
console.log(`timed out succeeded after ${ Date.now() - start }ms: ${tag||''}`)
console.log(`timed out succeeded after ${ Date.now() - start }ms: ${ tag || '' }`)
return;
}
clearTimeout(timer);
Expand All @@ -329,7 +330,7 @@ export function timeout<U>(promise: Promise<U>, t: number, tag?: Maybe<string>):
}
});
if (timedOut) {
console.log(`timed out errored after ${ Date.now() - start }ms: ${tag||''}`)
console.log(`timed out errored after ${ Date.now() - start }ms: ${ tag || '' }`)
return;
}
clearTimeout(timer);
Expand Down

0 comments on commit 4e40007

Please sign in to comment.