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

Commit bf63792

Browse files
committed
more modifications
1 parent 7b4d453 commit bf63792

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+176
-121
lines changed

api/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const router = require('express').Router();
2+
require('dotenv').config();
3+
4+
const mainRoutes = require('./routes/router');
5+
6+
router
7+
.use('/', mainRoutes);
8+
9+
module.exports = router;

api/middleware/.gitkeep

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

cdn/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const router = require('express').Router();
2+
require('dotenv').config();
3+
4+
const mainRoutes = require('./routes/router');
5+
6+
router
7+
.use('/', mainRoutes);
8+
9+
module.exports = router;

cdn/middleware/.gitkeep

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

routes/cdn.js cdn/routes/cdn.js

File renamed without changes.

cdn/routes/router.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const router = require('express').Router();
2+
require('dotenv').config();
3+
4+
const cdn = require('./cdn');
5+
const assets = require('./assets/router');
6+
7+
router
8+
.use('/cdn', cdn)
9+
.use('/assets', assets)
10+
11+
module.exports = router;

doc/api-documentation.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ info:
1111
name: "GNU Affero General Public License v3.0"
1212
url: "https://www.gnu.org/licenses/agpl-3.0.en.html"
1313
servers:
14-
- url: "https://thefemdevs.com/api/"
14+
- url: "https://api.thefemdevs.com/"
1515
description: "Production Server"
1616
paths:
1717
/user/get:

