Skip to content

Add optional functionality to get extended results and/or restrict to specific specified interfaces. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ var dev_ip = require('dev-ip');
dev_ip.getIp(); // "192.168.1.76" or false if nothing found (ie, offline user)
```

## Requesting full-blown objects as results

```javascript
var dev_ip = require('dev-ip');
dev_ip.getIp(null, { full: true }); // eg: { name: "en0", ip: "10.104.103.181", address: { ..., address: "10.104.103.181", ... }
```

## Requesting results only from selected interfaces

```javascript
var dev_ip = require('dev-ip');
dev_ip.getIp(null, { dev: [ "en0", "en1" ] });
```

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Run lint & tests with `gulp`.

Expand Down
22 changes: 18 additions & 4 deletions lib/dev-ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,32 @@ var messages = {
error: "Couldn't find a suitable IP for you to use. (You're probably offline!)"
};

exports.getIp = function (env) {
exports.getIp = function (env, opts) {

if (opts == null) {
opts = {};
}

var networkInterfaces = os.networkInterfaces();

var matches = [];
var returnValue;

// loop through results and check that it's an IPv4 address & it's not internal
_.each(networkInterfaces, function (_interface) {
_.each(networkInterfaces, function (_interface, _name) {
_.each(_interface, function (address) {
if (address.internal === false && address.family === "IPv4") {
matches.push(address.address);
var iface_match = (opts.dev == null) || (opts.dev === _name) || _.contains(opts.dev, _name);
if (address.internal === false && address.family === "IPv4" && iface_match) {
if (opts.full) {
var info = {
name: _name,
ip: address.address,
address: address
};
matches.push(info);
} else {
matches.push(address.address);
}
}
});
});
Expand Down
127 changes: 127 additions & 0 deletions test/devip.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ var devIp = require("../lib/dev-ip");
var respNone = require("./fixtures/resp-none");
var respSingle = require("./fixtures/resp-single");
var respMultiple = require("./fixtures/resp-multiple");
var respMany = require("./fixtures/resp-many");
var sinon = require("sinon");
var assert = require("chai").assert;
var os = require("os");

// From the resp files
var match1 = "10.104.103.181";
var match2 = "10.104.100.12";
var match3 = "192.168.2.1";
var match4 = "192.168.56.1";

var regex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;

Expand Down Expand Up @@ -79,3 +82,127 @@ describe("Getting the IP with no results", function () {
assert.equal(actual, expected);
});
});

describe("Getting full info with a single result", function () {
var osStub;
var result;
before(function () {
osStub = sinon.stub(os, "networkInterfaces").returns(respSingle);
});
beforeEach(function () {
result = devIp.getIp(null, {full: true});
});
after(function () {
osStub.restore();
});
it("should return the full info object when a single match found", function () {
var expected = {
name: "en0",
ip: "10.104.103.181",
address: {
address: "10.104.103.181",
family: "IPv4",
internal: false
}
};
assert.deepEqual(result, expected);
});
});

describe("Getting full info with Multiple results", function () {
var osStub;
var result;
before(function () {
osStub = sinon.stub(os, "networkInterfaces").returns(respMultiple);
});
beforeEach(function () {
result = devIp.getIp(null, {full: true});
});
after(function () {
osStub.restore();
});
it("should return an array of results", function () {
var expected1 = {
name: "en0",
ip: "10.104.103.181",
address: {
address: "10.104.103.181",
family: "IPv4",
internal: false
}
};
var expected2 = {
name: "en0",
ip: "10.104.100.12",
address: {
address: "10.104.100.12",
family: "IPv4",
internal: false
}
};

assert.deepEqual(result[0], expected1);
assert.deepEqual(result[1], expected2);
});
});

describe("Getting full info with no results", function () {
var osStub;
var result;
before(function () {
osStub = sinon.stub(os, "networkInterfaces").returns(respNone);
});
after(function () {
osStub.restore();
});
it("should return false", function () {
var actual = devIp.getIp(null, {full: true});
assert.isFalse(actual);
});
it("should return an error message if used on command line", function () {
var actual = devIp.getIp("cli", {full: true});
var expected = "Couldn't find a suitable IP for you to use. (You're probably offline!)";
assert.equal(actual, expected);
});
});

describe("Getting the IPs with many results", function () {
var osStub;
var result;
before(function () {
osStub = sinon.stub(os, "networkInterfaces").returns(respMany);
});
beforeEach(function () {
result = devIp.getIp(null);
});
after(function () {
osStub.restore();
});
it("should return only requested values", function () {
assert.equal(result.length, 4);
assert.equal(result[0], match1);
assert.equal(result[1], match2);
assert.equal(result[2], match3);
assert.equal(result[3], match4);
});
});

describe("Getting only IPs from requested interfaces with many results", function () {
var osStub;
var result;
before(function () {
osStub = sinon.stub(os, "networkInterfaces").returns(respMany);
});
beforeEach(function () {
result = devIp.getIp(null, {dev: ["en0", "en1"]});
});
after(function () {
osStub.restore();
});
it("should return only requested values", function () {
assert.equal(result.length, 3);
assert.equal(result[0], match1);
assert.equal(result[1], match2);
assert.equal(result[2], match3);
});
});
59 changes: 59 additions & 0 deletions test/fixtures/resp-many.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = {
lo0: [
{
address: 'fe80::1',
family: 'IPv6',
internal: true
},
{
address: '127.0.0.1',
family: 'IPv4',
internal: true
},
{
address: '::1',
family: 'IPv6',
internal: true
}
],
en0: [
{
address: 'fe80::22c9:d0ff:fe44:6415',
family: 'IPv6',
internal: false },
{
address: '10.104.103.181',
family: 'IPv4',
internal: false
},
{
address: '10.104.100.12',
family: 'IPv4',
internal: false
}
],
en1: [
{
address: 'fe80::22c9:d0ff:fe44:6415',
family: 'IPv6',
internal: false
},
{
address: '192.168.2.1',
family: 'IPv4',
internal: false
}
],
en2: [
{
address: 'fe80::22c9:d0ff:fe44:6415',
family: 'IPv6',
internal: false
},
{
address: '192.168.56.1',
family: 'IPv4',
internal: false
}
]
}