support for a secret key for outside requests
All checks were successful
Docker build image / build (push) Successful in 26s

This commit is contained in:
Fijxu 2024-12-17 03:18:04 -03:00
parent 64dd07e2b3
commit a015576902
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4

View file

@ -5,6 +5,7 @@ const http = require('http');
const port = env.PORT || "3000" const port = env.PORT || "3000"
const address = env.ADDRESS || "0.0.0.0" const address = env.ADDRESS || "0.0.0.0"
const key = env.KEY
let tokens = {}; let tokens = {};
@ -26,6 +27,7 @@ const refreshTokens = async () => {
const { visitorData, poToken } = await generate(); const { visitorData, poToken } = await generate();
tokens.visitorData = visitorData; tokens.visitorData = visitorData;
tokens.poToken = poToken; tokens.poToken = poToken;
console.log("[DEBUG] generateTokens finished")
} catch (e) { } catch (e) {
console.error(`refreshTokens: Error ${e}`) console.error(`refreshTokens: Error ${e}`)
} }
@ -36,15 +38,38 @@ const server = http.Server(async (req, res) => {
case "/generate": case "/generate":
if (tokens.visitorData == undefined || tokens.poToken == undefined) { if (tokens.visitorData == undefined || tokens.poToken == undefined) {
res.writeHead(500) res.writeHead(500)
res.write("Tokens are not yet generated");
return res.end() return res.end()
} }
const host = req.headers.host;
const json = { const json = {
potoken: tokens.poToken, potoken: tokens.poToken,
visitorData: tokens.visitorData, visitorData: tokens.visitorData,
}; };
res.writeHead(200, { 'Content-Type': 'text/json' })
res.write(JSON.stringify(json)) if (host === `127.0.0.1:${port}` || host === `localhost:${port}`) {
return res.end() res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(json));
return res.end();
} else {
if (key != "" && key != undefined) {
const auth = req.headers.authorization;
if (auth == `Bearer ${key}`) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(JSON.stringify(json));
return res.end();
} else {
res.writeHead(401, { 'Content-Type': 'text/plain' });
return res.end();
}
} else {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(JSON.stringify(json));
return res.end();
}
}
case "/update": case "/update":
await refreshTokens(); await refreshTokens();
res.writeHead(200, { 'Content-Type': 'text/plain' }) res.writeHead(200, { 'Content-Type': 'text/plain' })