add supporer key program

This commit is contained in:
miloschwartz 2025-03-20 22:16:02 -04:00
parent 1c2ba4076a
commit cdc415079c
No known key found for this signature in database
17 changed files with 908 additions and 74 deletions

View file

@ -10,6 +10,8 @@ import {
} from "@server/lib/consts";
import { passwordSchema } from "@server/auth/passwordSchema";
import stoi from "./stoi";
import db from "@server/db";
import { SupporterKey, supporterKey } from "@server/db/schema";
const portSchema = z.number().positive().gt(0).lte(65535);
@ -155,6 +157,10 @@ const configSchema = z.object({
export class Config {
private rawConfig!: z.infer<typeof configSchema>;
supporterData: SupporterKey | null = null;
supporterHiddenUntil: number | null = null;
constructor() {
this.loadConfig();
}
@ -183,7 +189,9 @@ export class Config {
}
if (process.env.APP_BASE_DOMAIN) {
console.log("You're using deprecated environment variables. Transition to the configuration file. https://docs.fossorial.io/");
console.log(
"You're using deprecated environment variables. Transition to the configuration file. https://docs.fossorial.io/"
);
}
if (!environment) {
@ -235,6 +243,17 @@ export class Config {
: "false";
process.env.DASHBOARD_URL = parsedConfig.data.app.dashboard_url;
try {
this.checkSupporterKey();
} catch (error) {
console.error("Error checking supporter key:", error);
}
if (this.supporterData) {
process.env.SUPPORTER_DATA = JSON.stringify(this.supporterData);
console.log("Thank you for being a supporter of Pangolin!");
}
this.rawConfig = parsedConfig.data;
}
@ -251,6 +270,86 @@ export class Config {
public getDomain(domainId: string) {
return this.rawConfig.domains[domainId];
}
public hideSupporterKey(days: number = 7) {
const now = new Date().getTime();
if (this.supporterHiddenUntil && now < this.supporterHiddenUntil) {
return;
}
this.supporterHiddenUntil = now + 1000 * 60 * 60 * 24 * days;
}
public isSupporterKeyHidden() {
const now = new Date().getTime();
if (this.supporterHiddenUntil && now < this.supporterHiddenUntil) {
return true;
}
return false;
}
public async checkSupporterKey() {
const [key] = await db.select().from(supporterKey).limit(1);
if (!key) {
return;
}
const { key: licenseKey, githubUsername } = key;
const response = await fetch(
"https://api.dev.fossorial.io/api/v1/license/validate",
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
licenseKey,
githubUsername
})
}
);
if (!response.ok) {
this.supporterData = key;
return;
}
const data = await response.json();
if (!data.data.valid) {
this.supporterData = {
...key,
valid: false
};
return;
}
this.supporterData = {
...key,
valid: true
};
// update the supporter key in the database
await db.transaction(async (trx) => {
await trx.delete(supporterKey);
await trx.insert(supporterKey).values({
githubUsername,
key: licenseKey,
tier: data.data.tier || null,
phrase: data.data.cutePhrase || null,
valid: true
});
});
}
public getSupporterData() {
return this.supporterData;
}
}
export const config = new Config();