functions/util-fuctions.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Timer {
2929
}
3030
}
3131
static timestamp = v => new Intl.DateTimeFormat(this.#timesettings.locale, this.#timesettings.options).format(v)
32-
static elapsedTime = (timestamp) => isNaN(timestamp) ? TypeError("Timestamp must be a number") : Object.entries({ year: Math.floor(Math.floor(timestamp) / (60 ** 2) / 24 / 30 / 12), month: Math.floor(Math.floor(timestamp) / (60 ** 2) / 24 / 30) % 12, day: Math.floor(Math.floor(timestamp) / (60 ** 2) / 24) % 30, hour: Math.floor(Math.floor(timestamp) / (60 ** 2)) % 24, minute: Math.floor(Math.floor(timestamp) / 60) % 60, second: Math.floor(timestamp) % 60 }).map(([key, value]) => value !== 0 ? `${value} ${key}${value == 1 ? '' : 's'}` : null).filter(value => value !== null).join(', ')
32+
static elapsedTime = (timestamp) => isNaN(timestamp) ? TypeError("Timestamp must be a number") : Object.entries({ year: Math.floor(Math.floor(timestamp) / (60*60*24*30*12)), month: Math.floor(Math.floor(timestamp) / (60*60*24*30)) % 12, day: Math.floor(Math.floor(timestamp) / (60*60*24)) % 30, hour: Math.floor(Math.floor(timestamp) / (60*60)) % 24, minute: Math.floor(Math.floor(timestamp) / 60) % 60, second: Math.floor(timestamp) % 60 }).map(([key, value]) => value !== 0 ? `${value} ${key}${value == 1 ? '' : 's'}` : null).filter(value => value !== null).join(', ')
3333
static stringToMilliseconds = (timeString) => typeof timeString !== 'string' ? TypeError("Time String must be a string") : timeString.split(' ').map(value => { switch (value.slice(-1)) { case 'w': return value.slice(0, -1) * 604800000; case 'd': return value.slice(0, -1) * 86400000; case 'h': return value.slice(0, -1) * 3600000; case 'm': return value.slice(0, -1) * 60000; case 's': return value.slice(0, -1) * 1000; } }).reduce((a, b) => a + b, 0);
3434
static stringToSeconds = (timeString) => this.stringToMilliseconds(timeString) / 1000;
3535
static unixTime = (date) => (!(date instanceof Date) && typeof date !== 'number') ? TypeError("Date must be a Date object") : Math.round(date.getTime() / 1000)
@@ -63,30 +63,19 @@ class Formatter {
6363
}
6464

6565
class ArrayAndJSON {
66-
static combineArrays = new Array().concat
67-
static combineJSON = (d1, d2) => Object.fromEntries([...Object.entries(d1), ...Object.entries(d2)])
68-
static arrayToJSON = (array) => {
69-
if (array.every(subdata => Array.isArray(subdata) && subdata.length == 2)) return Object.fromEntries(array)
70-
let json = {};
71-
array.forEach((v, i) => {
72-
json[i] = v;
73-
});
74-
return json;
75-
}
66+
static combineArrays = Array.prototype.concat
67+
static combineJSON = Object.assign
68+
static arrayToJSON = (array) => Object.fromEntries(array.map((v, i) => (Array.isArray(v) && v.length == 2) ? v : [i, v]))
7669
static JSONToArray = (json, keys = false) => (keys) ? Object.entries(json) : Object.values(json)
7770
static arrayToSet = (array) => new Set(array)
78-
static setToArray = (set) => [...set]
79-
static arrayToMap = (array) => new Map(array)
80-
static mapToArray = (map) => [...map]
81-
static _par = (array) => array = array.sort(() => Math.random() > 0.5 ? 1 : -1)
82-
static arrayRandomizer = (a, it = 25) => {
83-
for (let i = 0; i < it; ++i) this._par(a);
84-
return a
85-
}
71+
static setToArray = (set) => Array.from(set)
72+
static objectToMap = (obj) => new Map(Object.entries(obj))
73+
static mapToObject = (map) => Object.fromEntries(map.entries())
74+
static arrayRandomizer = (a) => Array.from(a).sort(() => Math.random() > 0.5 ? 1 : -1);
8675
}
8776

8877
class Cryptography {
89-
static Utf8Encode = (string) => string.replace(/\r\n/g, '\n').split('').map((c) => c.charCodeAt(0)).map(char => { if (char < 128) return String.fromCharCode(char); else if ((char > 127) && (char < 2048)) return String.fromCharCode((char >> 6) | 192) + String.fromCharCode((char & 63) | 128); else return String.fromCharCode((char >> 12) | 224) + String.fromCharCode(((char >> 6) & 63) | 128) + String.fromCharCode((char & 63) | 128); }).join('');
78+
static Utf8Encode = (string) => string.replace(/\r\n/g, '\n').split('').map((c) => c.charCodeAt(0)).map(char => (char < 128) ? String.fromCharCode(char) : ((char > 127) && (char < 2048)) ? String.fromCharCode((char >> 6) | 192) + String.fromCharCode((char & 63) | 128) : String.fromCharCode((char >> 12) | 224) + String.fromCharCode(((char >> 6) & 63) | 128) + String.fromCharCode((char & 63) | 128)).join('');
9079
static SHA1 = (msg) => {
9180
const rotate_left = (n, s) => (n << s) | (n >>> (32 - s))
9281
const cvt_hex = (val) => {

index.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
const app = require('express')();
22
const http = require('http');
3-
const axios = require('axios');
4-
const cron = require('node-cron');
3+
const cors = require('cors');
4+
const vhost = require('vhost');
55
const crypto = require('crypto');
66
const Admin = require('firebase-admin');
77
const { RateLimiterMemory } = require('rate-limiter-flexible');
88
require('dotenv').config();
99

10-
//- Routers
11-
const router = require('./routes/router');
12-
1310
const RateLimiter = new RateLimiterMemory({
1411
points: 30,
1512
duration: 1,
@@ -70,6 +67,14 @@ const Database = new (require('./functions/database'))();
7067

7168
setInterval(_ => (!reqLogs[0]) ? null : Database.emit('access', reqLogs.shift()), 500)
7269

70+
/** @type {cors.CorsOptions} */
71+
const CORSPerms = {
72+
credentials: true,
73+
maxAge: 86400,
74+
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'HEAD'],
75+
optionsSuccessStatus: 200,
76+
}
77+
7378
app
7479
.set('view engine', 'pug')
7580
.set('case sensitive routing', false)
@@ -129,7 +134,13 @@ app
129134
.use(TRACE)
130135
.use(MRL)
131136
.use(Headers)
132-
.use('/', router)
137+
.use(cors(CORSPerms))
138+
.use(vhost('api.thefemdevs.com', require('./api/')))
139+
.use(vhost('oss.thefemdevs.com', require('./oss/')))
140+
.use(vhost('cdn.thefemdevs.com', require('./cdn/')))
141+
.use(vhost('www.thefemdevs.com', require('./routes/router')))
142+
.use(vhost('thefemdevs.com', require('./routes/router')))
143+
.use(vhost('localhost', require('./routes/router')))
133144
.get(`/robots.txt`, (_, res) => {
134145
res
135146
.sendFile(`${process.cwd()}/metadata/robots.txt`)
@@ -142,7 +153,7 @@ app
142153
.use((req, res, next) => {
143154
const { path } = req;
144155
const methodUsed = req.method.toUpperCase();
145-
let allowedMethods = router.stack.filter(r => r.route && r.route.path === path)
156+
let allowedMethods = app._router.stack.filter(r => r.route && r.route.path === path)
146157
if (allowedMethods.length == 0) return next();
147158
allowedMethods.map(r => r.route.stack[0])
148159
allowedMethods = { ...allowedMethods[0] }

middleware/APITokenMgr.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// const Cryptolens = require('cryptolens');
22
require('dotenv').config();
33

4-
const TokenManager = require('../functions/crypto')
5-
const { RequestHandler } = require('express');
6-
7-
/** @type {import('express').RequestHandler} */ module.exports = async (req, res, next) => {
84

5+
/** @type {import('express').RequestHandler} */
6+
module.exports = async (req, res, next) => {
97
if (!req.headers['authorization']) return res.sendError(1);
10-
const [_, token] = req.headers['authorization'].split(' ');
8+
const token = req.headers['authorization'].split(' ')[1];
119
if (!token) return res.sendError(1);
12-
if (!TokenManager.verify(token)) return res.sendError(2);
10+
if (!require('../functions/crypto').verify(token)) return res.sendError(2);
1311
const connection = await req.Database.Pool.connect();
1412
const { rows } = await connection.query(`SELECT * FROM public.apitokens WHERE token = '${token}'`)
1513
if (rows.length == 0) return res.sendError(2)

middleware/errpages.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

middleware/headers.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
const { RequestHandler } = require('express');
2-
31
/** @type {import('express').RequestHandler} */ module.exports = (req, res, next) => {
2+
const { platform: os, versions: v } = process;
43
res
54
.setHeader('X-Repo', 'https://github.com/femdevs/femdev-website')
65
.setHeader('X-Live-Deploy', 'https://thefemdevs.com')
76
.setHeader('X-Repository-License', 'Affero General Public License v3.0 or newer (AGPL-3.0-or-later)')
8-
.setHeader(
9-
'X-OS',
10-
process.platform == 'win32' ? 'Windows' :
11-
process.platform == 'linux' ? 'Linux' :
12-
process.platform == 'darwin' ? 'MacOS' :
13-
'Other'
14-
)
15-
.setHeader('X-Node-Version', process.version)
7+
.setHeader('X-OS', os == 'win32' ? 'Windows' : os == 'linux' ? 'Linux' : os == 'darwin' ? 'MacOS' : 'Other')
8+
.setHeader('X-Node-Version', v.node)
169
.setHeader('Content-Security-Policy', "default-src *; script-src 'self' google.com *.google.com *.googlesyndication.com googlesyndication.com *.googleadservices.com googleadservices.com *.corbado.io corbado.io *.sentry-cdn.com sentry-cdn.com blob: 'unsafe-inline'; style-src 'self' 'unsafe-inline' fonts.googleapis.com; img-src *; font-src *; connect-src *; media-src *; object-src 'none';frame-ancestors *; form-action 'self'; upgrade-insecure-requests; block-all-mixed-content; sandbox allow-forms allow-same-origin allow-scripts; base-uri 'self'; manifest-src 'self'; require-trusted-types-for 'script';")
1710
.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
1811
.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')

middleware/rateLimit.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ module.exports = (rateLimiter) => (function (req, res, next) {
88
.consume(req.ip, (req.originalUrl.startsWith('/assets') ? 1 : 2))
99
.then(
1010
(data) => {
11+
const { remainingPoints: r, consumedPoints: c, msBeforeNext: m } = data;
1112
res
12-
.setHeader('X-RateLimit-Limit', data.remainingPoints + data.consumedPoints)
13-
.setHeader('X-RateLimit-Remaining', data.remainingPoints)
14-
.setHeader('X-RateLimit-Reset', data.msBeforeNext);
13+
.setHeader('X-RateLimit-Limit', r + c)
14+
.setHeader('X-RateLimit-Remaining', r)
15+
.setHeader('X-RateLimit-Reset', m);
1516
next();
1617
},
1718
(rej) => {

middleware/routeLogger.js

+55-55
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,66 @@ class CC {
66
static chalk = new Chalk.Instance({ level: 3 });
77
/** @type {Map<string,Chalk.Chalk>} */
88
static colors = new Map()
9-
.set('dr', CC.chalk.rgb(120, 0, 0))
10-
.set('lr', CC.chalk.rgb(250, 120, 120))
11-
.set('o', CC.chalk.rgb(255, 120, 0))
12-
.set('y', CC.chalk.yellow)
13-
.set('dg', CC.chalk.rgb(0, 150, 0))
14-
.set('lg', CC.chalk.rgb(0, 190, 0))
15-
.set('bg', CC.chalk.rgb(100, 255, 0))
16-
.set('db', CC.chalk.rgb(0, 0, 139))
17-
.set('lb', CC.chalk.rgb(65, 105, 225))
18-
.set('bb', CC.chalk.rgb(0, 191, 255))
19-
.set('p', CC.chalk.rgb(255, 0, 255))
20-
.set('pi', CC.chalk.rgb(255, 105, 180))
21-
.set('w', CC.chalk.rgb(255, 255, 255))
22-
.set('g', CC.chalk.rgb(120, 120, 120))
23-
.set('b', CC.chalk.rgb(0, 0, 0));
24-
static CPrep = (color) => CC.colors.get(color).bold.underline;
25-
/** @type {Map<string, Map<string,Chalk.Chalk>>} */
9+
.set('dr', this.chalk.rgb(120, 0, 0))
10+
.set('lr', this.chalk.rgb(250, 120, 120))
11+
.set('o', this.chalk.rgb(255, 120, 0))
12+
.set('y', this.chalk.yellow)
13+
.set('dg', this.chalk.rgb(0, 150, 0))
14+
.set('lg', this.chalk.rgb(0, 190, 0))
15+
.set('bg', this.chalk.rgb(100, 255, 0))
16+
.set('db', this.chalk.rgb(0, 0, 139))
17+
.set('lb', this.chalk.rgb(65, 105, 225))
18+
.set('bb', this.chalk.rgb(0, 191, 255))
19+
.set('p', this.chalk.rgb(255, 0, 255))
20+
.set('pi', this.chalk.rgb(255, 105, 180))
21+
.set('w', this.chalk.rgb(255, 255, 255))
22+
.set('g', this.chalk.rgb(120, 120, 120))
23+
.set('b', this.chalk.rgb(0, 0, 0));
24+
static CPrep = (color) => this.colors.get(color).bold.underline;
25+
/** @type {Map<string, Map<string, Chalk.Chalk>>} */
2626
static Dict = new Map()
2727
.set('status', (new Map()
28-
.set('1', CC.CPrep('dg'))
29-
.set('2', CC.CPrep('dg'))
30-
.set('3', CC.CPrep('lb'))
31-
.set('4', CC.CPrep('y'))
32-
.set('5', CC.CPrep('dr'))
33-
.set('default', CC.CPrep('w'))))
28+
.set('1', this.CPrep('dg'))
29+
.set('2', this.CPrep('dg'))
30+
.set('3', this.CPrep('lb'))
31+
.set('4', this.CPrep('y'))
32+
.set('5', this.CPrep('dr'))
33+
.set('default', this.CPrep('w'))))
3434
.set('method', (new Map()
35-
.set('GET', CC.CPrep('dg'))
36-
.set('POST', CC.CPrep('db'))
37-
.set('PUT', CC.CPrep('y'))
38-
.set('DELETE', CC.CPrep('dr'))
39-
.set('PATCH', CC.CPrep('p'))
40-
.set('HEAD', CC.CPrep('lb'))
41-
.set('OPTIONS', CC.CPrep('o'))
42-
.set('TRACE', CC.CPrep('bb'))
43-
.set('CONNECT', CC.CPrep('bg'))))
35+
.set('GET', this.CPrep('dg'))
36+
.set('POST', this.CPrep('db'))
37+
.set('PUT', this.CPrep('y'))
38+
.set('DELETE', this.CPrep('dr'))
39+
.set('PATCH', this.CPrep('p'))
40+
.set('HEAD', this.CPrep('lb'))
41+
.set('OPTIONS', this.CPrep('o'))
42+
.set('TRACE', this.CPrep('bb'))
43+
.set('CONNECT', this.CPrep('bg'))))
4444
.set('resTime', (new Map()
45-
.set('0', CC.CPrep('lg'))
46-
.set('1', CC.CPrep('dg'))
47-
.set('2', CC.CPrep('y'))
48-
.set('3', CC.CPrep('dr'))
49-
.set('4', CC.CPrep('dr'))
50-
.set('5', CC.CPrep('p'))
51-
.set('6', CC.CPrep('bb'))
52-
.set('7', CC.CPrep('db'))
53-
.set('8', CC.CPrep('lb'))
54-
.set('9', CC.CPrep('w'))
55-
.set('10', CC.CPrep('g'))))
45+
.set('0', this.CPrep('lg'))
46+
.set('1', this.CPrep('dg'))
47+
.set('2', this.CPrep('y'))
48+
.set('3', this.CPrep('dr'))
49+
.set('4', this.CPrep('dr'))
50+
.set('5', this.CPrep('p'))
51+
.set('6', this.CPrep('bb'))
52+
.set('7', this.CPrep('db'))
53+
.set('8', this.CPrep('lb'))
54+
.set('9', this.CPrep('w'))
55+
.set('10', this.CPrep('g'))))
5656
.set('bytes', (new Map()
57-
.set('0', CC.colors.get('g'))
58-
.set('1', CC.colors.get('lg'))
59-
.set('2', CC.colors.get('dg'))
60-
.set('3', CC.colors.get('y'))
61-
.set('4', CC.colors.get('lr'))
62-
.set('5', CC.colors.get('dr'))
63-
.set('-1', CC.colors.get('w'))))
64-
static status = (code) => (CC.Dict.get('status').get(String(code).at(0)))(code);
65-
static path = (CC.colors.get('db'));
66-
static method = (method) => (CC.Dict.get('method').get(method))(method);
67-
static resTime = (t) => (CC.Dict.get('resTime').get(String(Math.min(Math.floor(t / 100), 10))))(`${t}ms`)
68-
static bytes = (bytes) => (CC.Dict.get('bytes').get(String([0, 1, 5, 10, 50, 100].findIndex((v) => ((Math.ceil(Number(bytes) / (10 ** 5))) <= v)))))(`${new Intl.NumberFormat('en-US').format(bytes)} bytes`);
57+
.set('0', this.colors.get('g'))
58+
.set('1', this.colors.get('lg'))
59+
.set('2', this.colors.get('dg'))
60+
.set('3', this.colors.get('y'))
61+
.set('4', this.colors.get('lr'))
62+
.set('5', this.colors.get('dr'))
63+
.set('-1', this.colors.get('w'))))
64+
static status = (code) => (this.Dict.get('status').get(String(code).at(0)))(code);
65+
static path = (this.colors.get('db'));
66+
static method = (method) => (this.Dict.get('method').get(method))(method);
67+
static resTime = (t) => (this.Dict.get('resTime').get(String(Math.min(Math.floor(t / 100), 10))))(`${t}ms`)
68+
static bytes = (bytes) => (this.Dict.get('bytes').get(String([0, 1, 5, 10, 50, 100].findIndex((v) => ((Math.ceil(Number(bytes) / (10 ** 5))) <= v)))))(`${new Intl.NumberFormat('en-US').format(bytes)} bytes`);
6969
}
7070

7171
/** @type {import('express').RequestHandler} */

oss/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const router = require('express').Router();
2+
require('dotenv').config();
3+
4+
const mainRoutes = require('./routes/router');
5+
6+
router
7+
.use('/', mainRoutes);
8+
9+
module.exports = router;
File renamed without changes.

oss/routes/router.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const router = require('express').Router();
2+
require('dotenv').config();
3+
4+
const projects = require('./project');
5+
6+
router
7+
.use('/projects', projects)
8+
9+
module.exports = router;

0 commit comments

Comments
 (0)