Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. var cookieParser = require('cookie-parser');
  2. var bodyParser = require('body-parser');
  3. var path = require('path');
  4. var http = require('http');
  5. var express = require('express');
  6. var fs = require('fs');
  7. var app = express();
  8. var mysql = require('mysql');
  9. var after = require("after");
  10. var bcrypt = require('bcrypt');
  11. var https = require('https');
  12.  
  13. var privateKey = fs.readFileSync('key.pem');
  14. var certificate = fs.readFileSync('cert.pem');
  15. var credentials = {key: privateKey, cert: certificate};
  16. var app = express();
  17.  
  18. var httpsServer = https.createServer(credentials, app);
  19. var httpServer = http.createServer(app);
  20.  
  21. httpsServer.listen(3000);
  22. httpServer.listen(3001);
  23.  
  24.  
  25. var connection = mysql.createConnection({
  26. host : 'localhost',
  27. port: '/var/run/mysqld/mysqld.sock',
  28. user : 'root',
  29. password : 'YourBigM@m@n4815162342',
  30. database : 'LLDB',
  31. charset: "utf8_general_ci"
  32. });
  33. const saltRounds = 10;
  34. connection.connect();
  35.  
  36. app.use(bodyParser.urlencoded({ extended: false }));
  37. app.use(bodyParser.json());
  38. app.use(cookieParser());
  39. app.use(express.static(__dirname + '/public'));
  40.  
  41. app.get('/', function (req, res) {
  42. //console.log("Cookies: ", req.cookies);
  43. res.sendFile(__dirname+ '/private/home.html');
  44. });
  45.  
  46. app.all('/signup', function (req, res) {
  47. var bannedEmailDomains = [ "mvrht", "noicd", "10minuteemail" , "10minutemail", "20email", "dropmail"];
  48. var signupStatus = {};
  49. var usernameErrors = [];
  50. var emailErrors = [];
  51. var passwordErrors = [];
  52.  
  53. var finished = after(3, sendSignupStatus);
  54.  
  55. //Username
  56. if(!req.body.username || req.body.username.length < 4) {
  57. usernameErrors.push("Must be at least 4 characters long");
  58. finished();
  59. }
  60. else {
  61. if(!isASCII(req.body.username, false))
  62. usernameErrors.push("Invalid symbols");
  63. if(req.body.username.length > 30)
  64. usernameErrors.push("Must be less than 30 characters long");
  65.  
  66. if(usernameErrors.length === 0) {
  67. connection.query("SELECT * FROM user_main_info WHERE username = '"+req.body.username + "';", function(error, result, field) {
  68. if(result.length > 0 ) {
  69. usernameErrors.push("Account already exists");
  70. }
  71.  
  72. finished();
  73. });
  74. }
  75. else
  76. finished();
  77. }
  78.  
  79. //Email
  80. if(!req.body.email) {
  81. emailErrors.push("Email not specified");
  82. finished();
  83. }
  84. else {
  85. if(!isASCII(req.body.email,true))
  86. emailErrors.push("Invalid symbols");
  87. if(req.body.email.length > 30)
  88. emailErrors.push("Must be less than 30 characters long");
  89.  
  90. for(var i = 0; i < bannedEmailDomains.length; i++) {
  91. if(req.body.email.indexOf(bannedEmailDomains[i]) !== -1) {
  92. emailErrors.push("Invalid email domain");
  93. break;
  94. }
  95. }
  96.  
  97. if(req.body.email.indexOf("@") === -1 || req.body.email.indexOf(" ") !== -1)
  98. emailErrors.push("Invalid email");
  99.  
  100. if(emailErrors.length === 0) {
  101. connection.query("SELECT * FROM user_main_info WHERE email = '"+req.body.email + "';", function(error, result, field) {
  102. if(result.length > 0 ) {
  103. emailErrors.push("This email is taken");
  104. }
  105.  
  106. finished();
  107. });
  108. }
  109. else
  110. finished();
  111. }
  112.  
  113. //Password
  114. if(!req.body.password || req.body.password.length < 10) {
  115. passwordErrors.push("Must be at least 10 characters long");
  116. finished();
  117. }
  118. else
  119. finished();
  120.  
  121.  
  122. function sendSignupStatus() {
  123. signupStatus["usernameErrors"] = usernameErrors;
  124. signupStatus["emailErrors"] = emailErrors;
  125. signupStatus["passwordErrors"] = passwordErrors;
  126.  
  127. if(signupStatus["usernameErrors"].length !== 0 ||
  128. signupStatus["emailErrors"].length !== 0 ||
  129. signupStatus["emailErrors"].length !== 0) {
  130. res.send(JSON.stringify(signupStatus));
  131. }
  132. else {
  133. bcrypt.genSalt(10, function(err, salt) {
  134. bcrypt.hash(req.body.password, salt, function(err, hash) {
  135. connection.query("INSERT INTO user_main_info (username, email, password, salt) VALUES ('"+ req.body.username+"', '"+req.body.email +"', '"+ hash + "', '"+salt+"');");
  136.  
  137. res.send("1");
  138. });
  139. });
  140. }
  141. }
  142. });
  143.  
  144.  
  145. app.get('/:id', function (req, res) {
  146.  
  147. if(req.params.id !== "profile")
  148. {
  149. res.status(404);
  150. res.send('Not found!');
  151. }
  152.  
  153. res.sendFile(__dirname+ '/private/profile.html');
  154. });
  155.  
  156. app.listen(1337);
  157. //Checks if string contains only ASCII characters
  158. function isASCII(str, acceptSpecial) {
  159. if(acceptSpecial)
  160. return /^[\x00-\x7F]*$/.test(str);
  161. else
  162. return /^[0-9a-zA-Z]+$/.test(str);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement