Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
ben do code frfr for real
Browse files Browse the repository at this point in the history
  • Loading branch information
auxiliaryfrfr committed Nov 6, 2023
1 parent 79e1cb3 commit 8a21957
Show file tree
Hide file tree
Showing 53 changed files with 498 additions and 8,880 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,5 @@ assets/stylesheets/styles.css

# vscode
.vscode/*

package_lock.json
Binary file added bin/yt-dlp
Binary file not shown.
Binary file added bin/yt-dlp.exe
Binary file not shown.
4 changes: 0 additions & 4 deletions config/config.json

This file was deleted.

6 changes: 0 additions & 6 deletions credits

This file was deleted.

4 changes: 2 additions & 2 deletions functions/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class PGDatabase extends events.EventEmitter {
.on('token', (data) => {
this.pool.connect().then(connection => {
connection.query(
`INSERT INTO public.apitokens (token, associatedfirebaseuid, licenseKey) VALUES ($1, $2, $3)`,
[data.generatedToken, data.firebaseuid, tokenData.key]
`INSERT INTO public.apitokens (token, associatedfirebaseuid, stripesub) VALUES ($1, $2, $3)`,
[data.generatedToken, data.firebaseuid, data.sub]
).catch(console.error)
connection.query(
`INSERT INTO public.apiUsage (apiToken, totalUses) VALUES ($1, $2)`,
Expand Down
185 changes: 27 additions & 158 deletions functions/errorResponse.js
Original file line number Diff line number Diff line change
@@ -1,158 +1,27 @@
const errorRes = (code) => {
if (isNaN(code)) throw new Error('Invalid error code provided')
switch (code) {
case 1:
return {
httpCode: 401,
code: 1,
message: 'No API key provided',
}
case 2:
return {
httpCode: 401,
code: 2,
message: 'Invalid API key provided',
}
case 3:
return {
httpCode: 403,
code: 3,
message: 'The provided API key has been disabled',
}
case 4:
return {
httpCode: 400,
code: 4,
message: 'Missing Query Paramater(s)'
}
case 5:
return {
httpCode: 400,
code: 5,
message: 'Invalid Query Paramater(s)'
}
case 6:
return {
httpCode: 400,
code: 6,
message: 'Missing Header Paramater(s)'
}
case 7:
return {
httpCode: 400,
code: 7,
message: 'Invalid Header Paramater(s)'
}
case 8:
return {
httpCode: 400,
code: 8,
message: 'Missing Body Paramater(s)'
}
case 9:
return {
httpCode: 400,
code: 9,
message: 'Invalid Body Paramater(s)'
}
case 10:
return {
httpCode: 400,
code: 10,
message: 'Unable to encrypt data'
}
case 11:
return {
httpCode: 400,
code: 11,
message: 'Unable to decrypt data'
}
case 12:
return {
httpCode: 403,
code: 12,
message: 'You are not authorized to access this resource'
}
case 13:
return {
httpCode: 404,
code: 13,
message: 'Unable to find requested resource'
}
case 14:
return {
httpCode: 500,
code: 14,
message: 'Unable to delete requested user'
}
case 15:
return {
httpCode: 500,
code: 15,
message: 'Unable to delete requested token'
}
case 16:
return {
httpCode: 403,
code: 16,
message: 'The request was denied due to being blacklisted',
}
case 17:
return {
httpCode: 423,
code: 17,
message: 'This endpoint is currently disabled',
}
case 18:
return {
httpCode: 403,
code: 18,
message: 'The license associated with the provided API key has expired',
}
case 19:
return {
httpCode: 403,
code: 19,
message: 'The license associated with the provided API key has been disabled',
}
case 20:
return {
httpCode: 403,
code: 20,
message: 'The license associated with the provided API key has been revoked',
}
case 21:
return {
httpCode: 501,
code: 21,
message: 'This endpoint has not been implemented yet',
}
case 22:
return {
httpCode: 500,
code: 22,
message: 'Unable to create requested user',
}
case 23:
return {
httpCode: 500,
code: 23,
message: 'Unable to update requested user',
}
case 24:
return {
httpCode: 500,
code: 24,
message: 'Unable to create requested user',
}
case 25:
default:
return {
httpCode: 500,
code: 0,
message: 'Unknown error',
}
}
}

module.exports = errorRes;
module.exports = new Map()
.set(0, { httpCode: 500, code: 0, message: 'Unknown error' })
.set(1, { httpCode: 401, code: 1, message: 'No API key provided', })
.set(2, { httpCode: 401, code: 2, message: 'Invalid API key provided', })
.set(3, { httpCode: 403, code: 3, message: 'The provided API key has been disabled', })
.set(4, { httpCode: 400, code: 4, message: 'Missing Query Paramater(s)' })
.set(5, { httpCode: 400, code: 5, message: 'Invalid Query Paramater(s)' })
.set(6, { httpCode: 400, code: 6, message: 'Missing Header Paramater(s)' })
.set(7, { httpCode: 400, code: 7, message: 'Invalid Header Paramater(s)' })
.set(8, { httpCode: 400, code: 8, message: 'Missing Body Paramater(s)' })
.set(9, { httpCode: 400, code: 9, message: 'Invalid Body Paramater(s)' })
.set(10, { httpCode: 400, code: 10, message: 'Unable to encrypt data' })
.set(11, { httpCode: 400, code: 11, message: 'Unable to decrypt data' })
.set(12, { httpCode: 403, code: 12, message: 'You are not authorized to access this resource' })
.set(13, { httpCode: 404, code: 13, message: 'Unable to find requested resource' })
.set(14, { httpCode: 500, code: 14, message: 'Unable to delete requested user' })
.set(15, { httpCode: 500, code: 15, message: 'Unable to delete requested token' })
.set(16, { httpCode: 403, code: 16, message: 'The request was denied due to being blacklisted', })
.set(17, { httpCode: 423, code: 17, message: 'This endpoint is currently disabled', })
.set(18, { httpCode: 403, code: 18, message: 'The license associated with the provided API key has expired', })
.set(19, { httpCode: 403, code: 19, message: 'The license associated with the provided API key has been disabled', })
.set(20, { httpCode: 403, code: 20, message: 'The license associated with the provided API key has been revoked', })
.set(21, { httpCode: 501, code: 21, message: 'This endpoint has not been implemented yet', })
.set(22, { httpCode: 500, code: 22, message: 'Unable to create requested user', })
.set(23, { httpCode: 500, code: 23, message: 'Unable to update requested user', })
.set(24, { httpCode: 500, code: 24, message: 'Unable to create requested user', })
.get
15 changes: 7 additions & 8 deletions functions/util-fuctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ class Formatter {
}

class ArrayAndJSON {
static combineArrays = (array1, array2) => [...array1, ...array2]
static combineJSON = (json1, json2) => ({ ...json1, ...json2 })
static combineArrays = new Array().concat
static combineJSON = (d1, d2) => Object.fromEntries([...Object.entries(d1), ...Object.entries(d2)])
static arrayToJSON = (array) => {
if (array.every(subdata => Array.isArray(subdata) && subdata.length == 2)) return Object.fromEntries(array)
let json = {};
array.forEach((v, i) => {
json[i] = v;
Expand All @@ -77,12 +78,10 @@ class ArrayAndJSON {
static setToArray = (set) => [...set]
static arrayToMap = (array) => new Map(array)
static mapToArray = (map) => [...map]
static #privateArrayRandomizer = (array) => array.sort(() => Math.random() > 0.5 ? 1 : -1)
static arrayRandomizer = (array, iterations = 25) => {
let newArray = array,
i = 0;
while (i < iterations) newArray = this.#privateArrayRandomizer(newArray); i++;
return newArray;
static _par = (array) => array = array.sort(() => Math.random() > 0.5 ? 1 : -1)
static arrayRandomizer = (a, it = 25) => {
for (let i = 0; i < it; ++i) this._par(a);
return a
}
}

Expand Down
2 changes: 1 addition & 1 deletion functions/utilities.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
aprilFools: () => new Date().getMonth() === 3 && new Date().getDate() === 1
aprilFools: _ => new Date().getMonth() === 3 && new Date().getDate() === 1
};
78 changes: 34 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const app = require('express')();
const Sentry = require('@sentry/node');
const Intigrations = require('@sentry/integrations');
const Profiling = require('@sentry/profiling-node');
const http = require('http');
const axios = require('axios');
const cron = require('node-cron');
Expand All @@ -18,30 +17,29 @@ const RateLimiter = new RateLimiterMemory({
duration: 1,
})

const integrations = [
new Intigrations.ExtraErrorData({ depth: 10 }),
new Intigrations.SessionTiming(),
new Intigrations.Transaction(),
new Intigrations.CaptureConsole({ levels: ['error', 'critical', 'fatal', 'warn'] }),
new Sentry.Integrations.Http({ tracing: true, breadcrumbs: true }),
new Sentry.Integrations.Express({ app }),
new Sentry.Integrations.Postgres(),
]

if (process.env.NODE_ENV !== 'production') integrations.push(new Profiling.ProfilingIntegration())


Sentry.init({
const sentryInit = {
dsn: process.env.SENTRY_DSN,
sampleRate: 1.0,
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
serverName: require('os').hostname(),
integrations,
integrations: [
new Intigrations.ExtraErrorData({ depth: 10 }),
new Intigrations.SessionTiming(),
new Intigrations.Transaction(),
new Intigrations.CaptureConsole({ levels: ['error', 'critical', 'fatal', 'warn'] }),
new Sentry.Integrations.Http({ tracing: true, breadcrumbs: true }),
new Sentry.Integrations.Express({ app }),
new Sentry.Integrations.Postgres(),
],
environment: process.env.NODE_ENV || 'development',
release: require(`./package.json`).version,
sendDefaultPii: true
});
}

// if (process.env.NODE_ENV !== 'production') sentryInit.integrations.push(new Profiling.ProfilingIntegration())

Sentry.init(sentryInit);

//- Middleware
const IPM = require('./middleware/IP'); //? IP Middleware
Expand All @@ -52,8 +50,16 @@ const RL = require('./middleware/routeLogger'); //? Route Logger
const Headers = require('./middleware/headers'); //? Header Setter
const EPR = require('./middleware/errorPages')(Sentry) //? Error Page Renderer
const four0four = require('./middleware/404'); //? 404 Handler
const errPages = require('./middleware/errpages')

const reqLogs = [];

/**
* @type {Map<String, Map<String, any>|String>}
* @desciption
* Used to store data throughout requests
*/
const Persistance = new Map()
class Formatter {
static perms = {
readData: 1 << 0, // 1
Expand Down Expand Up @@ -99,11 +105,13 @@ app
.use(Sentry.Handlers.tracingHandler())
.use((req, _, next) => {
req.reqLogs = reqLogs;
req.Persistance = Persistance
req.Sentry = Sentry;
req.FirebaseAdmin = AdminApp;
req.auth = AdminApp.auth();
req.Database = Database;
req.Formatter = Formatter;
req.getErrPage = (c, d) => errPages.get(c).call(d)
req.checkPerms = (userbit, ...neededPerms) => (Formatter.permissionBitToReadable(userbit).some(['admin', 'owner'].includes)) ? true : neededPerms.some(Formatter.permissionBitToReadable(userbit).includes);
next();
})
Expand Down Expand Up @@ -144,18 +152,7 @@ app
)
) return res.status(403).render(
`misc/403.pug`,
{
errData: {
path: req.path,
code: 403,
reason: 'You are banned from accessing this website.'
},
meta: {
title: `403 - Forbidden`,
desc: `403 - Forbidden`,
url: `https://thefemdevs.com/errors/403`
}
}
req.getErrPage(403, { path: req.path })
)
next();
}
Expand Down Expand Up @@ -186,23 +183,16 @@ app
if (allowedMethods[methodUsed]) return next();
res.status(405).render(
`misc/405.pug`,
{
errData: {
path,
allowedMethods: Object.keys(allowedMethods).map(m => m.toUpperCase()).join(', '),
methodUsed: methodUsed,
},
meta: {
title: '405 - Method Not Allowed',
desc: '405 - Method Not Allowed',
url: 'https://thefemdevs.com/errors/405',
}
}
req.getErrPage(405, { path, allowedMethods, methodUsed })
);
})
.use(Sentry.Handlers.errorHandler())
.use(EPR)
.use(four0four)
.use(four0four);

const server = http
.createServer(app)
.on('listening', _ => console.log("HTTP Server is UP"))

cron
.schedule('*/5 * * * *', async () => {
Expand All @@ -215,5 +205,5 @@ cron
})


app
.listen(process.env.PORT || 3000, () => console.log('server is up'));
server
.listen(process.env.PORT || 3000)
Loading

0 comments on commit 8a21957

Please sign in to comment.