api: clean up imports & consts

This commit is contained in:
wukko 2024-05-16 14:20:40 +06:00
parent 592bfe24cf
commit b5c81084c8
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 34 additions and 28 deletions

View file

@ -1,25 +1,40 @@
import cors from "cors";
import rateLimit from "express-rate-limit";
import { randomBytes } from "crypto";
const ipSalt = randomBytes(64).toString('hex');
import { env, version } from "../modules/config.js";
import match from "../modules/processing/match.js";
import { languageCode } from "../modules/sub/utils.js";
import { createResponse, normalizeRequest, getIP } from "../modules/processing/request.js";
import { generateHmac, generateSalt } from "../modules/sub/crypto.js";
import { Bright, Cyan } from "../modules/sub/consoleText.js";
import stream from "../modules/stream/stream.js";
import { languageCode } from "../modules/sub/utils.js";
import loc from "../localization/manager.js";
import { generateHmac } from "../modules/sub/crypto.js";
import { createResponse, normalizeRequest, getIP } from "../modules/processing/request.js";
import { verifyStream, getInternalStream } from "../modules/stream/manage.js";
import { extract } from "../modules/processing/url.js";
import match from "../modules/processing/match.js";
import stream from "../modules/stream/stream.js";
const acceptRegex = /^application\/json(; charset=utf-8)?$/;
const ipSalt = generateSalt();
const corsConfig = env.corsWildcard ? {} : {
origin: env.corsURL,
optionsSuccessStatus: 200
}
export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
const corsConfig = env.corsWildcard ? {} : {
origin: env.corsURL,
optionsSuccessStatus: 200
};
const startTime = new Date();
const startTimestamp = startTime.getTime();
const serverInfo = {
version: version,
commit: gitCommit,
branch: gitBranch,
name: env.apiName,
url: env.apiURL,
cors: Number(env.corsWildcard),
startTime: `${startTimestamp}`
}
const apiLimiter = rateLimit({
windowMs: env.rateLimitWindow * 1000,
@ -46,9 +61,6 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
}
})
const startTime = new Date();
const startTimestamp = startTime.getTime();
app.set('trust proxy', ['loopback', 'uniquelocal']);
app.use('/api', cors({
@ -101,8 +113,6 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
}
})
const acceptRegex = /^application\/json(; charset=utf-8)?$/;
app.post('/api/json', async (req, res) => {
const request = req.body;
const lang = languageCode(req);
@ -192,15 +202,7 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
app.get('/api/serverInfo', (req, res) => {
try {
return res.status(200).json({
version: version,
commit: gitCommit,
branch: gitBranch,
name: env.apiName,
url: env.apiURL,
cors: Number(env.corsWildcard),
startTime: `${startTimestamp}`
});
return res.status(200).json(serverInfo);
} catch {
return res.destroy();
}

View file

@ -1,6 +1,10 @@
import { createHmac, createCipheriv, createDecipheriv } from "crypto";
import { createHmac, createCipheriv, createDecipheriv, randomBytes } from "crypto";
const algorithm = "aes256"
const algorithm = "aes256";
export function generateSalt() {
return randomBytes(64).toString('hex');
}
export function generateHmac(str, salt) {
return createHmac("sha256", salt).update(str).digest("base64url");