Guest User

Untitled

a guest
Mar 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var net = require("net");
  2. var crypto = require('crypto');
  3. var clients = {}, internalCount = 0;
  4. var theBuffer = new Buffer(256);
  5.  
  6. //remove newline and carriage returns
  7. function cleanInput(data) {
  8.     return data.replace(/\r|\n/g,"");
  9. }
  10.  
  11.  
  12. // create server and set up the initial conection handler
  13. var server = net.createServer(function(socket) {
  14.     socket.setEncoding('ascii');
  15.     // Client object to manage each new socket
  16.     var username = 'client-' + ++internalCount;
  17.     //Give the client's session a temporary username until they log in
  18.     clients[username] = new Client(socket, username);
  19.     console.log("New client connected with id: " + username );
  20. });
  21.  
  22.  
  23. // New connection, make a new 'Client' to represent it
  24. function Client(socket, username){
  25.     var self = this;
  26.     this.socket = socket;
  27.     this.username = username;
  28.     this.state = "new";
  29.     socket.on('data', function (chunk) { self.data(chunk); });
  30.     socket.on('end', function (endle) { self.end(endle); });
  31.     socket.write("Welcome to the Telnet server!\n \r Please type \"create\" and then your username, password, and password again to \n \r create an account. \n \r Otherwise, type \"load\" and then your username \n \r and password to log in. \n \r :");
  32. }
  33.  
  34. //Process the user's data
  35. Client.prototype.data = function(data){
  36.     var len = theBuffer.write(data, 0, encoding='ascii');
  37.     var text = theBuffer.toString("ascii", 0, len);
  38.     text = cleanInput(text);
  39.     theBuffer.fill(0);
  40.     console.log(text);
  41.     if(text === "quit") {
  42.         this.socket.end('Goodbye!\n');
  43.     }
  44.        
  45.     else if (this.state == "loggedin") {
  46.         for (var client in clients) {
  47.             if (clients[client] != this) {
  48.             clients[client].socket.write(this.username + ": " + text + "\n \r :");
  49.             }
  50.         }
  51.         this.socket.write("\n \r :");
  52.     }
  53.     else {
  54.         text = text.toLowerCase();
  55.         this.processNew(text);
  56.     }
  57. }
  58.  
  59. //Pretty obvious
  60. Client.prototype.end = function(socket) {
  61.     console.log("Client " + this.username + " disconnected");
  62.     delete clients[this.username];
  63.     console.log(clients);
  64. }
  65.  
  66. //Processes the data from someone who isn't logged in
  67. Client.prototype.processNew = function(text) {
  68.     var analyzeString = text.split(" ");
  69.     console.log(analyzeString);
  70.     if ((analyzeString.length != 3) && (analyzeString.length != 4) && (this.state == "new")) {this.socket.write("Wrong number of words entered.\n \r :"); return;}
  71.     var command = analyzeString[0];
  72.     var user = analyzeString[1];
  73.     var pass = analyzeString[2];
  74.     var passver = analyzeString[3];
  75.     if (this.state == "new") {
  76.         if (command == "create") {
  77.         if (clients.hasOwnProperty(user)) {socket.write("Sorry, username is taken \n \r :"); return; }
  78.         if (pass !== passver) {this.socket.write("Your password did not match \n \r :"); return;}
  79.             this.state = "create";
  80.             this.tempUsername = user;
  81.             this.tempPassword = pass;
  82.             this.socket.write("Which is cuter? Please type \"bunny\" or \"shark\" Exclude the quotes. \n \r :");
  83.             return;
  84.         }
  85.        
  86.         if (command == "load") {
  87.             if ((clients.hasOwnProperty(user)) && (crypto.createHmac('sha512', "sexynaughtysaltyme").update(pass).digest('base64') == clients[user].password)) {
  88.                 clients[user] = clients[this.username];
  89.                 delete clients[this.username];
  90.                 this.username = user;
  91.                 this.state = "loggedin";
  92.                 console.log(this.username + "logged in");
  93.                 this.socket.write("You have logged in successfully " + this.username + ". \n \r :");
  94.             }
  95.             else {
  96.                 this.socket.write("You entered the wrong username/password combination \n \r :");
  97.             }
  98.         }
  99.     }
  100.     else if (this.state == "create") {
  101.         if (command == "bunny") {
  102.         clients[this.tempUsername] = clients[this.username];
  103.         delete clients[this.username];
  104.         this.createAccount(this.tempUsername, this.tempPassword);
  105.         this.socket.write("Account created. Please type \"load username password\" to login. \n \r :");
  106.         this.state = "new";
  107.         }
  108.         else {
  109.             this.socket.end("Really?\n");
  110.         }
  111.     }
  112.        
  113. }
  114. //Simple enough account creation code
  115. Client.prototype.createAccount = function(username, password) {
  116.     if (clients.hasOwnProperty[username]) {console.log("Line 115. WTF HAPPENED?"); this.socket.write("Bizzare error"); return;}
  117.     this.username = username;
  118.     this.password = crypto.createHmac('sha512', "sexynaughtysaltyme").update(this.tempPassword).digest('base64');
  119.     delete this.tempUsername;
  120.     delete this.tempPassword;
  121.     console.log("New user created with username: " + this.username);
  122. }
  123.  
  124. console.log("Server Started");
  125. server.listen(8888);
Add Comment
Please, Sign In to add comment