exported webserver

This commit is contained in:
Flummi 2016-08-23 17:30:35 +00:00
parent 984e5a9d24
commit f1ce2ebc75
5 changed files with 212 additions and 219 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "keinBotv2", "name": "keinBotv2",
"version": "0.1.0", "version": "0.1.1",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {
"start": "node src/main.js" "start": "node src/main.js"

View File

@ -3,9 +3,7 @@ var http = require('http');
var https = require('https'); var https = require('https');
var exec = require('child_process').exec; var exec = require('child_process').exec;
var probe = require('node-ffprobe'); var probe = require('node-ffprobe');
var uuid = require('uuid');
var crypto = require('crypto'); var crypto = require('crypto');
var path = require('path');
var bot, sql, cfg; var bot, sql, cfg;
var debug = true; var debug = true;
@ -21,7 +19,6 @@ function Lib(tbot, tsql, tcfg) {
Lib.prototype.getUser = (e, cbgu) => { Lib.prototype.getUser = (e, cbgu) => {
var u = e.user.getNick(); var u = e.user.getNick();
var n = e.network; var n = e.network;
//console.log(e.user);
if(!e.user.hasOwnProperty('hostname')) { if(!e.user.hasOwnProperty('hostname')) {
bot.write('WHOIS '+u, n, () => { bot.write('WHOIS '+u, n, () => {
bot.once('data', (err, msg) => { bot.once('data', (err, msg) => {

View File

@ -1,18 +1,10 @@
var cfg = require('../../cfg.json'); var cfg = require('../../cfg.json');
var fs = require('fs-extra'); var fs = require('fs-extra');
var http = require('http');
var https = require('https');
var mysql = require('mysql'); var mysql = require('mysql');
var bot = require('coffea')(); var bot = require('coffea')();
var crypto = require('crypto');
var path = require('path');
var exec = require('child_process').exec;
var probe = require('node-ffprobe');
var Lib = require('./lib.js'); var Lib = require('./lib.js');
var swig = require('swig'); var Websrv = require('./websrv.js');
var templates = {};
var debug = false;
var sql; var sql;
var haDC = () => { var haDC = () => {
@ -27,6 +19,7 @@ var haDC = () => {
haDC(); haDC();
var lib = new Lib(bot, sql, cfg); var lib = new Lib(bot, sql, cfg);
var websrv = new Websrv(bot, sql, cfg);
cfg.server.forEach((e,i,a) => { cfg.server.forEach((e,i,a) => {
bot.add({ bot.add({
@ -45,19 +38,14 @@ cfg.server.forEach((e,i,a) => {
bot.on('motd', (e) => { bot.on('motd', (e) => {
console.log("motd von "+e.network+" erhalten"); console.log("motd von "+e.network+" erhalten");
//bot.join("#kbot-dev"); // tmp
bot.write('MODE f0ckyou +B', e.network, (c)=>{}); // Botflag
}); });
bot.on('message', (e) => { bot.on('message', (e) => {
trigger.trigger.forEach((entry,i,a) => { trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call)) { if(e.message.match(entry.call)) {
lib.getUserlevel(e, (cb) => { lib.getUserlevel(e, (cb) => {
if(cb.level >= entry.level) { if(cb.level >= entry.level)
//setTimeout(()=>{
entry.func(e); entry.func(e);
//}, 100);
}
else else
e.reply('no permission, min level '+entry.level+' required'); e.reply('no permission, min level '+entry.level+' required');
}); });
@ -65,207 +53,10 @@ bot.on('message', (e) => {
}); });
}); });
// Webserver
http.createServer((req, res) => {
//console.log(req.connection.remoteAddress);
if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) {
var filePath = '.' + req.url;
var url = req.url.split("/")[1];
if(filePath == './')
filePath = './index.html';
var extname = String(path.extname(filePath)).toLowerCase();
var contentType = 'text/html';
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.mp3': 'audio/mpeg',
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.css': 'text/css',
'.ogg': 'audio/ogg'
};
if(filePath == "./index.html") { // mainpage
var tpl = swig.compile(templates.index);
var data = { items: [] };
sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc", (err, rows, fields) => {
rows.forEach((e,i,a) => {
data.items.push({ "id": e.id, "mime": e.mime });
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(data), 'utf-8');
});
}
else if(Number.isInteger(parseInt(url))) { // itempage
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "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
sql.query(query, [url, url, url], (err, rows, fields) => {
var tpl = swig.compile(templates.item);
var data = {
id: '',
username: '',
item: '',
src: '',
dest: '',
mime: '',
size: '',
userchannel: '',
usernetwork: '',
next: null,
prev: null
};
if(rows[0].length) {
var e = rows[0][0];
switch(e.mime) {
case "image/png":
case "image/jpeg":
case "image/gif":
data.item = 'image';
break;
case "video/webm":
case "video/mp4":
data.item = 'video';
break;
case "audio/mpeg":
case "audio/ogg":
data.item = 'audio';
break;
}
data.id = e.id;
data.username = e.username;
data.src = e.src;
data.dest = e.dest;
data.mime = e.mime;
data.size = lib.formatSize(e.size);
data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork;
if(rows[1].length)
data.next = rows[1][0].id;
if(rows[2].length)
data.prev = rows[2][0].id;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(data), 'utf-8');
});
}
else if(filePath == "./random") {
sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
res.writeHead(301, {
'Cache-Control': 'no-cache, public',
'Location': '/' + rows[0].id
});
res.end();
});
}
else if(filePath == "./how") {
var tpl = swig.compile(templates.how);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./contact") {
var tpl = swig.compile(templates.contact);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./scripts") {
var tpl = swig.compile(templates.scripts);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file
contentType = mimeTypes[extname];
switch(contentType) {
case "video/webm":
case "video/mp4":
case "audio/mpeg":
case "audio/ogg":
var start = 0;
var end = 0;
var range = req.headers['range'];
var stat = fs.statSync(filePath);
if(range != null) {
start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-')));
end = parseInt(range.slice(range.indexOf('-')+1, range.length));
}
if(isNaN(end) || end == 0) end = stat.size-1;
if(start > end) return;
res.writeHead(206, {
'Connection':'close',
'Content-Type':contentType,
'Content-Length':end - start,
'Content-Range':'bytes '+start+'-'+end+'/'+stat.size,
'Transfer-Encoding':'chunked'
});
var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end});
stream.pipe(res);
break;
default:
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
else {
res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' });
res.end(content, 'utf-8');
}
});
break;
}
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
else {
}
});
}
else { // errorpage
res.writeHead(404);
res.end('404 - f0ck you', 'utf-8');
}
}
else {
res.writeHead(403);
res.end('403 - forbidden');
}
}).listen(cfg.webserver.port);
var getTpls = () => {
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"),
"scripts": fs.readFileSync("./s/scripts.tpl.html", "utf-8")
};
};
lib.loadUser((cb)=>{ lib.loadUser((cb)=>{
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"); console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
}); });
getTpls(); websrv.getTpls();
var trigger = { var trigger = {
trigger: [], trigger: [],

205
src/websrv.js Normal file
View File

@ -0,0 +1,205 @@
var fs = require('fs-extra');
var http = require('http');
var path = require('path');
var swig = require('swig');
var templates = {};
var bot, sql, cfg;
module.exports = Websrv;
function Websrv(tbot, tsql, tcfg) {
this.bot = bot = tbot;
this.sql = sql = tsql;
this.cfg = cfg = tcfg;
http.createServer((req, res) => {
if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) {
var filePath = '.' + req.url;
var url = req.url.split("/")[1];
if(filePath == './')
filePath = './index.html';
var extname = String(path.extname(filePath)).toLowerCase();
var contentType = 'text/html';
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.mp3': 'audio/mpeg',
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.css': 'text/css',
'.ogg': 'audio/ogg'
};
if(filePath == "./index.html") { // mainpage
var tpl = swig.compile(templates.index);
var data = { items: [] };
sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc", (err, rows, fields) => {
rows.forEach((e,i,a) => {
data.items.push({ "id": e.id, "mime": e.mime });
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(data), 'utf-8');
});
}
else if(Number.isInteger(parseInt(url))) { // itempage
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "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
sql.query(query, [url, url, url], (err, rows, fields) => {
var tpl = swig.compile(templates.item);
var data = {
id: '',
username: '',
item: '',
src: '',
dest: '',
mime: '',
size: '',
userchannel: '',
usernetwork: '',
next: null,
prev: null
};
if(rows[0].length) {
var e = rows[0][0];
switch(e.mime) {
case "image/png":
case "image/jpeg":
case "image/gif":
data.item = 'image';
break;
case "video/webm":
case "video/mp4":
data.item = 'video';
break;
case "audio/mpeg":
case "audio/ogg":
data.item = 'audio';
break;
}
data.id = e.id;
data.username = e.username;
data.src = e.src;
data.dest = e.dest;
data.mime = e.mime;
data.size = lib.formatSize(e.size);
data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork;
if(rows[1].length)
data.next = rows[1][0].id;
if(rows[2].length)
data.prev = rows[2][0].id;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(data), 'utf-8');
});
}
else if(filePath == "./random") {
sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
res.writeHead(301, {
'Cache-Control': 'no-cache, public',
'Location': '/' + rows[0].id
});
res.end();
});
}
else if(filePath == "./how") {
var tpl = swig.compile(templates.how);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./contact") {
var tpl = swig.compile(templates.contact);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./scripts") {
var tpl = swig.compile(templates.scripts);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file
contentType = mimeTypes[extname];
switch(contentType) {
case "video/webm":
case "video/mp4":
case "audio/mpeg":
case "audio/ogg":
var start = 0;
var end = 0;
var range = req.headers['range'];
var stat = fs.statSync(filePath);
if(range != null) {
start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-')));
end = parseInt(range.slice(range.indexOf('-')+1, range.length));
}
if(isNaN(end) || end == 0) end = stat.size-1;
if(start > end) return;
res.writeHead(206, {
'Connection':'close',
'Content-Type':contentType,
'Content-Length':end - start,
'Content-Range':'bytes '+start+'-'+end+'/'+stat.size,
'Transfer-Encoding':'chunked'
});
var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end});
stream.pipe(res);
break;
default:
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
else {
res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' });
res.end(content, 'utf-8');
}
});
break;
}
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
});
}
else { // errorpage
res.writeHead(404);
res.end('404 - f0ck you', 'utf-8');
}
}
else {
res.writeHead(403);
res.end('403 - forbidden');
}
}).listen(cfg.webserver.port);
}
Websrv.prototype.getTpls = () => {
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"),
"scripts": fs.readFileSync("./s/scripts.tpl.html", "utf-8")
};
};

View File

@ -1,2 +1,2 @@
#!/bin/bash #!/bin/bash
tmux new -d -s bot 'node src/main.js'; tmux new -d -s bot 'npm start';