From 2b8ec51abb846040efcbbb1d4c2b6d1e4b6bab01 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 7 Nov 2013 13:32:56 +0100 Subject: webchat: refactored hello_web.js --- webchat/hello_web.js | 81 +++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 36 deletions(-) (limited to 'webchat/hello_web.js') diff --git a/webchat/hello_web.js b/webchat/hello_web.js index 8e2a0bb8..076097f0 100644 --- a/webchat/hello_web.js +++ b/webchat/hello_web.js @@ -4,59 +4,68 @@ var sockjs = require('sockjs'); var connect = require('connect'); var irc = require('irc'); var Clients = []; -Clients.broadcast = function(object) { + +Clients.broadcast = function(object) { //broadcast to all clients Clients.forEach(function(client) { client.write(JSON.stringify(object)); }); } -var pingTimeoutDelay = 5*60*1000 -var lastping = setTimeout(reconnect, pingTimeoutDelay) -var irc_client = new irc.Client('irc.freenode.net', 'kweb', { - channels: ['#krebs'], - sasl: true, +var irc_reconnect = function() { //reconnt to irc + console.log("reconnecting due to pingtimeout"); + irc_client.disconnect(); + irc_client.connect(); +} + +var pingTimeoutDelay = 5*60*1000 +var lastping = setTimeout(irc_reconnect, pingTimeoutDelay) + +var irc_client = new irc.Client('irc.freenode.net', 'kweb', { //create irc_client to talk to irc + channels: ['#krebs'], //todo: read from local_config + sasl: true, secure: true, - userName: 'kweb', - realName: 'kweb', + userName: 'kweb', //todo: read from local_config + realName: 'kweb', //todo: read from local_config password: fs.readFileSync(__dirname+'/local_config/irc.key').toString(), - debug: true, + debug: false, showErrors: true, + floodProtection: true, port: 6697, autoRejoin: true, autoConnect: true, stripColors: true }); -var reconnect = function() { - console.log("reconnecting due to pingtimeout"); - irc_client.disconnect(); - irc_client.connect(); -} -irc_client.on('ping', function(server) { +irc_client.on('ping', function(server) { //restart timer on server ping console.log("got ping from server, renewing timeout for automatic reconnect"); clearTimeout(lastping); - lastping = setTimeout(reconnect, pingTimeoutDelay); + lastping = setTimeout(irc_reconnect, pingTimeoutDelay); //reconnect after irc timeout }) irc_client.on('message#krebs', function(from, message) { console.log({ from: from, message: message }); - Clients.broadcast({ from: from, message: message }); + Clients.broadcast({ from: from, message: message }); //broadcast irc messages to all connected clients clearTimeout(lastping); }); +var web_serv_options = { //certificates for https + key: fs.readFileSync(__dirname+'/local_config/server_npw.key'), + cert: fs.readFileSync(__dirname+'/local_config/server.crt'), +}; + var echo = sockjs.createServer(); echo.on('connection', function(conn) { var origin = conn.remoteAddress; Clients.push(conn); Clients.broadcast({from: 'system', message: origin + ' has joined'}) - irc_client.say("#krebs", origin + ' has joined'); - conn.write(JSON.stringify({from: 'system', message: 'hello'})) +// irc_client.say("#krebs", origin + ' has joined'); + conn.write(JSON.stringify({from: 'system', message: 'hello'})) //welcome message conn.on('data', function(data) { console.log('data:',data); try { var object = JSON.parse(data); - if (/^\/nick\s+(.+)$/.test(object.message)) { + if (/^\/nick\s+(.+)$/.test(object.message)) { //if nick is send use nick instead of ip object.from = origin; } else if (typeof object.nick === 'string') { object.from = object.nick; @@ -71,35 +80,35 @@ echo.on('connection', function(conn) { console.log(error); } }); -conn.on('close', function() { +conn.on('close', function() { //propagate if client quits the page Clients.splice(Clients.indexOf(conn)); Clients.broadcast({from: 'system', message: origin + ' has quit'}) - irc_client.say("#krebs", origin + ' has quit'); +// irc_client.say("#krebs", origin + ' has quit'); }); }); -var options = { - key: fs.readFileSync(__dirname+'/local_config/server_npw.key'), - cert: fs.readFileSync(__dirname+'/local_config/server.crt'), -}; var app = connect() .use(connect.logger('dev')) .use(connect.static(__dirname+'/public')) .use( function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); - res.write(''); - res.write(''); - res.write(''); - res.write(''); - res.write(''); - res.write('
'); - res.write('hello, this is the official krebs support:
'); - res.write('
'); - res.end('
'); + page_template='\n'; + page_template+='\n'; + page_template+='\n'; + page_template+='\n'; + page_template+='\n'; + page_template+='
'; + page_template+='
'; + page_template+='
'; + page_template+='hello, this is the official krebs support:
\n'; + page_template+='
\n'; + page_template+='
'; + page_template+='
'; + res.end(page_template); }) -var server = http.createServer(options, app); +var server = http.createServer(web_serv_options, app); echo.installHandlers(server, {prefix:'/echo'}); server.listen(1337, '0.0.0.0'); console.log('Server running at https://127.0.0.1:1337/'); -- cgit v1.2.3