|
| 1 | +//Rewrite for logging to files |
| 2 | +var log = console.log; |
| 3 | +//Dependencies |
| 4 | +var http = require("http"); |
| 5 | +var fs = require("fs"); |
| 6 | +var path = require("path"); |
| 7 | +// var os = require("os"); |
| 8 | +// var exec = require('child_process').exec; |
| 9 | +//Runtime variables |
| 10 | +var cfg_dir = __dirname + '/config/'; |
| 11 | +var cfg_map = {}; |
| 12 | + |
| 13 | +var processFile = function(fileName){ |
| 14 | + var filePath = cfg_dir + fileName; |
| 15 | + var fileExt = path.extname(fileName); |
| 16 | + if(fileExt != '.json'){ |
| 17 | + return log('Problem with file "' + filePath + '". There must be .json file extension.'); |
| 18 | + } |
| 19 | + |
| 20 | + fs.readFile(filePath, 'utf-8', function(err, data) { |
| 21 | + try{ |
| 22 | + if(err) throw err; |
| 23 | + |
| 24 | + var cfg = JSON.parse(data); |
| 25 | + if([cfg.path, cfg.user, cfg.commands].indexOf(undefined) !== -1){ |
| 26 | + throw new Error('Bad config file "' + filePath + '". It need to be json object with path, user and commands keys.'); |
| 27 | + } |
| 28 | + } catch(e) { |
| 29 | + return log(e); |
| 30 | + } |
| 31 | + //Populate good cfg object to objects map by filename without extension |
| 32 | + return cfg_map[path.basename(fileName, fileExt)] = cfg; |
| 33 | + }); |
| 34 | +}; |
| 35 | + |
| 36 | +// Readfiles to object on server start |
| 37 | +fs.readdir(cfg_dir, function(wtf, files){ |
| 38 | + for(var i in files){ |
| 39 | + try{ |
| 40 | + processFile(files[i]); |
| 41 | + fs.watchFile(cfg_dir + files[i], function(prev, next) { |
| 42 | + processFile(files[i]); |
| 43 | + }); |
| 44 | + } catch(e) { |
| 45 | + log(e); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + //Watch for changes |
| 50 | + fs.watch(cfg_dir, function(event, fileName) { |
| 51 | + processFile(fileName); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +// create a server |
| 56 | +http.createServer(function(request, response) { |
| 57 | + request.url = request.url.slice(1); |
| 58 | + //Fucking favicon.ico! |
| 59 | + if(request.url == 'favicon.ico'){ |
| 60 | + return request.connection.destroy(); |
| 61 | + } |
| 62 | + if(request.method == 'POST' && cfg_map[request.url]){ |
| 63 | + var body = ''; |
| 64 | + request.on('data', function (data) { |
| 65 | + body += data; |
| 66 | + if (body.length > 1e6) { |
| 67 | + // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST |
| 68 | + request.connection.destroy(); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + request.on('end', function () { |
| 73 | + log('End responce'); |
| 74 | + }); |
| 75 | + } |
| 76 | + log(cfg_map); |
| 77 | + // on every request, we'll output 'Hello world' |
| 78 | + response.end("Hello world from Cloud9 changed!"); |
| 79 | +}).listen(process.env.PORT, process.env.IP); |
| 80 | + |
| 81 | +// Note: when spawning a server on Cloud9 IDE, |
| 82 | +// listen on the process.env.PORT and process.env.IP environment variables |
| 83 | + |
| 84 | +// Click the 'Run' button at the top to start your server, |
| 85 | +// then click the URL that is emitted to the Output tab of the console |
0 commit comments