2024-12-14 17:24:29 -03:00
|
|
|
const { env } = require('process');
|
2024-08-05 22:03:42 -04:00
|
|
|
const { createTask } = require('./lib/task')
|
|
|
|
const { fetchVisitorData } = require('./lib/workflow')
|
2024-12-14 17:24:29 -03:00
|
|
|
const http = require('http');
|
2024-10-12 01:57:18 -03:00
|
|
|
|
2024-12-14 17:24:29 -03:00
|
|
|
const port = env.PORT || "3000"
|
|
|
|
const address = env.ADDRESS || "0.0.0.0"
|
2024-12-17 03:18:04 -03:00
|
|
|
const key = env.KEY
|
2024-12-21 17:07:50 -03:00
|
|
|
const nokey = env.USE_KEY || "false"
|
2024-10-12 01:57:18 -03:00
|
|
|
|
2024-12-14 17:24:29 -03:00
|
|
|
let tokens = {};
|
2024-10-17 23:45:22 -03:00
|
|
|
|
2024-08-05 22:03:42 -04:00
|
|
|
const generate = async () => {
|
2024-10-12 02:32:56 -03:00
|
|
|
console.debug("[DEBUG] generate called")
|
|
|
|
try {
|
|
|
|
const visitorData = await fetchVisitorData()
|
|
|
|
const task = await createTask(visitorData)
|
|
|
|
const { poToken } = await task.start()
|
|
|
|
return { visitorData, poToken }
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`generate: Error ${e}`)
|
|
|
|
}
|
2024-08-05 22:03:42 -04:00
|
|
|
}
|
|
|
|
|
2024-12-14 17:24:29 -03:00
|
|
|
const refreshTokens = async () => {
|
|
|
|
console.log("[DEBUG] generateTokens called")
|
2024-10-12 01:57:18 -03:00
|
|
|
try {
|
|
|
|
const { visitorData, poToken } = await generate();
|
2024-12-14 17:24:29 -03:00
|
|
|
tokens.visitorData = visitorData;
|
|
|
|
tokens.poToken = poToken;
|
2024-12-17 03:18:04 -03:00
|
|
|
console.log("[DEBUG] generateTokens finished")
|
2024-12-14 17:24:29 -03:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`refreshTokens: Error ${e}`)
|
2024-10-12 01:57:18 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-14 17:24:29 -03:00
|
|
|
const server = http.Server(async (req, res) => {
|
|
|
|
switch (req.url) {
|
|
|
|
case "/generate":
|
|
|
|
if (tokens.visitorData == undefined || tokens.poToken == undefined) {
|
|
|
|
res.writeHead(500)
|
2024-12-17 03:18:04 -03:00
|
|
|
res.write("Tokens are not yet generated");
|
2024-12-14 17:24:29 -03:00
|
|
|
return res.end()
|
|
|
|
}
|
2024-12-17 03:18:04 -03:00
|
|
|
|
|
|
|
const host = req.headers.host;
|
|
|
|
|
2024-12-14 17:24:29 -03:00
|
|
|
const json = {
|
|
|
|
potoken: tokens.poToken,
|
|
|
|
visitorData: tokens.visitorData,
|
|
|
|
};
|
2024-12-17 03:18:04 -03:00
|
|
|
|
2024-12-21 17:07:50 -03:00
|
|
|
if (nokey) {
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
|
|
res.write(JSON.stringify(json));
|
|
|
|
return res.end();
|
|
|
|
}
|
|
|
|
|
2024-12-17 03:18:04 -03:00
|
|
|
if (host === `127.0.0.1:${port}` || host === `localhost:${port}`) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2024-12-14 17:24:29 -03:00
|
|
|
case "/update":
|
|
|
|
await refreshTokens();
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' })
|
|
|
|
res.write("Tokens updated")
|
|
|
|
return res.end()
|
|
|
|
default:
|
|
|
|
return res.end()
|
2024-10-17 23:45:22 -03:00
|
|
|
}
|
2024-12-14 17:24:29 -03:00
|
|
|
})
|
|
|
|
|
2024-10-17 23:45:22 -03:00
|
|
|
|
2024-12-14 17:24:29 -03:00
|
|
|
refreshTokens();
|
|
|
|
setInterval(refreshTokens, (env.RENEW_INTERVAL || 60) * 1000)
|
|
|
|
server.listen(port, address, () => {
|
|
|
|
console.log("Listening at port", port);
|
|
|
|
})
|
2024-10-12 01:57:18 -03:00
|
|
|
|
2024-08-05 22:03:42 -04:00
|
|
|
module.exports = { generate }
|