"use strict"; process.title = 'codeyourcloud'; var webSocketsServerPort = 8080; var fs = require('fs'); var Connection = require('ssh2'); var webSocketServer = require('websocket').server; var https = require('http'); var server = https.createServer(function (req, res){ } ); server.listen(webSocketsServerPort, function (){ console.log((new Date()) + " Server is listening on port " + webSocketsServerPort); } ); var wsServer = new webSocketServer({ httpServer: server} ); var sessionIndex = [] ; var FILE = [] ; var sessions = [] ; var total = 0; var clients = [] ; var ssh_port; var ssh_host; var ssh_login; var ssh_pass; wsServer.on('request', function (request){ var connection = request.accept(null , request.origin); connection.id = ++total; if (_AN_Read_length('length', clients) < connection.id) { clients.push(connection); } connection.on('message', function (message){ try { var json = JSON.parse(message.utf8Data); if (json.type === "request") { if (json.data === "init") { addToSession(json.session, connection.id); _AN_Call_send("send", connection, JSON.stringify({ type: "init", id: connection.id, session: sessionIndex[connection.id - 1]} )); } } if (json.type === "command") { if (json.command === "save") { var array = sessions[sessionIndex[connection.id - 1]]; for (var i = 0; i < _AN_Read_length("length", array); i++ ){ if (array[i] !== connection.id) { _AN_Call_send("send", clients[array[i] - 1], JSON.stringify({ type: "command", command: "save"} )); } } } if (json.command === "rename") { var newTitle = json.title; var array = sessions[sessionIndex[connection.id - 1]]; for (var i = 0; i < _AN_Read_length("length", array); i++ ){ if (array[i] !== connection.id) { _AN_Call_send("send", clients[array[i] - 1], JSON.stringify({ type: "command", command: "rename", title: newTitle} )); } } } if (json.command === "mode") { var newMode = json.mode; var array = sessions[sessionIndex[connection.id - 1]]; for (var i = 0; i < _AN_Read_length("length", array); i++ ){ if (array[i] !== connection.id) { _AN_Call_send("send", clients[array[i] - 1], JSON.stringify({ type: "command", command: "mode", mode: newMode} )); } } } if (json.command === "theme") { var newTheme = json.theme; var array = sessions[sessionIndex[connection.id - 1]]; for (var i = 0; i < _AN_Read_length("length", array); i++ ){ if (array[i] !== connection.id) { _AN_Call_send("send", clients[array[i] - 1], JSON.stringify({ type: "command", command: "theme", theme: newTheme} )); } } } } if (json.type === "comment") { var text = json.comment; var name = json.sender; var mail = json.email; var output = name + "\n" + mail + "\n" + text + "\n*******************************\n"; fs.appendFile('comments.txt', output, function (err){ } ); } if (json.type === "update") { var array = sessions[sessionIndex[connection.id - 1]]; for (var i = 0; i < _AN_Read_length("length", array); i++ ){ _AN_Call_send("send", clients[array[i] - 1], JSON.stringify({ type: "update", name: json.name} )); } } if (json.type === "ssh") { if (json.ssh === "init") { ssh_port = _AN_Read_port("port", json); ssh_host = _AN_Read_host("host", json); ssh_login = json.login; ssh_pass = json.password; startSSH(); } if (json.ssh === "folder") { ssh_dir(json.path); } } } catch (e) { } } ); connection.on('close', function (connection){ try { var index = connection.id - 1; var id = connection.id; var s = sessionIndex[index]; sessionIndex.splice(index, 1); clients.splice(index, 1); var ind = sessions[s].indexOf(id); sessions[s].splice(ind, 1); if (_AN_Read_length('length', session[s]) === 0) { sessions.splice(s, 1); } FILE.splice(s, 1); } catch (e) { } } ); function addToSession(session, id){ for (var i = 0; i < _AN_Read_length('length', FILE); i++ ){ if (FILE[i] === session) { sessionIndex.push(i); sessions[i].push(id); return ; } } addSession(session); addToSession(session, id); } function addSession(session){ FILE.push(session); var index = FILE.indexOf(session); sessions[index] = [] ; } function removeSession(session){ var index = FILE.indexOf(session); FILE.splice(index, 1); sessions.split(index, 1); } var c; function startSSH(){ c = new Connection(); c.on('ready', function (){ ssh_dir('/'); } ); c.on('error', function (err){ } ); c.on('end', function (){ } ); c.on('close', function (had_error){ } ); c.connect({ host: ssh_host, port: ssh_port, username: ssh_login, password: ssh_pass} ); ssh_dir('/'); } function stopSSH(){ sftp.close(handle, function (err){ if (err) throw err sftp.end(); } ); } function ssh_dir(path){ c.sftp(function (err, sftp){ if (err) { _AN_Call_send('send', connection, JSON.stringify({ type: "bad"} )); throw err } _AN_Call_send("send", connection, JSON.stringify({ type: "ok"} )); sftp.on('end', function (){ } ); sftp.opendir(path, function readdir(err, handle){ if (err) throw err sftp.readdir(handle, function (err, list){ if (err) throw err if (list === false ) { return ; } var array = [] ; for (var i = 0; i < _AN_Read_length('length', list); i++ ){ array.push(list[i].filename); } _AN_Call_send('send', connection, JSON.stringify({ type: "ssh", ssh: "folder", path: path, data: array} )); readdir(undefined, handle); } ); } ); } ); } } );