From 42b84c134c7bdfa26d6f07bd78537ba7fe33ebb2 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 23 Oct 2016 00:10:45 +0200 Subject: [PATCH 1/8] verbose output if debug is activated --- src/trigger/gitpull.js | 6 ++++-- src/websrv.js | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/trigger/gitpull.js b/src/trigger/gitpull.js index 07f813c..616010f 100644 --- a/src/trigger/gitpull.js +++ b/src/trigger/gitpull.js @@ -9,8 +9,10 @@ module.exports = (lib) => { func: (e) => { exec('cd ../../ & git pull', (error, stdout) => { if(error === null) { - e.reply( 'git pull suxxessfully.' ); - //e.reply(stdout); + if(!lib.debug) + e.reply('git pull suxxessfully.'); + else + lib.log(stdout); } }); }, diff --git a/src/websrv.js b/src/websrv.js index 1a63828..9ccec2e 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -46,8 +46,12 @@ function Websrv(tlib) { lib.bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+') in branch '+branch, 'n0xy' ); if( lib.cfg.debugchannel === '#f0ck' ) { exec('cd ../ & git pull', (error, stdout) => { - if(error === null) - lib.bot.send( lib.cfg.debugchannel, 'git pull suxxessfully.', 'n0xy' ); + if(error === null) { + if(!lib.debug) + lib.bot.send(lib.cfg.debugchannel, 'git pull suxxessfully.', 'n0xy'); + else + lib.log(stdout); + } }); } } From 8c0551cf9ae7478297905e810ec864ef5d2ddaf6 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 23 Oct 2016 00:35:32 +0200 Subject: [PATCH 2/8] webservertest --- package.json | 1 + src/websrv.js | 346 +++++--------------------------------------------- 2 files changed, 32 insertions(+), 315 deletions(-) diff --git a/package.json b/package.json index a6a0772..68e6cf7 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "file-type": "^3.8.0", "forever-monitor": "^1.7.0", "fs-extra": "^0.30.0", + "hapi": "^15.2.0", "mysql": "^2.11.1", "read-chunk": "^2.0.0", "request": "^2.74.0", diff --git a/src/websrv.js b/src/websrv.js index 9ccec2e..99ea7c7 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -1,10 +1,7 @@ -var fs = require('fs-extra'); -var http = require('http'); -var path = require('path'); -var swig = require('swig'); -var urlm = require('url'); +var hapi = require('hapi'); var cfg = require('../cfg/websrv.json'); -var exec = require('child_process').exec; + +const server = new hapi.Server(); var templates = {}; var lib; @@ -13,316 +10,35 @@ module.exports = Websrv; function Websrv(tlib) { this.lib = lib = tlib; - Websrv.prototype.getTpls(); + server.connection({ + port: cfg.port + }); - 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'; - if(req.method == 'POST') { - if(filePath == './git' && req.headers['x-gitlab-token'] == cfg.gittoken) { - var body = ''; - req.on('data', (data) => { - body += data; - if(body.length > 1e6) - req.connection.destroy(); - }); - req.on('end', () => { - body = JSON.parse(body); - var eventname = body.event_name; - var autor = body.user_name; - var branch = body.ref.split('/')[2]; - - if(branch === "master") { - var commit = ""; - try { - commit = body.commits[body.commits.length-1].message.replace('\n','').trim(); - } - catch(ex) { - 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' ); - if( lib.cfg.debugchannel === '#f0ck' ) { - exec('cd ../ & git pull', (error, stdout) => { - if(error === null) { - 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); - } - } - else { - 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', - '.ogg': 'audio/ogg' - }; - if(filePath == "./index.html") { // mainpage - var tpl = swig.compile(templates.index); - var data = { - items: [], - last: 10000 - }; - lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => { - rows.forEach((e,i,a) => { - data.items.push({ "id": e.id, "mime": e.mime }); - data.last = e.id; - }); - 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 - lib.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: '', - thumb: null, - 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.srcurl = e.src; - data.src = urlm.parse(e.src).hostname; - data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null; - - data.dest = e.dest; - data.mime = e.mime; - data.size = lib.formatSize(e.size); - data.userchannel = e.userchannel; - data.usernetwork = e.usernetwork; - data.timestamp = new Date(e.stamp * 1000).toString(); - 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") { - lib.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 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) => { - var items = []; - rows.forEach((e,i,a) => { - items.push({ - 'id': e.id, - 'mime': e.mime - }); - }); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(JSON.stringify(items), 'utf-8'); - }); - } - else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination - var eps = 50; - var id = url[3]; - - lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => { - var items = { - "items": [], - "last": id - }; - rows.forEach((e,i,a) => { - items.items.push({ - 'id': e.id, - 'mime': e.mime - }); - items.last = e.id; - }); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(JSON.stringify(items), 'utf-8'); - }); - } - else if(Number.isInteger(parseInt(url[2]))) { // Item - 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 - lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => { - var data; - if(rows[0].length) { - var e = rows[0][0]; - data = { - id: e.id, - username: e.username, - src: e.src, - dest: e.dest, - mime: e.mime, - size: e.size, - userchannel: e.userchannel, - usernetwork: e.usernetwork, - next: null, - prev: null - }; - if(rows[1].length) - data.next = rows[1][0].id; - if(rows[2].length) - data.prev = rows[2][0].id; - } - else - data = { error: 'nope' }; - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(JSON.stringify(data), 'utf-8'); - }); - } - } - else { // errorpage - res.writeHead(404); - res.end('404 - f0ck you', 'utf-8'); + server.route({ + method: 'GET', + path: '/', + handler: function (req, reply) { + return reply('hello world'); + } + }); + server.route({ + method: 'GET', + path: '/{ID}', + handler: function (req, reply) { + return reply('ID: ' + encodeURIComponent(req.params.ID)); + }, + config: { + validate: { + params: { + ID: Joi.number() } } } - else { - res.writeHead(403); - res.end('403 - forbidden'); - } - }).listen(cfg.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") - }; -}; \ No newline at end of file + }); + + server.start((err) => { + if(err) + throw err; + lib.bot.send(lib.cfg.debugchannel, 'Server running at:', server.info.uri, 'n0xy'); + }); +} \ No newline at end of file From 062a351d1861ed6c00f047d4018c5cbac6c6be45 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 23 Oct 2016 00:39:23 +0200 Subject: [PATCH 3/8] modified: package.json modified: src/websrv.js --- package.json | 1 + src/websrv.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 68e6cf7..24fbeff 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "forever-monitor": "^1.7.0", "fs-extra": "^0.30.0", "hapi": "^15.2.0", + "joi": "^9.2.0", "mysql": "^2.11.1", "read-chunk": "^2.0.0", "request": "^2.74.0", diff --git a/src/websrv.js b/src/websrv.js index 99ea7c7..7da7a2f 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -1,4 +1,5 @@ var hapi = require('hapi'); +var joi = require('joi'); var cfg = require('../cfg/websrv.json'); const server = new hapi.Server(); @@ -30,7 +31,7 @@ function Websrv(tlib) { config: { validate: { params: { - ID: Joi.number() + ID: joi.number() } } } From 503d9708b218c2b7761d7fb260dc5b0f3676d75b Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 23 Oct 2016 00:43:24 +0200 Subject: [PATCH 4/8] ups --- src/websrv.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websrv.js b/src/websrv.js index 7da7a2f..8759667 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -40,6 +40,6 @@ function Websrv(tlib) { server.start((err) => { if(err) throw err; - lib.bot.send(lib.cfg.debugchannel, 'Server running at:', server.info.uri, 'n0xy'); + console.log('Server running at:', server.info.uri); }); } \ No newline at end of file From 4600d8e670ab07ffaae45750d7493ff33ca39996 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 23 Oct 2016 00:47:22 +0200 Subject: [PATCH 5/8] modified: src/main.js new file: src/websrv.bak.js modified: src/websrv.js --- src/main.js | 4 +- src/websrv.bak.js | 328 ++++++++++++++++++++++++++++++++++++++++++++++ src/websrv.js | 2 +- 3 files changed, 332 insertions(+), 2 deletions(-) create mode 100644 src/websrv.bak.js diff --git a/src/main.js b/src/main.js index 6633744..05f5ab3 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,7 @@ var Lib = require('./lib.js'); var Websrv = require('./websrv.js'); +var Websrvbak = require('./websrv.bak.js'); // devtmp var lib = new Lib(); -var websrv = new Websrv(lib); \ No newline at end of file +var websrv = new Websrv(lib); +var websrvbak = new Websrvbak(lib); // devtmp \ No newline at end of file diff --git a/src/websrv.bak.js b/src/websrv.bak.js new file mode 100644 index 0000000..82bba93 --- /dev/null +++ b/src/websrv.bak.js @@ -0,0 +1,328 @@ +var fs = require('fs-extra'); +var http = require('http'); +var path = require('path'); +var swig = require('swig'); +var urlm = require('url'); +var cfg = require('../cfg/websrv.json'); +var exec = require('child_process').exec; + +var templates = {}; +var lib; + +module.exports = Websrv; +function Websrv(tlib) { + this.lib = lib = tlib; + + Websrv.prototype.getTpls(); + + 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'; + if(req.method == 'POST') { + if(filePath == './git' && req.headers['x-gitlab-token'] == cfg.gittoken) { + var body = ''; + req.on('data', (data) => { + body += data; + if(body.length > 1e6) + req.connection.destroy(); + }); + req.on('end', () => { + body = JSON.parse(body); + var eventname = body.event_name; + var autor = body.user_name; + var branch = body.ref.split('/')[2]; + + if(branch === "master") { + var commit = ""; + try { + commit = body.commits[body.commits.length-1].message.replace('\n','').trim(); + } + catch(ex) { + 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' ); + if( lib.cfg.debugchannel === '#f0ck' ) { + exec('cd ../ & git pull', (error, stdout) => { + if(error === null) { + 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); + } + } + else { + 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', + '.ogg': 'audio/ogg' + }; + if(filePath == "./index.html") { // mainpage + var tpl = swig.compile(templates.index); + var data = { + items: [], + last: 10000 + }; + lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => { + rows.forEach((e,i,a) => { + data.items.push({ "id": e.id, "mime": e.mime }); + data.last = e.id; + }); + 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 + lib.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: '', + thumb: null, + 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.srcurl = e.src; + data.src = urlm.parse(e.src).hostname; + data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null; + + data.dest = e.dest; + data.mime = e.mime; + data.size = lib.formatSize(e.size); + data.userchannel = e.userchannel; + data.usernetwork = e.usernetwork; + data.timestamp = new Date(e.stamp * 1000).toString(); + 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") { + lib.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 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) => { + var items = []; + rows.forEach((e,i,a) => { + items.push({ + 'id': e.id, + 'mime': e.mime + }); + }); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(items), 'utf-8'); + }); + } + else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination + var eps = 50; + var id = url[3]; + + lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => { + var items = { + "items": [], + "last": id + }; + rows.forEach((e,i,a) => { + items.items.push({ + 'id': e.id, + 'mime': e.mime + }); + items.last = e.id; + }); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(items), 'utf-8'); + }); + } + else if(Number.isInteger(parseInt(url[2]))) { // Item + 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 + lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => { + var data; + if(rows[0].length) { + var e = rows[0][0]; + data = { + id: e.id, + username: e.username, + src: e.src, + dest: e.dest, + mime: e.mime, + size: e.size, + userchannel: e.userchannel, + usernetwork: e.usernetwork, + next: null, + prev: null + }; + if(rows[1].length) + data.next = rows[1][0].id; + if(rows[2].length) + data.prev = rows[2][0].id; + } + else + data = { error: 'nope' }; + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(data), 'utf-8'); + }); + } + } + else { // errorpage + res.writeHead(404); + res.end('404 - f0ck you', 'utf-8'); + } + } + } + else { + res.writeHead(403); + res.end('403 - forbidden'); + } + }).listen(cfg.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") + }; +}; \ No newline at end of file diff --git a/src/websrv.js b/src/websrv.js index 8759667..392896c 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -12,7 +12,7 @@ function Websrv(tlib) { this.lib = lib = tlib; server.connection({ - port: cfg.port + port: cfg.port+1 }); server.route({ From 5f653ff6506ba32910fa332f17a0c5dc7ef6f596 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 25 Oct 2016 14:55:22 +0200 Subject: [PATCH 6/8] =?UTF-8?q?=C3=9Cbersich..was=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/trigger/user.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/trigger/user.js b/src/trigger/user.js index 1413c9c..faba814 100644 --- a/src/trigger/user.js +++ b/src/trigger/user.js @@ -5,11 +5,7 @@ module.exports = (lib) => { level: 0, active: 1, func: (e) => { - if(!(user = e.message.match(/^\!user( +@(.*))?/i)[2])) - user = e.user.getNick(); - else - user = user.trim(); - lib.bot.whois(user, e.network, (err, data) => { + lib.bot.whois(!(user = e.message.match(/^\!user( +@(.*))?/i)[2])?e.user.getNick():user.trim(), e.network, (err, data) => { e.reply(data); }); }, From 5aabcb67115c0eceddae48ca8acdeb5974e1c50d Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 26 Oct 2016 15:46:23 +0200 Subject: [PATCH 7/8] MimeType in Debug --- src/trigger/parser.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/trigger/parser.js b/src/trigger/parser.js index 3afdc28..b300145 100644 --- a/src/trigger/parser.js +++ b/src/trigger/parser.js @@ -138,6 +138,7 @@ module.exports = (lib) => { (err, res, data) => { if(!err) { var type = res.headers['content-type']; + lib.log('MimeType: '+type); var length = res.headers['content-length']; if(cfg.allowedMimes.hasOwnProperty(type)) { //if(length <= cfg.maxFileSize) { From 73a9a90d4393a79fff958b7e34b4246f4450e5d9 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 26 Oct 2016 15:48:51 +0200 Subject: [PATCH 8/8] muh --- package.json | 2 - src/main.js | 4 +- src/websrv.bak.js | 328 ---------------------------------------- src/websrv.js | 373 ++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 329 insertions(+), 378 deletions(-) delete mode 100644 src/websrv.bak.js diff --git a/package.json b/package.json index 24fbeff..a6a0772 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,6 @@ "file-type": "^3.8.0", "forever-monitor": "^1.7.0", "fs-extra": "^0.30.0", - "hapi": "^15.2.0", - "joi": "^9.2.0", "mysql": "^2.11.1", "read-chunk": "^2.0.0", "request": "^2.74.0", diff --git a/src/main.js b/src/main.js index 05f5ab3..6633744 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,5 @@ var Lib = require('./lib.js'); var Websrv = require('./websrv.js'); -var Websrvbak = require('./websrv.bak.js'); // devtmp var lib = new Lib(); -var websrv = new Websrv(lib); -var websrvbak = new Websrvbak(lib); // devtmp \ No newline at end of file +var websrv = new Websrv(lib); \ No newline at end of file diff --git a/src/websrv.bak.js b/src/websrv.bak.js deleted file mode 100644 index 82bba93..0000000 --- a/src/websrv.bak.js +++ /dev/null @@ -1,328 +0,0 @@ -var fs = require('fs-extra'); -var http = require('http'); -var path = require('path'); -var swig = require('swig'); -var urlm = require('url'); -var cfg = require('../cfg/websrv.json'); -var exec = require('child_process').exec; - -var templates = {}; -var lib; - -module.exports = Websrv; -function Websrv(tlib) { - this.lib = lib = tlib; - - Websrv.prototype.getTpls(); - - 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'; - if(req.method == 'POST') { - if(filePath == './git' && req.headers['x-gitlab-token'] == cfg.gittoken) { - var body = ''; - req.on('data', (data) => { - body += data; - if(body.length > 1e6) - req.connection.destroy(); - }); - req.on('end', () => { - body = JSON.parse(body); - var eventname = body.event_name; - var autor = body.user_name; - var branch = body.ref.split('/')[2]; - - if(branch === "master") { - var commit = ""; - try { - commit = body.commits[body.commits.length-1].message.replace('\n','').trim(); - } - catch(ex) { - 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' ); - if( lib.cfg.debugchannel === '#f0ck' ) { - exec('cd ../ & git pull', (error, stdout) => { - if(error === null) { - 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); - } - } - else { - 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', - '.ogg': 'audio/ogg' - }; - if(filePath == "./index.html") { // mainpage - var tpl = swig.compile(templates.index); - var data = { - items: [], - last: 10000 - }; - lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => { - rows.forEach((e,i,a) => { - data.items.push({ "id": e.id, "mime": e.mime }); - data.last = e.id; - }); - 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 - lib.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: '', - thumb: null, - 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.srcurl = e.src; - data.src = urlm.parse(e.src).hostname; - data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null; - - data.dest = e.dest; - data.mime = e.mime; - data.size = lib.formatSize(e.size); - data.userchannel = e.userchannel; - data.usernetwork = e.usernetwork; - data.timestamp = new Date(e.stamp * 1000).toString(); - 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") { - lib.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 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) => { - var items = []; - rows.forEach((e,i,a) => { - items.push({ - 'id': e.id, - 'mime': e.mime - }); - }); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(JSON.stringify(items), 'utf-8'); - }); - } - else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination - var eps = 50; - var id = url[3]; - - lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => { - var items = { - "items": [], - "last": id - }; - rows.forEach((e,i,a) => { - items.items.push({ - 'id': e.id, - 'mime': e.mime - }); - items.last = e.id; - }); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(JSON.stringify(items), 'utf-8'); - }); - } - else if(Number.isInteger(parseInt(url[2]))) { // Item - 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 - lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => { - var data; - if(rows[0].length) { - var e = rows[0][0]; - data = { - id: e.id, - username: e.username, - src: e.src, - dest: e.dest, - mime: e.mime, - size: e.size, - userchannel: e.userchannel, - usernetwork: e.usernetwork, - next: null, - prev: null - }; - if(rows[1].length) - data.next = rows[1][0].id; - if(rows[2].length) - data.prev = rows[2][0].id; - } - else - data = { error: 'nope' }; - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(JSON.stringify(data), 'utf-8'); - }); - } - } - else { // errorpage - res.writeHead(404); - res.end('404 - f0ck you', 'utf-8'); - } - } - } - else { - res.writeHead(403); - res.end('403 - forbidden'); - } - }).listen(cfg.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") - }; -}; \ No newline at end of file diff --git a/src/websrv.js b/src/websrv.js index 392896c..82bba93 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -1,45 +1,328 @@ -var hapi = require('hapi'); -var joi = require('joi'); -var cfg = require('../cfg/websrv.json'); - -const server = new hapi.Server(); - -var templates = {}; -var lib; - -module.exports = Websrv; -function Websrv(tlib) { - this.lib = lib = tlib; - - server.connection({ - port: cfg.port+1 - }); - - server.route({ - method: 'GET', - path: '/', - handler: function (req, reply) { - return reply('hello world'); - } - }); - server.route({ - method: 'GET', - path: '/{ID}', - handler: function (req, reply) { - return reply('ID: ' + encodeURIComponent(req.params.ID)); - }, - config: { - validate: { - params: { - ID: joi.number() - } - } - } - }); - - server.start((err) => { - if(err) - throw err; - console.log('Server running at:', server.info.uri); - }); -} \ No newline at end of file +var fs = require('fs-extra'); +var http = require('http'); +var path = require('path'); +var swig = require('swig'); +var urlm = require('url'); +var cfg = require('../cfg/websrv.json'); +var exec = require('child_process').exec; + +var templates = {}; +var lib; + +module.exports = Websrv; +function Websrv(tlib) { + this.lib = lib = tlib; + + Websrv.prototype.getTpls(); + + 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'; + if(req.method == 'POST') { + if(filePath == './git' && req.headers['x-gitlab-token'] == cfg.gittoken) { + var body = ''; + req.on('data', (data) => { + body += data; + if(body.length > 1e6) + req.connection.destroy(); + }); + req.on('end', () => { + body = JSON.parse(body); + var eventname = body.event_name; + var autor = body.user_name; + var branch = body.ref.split('/')[2]; + + if(branch === "master") { + var commit = ""; + try { + commit = body.commits[body.commits.length-1].message.replace('\n','').trim(); + } + catch(ex) { + 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' ); + if( lib.cfg.debugchannel === '#f0ck' ) { + exec('cd ../ & git pull', (error, stdout) => { + if(error === null) { + 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); + } + } + else { + 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', + '.ogg': 'audio/ogg' + }; + if(filePath == "./index.html") { // mainpage + var tpl = swig.compile(templates.index); + var data = { + items: [], + last: 10000 + }; + lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => { + rows.forEach((e,i,a) => { + data.items.push({ "id": e.id, "mime": e.mime }); + data.last = e.id; + }); + 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 + lib.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: '', + thumb: null, + 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.srcurl = e.src; + data.src = urlm.parse(e.src).hostname; + data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null; + + data.dest = e.dest; + data.mime = e.mime; + data.size = lib.formatSize(e.size); + data.userchannel = e.userchannel; + data.usernetwork = e.usernetwork; + data.timestamp = new Date(e.stamp * 1000).toString(); + 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") { + lib.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 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) => { + var items = []; + rows.forEach((e,i,a) => { + items.push({ + 'id': e.id, + 'mime': e.mime + }); + }); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(items), 'utf-8'); + }); + } + else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination + var eps = 50; + var id = url[3]; + + lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => { + var items = { + "items": [], + "last": id + }; + rows.forEach((e,i,a) => { + items.items.push({ + 'id': e.id, + 'mime': e.mime + }); + items.last = e.id; + }); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(items), 'utf-8'); + }); + } + else if(Number.isInteger(parseInt(url[2]))) { // Item + 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 + lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => { + var data; + if(rows[0].length) { + var e = rows[0][0]; + data = { + id: e.id, + username: e.username, + src: e.src, + dest: e.dest, + mime: e.mime, + size: e.size, + userchannel: e.userchannel, + usernetwork: e.usernetwork, + next: null, + prev: null + }; + if(rows[1].length) + data.next = rows[1][0].id; + if(rows[2].length) + data.prev = rows[2][0].id; + } + else + data = { error: 'nope' }; + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(data), 'utf-8'); + }); + } + } + else { // errorpage + res.writeHead(404); + res.end('404 - f0ck you', 'utf-8'); + } + } + } + else { + res.writeHead(403); + res.end('403 - forbidden'); + } + }).listen(cfg.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") + }; +}; \ No newline at end of file