Guest User

Untitled

a guest
Mar 13th, 2018
132
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. function cleanInput(data) {
  7.     return data.replace(/\r|\n/g,"");
  8. }
  9.  
  10.  
  11. // create server and set up the initial conection handler
  12. var server = net.createServer(function(socket) {
  13.     socket.setEncoding('ascii');
  14.     // Client object to manage each new socket
  15.     var username = 'client-' + ++internalCount;
  16.     // simple central registry for clients for broadcasting, this would be better
  17.     // as a more fully built thing
  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.  
  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. Client.prototype.end = function(socket) {
  60.     console.log("Client " + this.username + " disconnected");
  61.     delete clients[this.username];
  62.     console.log(clients);
  63. }
  64.  
  65. Client.prototype.processNew = function(text) {
  66.     var analyzeString = text.split(" ");
  67.     console.log(analyzeString);
  68.     if ((analyzeString.length != 3) && (analyzeString.length != 4) && (this.state == "new")) {this.socket.write("Wrong number of words entered.\n \r :"); return;}
  69.     var command = analyzeString[0];
  70.     var user = analyzeString[1];
  71.     var pass = analyzeString[2];
  72.     var passver = analyzeString[3];
  73.     if (this.state == "new") {
  74.         if (command == "create") {
  75.         if (clients.hasOwnProperty(user)) {socket.write("Sorry, username is taken \n \r :"); return; }
  76.         if (pass !== passver) {this.socket.write("Your password did not match \n \r :"); return;}
  77.             this.state = "create";
  78.             this.tempUsername = user;
  79.             this.tempPassword = pass;
  80.             this.socket.write("Which is cuter? Please type \"bunny\" or \"shark\" Exclude the quotes. \n \r :");
  81.             return;
  82.         }
  83.        
  84.         if (command == "load") {
  85.             if ((clients.hasOwnProperty(user)) && (crypto.createHmac('sha512', "sexynaughtysaltyme").update(pass).digest('base64') == clients[user].password)) {
  86.                 clients[user] = clients[this.username];
  87.                 delete clients[this.username];
  88.                 this.username = user;
  89.                 this.state = "loggedin";
  90.                 console.log(this.username + "logged in");
  91.                 this.socket.write("You have logged in successfully " + this.username + ". \n \r :");
  92.             }
  93.             else {
  94.                 this.socket.write("You entered the wrong username/password combination \n \r :");
  95.             }
  96.         }
  97.     }
  98.     else if (this.state == "create") {
  99.         if (command == "bunny") {
  100.         clients[this.tempUsername] = clients[this.username];
  101.         delete clients[this.username];
  102.         this.createAccount(this.tempUsername, this.tempPassword);
  103.         this.socket.write("Account created. Please type \"load username password\" to login. \n \r :");
  104.         this.state = "new";
  105.         }
  106.         else {
  107.             this.socket.end("Really?\n");
  108.         }
  109.     }
  110.        
  111. }
  112.  
  113. Client.prototype.createAccount = function(username, password) {
  114.     this.username = username;
  115.     this.password = crypto.createHmac('sha512', "sexynaughtysaltyme").update(this.tempPassword).digest('base64');
  116.     delete this.tempUsername;
  117.     delete this.tempPassword;
  118.     console.log("New user created with username: " + this.username);
  119. }
  120.  
  121. console.log("Server Started");
  122. server.listen(8888);
Add Comment
Please, Sign In to add comment