2024-08-05 22:03:42 -04:00
|
|
|
const { createTask } = require('./lib/task')
|
|
|
|
const { fetchVisitorData } = require('./lib/workflow')
|
2024-10-12 01:57:18 -03:00
|
|
|
const { Redis } = require('ioredis')
|
|
|
|
|
|
|
|
const redis = new Redis({ host: process.env.HOST || config.host });
|
2024-10-17 23:45:22 -03:00
|
|
|
const sub = new Redis({ host: process.env.HOST || config.host });
|
2024-10-12 01:57:18 -03:00
|
|
|
|
|
|
|
redis.on('error', (error) => {
|
2024-10-12 02:32:56 -03:00
|
|
|
console.error('Could not connect to Redis:', error);
|
|
|
|
process.exit(1);
|
2024-10-12 01:57:18 -03:00
|
|
|
});
|
2024-08-05 22:03:42 -04:00
|
|
|
|
2024-10-17 23:45:22 -03:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
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-10-12 01:57:18 -03:00
|
|
|
const run = async () => {
|
|
|
|
try {
|
|
|
|
const { visitorData, poToken } = await generate();
|
2024-10-12 02:32:56 -03:00
|
|
|
redis.set("invidious:po_token", poToken)
|
|
|
|
redis.set("invidious:visitor_data", visitorData)
|
2024-10-12 01:57:18 -03:00
|
|
|
} catch(e) {
|
2024-10-12 02:32:56 -03:00
|
|
|
console.error(`run: Error ${e}`)
|
2024-10-12 01:57:18 -03:00
|
|
|
}
|
|
|
|
console.log("New tokens sent to redis")
|
|
|
|
}
|
|
|
|
|
2024-10-17 23:45:22 -03:00
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
|
2024-10-12 01:57:18 -03:00
|
|
|
run();
|
2024-10-12 02:32:56 -03:00
|
|
|
setInterval(run, (process.env.RENEW_INTERVAL || 60) * 1000)
|
2024-10-12 01:57:18 -03:00
|
|
|
|
2024-08-05 22:03:42 -04:00
|
|
|
module.exports = { generate }
|