Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. let Auth = require("./Auth.js");
  2. let WebSocketServer = require("websocket").server;
  3. let http = require("http");
  4. let nodeMailer = require("nodemailer");
  5. let server = http.createServer(function(request, response){});
  6.  
  7. server.listen(800, function(){});
  8. wsServer = new WebSocketServer({httpServer: server});
  9.  
  10. let clients = {};
  11. let sessionInfo = {};
  12.  
  13. function validateProperties(data, properties) {
  14. for (i in properties) {
  15. if (!(properties[i] in data)) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21.  
  22. wsServer.on("request", function(request) {
  23. let connection = request.accept(null, request.origin);
  24. console.log("Connected to " + request.remoteAddress);
  25.  
  26. // Find an index to assign it to
  27. let index = 0;
  28. while (index in clients)
  29. index++;
  30.  
  31. clients[index] = connection;
  32. sessionInfo[index] = {};
  33.  
  34. connection.on("message", function(message) {
  35. if (message.type === "utf8") {
  36. let data = "";
  37. try {
  38. data = JSON.parse(message.utf8Data);
  39. } catch (err) {
  40. return;
  41. }
  42.  
  43. let curClient = clients[index];
  44. let session = sessionInfo[index];
  45.  
  46. if (!("what" in data)) {
  47. return;
  48. } else if (data.what == "begin-login") {
  49. if (!validateProperties(data, ["username"]))
  50. return;
  51.  
  52. Auth.getSaltForUser(data.username, function(salt) {
  53. let token = Auth.randomHex(64);
  54. session.loginToken = token;
  55. session.salt = salt;
  56.  
  57. curClient.send(JSON.stringify({
  58. what: "login-token",
  59. token: token,
  60. salt: salt
  61. }));
  62. }, function() {
  63. curClient.send(JSON.stringify({
  64. what: "bad-login"
  65. }));
  66. });
  67. } else if (data.what == "login") {
  68. if (!validateProperties(data, ["username", "value"]))
  69. return;
  70.  
  71. Auth.checkLogin(data.username, session.loginToken, data.value, function() {
  72. // Check if this user is already logged in, and don't log in if so
  73. for (i in sessionInfo) {
  74. if (sessionInfo[i].username == data.username) {
  75. curClient.send(JSON.stringify({
  76. what: "already-logged-in"
  77. }));
  78.  
  79. return;
  80. }
  81. }
  82.  
  83. delete session.loginToken;
  84.  
  85. session.loggedIn = true;
  86. session.username = data.username;
  87. curClient.send(JSON.stringify({
  88. what: "logged-in",
  89. user: session.username
  90. }));
  91. }, function() {
  92. curClient.send(JSON.stringify({
  93. what: "bad-login"
  94. }));
  95. });
  96. } else if (data.what == "begin-create-account") {
  97. let salt = Auth.randomHex(64);
  98. session.newSalt = salt;
  99.  
  100. curClient.send(JSON.stringify({
  101. what: "new-account-salt",
  102. salt: salt
  103. }));
  104. } else if (data.what == "create-account") {
  105. if (!validateProperties(data, ["username", "storedPW", "email"]))
  106. return;
  107.  
  108. if (!session.newSalt)
  109. return;
  110.  
  111. Auth.getUserInfo(data.username, function() {
  112. // Username is already taken!
  113. curClient.send(JSON.stringify({
  114. what: "username-taken"
  115. }));
  116. }, function() {
  117. curClient.send(JSON.stringify({
  118. what: "activation-email-sent"
  119. }));
  120.  
  121. let activationCode = Auth.randomHex(10);
  122. Auth.addUnverifiedUser(data.username, session.newSalt, data.storedPW, data.email, activationCode);
  123.  
  124. // Send verification email
  125. let transporter = nodeMailer.createTransport({
  126. service: "gmail",
  127. auth: {
  128. // Replace this with the correct email details!
  129. user: "email@gmail.com",
  130. pass: "emailpassword"
  131. }
  132. });
  133.  
  134. let mailOptions = {
  135. from: "email@gmail.com",
  136. to: data.email,
  137. subject: "MomoRTS Account Verification",
  138. // Replace this with the correct url!!!!
  139. html: `<a href="http://localhost:8080/?user=${data.username}&code=${activationCode}">Activation URL</a>`
  140. };
  141.  
  142. transporter.sendMail(mailOptions, function(error, info){
  143. if (error) {
  144. console.log(error);
  145. } else {
  146. console.log(`Verification email sent to ${data.email}: ` + info.response);
  147. }
  148. });
  149. });
  150. } else if (data.what == "verify-account") {
  151. if (!validateProperties(data, ["username", "activationCode"]))
  152. return;
  153.  
  154. Auth.verifyUser(data.username, data.activationCode, function() {
  155. curClient.send(JSON.stringify({
  156. what: "account-activated"
  157. }));
  158. });
  159. } else if (data.what == "log-out") {
  160. sessionInfo[index] = {};
  161.  
  162. // Sample of setting account details (obviously not accurate)
  163. } else if (data.what == "final-game-winning-move") {
  164. Auth.getDetails(session.username, function(details) {
  165. details.MMR += 50;
  166. Auth.setDetails(session.username, details);
  167. });
  168. }
  169.  
  170.  
  171. }
  172. });
  173.  
  174. connection.on("close", function(connection) {
  175. delete clients[index];
  176. delete sessionInfo[index];
  177. });
  178. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement