From 11d236eb9f49710e5cf685c076911bf2f757d312 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sun, 21 Oct 2012 23:18:55 +0200 Subject: [PATCH] connection and ping support --- README.md | 12 ++++---- lib/ns_client.js | 73 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 62fa1e4..4d4c23e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Chromesoul is a minimalist NetSoul client for Google Chrome. It aim to connect t ## Why? -When you're on the PIE, you're stuck in it unless you are connected to netsoul, and with multiple boot and mobile devices it's a real pain to configure many NetSoul clients. That's why chromesoul is awesome: not only it's a multi-platforme NS client, but with the Chrome extension synchronisation it's automaticaly installed. +When you're on the PIE, you're stuck in it unless you are connected to netsoul, and with multiple boot and mobile devices it's a real pain to configure many NetSoul clients. That's why chromesoul is awesome: not only it's a cross-platform NS client, but with the Chrome synchronisation it's automaticaly installed and configured. ## Requirements @@ -15,18 +15,18 @@ Sockets have been introduced in Chromium 24. Therefore, it is required to have a ## Features -### Active state -Staying in an active state is the main purpose of this extension. It will be implemented as soon as possible. - ### Password encryption -Storing your socks password encrypted is a priority but is not available yet. Yes, the chromesoul dev version is not secured; so is the official NetSoul server which doesn't store a hash but the password itself (it's a requirement from the NetSoul protocol). +Storing your socks password encrypted is a priority but is not available yet. Yes, the chromesoul dev version is unsafe; so is the official NetSoul server which doesn't store a hash but the password itself (it's a requirement from the NetSoul protocol). ### Messages -Because the only purpose of this extension is to provide an access to internet when you're on the PIE, there is no plan to support messages at this time. Thoses stupid guys who have fun spamming everyone by broadcasting messages are the second reasons why you won't see any message using chromesoul. +Because the only purpose of this extension is to provide an access to internet when you're on the PIE, there is no plan to support messages at this time. Thoses stupid guys who have fun spamming everyone by broadcasting messages are the second reasons why you won't see any message using chromesoul. Maybe one day I'll write a chat interface, however it will be unobtrusive. ### Contacts If you cannot send and receive messages, you don't need a contact list. +### State change +Same as contacts. Why changing your state if you're not gonna talk? + ## Licence diff --git a/lib/ns_client.js b/lib/ns_client.js index cb9f529..c7f2a67 100644 --- a/lib/ns_client.js +++ b/lib/ns_client.js @@ -19,20 +19,52 @@ var NsClient = function() { this.state = "actif"; this.allowed_statuses = ["actif", "away", "idle", "lock"]; this.is_connected = false; + this.socket = null; }; NsClient.prototype.connect = function() { var cnt = function(elem) { return function(infos) { if (typeof infos.login !== "undefined" && typeof infos.pwd_socks !== "undefined") { - elem.is_connected = true; - console.info("connected to the netsoul server"); - elem.updateStatus(); + chrome.socket.create('tcp', {}, function(sock_inf) { + elem.socket = sock_inf.socketId; + chrome.socket.connect(elem.socket, "ns-server.epita.fr", 4242, function(res) { + chrome.socket.read(elem.socket, null, function(rd_inf) { + if (rd_inf.resultCode > 0) { + var data = ab2str(rd_inf.data).split(' '), + auth = "ext_user_log "; + auth += infos.login + " "; + auth += hex_md5(data[2] + "-" + data[3] + "/" + data[4] + infos.pwd_socks) + " "; + auth += "chromesoul chromesoul\n"; + console.log("recv: " + ab2str(rd_inf.data)); + console.log("sent: auth_ag ext_user none none\n"); + chrome.socket.write(elem.socket, str2ab("auth_ag ext_user none none\n"), function(w_inf) { + chrome.socket.read(elem.socket, null, function(rd_inf) { + if (rd_inf.resultCode > 0) { + console.log("recv: " + ab2str(rd_inf.data)); + console.log("sent: " + auth); + chrome.socket.write(elem.socket, str2ab(auth), function(w_inf) { + chrome.socket.read(elem.socket, null, function(rd_inf) { + if (rd_inf.resultCode > 0) { + console.log("recv: " + ab2str(rd_inf.data)); + elem.is_connected = true; + console.info("connected to the netsoul server"); + elem.updateStatus(); + elem.daemonize(); + } + }); + }); + } + }); + }); + } + }); + }); + }); } }; }; - console.log('connecting...'); if (!this.is_connected) { this.storage.get(null, cnt(this)); } else { @@ -42,9 +74,32 @@ NsClient.prototype.connect = function() { NsClient.prototype.disconnect = function() { if (this.is_connected) { + chrome.socket.disconnect(this.socket); this.is_connected = false; console.info("disconnected"); - this.updateStatus(); + } else { + console.warn("not connected"); + } +}; + +NsClient.prototype.daemonize = function() { + var dm = function(elem) { + return function(rd_inf) { + if (rd_inf.resultCode > 0) { + var data = ab2str(rd_inf.data); + console.log("- recv: " + data); + if (data.substr(0, 5) === "ping ") { + console.log("- sent: " + data); + chrome.socket.write(elem.socket, rd_inf.data, function(w_inf) { + chrome.socket.read(elem.socket, null, this); + }); + } + } + }; + }; + + if (this.is_connected) { + chrome.socket.read(this.socket, null, dm(this)); } else { console.warn("not connected"); } @@ -52,7 +107,11 @@ NsClient.prototype.disconnect = function() { NsClient.prototype.updateStatus = function() { if (this.is_connected) { - console.info("updating status"); + var status_msg = "user_cmd state "; + status_msg += this.state + ":"; + status_msg += Math.round(new Date().getTime() / 1000) + "\n"; + console.log("+send: " + status_msg); + chrome.socket.write(this.socket, str2ab(status_msg), function(w_inf) {}); } else { console.warn("not connected"); } @@ -94,6 +153,6 @@ NsClient.prototype.init = function() { }; con(this).apply(); - setInterval(con(this), 60000); + setInterval(con(this), 10000); setInterval(upd(this), 600000); };