Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const bodyParser = require('body-parser');
- const url = require('url');
- const Jimp = require('jimp');
- const fs = require('fs');
- const path = require('path');
- const crypto = require('crypto');
- const pip = require('public-ip');
- const port = 11111;
- const password = 'wC2Gs@cHG_artybyte_gBLcW7@d$s^';
- const FGBLACK = "\x1b[30m"
- const FGRED = "\x1b[31m"
- const FBGREEN = "\x1b[32m"
- const FGYELLOW = "\x1b[33m"
- const FGBLUE = "\x1b[34m"
- const FGMAGENTA = "\x1b[35m"
- const FGCYAN = "\x1b[36m"
- const FGWHITE = "\x1b[37m"
- const BGBLACK = "\x1b[40m"
- const BGRED = "\x1b[41m"
- const BGGREEN = "\x1b[42m"
- const BGYELLOW = "\x1b[43m"
- const BGBLUE = "\x1b[44m"
- const BGMAGENTA = "\x1b[45m"
- const BGCYAN = "\x1b[46m"
- const BGWHITE = "\x1b[47m"
- const EOL = '\x1b[0m';
- var PUBLIC_IP = "";
- pip.v4().then(ip => {
- PUBLIC_IP = ip;
- });
- // Create a new instance of express
- const app = express();
- const BaseDirectory = path.join(__dirname, 'public');
- const WorkDir = "public/repository/";
- // Tell express to use the body-parser middleware and to not parse extended bodies
- app.use(bodyParser.urlencoded({ extended: true, limit: "50mb" }));
- app.use(express.static(BaseDirectory));
- function GUD(sid){ // get user directory
- return crypto.createHash('md5').update(sid).digest('hex');
- }
- function validateClientFolder(sid, username){
- // var hash = crypto.createHash('md5').update(name).digest('hex');
- var hash = GUD(sid);
- var dir = path.join(WorkDir, hash);
- if (!fs.existsSync(dir)) {
- fs.mkdirSync(dir);
- }
- let jsonFile = path.join(dir, 'manifest.json');
- if (!fs.existsSync(jsonFile)){
- var arr = new Array();
- arr[0] = {};
- var pics = [];
- var p = path.join(WorkDir, GUD(sid));
- if (fs.existsSync(p)){
- fs.readdir(p, (err, files) => {
- files.forEach((file) => {
- pics.push(file);
- });
- });
- }
- arr[0]['sid'] = sid;
- arr[0]['username'] = username;
- arr[0]['pictures'] = pics;
- var str = JSON.stringify(arr);
- fs.writeFile(jsonFile, str, (err) => {
- console.log(err);
- });
- // file not found
- } else {
- // do something if file found
- }
- }
- function performJSONData(sid, filename, description){
- var hash = GUD(sid);
- var pah_ = path.join(hash, 'manifest.json');
- var path_ = path.join(WorkDir, pah_);
- var file = '';
- if (fs.existsSync(path_)){
- fs.readFile(path_, 'utf8', function (err,data) {
- if (err) {
- return console.log(err);
- }
- file = data;
- var data = JSON.parse(file);
- let pth = path.join(WorkDir, hash);
- let date_ob = new Date();
- let date = ("0" + date_ob.getDate()).slice(-2);
- let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
- let year = date_ob.getFullYear();
- let hours = date_ob.getHours();
- let minutes = date_ob.getMinutes();
- let seconds = date_ob.getSeconds();
- let arr = new Array();
- arr[0] = {};
- arr[0]['filename'] = filename;
- arr[0]['creation_date'] = ( date + "." + month + "." + year + "-"+hours+":"+minutes+":"+seconds );
- arr[0]['description'] = description;
- //data['pictures'].push(arr);
- data[0]['pictures'].push(arr);
- let convert = JSON.stringify(data);
- fs.writeFile(path_, convert, (err) => {
- console.log(err);
- });
- });
- }
- }
- // todo: fix crash after updatePicturesInJSON
- function generateLink(){
- return "http://" + PUBLIC_IP + ":" + port;
- }
- function generateImage(raw_json, w, h, res, sid, username, descr){
- validateClientFolder(sid, username);
- var imageData = JSON.parse(raw_json);
- var width = Number.parseInt(w);
- var height = Number.parseInt(h);
- var count = 0;
- var p = path.join(WorkDir, GUD(sid));
- fs.readdir(p, (err, f) => {
- let files = f.filter(file => {
- return path.extname(file).toLowerCase() === '.png';
- });
- count = files.length;
- count = count + 1;
- var filename = 'screenshot'+count+'.png';
- let image = new Jimp(width, height, function (err, image) {
- if (err) throw err;
- imageData.forEach((row, y) => {
- row.forEach((color, x) => {
- image.setPixelColor(Number.parseInt(color), Number.parseInt(y), Number.parseInt(x));
- });
- });
- image.write(p+'/'+filename, (err) => {
- if (err) throw err;
- });
- //res.download(__dirname + "/img/" + filename, filename);
- });
- console.log('successfully created image');
- var URL = generateLink() + "/getimage?key=" + password + "&name=" + filename + "&sid=" + sid;
- res.send(URL);
- performJSONData(sid, filename, descr);
- });
- }
- app.get('/getimage', (req, res) => {
- console.log(FGRED, 'GET /getimage', FGCYAN, "IP: ", FGRED, req.ip, EOL)
- const data = url.parse(req.url,true).query;
- var key = data['key'];
- if (key == null) {
- key = '';
- }
- if (key.localeCompare(password) != 0){
- res.status(404).end('<h1>Access denied</h1>');
- } else {
- var name = data['name'];
- var sid = data['sid'];
- if (name != null & sid != null) {
- var p = path.join(WorkDir, GUD(sid));
- fs.readdir(p, (err, files) => {
- var cfile = "";
- files.forEach((file) => {
- if ( name.localeCompare(file) == 0 ) {
- cfile = file;
- }
- });
- if (cfile != ""){
- let p1 = path.join(WorkDir, GUD(sid));
- let p = path.join(p1, cfile);
- res.sendFile(p, {root: __dirname});
- } else {
- res.status(404).end('<h1>Access denied</h1>');
- }
- });
- } else {
- res.status(404).end('<h1>Access denied</h1>');
- }
- }
- //res.sendFile(path.join(__dirname, 'img/screenshot1.png'));
- });
- app.get('/poolgallery', function(req, res) {
- var data = [];
- fs.readdirSync(WorkDir, (err, files) => {
- // here we have a user directories (hash-named)
- console.log('a: ' + files.length);
- files.forEach((personal_dir) => {
- console.log('a: ' + personal_dir);
- let manifest = '';
- fs.readFileSync(path.join(WorkDir, personal_dir) + '/manifest.json', 'utf8', (err,d) => {
- if (err) {
- return console.log(err);
- }
- manifest = d;
- let parsed_manifest = JSON.parse(manifest);
- let pics = [];
- parsed_manifest[0]['pictures'].forEach((pic) => {
- let p = pic;
- p['url'] = generateLink() + '/repository/' + personal_dir + '/'+pic[0]['filename'];
- pics.push(p);
- });
- let user_arr = [{
- username: m[0]['username'],
- steamid: m[0]['sid'],
- pictures: pics
- }]
- console.log(user_arr);
- res.end('+joj');
- });
- });
- });
- });
- // Route that receives a POST request to /sms
- app.post('/saveimage', function (req, res) {
- console.log(FBGREEN, BGYELLOW, 'Request IP: ' + req.ip, EOL)
- console.log(FGRED, 'POST /saveimage', FGCYAN, "IP: ", FGRED, req.ip, EOL)
- const body = req.body.Body
- let key = req.body['key'];
- if ( key.localeCompare(password) == 0 ) {
- var pixeldata = req.body['pixeldata']
- var w_ = req.body['width']
- var h_ = req.body['height']
- var sid = req.body['sid']
- var username = req.body['username']
- var descr = req.body['description']
- if ( pixeldata == null | w_ == null | h_ == null | sid == null | username == null | descr == null ) {
- res.status(404).end('<h1>Wrong ray</h1>');
- } else {
- console.log(FGMAGENTA, descr, EOL);
- var binaryData = Buffer.from(pixeldata, "base64");
- var decJson = binaryData.toString("utf8");
- //checkDataFile(sid, username);
- generateImage(decJson, w_, h_, res, sid, username, descr);
- }
- } else {
- res.status(404).end('<h1>Access denied</h1>');
- }
- });
- // Tell our app to listen on port 3000
- app.listen(port, function (err) {
- if (err) {
- throw err
- }
- console.log(FGCYAN, BGMAGENTA, 'Server started on port: ' + port, EOL)
- });
- process.on('uncaughtException', function (err) {
- console.error(err);
- console.log(FGRED, BGWHITE, "Node NOT Exiting...", EOL);
- });
Add Comment
Please, Sign In to add comment