Guest User

Untitled

a guest
Apr 9th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. var http = require('http'),
  2. url = require('url'),
  3. fs = require('fs');
  4. var mime = require('mime');
  5. var mysql = require('mysql');
  6. var dateFormat = require('dateformat');
  7. var log_file_err = fs.createWriteStream(__dirname + '/NodeError.log', { flags: 'a' });
  8.  
  9. var MySQLConnection = mysql.createConnection({
  10. host: "localhost",
  11. user: "---",
  12. password: "---",
  13. database: "---"
  14. });
  15.  
  16. MySQLConnection.connect(function (err) {
  17. if (err) throw err;
  18. });
  19.  
  20. var SelectSQL = function (SQLCommand, callback) {
  21. MySQLConnection.query(SQLCommand, function (err, data, fields) {
  22. if (err)
  23. console.log(err);
  24. else {
  25. callback(data);
  26. }
  27. });
  28. };
  29.  
  30. var UpdateSQL = function (SQLCommand, callback) {
  31. MySQLConnection.query(SQLCommand, function (err, data, fields) {
  32. if (err)
  33. console.log(err);
  34. else {
  35. callback(data);
  36. }
  37. });
  38. };
  39.  
  40. http.createServer(function (req, res) {
  41. var Nowdate = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
  42. var query = url.parse(req.url, true).query;
  43.  
  44. process.on('uncaughtException', function (err) {
  45. log_file_err.write('Caught exception: ' + err.stack + '-> ' + Nowdate + 'n');
  46. console.log(err.stack);
  47. });
  48.  
  49. var UniqID = query["id"];
  50. if (typeof UniqID === 'undefined') {
  51. res.end("Hello!");
  52. }
  53. var useragent = req.headers['user-agent'];
  54. var userip = req.headers['x-forwarded-for'] ||
  55. req.connection.remoteAddress ||
  56. req.socket.remoteAddress ||
  57. (req.connection.socket ? req.connection.socket.remoteAddress : null);
  58. if (userip.substr(0, 7) == "::ffff:") {
  59. userip = userip.substr(7)
  60. }
  61.  
  62. function RunUpdateSQL(MySQLQuery) {
  63. UpdateSQL(MySQLQuery, function (data) {
  64. });
  65. }
  66.  
  67. function RunSelectSQL() {
  68. SelectSQL("SELECT * FROM DownloadKey WHERE UniqueidPrimary='" + UniqID + "' order by id desc LIMIT 1", function (data) {
  69. var OutputInfo = '';
  70. var FileLoaded = false;
  71. if (Object.keys(data).length < 1) {
  72. OutputInfo = "Error Detected";
  73. res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  74. res.end(OutputInfo);
  75. }
  76. else {
  77. var isLarger = new Date(Nowdate) > new Date(data[0].Timestamp);
  78. if (isLarger) {
  79. OutputInfo = "Link Expired";
  80. res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  81. res.end(OutputInfo);
  82. }
  83. else {
  84. var newFileName = data[0].FilePath + data[0].FileName;
  85. if (fs.existsSync(newFileName)) {
  86. RunUpdateSQL("UPDATE DownloadKey SET Downloads=1 WHERE UniqueidPrimary='" + UniqID + "'");
  87. res.setHeader('Content-disposition', 'attachment; filename*=UTF-8''' + encodeURIComponent(data[0].FileName));
  88. res.setHeader('Content-Description', 'File Transfer');
  89. res.setHeader('Content-Type','application/octet-stream');
  90. res.setHeader('Content-Transfer-Encoding', 'Binary');
  91. fs.readFile(newFileName, function (err, content) {
  92. if (err) {
  93. OutputInfo = "Link Is not Valid";
  94. res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  95. res.end(OutputInfo);
  96. } else {
  97. res.setHeader('Content-Length', content.length);
  98. res.end(content);
  99. }
  100. });
  101. } else {
  102. OutputInfo = "Link is Not Valid";
  103. res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  104. res.end(OutputInfo);
  105. }
  106. }
  107. }
  108. });
  109. }
  110.  
  111. RunUpdateSQL("UPDATE DownloadKey SET UserAgent='" + useragent + "',UserIP='" + userip + "' WHERE UniqueidPrimary='" + UniqID + "'");
  112. RunSelectSQL();
  113.  
  114. }).listen(3333);
  115. console.log("NodeJS WebApp running at http://localhost:3333/");
Add Comment
Please, Sign In to add comment