Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var net = require("net");
- var crypto = require('crypto');
- var clients = {}, internalCount = 0;
- var theBuffer = new Buffer(256);
- //remove newline and carriage returns
- function cleanInput(data) {
- return data.replace(/\r|\n/g,"");
- }
- // create server and set up the initial conection handler
- var server = net.createServer(function(socket) {
- socket.setEncoding('ascii');
- // Client object to manage each new socket
- var username = 'client-' + ++internalCount;
- //Give the client's session a temporary username until they log in
- clients[username] = new Client(socket, username);
- console.log("New client connected with id: " + username );
- });
- // New connection, make a new 'Client' to represent it
- function Client(socket, username){
- var self = this;
- this.socket = socket;
- this.username = username;
- this.state = "new";
- socket.on('data', function (chunk) { self.data(chunk); });
- socket.on('end', function (endle) { self.end(endle); });
- 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 :");
- }
- //Process the user's data
- Client.prototype.data = function(data){
- var len = theBuffer.write(data, 0, encoding='ascii');
- var text = theBuffer.toString("ascii", 0, len);
- text = cleanInput(text);
- theBuffer.fill(0);
- console.log(text);
- if(text === "quit") {
- this.socket.end('Goodbye!\n');
- }
- else if (this.state == "loggedin") {
- for (var client in clients) {
- if (clients[client] != this) {
- clients[client].socket.write(this.username + ": " + text + "\n \r :");
- }
- }
- this.socket.write("\n \r :");
- }
- else {
- text = text.toLowerCase();
- this.processNew(text);
- }
- }
- //Pretty obvious
- Client.prototype.end = function(socket) {
- console.log("Client " + this.username + " disconnected");
- delete clients[this.username];
- console.log(clients);
- }
- //Processes the data from someone who isn't logged in
- Client.prototype.processNew = function(text) {
- var analyzeString = text.split(" ");
- console.log(analyzeString);
- if ((analyzeString.length != 3) && (analyzeString.length != 4) && (this.state == "new")) {this.socket.write("Wrong number of words entered.\n \r :"); return;}
- var command = analyzeString[0];
- var user = analyzeString[1];
- var pass = analyzeString[2];
- var passver = analyzeString[3];
- if (this.state == "new") {
- if (command == "create") {
- if (clients.hasOwnProperty(user)) {socket.write("Sorry, username is taken \n \r :"); return; }
- if (pass !== passver) {this.socket.write("Your password did not match \n \r :"); return;}
- this.state = "create";
- this.tempUsername = user;
- this.tempPassword = pass;
- this.socket.write("Which is cuter? Please type \"bunny\" or \"shark\" Exclude the quotes. \n \r :");
- return;
- }
- if (command == "load") {
- if ((clients.hasOwnProperty(user)) && (crypto.createHmac('sha512', "sexynaughtysaltyme").update(pass).digest('base64') == clients[user].password)) {
- clients[user] = clients[this.username];
- delete clients[this.username];
- this.username = user;
- this.state = "loggedin";
- console.log(this.username + "logged in");
- this.socket.write("You have logged in successfully " + this.username + ". \n \r :");
- }
- else {
- this.socket.write("You entered the wrong username/password combination \n \r :");
- }
- }
- }
- else if (this.state == "create") {
- if (command == "bunny") {
- clients[this.tempUsername] = clients[this.username];
- delete clients[this.username];
- this.createAccount(this.tempUsername, this.tempPassword);
- this.socket.write("Account created. Please type \"load username password\" to login. \n \r :");
- this.state = "new";
- }
- else {
- this.socket.end("Really?\n");
- }
- }
- }
- //Simple enough account creation code
- Client.prototype.createAccount = function(username, password) {
- if (clients.hasOwnProperty[username]) {console.log("Line 115. WTF HAPPENED?"); this.socket.write("Bizzare error"); return;}
- this.username = username;
- this.password = crypto.createHmac('sha512', "sexynaughtysaltyme").update(this.tempPassword).digest('base64');
- delete this.tempUsername;
- delete this.tempPassword;
- console.log("New user created with username: " + this.username);
- }
- console.log("Server Started");
- server.listen(8888);
Add Comment
Please, Sign In to add comment