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);
- 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;
- // simple central registry for clients for broadcasting, this would be better
- // as a more fully built thing
- 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 :");
- }
- 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);
- }
- }
- Client.prototype.end = function(socket) {
- console.log("Client " + this.username + " disconnected");
- delete clients[this.username];
- console.log(clients);
- }
- 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");
- }
- }
- }
- Client.prototype.createAccount = function(username, password) {
- 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