Repostcheck via Checksum

This commit is contained in:
Flummi 2016-08-15 09:18:09 +02:00
parent 2018167d49
commit 4063ccd706

View File

@ -5,6 +5,7 @@ var https = require('https');
var mysql = require('mysql'); var mysql = require('mysql');
var bot = require('coffea')(); var bot = require('coffea')();
var uuid = require('uuid'); var uuid = require('uuid');
var crypto = require('crypto');
const path = require('path'); const path = require('path');
var sql; var sql;
@ -48,18 +49,29 @@ bot.on('message', (e) => {
dl(entry, "./b/"+tmpdest, (cb) => { // download item dl(entry, "./b/"+tmpdest, (cb) => { // download item
if(cb.status === true) { if(cb.status === true) {
var tmpuser = getUser(e.user.getNick(), e.network); var tmpuser = getUser(e.user.getNick(), e.network);
sql.query("insert into `f0ck`.`items` (`src`,`dest`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?)", [ getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => {
entry, checkRepostCheckSum(cbcs, (cbcrcs) => {
"./b/"+tmpdest+"."+cb.infos.ext, if(cbcrcs) {
tmpuser['nick'], sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [
e.channel.getName(), entry,
e.network, "./b/"+tmpdest+"."+cb.infos.ext,
Math.floor(new Date() / 1000), cb.infos.mime,
0 cb.infos.size,
]).on('result', (result) => { cbcs,
e.reply("https://f0ck.me/"+result.insertId+" - "+path.parse(entry).base+" ("+cb.infos.mime+", ~"+formatSize(cb.infos.size)+") from "+tmpuser['nick']+" ("+tmpuser['username']+"@"+tmpuser['hostname']+")"); tmpuser['nick'],
}).on('error', (msg) => { e.channel.getName(),
e.reply(msg); e.network,
Math.floor(new Date() / 1000),
0
]).on('result', (result) => {
e.reply("https://f0ck.me/"+result.insertId+" - "+path.parse(entry).base+" ("+cb.infos.mime+", ~"+formatSize(cb.infos.size)+") from "+tmpuser['nick']+" ("+tmpuser['username']+"@"+tmpuser['hostname']+")");
}).on('error', (msg) => {
e.reply(msg);
});
}
else
e.reply("repost motherf0cker");
});
}); });
} }
else else
@ -80,6 +92,11 @@ bot.on('message', (e) => {
e.reply(msg); e.reply(msg);
}); });
} }
else if(orig.match(/^\.hash/) && e.channel.getName() == "#f0ck") {
getCheckSum('./b/1fbd8b80.png', (cbcs) => {
e.reply(cbcs);
});
}
}); });
var getUser = (u, n) => { var getUser = (u, n) => {
@ -127,12 +144,28 @@ var checkRepost = (url, cbcr) => {
cbcr((rows[0].count == 0)?true:false); cbcr((rows[0].count == 0)?true:false);
}); });
}; };
var checkRepostCheckSum = (cs, cbcrcs) => {
sql.query("select count(*) as count from `f0ck`.`items` where `checksum` = ?", cs, (err, rows, fields) => {
cbcrcs((rows[0].count == 0)?true:false);
});
};
var formatSize = (size) => { var formatSize = (size) => {
var i = Math.floor(Math.log(size) / Math.log(1024)); var i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}; };
var getCheckSum = (file, cbcs) => {
var sha256sum = crypto.createHash('sha256');
var s = fs.ReadStream(file);
s.on('data', (d) => {
sha256sum.update(d);
});
s.on('end', () => {
var generated_hash = sha256sum.digest('hex');
cbcs(generated_hash);
});
};
// Webserver // Webserver