api: clean up imports & consts
This commit is contained in:
parent
592bfe24cf
commit
b5c81084c8
2 changed files with 34 additions and 28 deletions
|
@ -1,25 +1,40 @@
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import rateLimit from "express-rate-limit";
|
import rateLimit from "express-rate-limit";
|
||||||
import { randomBytes } from "crypto";
|
|
||||||
|
|
||||||
const ipSalt = randomBytes(64).toString('hex');
|
|
||||||
|
|
||||||
import { env, version } from "../modules/config.js";
|
import { env, version } from "../modules/config.js";
|
||||||
import match from "../modules/processing/match.js";
|
|
||||||
import { languageCode } from "../modules/sub/utils.js";
|
import { generateHmac, generateSalt } from "../modules/sub/crypto.js";
|
||||||
import { createResponse, normalizeRequest, getIP } from "../modules/processing/request.js";
|
|
||||||
import { Bright, Cyan } from "../modules/sub/consoleText.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 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 { verifyStream, getInternalStream } from "../modules/stream/manage.js";
|
||||||
import { extract } from "../modules/processing/url.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) {
|
export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
|
||||||
const corsConfig = env.corsWildcard ? {} : {
|
const startTime = new Date();
|
||||||
origin: env.corsURL,
|
const startTimestamp = startTime.getTime();
|
||||||
optionsSuccessStatus: 200
|
|
||||||
};
|
const serverInfo = {
|
||||||
|
version: version,
|
||||||
|
commit: gitCommit,
|
||||||
|
branch: gitBranch,
|
||||||
|
name: env.apiName,
|
||||||
|
url: env.apiURL,
|
||||||
|
cors: Number(env.corsWildcard),
|
||||||
|
startTime: `${startTimestamp}`
|
||||||
|
}
|
||||||
|
|
||||||
const apiLimiter = rateLimit({
|
const apiLimiter = rateLimit({
|
||||||
windowMs: env.rateLimitWindow * 1000,
|
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.set('trust proxy', ['loopback', 'uniquelocal']);
|
||||||
|
|
||||||
app.use('/api', cors({
|
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) => {
|
app.post('/api/json', async (req, res) => {
|
||||||
const request = req.body;
|
const request = req.body;
|
||||||
const lang = languageCode(req);
|
const lang = languageCode(req);
|
||||||
|
@ -192,15 +202,7 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
|
||||||
|
|
||||||
app.get('/api/serverInfo', (req, res) => {
|
app.get('/api/serverInfo', (req, res) => {
|
||||||
try {
|
try {
|
||||||
return res.status(200).json({
|
return res.status(200).json(serverInfo);
|
||||||
version: version,
|
|
||||||
commit: gitCommit,
|
|
||||||
branch: gitBranch,
|
|
||||||
name: env.apiName,
|
|
||||||
url: env.apiURL,
|
|
||||||
cors: Number(env.corsWildcard),
|
|
||||||
startTime: `${startTimestamp}`
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
return res.destroy();
|
return res.destroy();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
export function generateHmac(str, salt) {
|
||||||
return createHmac("sha256", salt).update(str).digest("base64url");
|
return createHmac("sha256", salt).update(str).digest("base64url");
|
||||||
|
|
Loading…
Reference in a new issue