Token generator for inv.nadeko.net
All checks were successful
Docker build image / build (push) Successful in 29s

This commit is contained in:
Fijxu 2024-10-17 23:45:22 -03:00
parent 84770fb410
commit 3a0481112f
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4

View file

@ -3,12 +3,34 @@ const { fetchVisitorData } = require('./lib/workflow')
const { Redis } = require('ioredis')
const redis = new Redis({ host: process.env.HOST || config.host });
const sub = new Redis({ host: process.env.HOST || config.host });
redis.on('error', (error) => {
console.error('Could not connect to Redis:', error);
process.exit(1);
});
sub.on('error', (error) => {
console.error('Could not connect to Redis:', error);
process.exit(1);
});
sub.subscribe("generate-token", (err, count) => {
if (err) {
console.error("Failed to subscribe: %s", err.message);
} else {
console.log(
`Subscribed successfully! This client is currently subscribed to ${count} channels.`
);
}
});
sub.on("message", (channel, user) => {
console.log(`${channel}`);
console.log(`Generating po_token and visitor_data for ${user}`);
userGen(user);
});
const generate = async () => {
console.debug("[DEBUG] generate called")
try {
@ -32,6 +54,17 @@ const run = async () => {
console.log("New tokens sent to redis")
}
const userGen = async (user) => {
try {
const { visitorData, poToken } = await generate();
redis.set(`invidious:${user}:po_token`, poToken)
redis.set(`invidious:${user}:visitor_data`, visitorData)
} catch(e) {
console.error(`userGen: Error ${e}`)
}
console.log(`Tokens generated successfully for user: ${user}`)
}
run();
setInterval(run, (process.env.RENEW_INTERVAL || 60) * 1000)