Merge pull request #240 from maxpoulin64/webirc

Implement WEBIRC
This commit is contained in:
Jérémie Astori 2016-05-02 00:54:45 -04:00
commit ba26724748
5 changed files with 96 additions and 6 deletions

View File

@ -35,6 +35,15 @@ module.exports = {
// //
bind: undefined, bind: undefined,
//
// Sets whether the server is behind a reverse proxy and should honor the
// X-Forwarded-For header or not.
//
// @type boolean
// @default false
//
reverseProxy: false,
// //
// Set the default theme. // Set the default theme.
// //
@ -97,6 +106,25 @@ module.exports = {
// //
lockNetwork: false, lockNetwork: false,
//
// WEBIRC support
//
// If enabled, The Lounge will pass the connecting user's host and IP to the
// IRC server. Note that this requires to obtain a password from the IRC network
// The Lounge will be connecting to and generally involves a lot of trust from the
// network you are connecting to.
//
// Format (standard): {"irc.example.net": "hunter1", "irc.example.org": "passw0rd"}
// Format (function):
// {"irc.example.net": function(client, args, trusted) {
// // here, we return a webirc object fed directly to `irc-framework`
// return {password: "hunter1", address: args.ip, hostname: "webirc/"+args.hostname};
// }}
//
// @type string | function(client, args):object(webirc)
// @default null
webirc: null,
// //
// Log settings // Log settings
// //

View File

@ -129,6 +129,7 @@ Client.prototype.connect = function(args) {
var client = this; var client = this;
var nick = args.nick || "lounge-user"; var nick = args.nick || "lounge-user";
var webirc = null;
var network = new Network({ var network = new Network({
name: args.name || "", name: args.name || "",
@ -138,7 +139,9 @@ Client.prototype.connect = function(args) {
password: args.password, password: args.password,
username: args.username || nick.replace(/[^a-zA-Z0-9]/g, ""), username: args.username || nick.replace(/[^a-zA-Z0-9]/g, ""),
realname: args.realname || "The Lounge User", realname: args.realname || "The Lounge User",
commands: args.commands commands: args.commands,
ip: args.ip,
hostname: args.hostname,
}); });
client.networks.push(network); client.networks.push(network);
@ -169,6 +172,26 @@ Client.prototype.connect = function(args) {
return; return;
} }
if (config.webirc !== null && network.host in config.webirc) {
args.ip = args.ip || (client.config && client.config.ip) || client.ip;
args.hostname = args.hostname || (client.config && client.config.hostname) || client.hostname || args.ip;
if (args.ip) {
if (config.webirc[network.host] instanceof Function) {
webirc = config.webirc[network.host](client, args);
} else {
webirc = {
password: config.webirc[network.host],
address: args.ip,
hostname: args.hostname
};
}
} else {
log.warn("Cannot find a valid WEBIRC configuration for " + nick
+ "!" + network.username + "@" + network.host);
}
}
network.irc = new ircFramework.Client(); network.irc = new ircFramework.Client();
network.irc.requestCap([ network.irc.requestCap([
"echo-message", "echo-message",
@ -185,6 +208,7 @@ Client.prototype.connect = function(args) {
localAddress: config.bind, localAddress: config.bind,
rejectUnauthorized: false, rejectUnauthorized: false,
auto_reconnect: false, // TODO: Enable auto reconnection auto_reconnect: false, // TODO: Enable auto reconnection
webirc: webirc,
}); });
network.irc.on("registered", function() { network.irc.on("registered", function() {

View File

@ -16,6 +16,8 @@ function Network(attr) {
username: "", username: "",
realname: "", realname: "",
channels: [], channels: [],
ip: null,
hostname: null,
id: id++, id: id++,
irc: null, irc: null,
serverOptions: { serverOptions: {
@ -45,7 +47,9 @@ Network.prototype.export = function() {
"password", "password",
"username", "username",
"realname", "realname",
"commands" "commands",
"ip",
"hostname"
]); ]);
network.nick = (this.irc && this.irc.user.nick) || ""; network.nick = (this.irc && this.irc.user.nick) || "";
network.join = _.map( network.join = _.map(

View File

@ -6,6 +6,7 @@ var ClientManager = require("./clientManager");
var express = require("express"); var express = require("express");
var fs = require("fs"); var fs = require("fs");
var io = require("socket.io"); var io = require("socket.io");
var dns = require("dns");
var Helper = require("./helper"); var Helper = require("./helper");
var config = {}; var config = {};
@ -19,8 +20,6 @@ module.exports = function(options) {
.use(index) .use(index)
.use(express.static("client")); .use(express.static("client"));
app.enable("trust proxy");
var server = null; var server = null;
var https = config.https || {}; var https = config.https || {};
var protocol = https.enable ? "https" : "http"; var protocol = https.enable ? "https" : "http";
@ -73,6 +72,14 @@ module.exports = function(options) {
} }
}; };
function getClientIp(req) {
if (!config.reverseProxy) {
return req.connection.remoteAddress;
} else {
return req.headers["x-forwarded-for"] || req.connection.remoteAddress;
}
}
function index(req, res, next) { function index(req, res, next) {
if (req.url.split("?")[0] !== "/") { if (req.url.split("?")[0] !== "/") {
return next(); return next();
@ -110,6 +117,9 @@ function init(socket, client, token) {
socket.on( socket.on(
"conn", "conn",
function(data) { function(data) {
// prevent people from overriding webirc settings
data.ip = null;
data.hostname = null;
client.connect(data); client.connect(data);
} }
); );
@ -185,6 +195,20 @@ function init(socket, client, token) {
} }
} }
function reverseDnsLookup(socket, client, token) {
client.ip = getClientIp(socket.request);
dns.reverse(client.ip, function(err, host) {
if (!err && host.length) {
client.hostname = host[0];
} else {
client.hostname = client.ip;
}
init(socket, client, token);
});
}
function auth(data) { function auth(data) {
var socket = this; var socket = this;
if (config.public) { if (config.public) {
@ -194,7 +218,11 @@ function auth(data) {
manager.clients = _.without(manager.clients, client); manager.clients = _.without(manager.clients, client);
client.quit(); client.quit();
}); });
init(socket, client); if (config.webirc) {
reverseDnsLookup(socket, client);
} else {
init(socket, client);
}
} else { } else {
var success = false; var success = false;
_.each(manager.clients, function(client) { _.each(manager.clients, function(client) {
@ -212,7 +240,11 @@ function auth(data) {
if (data.remember || data.token) { if (data.remember || data.token) {
token = client.token; token = client.token;
} }
init(socket, client, token); if (config.webirc !== null && !client.config["ip"]) {
reverseDnsLookup(socket, client, token);
} else {
init(socket, client, token);
}
return false; return false;
} }
}); });

View File

@ -24,6 +24,8 @@ describe("Network", function() {
commands: [], commands: [],
nick: "", nick: "",
join: "#thelounge,&foobar", join: "#thelounge,&foobar",
ip: null,
hostname: null
}); });
}); });
}); });