SponsorBlockServer/index.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-07-09 19:48:03 +02:00
var express = require('express');
var http = require('http');
// Create a service (the app object is just a callback).
var app = express();
//uuid service
var uuidv1 = require('uuid/v1');
//hashing service
var crypto = require('crypto');
var hash = crypto.createHash('sha256');
2019-07-09 19:48:03 +02:00
//load database
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./databases/sponsorTimes.db');
// Create an HTTP service.
http.createServer(app).listen(80);
2019-07-11 04:07:39 +02:00
//global salt that is added to every ip before hashing to
// make it even harder for someone to decode the ip
var globalSalt = "49cb0d52-1aec-4b89-85fc-fab2c53062fb";
2019-07-09 21:12:08 +02:00
//setup CORS correctly
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
2019-07-09 19:48:03 +02:00
//add the get function
2019-07-09 20:44:40 +02:00
app.get('/api/getVideoSponsorTimes', function (req, res) {
2019-07-09 19:48:03 +02:00
let videoID = req.query.videoID;
let sponsorTimes = [];
db.prepare("SELECT startTime, endTime FROM sponsorTimes WHERE videoID = ?").all(videoID, function(err, rows) {
if (err) console.log(err);
for (let i = 0; i < rows.length; i++) {
sponsorTimes[i] = [];
sponsorTimes[i][0] = rows[i].startTime;
sponsorTimes[i][1] = rows[i].endTime;
}
2019-07-09 22:43:17 +02:00
if (sponsorTimes.length == 0) {
2019-07-09 22:31:04 +02:00
res.sendStatus(404);
} else {
//send result
res.send({
sponsorTimes: sponsorTimes
})
}
2019-07-09 19:48:03 +02:00
});
});
//add the post function
2019-07-09 20:44:40 +02:00
app.get('/api/postVideoSponsorTimes', function (req, res) {
let videoID = req.query.videoID;
let startTime = req.query.startTime;
let endTime = req.query.endTime;
2019-07-11 01:25:49 +02:00
let userID = req.query.userID;
2019-07-11 01:25:49 +02:00
if (typeof videoID != 'string' || startTime == undefined || endTime == undefined || userID == undefined) {
//invalid request
res.sendStatus(400);
return;
}
2019-07-11 04:07:39 +02:00
//x-forwarded-for if this server is behind a proxy
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
//hash the ip so no one can get it from the database
let hashedIP = hash.update(ip + globalSalt).digest('hex');
2019-07-09 21:20:10 +02:00
startTime = parseFloat(startTime);
endTime = parseFloat(endTime);
let UUID = uuidv1();
db.prepare("INSERT INTO sponsorTimes VALUES(?, ?, ?, ?, ?, ?)").run(videoID, startTime, endTime, UUID, userID, hashedIP);
2019-07-09 19:48:03 +02:00
res.sendStatus(200);
2019-07-09 19:48:03 +02:00
});
app.get('/database.db', function (req, res) {
res.sendFile("./databases/sponsorTimes.db", { root: __dirname });
2019-07-09 19:48:03 +02:00
});