Skip to content

Commit

Permalink
Merge pull request thelounge#749 from thelounge/xpaw/hexip
Browse files Browse the repository at this point in the history
Add support for hexip ilines and fix storing client ip in config
  • Loading branch information
astorije authored Dec 11, 2016
2 parents 55599ad + 84ae9ed commit 8d3312d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
11 changes: 11 additions & 0 deletions defaults/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ module.exports = {
//
lockNetwork: false,

//
// Hex IP
//
// If enabled, clients' username will be set to their IP encoded has hex.
// This is done to share the real user IP address with the server for host masking purposes.
//
// @type boolean
// @default false
//
useHexIp: false,

//
// WEBIRC support
//
Expand Down
38 changes: 34 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ Client.prototype.connect = function(args) {
});
}

args.ip = args.ip || (client.config && client.config.ip) || client.ip;
args.hostname = args.hostname || (client.config && client.config.hostname) || client.hostname;

var network = new Network({
name: args.name || "",
host: args.host || "",
Expand Down Expand Up @@ -219,8 +222,9 @@ Client.prototype.connect = function(args) {
}

if (config.webirc && 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.hostname) {
args.hostname = args.ip;
}

if (args.ip) {
if (config.webirc[network.host] instanceof Function) {
Expand Down Expand Up @@ -259,7 +263,7 @@ Client.prototype.connect = function(args) {
host: network.host,
port: network.port,
nick: nick,
username: network.username,
username: config.useHexIp ? Helper.ip2hex(args.ip) : network.username,
gecos: network.realname,
password: network.password,
tls: network.tls,
Expand All @@ -270,6 +274,8 @@ Client.prototype.connect = function(args) {
auto_reconnect_max_retries: 360, // At least one hour (plus timeouts) worth of reconnections
webirc: webirc,
});

client.save();
};

Client.prototype.updateToken = function(callback) {
Expand Down Expand Up @@ -456,7 +462,31 @@ Client.prototype.quit = function() {
};

Client.prototype.clientAttach = function(socketId) {
this.attachedClients[socketId] = this.lastActiveChannel;
var client = this;
var save = false;

client.attachedClients[socketId] = client.lastActiveChannel;

// Update old networks to store ip and hostmask
client.networks.forEach(network => {
if (!network.ip) {
save = true;
network.ip = (client.config && client.config.ip) || client.ip;
}

if (!network.hostname) {
var hostmask = (client.config && client.config.hostname) || client.hostname;

if (hostmask) {
save = true;
network.hostmask = hostmask;
}
}
});

if (save) {
client.save();
}
};

Client.prototype.clientDetach = function(socketId) {
Expand Down
19 changes: 19 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var _ = require("lodash");
var path = require("path");
var os = require("os");
var fs = require("fs");
var net = require("net");
var bcrypt = require("bcrypt-nodejs");

var Helper = {
Expand All @@ -15,6 +16,7 @@ var Helper = {
setHome: setHome,
getVersion: getVersion,
getGitCommit: getGitCommit,
ip2hex: ip2hex,

password: {
hash: passwordHash,
Expand Down Expand Up @@ -75,6 +77,23 @@ function getUserLogsPath(name, network) {
return path.join(this.HOME, "logs", name, network);
}

function ip2hex(address) {
// no ipv6 support
if (!net.isIPv4(address)) {
return "00000000";
}

return address.split(".").map(function(octet) {
var hex = parseInt(octet, 10).toString(16);

if (hex.length === 1) {
hex = "0" + hex;
}

return hex;
}).join("");
}

function expandHome(shortenedPath) {
var home;

Expand Down
2 changes: 2 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ function init(socket, client) {
} else {
socket.emit("authorized");

client.ip = getClientIp(socket.request);

socket.on("disconnect", function() {
client.clientDetach(socket.id);
});
Expand Down
19 changes: 19 additions & 0 deletions test/tests/hexip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

const expect = require("chai").expect;
const Helper = require("../../src/helper");

describe("HexIP", function() {
it("should correctly convert IPv4 to hex", function() {
expect(Helper.ip2hex("66.124.160.150")).to.equal("427ca096");
expect(Helper.ip2hex("127.0.0.1")).to.equal("7f000001");
expect(Helper.ip2hex("0.0.0.255")).to.equal("000000ff");
});

it("unsupported addresses return default", function() {
expect(Helper.ip2hex("0.0.0.999")).to.equal("00000000");
expect(Helper.ip2hex("localhost")).to.equal("00000000");
expect(Helper.ip2hex("::1")).to.equal("00000000");
expect(Helper.ip2hex("2606:2800:220:1:248:1893:25c8:1946")).to.equal("00000000");
});
});

0 comments on commit 8d3312d

Please sign in to comment.