This commit is contained in:
Madeline
2022-09-26 22:41:21 +10:00
parent fcc0884e36
commit 99ee7e9400
280 changed files with 6800 additions and 3908 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+";
const alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+";
// binary to string lookup table
const b2s = alphabet.split("");
+7 -6
View File
@@ -2,7 +2,8 @@ import { Snowflake } from "@fosscord/util";
export function random(length = 6) {
// Declare all characters
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// Pick characers randomly
let str = "";
@@ -15,18 +16,18 @@ export function random(length = 6) {
export function snowflakeBasedInvite() {
// Declare all characters
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let base = BigInt(chars.length);
let snowflake = Snowflake.generateWorkerProcess();
// snowflakes hold ~10.75 characters worth of entropy;
// safe to generate a 8-char invite out of them
let str = "";
for (let i=0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
str.concat(chars.charAt(Number(snowflake % base)));
snowflake = snowflake / base;
}
return str.substr(3,8).split("").reverse().join("");
return str.substr(3, 8).split("").reverse().join("");
}
+11 -3
View File
@@ -2,13 +2,21 @@ import { Request } from "express";
import { ntob } from "./Base64";
import { FieldErrors } from "@fosscord/util";
export function checkLength(str: string, min: number, max: number, key: string, req: Request) {
export function checkLength(
str: string,
min: number,
max: number,
key: string,
req: Request,
) {
if (str.length < min || str.length > max) {
throw FieldErrors({
[key]: {
code: "BASE_TYPE_BAD_LENGTH",
message: req.t("common:field.BASE_TYPE_BAD_LENGTH", { length: `${min} - ${max}` })
}
message: req.t("common:field.BASE_TYPE_BAD_LENGTH", {
length: `${min} - ${max}`,
}),
},
});
}
}
+10 -9
View File
@@ -7,8 +7,8 @@ export interface hcaptchaResponse {
hostname: string;
credit: boolean;
"error-codes": string[];
score: number; // enterprise only
score_reason: string[]; // enterprise only
score: number; // enterprise only
score_reason: string[]; // enterprise only
}
export interface recaptchaResponse {
@@ -23,7 +23,7 @@ export interface recaptchaResponse {
const verifyEndpoints = {
hcaptcha: "https://hcaptcha.com/siteverify",
recaptcha: "https://www.google.com/recaptcha/api/siteverify",
}
};
export async function verifyCaptcha(response: string, ip?: string) {
const { security } = Config.get();
@@ -36,11 +36,12 @@ export async function verifyCaptcha(response: string, ip?: string) {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `response=${encodeURIComponent(response)}`
+ `&secret=${encodeURIComponent(secret!)}`
+ `&sitekey=${encodeURIComponent(sitekey!)}`
+ (ip ? `&remoteip=${encodeURIComponent(ip!)}` : ""),
body:
`response=${encodeURIComponent(response)}` +
`&secret=${encodeURIComponent(secret!)}` +
`&sitekey=${encodeURIComponent(sitekey!)}` +
(ip ? `&remoteip=${encodeURIComponent(ip!)}` : ""),
});
return await res.json() as hcaptchaResponse | recaptchaResponse;
}
return (await res.json()) as hcaptchaResponse | recaptchaResponse;
}
+31 -13
View File
@@ -25,27 +25,27 @@ const exampleData = {
name: "",
domain: "",
route: "",
type: "isp"
type: "isp",
},
languages: [
{
name: "",
native: ""
}
native: "",
},
],
currency: {
name: "",
code: "",
symbol: "",
native: "",
plural: ""
plural: "",
},
time_zone: {
name: "",
abbr: "",
offset: "",
is_dst: true,
current_time: ""
current_time: "",
},
threat: {
is_tor: false,
@@ -54,10 +54,10 @@ const exampleData = {
is_known_attacker: false,
is_known_abuser: false,
is_threat: false,
is_bogon: false
is_bogon: false,
},
count: 0,
status: 200
status: 200,
};
//TODO add function that support both ip and domain names
@@ -65,7 +65,9 @@ export async function IPAnalysis(ip: string): Promise<typeof exampleData> {
const { ipdataApiKey } = Config.get().security;
if (!ipdataApiKey) return { ...exampleData, ip };
return (await fetch(`https://api.ipdata.co/${ip}?api-key=${ipdataApiKey}`)).json() as any; // TODO: types
return (
await fetch(`https://api.ipdata.co/${ip}?api-key=${ipdataApiKey}`)
).json() as any; // TODO: types
}
export function isProxy(data: typeof exampleData) {
@@ -77,19 +79,35 @@ export function isProxy(data: typeof exampleData) {
}
export function getIpAdress(req: Request): string {
// @ts-ignore
return req.headers[Config.get().security.forwadedFor] || req.socket.remoteAddress;
return (
// @ts-ignore
req.headers[Config.get().security.forwadedFor] ||
req.socket.remoteAddress
);
}
export function distanceBetweenLocations(loc1: any, loc2: any): number {
return distanceBetweenCoords(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude);
return distanceBetweenCoords(
loc1.latitude,
loc1.longitude,
loc2.latitude,
loc2.longitude,
);
}
//Haversine function
function distanceBetweenCoords(lat1: number, lon1: number, lat2: number, lon2: number) {
function distanceBetweenCoords(
lat1: number,
lon1: number,
lat2: number,
lon2: number,
) {
const p = 0.017453292519943295; // Math.PI / 180
const c = Math.cos;
const a = 0.5 - c((lat2 - lat1) * p) / 2 + (c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2;
const a =
0.5 -
c((lat2 - lat1) * p) / 2 +
(c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2;
return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
}
+13 -7
View File
@@ -18,7 +18,8 @@ const blocklist: string[] = []; // TODO: update ones passwordblocklist is stored
* Returns: 0 > pw > 1
*/
export function checkPassword(password: string): number {
const { minLength, minNumbers, minUpperCase, minSymbols } = Config.get().register.password;
const { minLength, minNumbers, minUpperCase, minSymbols } =
Config.get().register.password;
var strength = 0;
// checks for total password len
@@ -42,19 +43,24 @@ export function checkPassword(password: string): number {
}
// checks if password only consists of numbers or only consists of chars
if (password.length == password.count(reNUMBER) || password.length === password.count(reUPPERCASELETTER)) {
if (
password.length == password.count(reNUMBER) ||
password.length === password.count(reUPPERCASELETTER)
) {
strength = 0;
}
let entropyMap: { [key: string]: number } = {};
for (let i = 0; i < password.length; i++) {
if (entropyMap[password[i]]) entropyMap[password[i]]++;
else entropyMap[password[i]] = 1;
}
let entropies = Object.values(entropyMap);
entropies.map(x => (x / entropyMap.length));
strength += entropies.reduceRight((a: number, x: number) => a - (x * Math.log2(x))) / Math.log2(password.length);
entropies.map((x) => x / entropyMap.length);
strength +=
entropies.reduceRight((a: number, x: number) => a - x * Math.log2(x)) /
Math.log2(password.length);
return strength;
}