Config: Start working on the config refactor

This commit is contained in:
Diego Magdaleno 2021-05-17 19:00:50 -05:00
parent 466c72d65f
commit bb2d3715ea

View File

@ -1,19 +1,4 @@
import { Config, Snowflake } from "@fosscord/server-util"; import Ajv, {JTDSchemaType} from "ajv/dist/jtd"
import crypto from "crypto";
export default {
init() {
return Config.init({ api: DefaultOptions });
},
get(): DefaultOptions {
return Config.getAll().api;
},
set(val: any) {
return Config.setAll({ api: val });
},
getAll: Config.getAll,
setAll: Config.setAll,
};
export interface RateLimitOptions { export interface RateLimitOptions {
count: number; count: number;
@ -64,7 +49,7 @@ export interface DefaultOptions {
login?: RateLimitOptions; login?: RateLimitOptions;
register?: RateLimitOptions; register?: RateLimitOptions;
}; };
channel?: {}; channel?: string;
// TODO: rate limit configuration for all routes // TODO: rate limit configuration for all routes
}; };
}; };
@ -107,85 +92,139 @@ export interface DefaultOptions {
}; };
} }
export const DefaultOptions: DefaultOptions = { const schema: JTDSchemaType<DefaultOptions, {rateLimitOptions: RateLimitOptions}> = {
definitions: {
rateLimitOptions: {
properties: {
count: {type: "int32"},
timespan: {type: "int32"}
}
}
},
properties: {
general: { general: {
instance_id: Snowflake.generate(), properties: {
instance_id: {type: "string"}
}
}, },
permissions: { permissions: {
properties: {
user: { user: {
createGuilds: true, properties: {
}, createGuilds: {type: "boolean"}
}
}
}
}, },
limits: { limits: {
properties: {
user: { user: {
maxGuilds: 100, properties: {
maxUsername: 32, maxGuilds: {type: "int32"},
maxFriends: 1000, maxFriends: {type: "int32"},
maxUsername: {type: "int32"}
}
}, },
guild: { guild: {
maxRoles: 250, properties: {
maxMembers: 250000, maxRoles: {type: "int32"},
maxChannels: 500, maxMembers: {type: "int32"},
maxChannelsInCategory: 50, maxChannels: {type: "int32"},
hideOfflineMember: 1000, maxChannelsInCategory: {type: "int32"},
hideOfflineMember: {type: "int32"}
}
}, },
message: { message: {
characters: 2000, properties: {
ttsCharacters: 200, characters: {type: "int32"},
maxReactions: 20, ttsCharacters: {type: "int32"},
maxAttachmentSize: 8388608, maxReactions: {type: "int32"},
maxBulkDelete: 100, maxAttachmentSize: {type: "int32"},
maxBulkDelete: {type: "int32"}
}
}, },
channel: { channel: {
maxPins: 50, properties: {
maxTopic: 1024, maxPins: {type: "int32"},
maxTopic: {type: "int32"},
},
}, },
rate: { rate: {
properties: {
ip: { ip: {
enabled: true, properties: {
count: 1000, enabled: {type: "boolean"},
timespan: 1000 * 60 * 10, count: {type: "int32"},
timespan: {type: "int32"},
}
}, },
routes: {}, routes: {
optionalProperties: {
auth: {
optionalProperties: {
login: {ref: 'rateLimitOptions'},
register: {ref: 'rateLimitOptions'}
}
}, },
channel: {type: "string"}
}
}
}
}
}
}, },
security: { security: {
jwtSecret: crypto.randomBytes(256).toString("base64"), properties: {
forwadedFor: null, jwtSecret: {type: "string"},
// forwadedFor: "X-Forwarded-For" // nginx/reverse proxy forwadedFor: {type: "string", nullable: true},
// forwadedFor: "CF-Connecting-IP" // cloudflare:
captcha: { captcha: {
enabled: false, properties: {
service: null, enabled: {type: "boolean"},
sitekey: null, service: {enum: ['hcaptcha', 'recaptcha'], nullable: true},
secret: null, sitekey: {type: "string", nullable: true},
}, secret: {type: "string", nullable: true}
}
}
}
}, },
login: { login: {
requireCaptcha: false, properties: {
requireCaptcha: {type: "boolean"}
}
}, },
register: { register: {
properties: {
email: { email: {
required: true, properties: {
allowlist: false, required: {type: "boolean"},
blocklist: true, allowlist: {type: "boolean"},
domains: [], // TODO: efficiently save domain blocklist in database blocklist: {type: "boolean"},
// domains: fs.readFileSync(__dirname + "/blockedEmailDomains.txt", { encoding: "utf8" }).split("\n"), domains: { elements: {
type: "string"
}
}
}
}, },
dateOfBirth: { dateOfBirth: {
required: true, properties: {
minimum: 13, required: {type: "boolean"},
minimum: {type: "int32"}
}
}, },
requireInvite: false, requireCaptcha: {type: "boolean"},
requireCaptcha: true, requireInvite: {type: "boolean"},
allowNewRegistration: true, allowNewRegistration: {type: "boolean"},
allowMultipleAccounts: true, allowMultipleAccounts: {type: "boolean"},
password: { password: {
minLength: 8, properties: {
minNumbers: 2, minLength: {type: "int32"},
minUpperCase: 2, minNumbers: {type: "int32"},
minSymbols: 0, minUpperCase: {type: "int32"},
blockInsecureCommonPasswords: false, minSymbols: {type: "int32"},
}, blockInsecureCommonPasswords: {type: "boolean"}
}, }
}; }
}
}
}
}