optional redirection to specific alternative

master
Fabien Benetou 1 year ago
parent a421a98861
commit 7765d199b3
  1. 25
      index.js

@ -4,7 +4,7 @@
// killall onhygi ; nohup node . &
// to add on tridactyl :
// autocmd DocStart .* js fetch('http://localhost:7788/check?url='+window.location.href).then(r => r.json()).then( r => { if(!r.passed) window.location.href = 'https://ggsc.berkeley.edu/' } )
// autocmd DocStart .* js fetch('http://localhost:7788/check?url='+window.location.href).then(r => r.json()).then( r => { if(!r.passed) { if (r.redirect) { window.location.href = r.redirect } else { window.location.href = 'https://ggsc.berkeley.edu/'} } } )
const http = require("http");
const fs = require("fs");
@ -34,7 +34,8 @@ function terminator(sig) {
if (typeof sig === "string") {
console.log('Received %s - terminating server app ...', sig);
console.log('trying to save on exit')
fs.writeFileSync(usageFilePath, JSON.stringify( usage ))
if (quota.length) fs.writeFileSync(usageFilePath, JSON.stringify( usage ))
// to avoiding busting usage quota
process.exit(1);
}
console.log('Node server stopped.');
@ -50,18 +51,22 @@ console.log('past usage', usage)
// does not apply in a P2P fashion, must rely on local configuration here config.json
let quota = [
{id:"ownhome", pattern:/.*fabien\.benetou\.fr.*/, perHour:10, perDay:200},
// unlimited {id:"ownhome", pattern:/.*fabien\.benetou\.fr.*/, perHour:10, perDay:200},
{id:"twitter",pattern:/.*twitter\.com.*/,perHour:1,perDay:12},
{id:"proton",pattern:/.*proton\.me.*/,perHour:6,perDay:6*12},
{id:"yt",pattern:/.*youtube\.com.*/,perHour:1,perDay:12},
{id:"yt",pattern:/https:\/\/www\.youtube\.com.*/,perHour:2,perDay:12}, // allow for redirection
{id:"yts",pattern:/https:\/\/youtube\.com.*/,perHour:2,perDay:12}, // allow for redirection, consider merged IDs
{id:"linkedin",pattern:/.*linkedin\.com.*/,perHour:1,perDay:12},
{id:"reddit",pattern:/.*reddit\.com.*/,perHour:1,perDay:12},
{id:"reddit",pattern:/.*reddit\.com.*/,perHour:3,perDay:12,redirect:'https://lemmy.world'}, // redirection then enough to read private messages
// reconsider the tridactyl check for permanently open pages, e.g TabEnter rather than DocStart
{id:"element",pattern:/.*element\.io.*/,perHour:1,perDay:12},
{id:"discord",pattern:/.*discord\.com.*/,perHour:1,perDay:12},
]
// could also check on time, e.g not before 7am nor after 10pm as done via tridactyl now
// bypass for e.g presentations
//quota = []
if (fs.existsSync( configFilePath ) ){
const configurationFromFile = JSON.parse( fs.readFileSync( configFilePath ).toString() )
console.log('found quota file')
@ -111,11 +116,17 @@ app.get('/check', (req, res) => {
let now = Date.now()/1000
usage[rule.id].push( now )
if ( usage[rule.id].filter( t => t>now-60*60).length > rule.perHour ){
res.json( { passed: false, msg: 'over quota per hour' } )
if (rule.redirect)
res.json( { passed: false, msg: 'over quota per day', redirect:rule.redirect } )
else
res.json( { passed: false, msg: 'over quota per day' } )
return
}
if ( usage[rule.id].filter( t => t>now-60*60*24).length > rule.perDay ){
res.json( { passed: false, msg: 'over quota per day' } )
if (rule.redirect)
res.json( { passed: false, msg: 'over quota per day', redirect:rule.redirect } )
else
res.json( { passed: false, msg: 'over quota per day' } )
return
}
res.json( { passed: true, msg: 'within quota' } )

Loading…
Cancel
Save