Merge branch 'f0ckdev' into 'master'

F0ckdev



See merge request !14
This commit is contained in:
Flummi 2016-10-26 13:49:40 +00:00
commit 8351f8a282
4 changed files with 333 additions and 330 deletions

View File

@ -9,8 +9,10 @@ module.exports = (lib) => {
func: (e) => { func: (e) => {
exec('cd ../../ & git pull', (error, stdout) => { exec('cd ../../ & git pull', (error, stdout) => {
if(error === null) { if(error === null) {
e.reply( 'git pull suxxessfully.' ); if(!lib.debug)
//e.reply(stdout); e.reply('git pull suxxessfully.');
else
lib.log(stdout);
} }
}); });
}, },

View File

@ -138,6 +138,7 @@ module.exports = (lib) => {
(err, res, data) => { (err, res, data) => {
if(!err) { if(!err) {
var type = res.headers['content-type']; var type = res.headers['content-type'];
lib.log('MimeType: '+type);
var length = res.headers['content-length']; var length = res.headers['content-length'];
if(cfg.allowedMimes.hasOwnProperty(type)) { if(cfg.allowedMimes.hasOwnProperty(type)) {
//if(length <= cfg.maxFileSize) { //if(length <= cfg.maxFileSize) {

View File

@ -5,11 +5,7 @@ module.exports = (lib) => {
level: 0, level: 0,
active: 1, active: 1,
func: (e) => { func: (e) => {
if(!(user = e.message.match(/^\!user( +@(.*))?/i)[2])) lib.bot.whois(!(user = e.message.match(/^\!user( +@(.*))?/i)[2])?e.user.getNick():user.trim(), e.network, (err, data) => {
user = e.user.getNick();
else
user = user.trim();
lib.bot.whois(user, e.network, (err, data) => {
e.reply(data); e.reply(data);
}); });
}, },

View File

@ -1,324 +1,328 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
var http = require('http'); var http = require('http');
var path = require('path'); var path = require('path');
var swig = require('swig'); var swig = require('swig');
var urlm = require('url'); var urlm = require('url');
var cfg = require('../cfg/websrv.json'); var cfg = require('../cfg/websrv.json');
var exec = require('child_process').exec; var exec = require('child_process').exec;
var templates = {}; var templates = {};
var lib; var lib;
module.exports = Websrv; module.exports = Websrv;
function Websrv(tlib) { function Websrv(tlib) {
this.lib = lib = tlib; this.lib = lib = tlib;
Websrv.prototype.getTpls(); Websrv.prototype.getTpls();
http.createServer((req, res) => { http.createServer((req, res) => {
if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) { if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) {
var filePath = '.' + req.url; var filePath = '.' + req.url;
var url = req.url.split("/")[1]; var url = req.url.split("/")[1];
if(filePath == './') if(filePath == './')
filePath = './index.html'; filePath = './index.html';
if(req.method == 'POST') { if(req.method == 'POST') {
if(filePath == './git' && req.headers['x-gitlab-token'] == cfg.gittoken) { if(filePath == './git' && req.headers['x-gitlab-token'] == cfg.gittoken) {
var body = ''; var body = '';
req.on('data', (data) => { req.on('data', (data) => {
body += data; body += data;
if(body.length > 1e6) if(body.length > 1e6)
req.connection.destroy(); req.connection.destroy();
}); });
req.on('end', () => { req.on('end', () => {
body = JSON.parse(body); body = JSON.parse(body);
var eventname = body.event_name; var eventname = body.event_name;
var autor = body.user_name; var autor = body.user_name;
var branch = body.ref.split('/')[2]; var branch = body.ref.split('/')[2];
if(branch === "master") { if(branch === "master") {
var commit = ""; var commit = "";
try { try {
commit = body.commits[body.commits.length-1].message.replace('\n','').trim(); commit = body.commits[body.commits.length-1].message.replace('\n','').trim();
} }
catch(ex) { catch(ex) {
commit = body.commits[body.commits.length-2].message.replace('\n','').trim(); commit = body.commits[body.commits.length-2].message.replace('\n','').trim();
} }
lib.bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+') in branch '+branch, 'n0xy' ); lib.bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+') in branch '+branch, 'n0xy' );
if( lib.cfg.debugchannel === '#f0ck' ) { if( lib.cfg.debugchannel === '#f0ck' ) {
exec('cd ../ & git pull', (error, stdout) => { exec('cd ../ & git pull', (error, stdout) => {
if(error === null) if(error === null) {
lib.bot.send( lib.cfg.debugchannel, 'git pull suxxessfully.', 'n0xy' ); if(!lib.debug)
}); lib.bot.send(lib.cfg.debugchannel, 'git pull suxxessfully.', 'n0xy');
} else
} lib.log(stdout);
}); }
res.writeHead(200, { 'Content-Type': 'text/html' }); });
res.end('muh', 'utf-8'); }
} }
else { });
res.writeHead(403); res.writeHead(200, { 'Content-Type': 'text/html' });
} res.end('muh', 'utf-8');
} }
else { else {
var extname = String(path.extname(filePath)).toLowerCase(); res.writeHead(403);
var contentType = 'text/html'; }
var mimeTypes = { }
'.html': 'text/html', else {
'.js': 'text/javascript', var extname = String(path.extname(filePath)).toLowerCase();
'.css': 'text/css', var contentType = 'text/html';
'.png': 'image/png', var mimeTypes = {
'.jpg': 'image/jpg', '.html': 'text/html',
'.gif': 'image/gif', '.js': 'text/javascript',
'.mp3': 'audio/mpeg', '.css': 'text/css',
'.mp4': 'video/mp4', '.png': 'image/png',
'.webm': 'video/webm', '.jpg': 'image/jpg',
'.ogg': 'audio/ogg' '.gif': 'image/gif',
}; '.mp3': 'audio/mpeg',
if(filePath == "./index.html") { // mainpage '.mp4': 'video/mp4',
var tpl = swig.compile(templates.index); '.webm': 'video/webm',
var data = { '.ogg': 'audio/ogg'
items: [], };
last: 10000 if(filePath == "./index.html") { // mainpage
}; var tpl = swig.compile(templates.index);
lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => { var data = {
rows.forEach((e,i,a) => { items: [],
data.items.push({ "id": e.id, "mime": e.mime }); last: 10000
data.last = e.id; };
}); lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => {
res.writeHead(200, { 'Content-Type': 'text/html' }); rows.forEach((e,i,a) => {
res.end(tpl(data), 'utf-8'); data.items.push({ "id": e.id, "mime": e.mime });
}); data.last = e.id;
} });
else if(Number.isInteger(parseInt(url))) { // itempage res.writeHead(200, { 'Content-Type': 'text/html' });
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item res.end(tpl(data), 'utf-8');
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item });
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item }
lib.sql.query(query, [url, url, url], (err, rows, fields) => { else if(Number.isInteger(parseInt(url))) { // itempage
var tpl = swig.compile(templates.item); var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
var data = { + "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
id: '', + "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
username: '', lib.sql.query(query, [url, url, url], (err, rows, fields) => {
item: '', var tpl = swig.compile(templates.item);
src: '', var data = {
dest: '', id: '',
mime: '', username: '',
size: '', item: '',
userchannel: '', src: '',
usernetwork: '', dest: '',
thumb: null, mime: '',
next: null, size: '',
prev: null userchannel: '',
}; usernetwork: '',
if(rows[0].length) { thumb: null,
var e = rows[0][0]; next: null,
switch(e.mime) { prev: null
case "image/png": };
case "image/jpeg": if(rows[0].length) {
case "image/gif": var e = rows[0][0];
data.item = 'image'; switch(e.mime) {
break; case "image/png":
case "video/webm": case "image/jpeg":
case "video/mp4": case "image/gif":
data.item = 'video'; data.item = 'image';
break; break;
case "audio/mpeg": case "video/webm":
case "audio/ogg": case "video/mp4":
data.item = 'audio'; data.item = 'video';
break; break;
} case "audio/mpeg":
data.id = e.id; case "audio/ogg":
data.username = e.username; data.item = 'audio';
break;
data.srcurl = e.src; }
data.src = urlm.parse(e.src).hostname; data.id = e.id;
data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null; data.username = e.username;
data.dest = e.dest; data.srcurl = e.src;
data.mime = e.mime; data.src = urlm.parse(e.src).hostname;
data.size = lib.formatSize(e.size); data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null;
data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork; data.dest = e.dest;
data.timestamp = new Date(e.stamp * 1000).toString(); data.mime = e.mime;
if(rows[1].length) data.size = lib.formatSize(e.size);
data.next = rows[1][0].id; data.userchannel = e.userchannel;
if(rows[2].length) data.usernetwork = e.usernetwork;
data.prev = rows[2][0].id; data.timestamp = new Date(e.stamp * 1000).toString();
} if(rows[1].length)
res.writeHead(200, { 'Content-Type': 'text/html' }); data.next = rows[1][0].id;
res.end(tpl(data), 'utf-8'); if(rows[2].length)
}); data.prev = rows[2][0].id;
} }
else if(filePath == "./random") { res.writeHead(200, { 'Content-Type': 'text/html' });
lib.sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => { res.end(tpl(data), 'utf-8');
res.writeHead(301, { });
'Cache-Control': 'no-cache, public', }
'Location': '/' + rows[0].id else if(filePath == "./random") {
}); lib.sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
res.end(); res.writeHead(301, {
}); 'Cache-Control': 'no-cache, public',
} 'Location': '/' + rows[0].id
else if(filePath == "./how") { });
var tpl = swig.compile(templates.how); res.end();
res.writeHead(200, { 'Content-Type': 'text/html' }); });
res.end(tpl(), 'utf-8'); }
} else if(filePath == "./how") {
else if(filePath == "./contact") { var tpl = swig.compile(templates.how);
var tpl = swig.compile(templates.contact); res.writeHead(200, { 'Content-Type': 'text/html' });
res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(tpl(), 'utf-8');
res.end(tpl(), 'utf-8'); }
} else if(filePath == "./contact") {
else if(filePath == "./scripts") { var tpl = swig.compile(templates.contact);
var tpl = swig.compile(templates.scripts); res.writeHead(200, { 'Content-Type': 'text/html' });
res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(tpl(), 'utf-8');
res.end(tpl(), 'utf-8'); }
} else if(filePath == "./scripts") {
else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file var tpl = swig.compile(templates.scripts);
contentType = mimeTypes[extname]; res.writeHead(200, { 'Content-Type': 'text/html' });
switch(contentType) { res.end(tpl(), 'utf-8');
case "video/webm": }
case "video/mp4": else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file
case "audio/mpeg": contentType = mimeTypes[extname];
case "audio/ogg": switch(contentType) {
var start = 0; case "video/webm":
var end = 0; case "video/mp4":
var range = req.headers['range']; case "audio/mpeg":
var stat = fs.statSync(filePath); case "audio/ogg":
if(range != null) { var start = 0;
start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-'))); var end = 0;
end = parseInt(range.slice(range.indexOf('-')+1, range.length)); var range = req.headers['range'];
} var stat = fs.statSync(filePath);
if(isNaN(end) || end == 0) end = stat.size-1; if(range != null) {
if(start > end) return; start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-')));
res.writeHead(206, { end = parseInt(range.slice(range.indexOf('-')+1, range.length));
'Connection':'close', }
'Content-Type':contentType, if(isNaN(end) || end == 0) end = stat.size-1;
'Content-Length':end - start, if(start > end) return;
'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, res.writeHead(206, {
'Transfer-Encoding':'chunked' 'Connection':'close',
}); 'Content-Type':contentType,
var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end}); 'Content-Length':end - start,
stream.pipe(res); 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size,
break; 'Transfer-Encoding':'chunked'
default: });
fs.readFile(filePath, (error, content) => { var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end});
if(error) { stream.pipe(res);
if(error.code == 'ENOENT') { break;
res.writeHead(200, { 'Content-Type': contentType }); default:
res.end('404 - f0ck you', 'utf-8'); fs.readFile(filePath, (error, content) => {
} if(error) {
else { if(error.code == 'ENOENT') {
res.writeHead(500); res.writeHead(200, { 'Content-Type': contentType });
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); res.end('404 - f0ck you', 'utf-8');
res.end(); }
} else {
} res.writeHead(500);
else { res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' }); res.end();
res.end(content, 'utf-8'); }
} }
}); else {
break; res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' });
} res.end(content, 'utf-8');
fs.readFile(filePath, (error, content) => { }
if(error) { });
if(error.code == 'ENOENT') { break;
res.writeHead(200, { 'Content-Type': contentType }); }
res.end('404 - f0ck you', 'utf-8'); fs.readFile(filePath, (error, content) => {
} if(error) {
else { if(error.code == 'ENOENT') {
res.writeHead(500); res.writeHead(200, { 'Content-Type': contentType });
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); res.end('404 - f0ck you', 'utf-8');
res.end(); }
} else {
} res.writeHead(500);
}); res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
} res.end();
else if(filePath.match(/^\.\/api/i)) { // api }
var url = filePath.split('/'); }
if(url[2] === undefined) { // Mainpage });
var query = "select * from `f0ck`.`items`"; }
lib.sql.query(query, (err, rows, fields) => { else if(filePath.match(/^\.\/api/i)) { // api
var items = []; var url = filePath.split('/');
rows.forEach((e,i,a) => { if(url[2] === undefined) { // Mainpage
items.push({ var query = "select * from `f0ck`.`items`";
'id': e.id, lib.sql.query(query, (err, rows, fields) => {
'mime': e.mime var items = [];
}); rows.forEach((e,i,a) => {
}); items.push({
res.writeHead(200, { 'Content-Type': 'text/html' }); 'id': e.id,
res.end(JSON.stringify(items), 'utf-8'); 'mime': e.mime
}); });
} });
else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination res.writeHead(200, { 'Content-Type': 'text/html' });
var eps = 50; res.end(JSON.stringify(items), 'utf-8');
var id = url[3]; });
}
lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => { else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination
var items = { var eps = 50;
"items": [], var id = url[3];
"last": id
}; lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
rows.forEach((e,i,a) => { var items = {
items.items.push({ "items": [],
'id': e.id, "last": id
'mime': e.mime };
}); rows.forEach((e,i,a) => {
items.last = e.id; items.items.push({
}); 'id': e.id,
res.writeHead(200, { 'Content-Type': 'text/html' }); 'mime': e.mime
res.end(JSON.stringify(items), 'utf-8'); });
}); items.last = e.id;
} });
else if(Number.isInteger(parseInt(url[2]))) { // Item res.writeHead(200, { 'Content-Type': 'text/html' });
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item res.end(JSON.stringify(items), 'utf-8');
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item });
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item }
lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => { else if(Number.isInteger(parseInt(url[2]))) { // Item
var data; var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
if(rows[0].length) { + "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
var e = rows[0][0]; + "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
data = { lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => {
id: e.id, var data;
username: e.username, if(rows[0].length) {
src: e.src, var e = rows[0][0];
dest: e.dest, data = {
mime: e.mime, id: e.id,
size: e.size, username: e.username,
userchannel: e.userchannel, src: e.src,
usernetwork: e.usernetwork, dest: e.dest,
next: null, mime: e.mime,
prev: null size: e.size,
}; userchannel: e.userchannel,
if(rows[1].length) usernetwork: e.usernetwork,
data.next = rows[1][0].id; next: null,
if(rows[2].length) prev: null
data.prev = rows[2][0].id; };
} if(rows[1].length)
else data.next = rows[1][0].id;
data = { error: 'nope' }; if(rows[2].length)
res.writeHead(200, { 'Content-Type': 'text/html' }); data.prev = rows[2][0].id;
res.end(JSON.stringify(data), 'utf-8'); }
}); else
} data = { error: 'nope' };
} res.writeHead(200, { 'Content-Type': 'text/html' });
else { // errorpage res.end(JSON.stringify(data), 'utf-8');
res.writeHead(404); });
res.end('404 - f0ck you', 'utf-8'); }
} }
} else { // errorpage
} res.writeHead(404);
else { res.end('404 - f0ck you', 'utf-8');
res.writeHead(403); }
res.end('403 - forbidden'); }
} }
}).listen(cfg.port); else {
} res.writeHead(403);
res.end('403 - forbidden');
Websrv.prototype.getTpls = () => { }
templates = { }).listen(cfg.port);
"index": fs.readFileSync("./s/index.tpl.html", "utf-8"), }
"item": fs.readFileSync("./s/item.tpl.html", "utf-8"),
"how": fs.readFileSync("./s/how.tpl.html", "utf-8"), Websrv.prototype.getTpls = () => {
"contact": fs.readFileSync("./s/contact.tpl.html", "utf-8") templates = {
}; "index": fs.readFileSync("./s/index.tpl.html", "utf-8"),
"item": fs.readFileSync("./s/item.tpl.html", "utf-8"),
"how": fs.readFileSync("./s/how.tpl.html", "utf-8"),
"contact": fs.readFileSync("./s/contact.tpl.html", "utf-8")
};
}; };