Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http'),
- url = require('url'),
- fs = require('fs');
- var mime = require('mime');
- var mysql = require('mysql');
- var dateFormat = require('dateformat');
- var log_file_err = fs.createWriteStream(__dirname + '/NodeError.log', { flags: 'a' });
- var MySQLConnection = mysql.createConnection({
- host: "localhost",
- user: "---",
- password: "---",
- database: "---"
- });
- MySQLConnection.connect(function (err) {
- if (err) throw err;
- });
- var SelectSQL = function (SQLCommand, callback) {
- MySQLConnection.query(SQLCommand, function (err, data, fields) {
- if (err)
- console.log(err);
- else {
- callback(data);
- }
- });
- };
- var UpdateSQL = function (SQLCommand, callback) {
- MySQLConnection.query(SQLCommand, function (err, data, fields) {
- if (err)
- console.log(err);
- else {
- callback(data);
- }
- });
- };
- http.createServer(function (req, res) {
- var Nowdate = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
- var query = url.parse(req.url, true).query;
- process.on('uncaughtException', function (err) {
- log_file_err.write('Caught exception: ' + err.stack + '-> ' + Nowdate + 'n');
- console.log(err.stack);
- });
- var UniqID = query["id"];
- if (typeof UniqID === 'undefined') {
- res.end("Hello!");
- }
- var useragent = req.headers['user-agent'];
- var userip = req.headers['x-forwarded-for'] ||
- req.connection.remoteAddress ||
- req.socket.remoteAddress ||
- (req.connection.socket ? req.connection.socket.remoteAddress : null);
- if (userip.substr(0, 7) == "::ffff:") {
- userip = userip.substr(7)
- }
- function RunUpdateSQL(MySQLQuery) {
- UpdateSQL(MySQLQuery, function (data) {
- });
- }
- function RunSelectSQL() {
- SelectSQL("SELECT * FROM DownloadKey WHERE UniqueidPrimary='" + UniqID + "' order by id desc LIMIT 1", function (data) {
- var OutputInfo = '';
- var FileLoaded = false;
- if (Object.keys(data).length < 1) {
- OutputInfo = "Error Detected";
- res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
- res.end(OutputInfo);
- }
- else {
- var isLarger = new Date(Nowdate) > new Date(data[0].Timestamp);
- if (isLarger) {
- OutputInfo = "Link Expired";
- res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
- res.end(OutputInfo);
- }
- else {
- var newFileName = data[0].FilePath + data[0].FileName;
- if (fs.existsSync(newFileName)) {
- RunUpdateSQL("UPDATE DownloadKey SET Downloads=1 WHERE UniqueidPrimary='" + UniqID + "'");
- res.setHeader('Content-disposition', 'attachment; filename*=UTF-8''' + encodeURIComponent(data[0].FileName));
- res.setHeader('Content-Description', 'File Transfer');
- res.setHeader('Content-Type','application/octet-stream');
- res.setHeader('Content-Transfer-Encoding', 'Binary');
- fs.readFile(newFileName, function (err, content) {
- if (err) {
- OutputInfo = "Link Is not Valid";
- res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
- res.end(OutputInfo);
- } else {
- res.setHeader('Content-Length', content.length);
- res.end(content);
- }
- });
- } else {
- OutputInfo = "Link is Not Valid";
- res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
- res.end(OutputInfo);
- }
- }
- }
- });
- }
- RunUpdateSQL("UPDATE DownloadKey SET UserAgent='" + useragent + "',UserIP='" + userip + "' WHERE UniqueidPrimary='" + UniqID + "'");
- RunSelectSQL();
- }).listen(3333);
- console.log("NodeJS WebApp running at http://localhost:3333/");
Add Comment
Please, Sign In to add comment