|
| 1 | +"use strict"; |
| 2 | +process.title = 'codeyourcloud'; |
| 3 | +var webSocketsServerPort = 8080; |
| 4 | +var fs = require('fs'); |
| 5 | +var Connection = require('ssh2'); |
| 6 | +var webSocketServer = require('websocket').server; |
| 7 | +var https = require('http');//https |
| 8 | +/* |
| 9 | +var options = { |
| 10 | + key: fs.readFileSync("../../../../etc/ssl/private/www.codeyourcloud.com.key"), |
| 11 | + cert: fs.readFileSync("../../../../etc/ssl/certs/www.codeyourcloud.com.crt"), |
| 12 | + ca: fs.readFileSync("../../../../etc/ssl/certs/www.codeyourcloud.com-geotrust.crt") |
| 13 | +};*/ |
| 14 | +var server = https.createServer(function(req, res){}); //options |
| 15 | + |
| 16 | +server.listen(webSocketsServerPort, function() { |
| 17 | + console.log((new Date()) + " Server is listening on port " + webSocketsServerPort); |
| 18 | +}); |
| 19 | +var wsServer = new webSocketServer({ |
| 20 | + httpServer: server |
| 21 | +}); |
| 22 | +/****************** |
| 23 | +****************** |
| 24 | +***************** |
| 25 | +*************** |
| 26 | +************** |
| 27 | +************ |
| 28 | +********** |
| 29 | +******** |
| 30 | +****** |
| 31 | +*** |
| 32 | +*/ |
| 33 | +var sessionIndex = []; |
| 34 | +//for every client, the index of the session |
| 35 | +/* |
| 36 | +the index is the id |
| 37 | +the actual connections |
| 38 | +*/ |
| 39 | +var FILE = []; |
| 40 | +/* |
| 41 | +the fileids of the respectice clients |
| 42 | +LINES UP WITH ARRAYS |
| 43 | +*/ |
| 44 | +var sessions = []; |
| 45 | +/* |
| 46 | +stores the indexes of the people in various sessions |
| 47 | +*/ |
| 48 | +var total = 0; |
| 49 | + |
| 50 | +var clients = []; |
| 51 | +/* |
| 52 | +############# |
| 53 | +CONNECTING |
| 54 | +############# |
| 55 | +1. you get the client id |
| 56 | +2. SERVER gets the fileid, locates the proper session and FILE |
| 57 | +2a. if there is no fileid, MAKE ONE |
| 58 | +3. index added to the proper session |
| 59 | +################### |
| 60 | +SAVING TO ALL PEOPLE IN A SESSION |
| 61 | +################### |
| 62 | +1. get client id |
| 63 | +2. all of the people in the session, except for the original person, SAVE |
| 64 | +!remember...the session is the INDEXES which can be located in the clients array |
| 65 | +*/ |
| 66 | +// |
| 67 | +/********* |
| 68 | +SSH |
| 69 | +********/ |
| 70 | +var ssh_port; |
| 71 | +var ssh_host; |
| 72 | +var ssh_login; |
| 73 | +var ssh_pass; |
| 74 | +// |
| 75 | +// |
| 76 | +wsServer.on('request', function(request) { |
| 77 | + //console.log((new Date()) + ' Connection from origin ' + request.origin + '.'); |
| 78 | + var connection = request.accept(null, request.origin); |
| 79 | + connection.id = ++total; |
| 80 | + if(clients.length < connection.id){ |
| 81 | + clients.push(connection); |
| 82 | + } |
| 83 | + /******** |
| 84 | + THE IMPORTANT PART |
| 85 | + ********/ |
| 86 | + connection.on('message', function(message) { |
| 87 | + try{ |
| 88 | + var json = JSON.parse(message.utf8Data); |
| 89 | + if(json.type === "request"){ |
| 90 | + if(json.data === "init"){ |
| 91 | + addToSession(json.session, connection.id); |
| 92 | + connection.send(JSON.stringify({type: "init", id: connection.id, session: sessionIndex[connection.id-1]})); |
| 93 | + |
| 94 | + } |
| 95 | + } |
| 96 | + if(json.type === "command"){ |
| 97 | + if(json.command === "save"){ |
| 98 | + var array = sessions[sessionIndex[connection.id - 1]]; |
| 99 | + for(var i = 0; i < array.length; i++){ |
| 100 | + if(array[i] !== connection.id){ |
| 101 | + clients[array[i]-1].send(JSON.stringify({type:"command",command:"save"})); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + if(json.command === "rename"){ |
| 106 | + var newTitle = json.title; |
| 107 | + var array = sessions[sessionIndex[connection.id - 1]]; |
| 108 | + for(var i = 0; i < array.length; i++){ |
| 109 | + if(array[i] !== connection.id){ |
| 110 | + clients[array[i]-1].send(JSON.stringify({type:"command",command:"rename", title: newTitle})); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + if(json.command === "mode"){ |
| 115 | + var newMode = json.mode; |
| 116 | + var array = sessions[sessionIndex[connection.id - 1]]; |
| 117 | + for(var i = 0; i < array.length; i++){ |
| 118 | + if(array[i] !== connection.id){ |
| 119 | + clients[array[i]-1].send(JSON.stringify({type:"command",command:"mode", mode: newMode})); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + if(json.command === "theme"){ |
| 124 | + var newTheme = json.theme; |
| 125 | + var array = sessions[sessionIndex[connection.id - 1]]; |
| 126 | + for(var i = 0; i < array.length; i++){ |
| 127 | + if(array[i] !== connection.id){ |
| 128 | + clients[array[i]-1].send(JSON.stringify({type:"command",command:"theme", theme: newTheme})); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + if(json.type === "comment"){ |
| 134 | + var text = json.comment; |
| 135 | + var name = json.sender; |
| 136 | + var mail = json.email; |
| 137 | + // |
| 138 | + var output = name + "\n" + mail + "\n" + text + "\n*******************************\n"; |
| 139 | + fs.appendFile('comments.txt', output, function (err) { |
| 140 | + |
| 141 | + }); |
| 142 | + } |
| 143 | + if(json.type === "update"){ |
| 144 | + |
| 145 | + var array = sessions[sessionIndex[connection.id - 1]]; |
| 146 | + for(var i = 0; i < array.length; i++){ |
| 147 | + |
| 148 | + clients[array[i]-1].send(JSON.stringify({type:"update", name: json.name})); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + /******** |
| 153 | + SSH |
| 154 | + *********/ |
| 155 | + if(json.type === "ssh"){ |
| 156 | + if(json.ssh === "init"){ |
| 157 | + // |
| 158 | + ssh_port = json.port; |
| 159 | + ssh_host = json.host; |
| 160 | + ssh_login = json.login; |
| 161 | + ssh_pass = json.password; |
| 162 | + startSSH(); |
| 163 | + } |
| 164 | + if(json.ssh === "folder"){ |
| 165 | + ssh_dir(json.path); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + catch(e){ |
| 170 | + } |
| 171 | + }); |
| 172 | + // user disconnected |
| 173 | + connection.on('close', function(connection) { |
| 174 | + try{ |
| 175 | + var index = connection.id - 1; |
| 176 | + var id = connection.id; |
| 177 | + var s = sessionIndex[index]; |
| 178 | + sessionIndex.splice(index, 1); |
| 179 | + clients.splice(index, 1); |
| 180 | + |
| 181 | + var ind = sessions[s].indexOf(id); |
| 182 | + sessions[s].splice(ind, 1); |
| 183 | + if(session[s].length === 0){ |
| 184 | + sessions.splice(s,1); |
| 185 | + } |
| 186 | + FILE.splice(s, 1); |
| 187 | + |
| 188 | + } |
| 189 | + catch(e){} |
| 190 | + }); |
| 191 | + |
| 192 | +function addToSession(session, id){ |
| 193 | + for(var i = 0; i < FILE.length; i++){ |
| 194 | + if(FILE[i] === session){ |
| 195 | + sessionIndex.push(i); |
| 196 | + sessions[i].push(id); |
| 197 | + return; |
| 198 | + } |
| 199 | + } |
| 200 | + addSession(session); |
| 201 | + addToSession(session, id); |
| 202 | +} |
| 203 | +function addSession(session){ |
| 204 | + FILE.push(session); |
| 205 | + var index = FILE.indexOf(session); |
| 206 | + sessions[index] = []; |
| 207 | +} |
| 208 | +function removeSession(session){ |
| 209 | + var index = FILE.indexOf(session); |
| 210 | + // |
| 211 | + FILE.splice(index,1); |
| 212 | + sessions.split(index,1); |
| 213 | +} |
| 214 | +/*********** |
| 215 | +SSH |
| 216 | +************/ |
| 217 | +var c; |
| 218 | +function startSSH(){ |
| 219 | + c = new Connection(); |
| 220 | + c.on('ready', function() { |
| 221 | + ssh_dir('/'); |
| 222 | + }); |
| 223 | + c.on('error', function(err) { |
| 224 | + //console.log('Connection :: error :: ' + err); |
| 225 | + }); |
| 226 | + c.on('end', function() { |
| 227 | + //console.log('Connection :: end'); |
| 228 | + }); |
| 229 | + c.on('close', function(had_error) { |
| 230 | + //console.log('Connection :: close'); |
| 231 | + }); |
| 232 | + c.connect({ |
| 233 | + host: ssh_host, |
| 234 | + port: ssh_port, |
| 235 | + username: ssh_login, |
| 236 | + password: ssh_pass |
| 237 | + }); |
| 238 | + ssh_dir('/'); |
| 239 | +} |
| 240 | +function stopSSH(){ |
| 241 | + sftp.close(handle, function(err) { |
| 242 | + if (err) throw err; |
| 243 | + //console.log('SFTP :: Handle closed'); |
| 244 | + sftp.end(); |
| 245 | + }); |
| 246 | +} |
| 247 | +function ssh_dir(path){ |
| 248 | + c.sftp(function(err, sftp) { |
| 249 | + if (err){ |
| 250 | + connection.send(JSON.stringify({type: "bad"})); |
| 251 | + throw err; |
| 252 | + } |
| 253 | + connection.send(JSON.stringify({type: "ok"})); |
| 254 | + sftp.on('end', function() { |
| 255 | + }); |
| 256 | + sftp.opendir(path, function readdir(err, handle) { |
| 257 | + if (err) throw err; |
| 258 | + sftp.readdir(handle, function(err, list) { |
| 259 | + if (err) throw err; |
| 260 | + if (list === false) { |
| 261 | + return; |
| 262 | + } |
| 263 | + var array = []; |
| 264 | + for(var i = 0; i < list.length; i++){ |
| 265 | + array.push(list[i].filename); |
| 266 | + } |
| 267 | + connection.send(JSON.stringify({type: "ssh", ssh: "folder", path: path, data: array})); |
| 268 | + readdir(undefined, handle); |
| 269 | + }); |
| 270 | + }); |
| 271 | + }); |
| 272 | +} |
| 273 | +}); |
0 commit comments