Skip to content
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

Parse IPv6 host & numeric host on Linux #94

Merged
merged 6 commits into from
Nov 30, 2019
Merged
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

a ping wrapper for nodejs

@last-modified: 2019-03-09
@last-modified: 2019-06-07

# LICENSE MIT

Expand Down Expand Up @@ -143,6 +143,11 @@ them are expected to be cross platform.
* By setting `numeric`, `timeout` or `min_reply` to false, you can run `ping`
without corresponding arguments.

* For LINUX users, since we have enable `shell` option in child_process.spawn,
make sure arguments you are passing in into this library are sanitized.
Otherwise, any input containing shell metacharacters may be used to trigger
arbitrary command

# FAQ

* It does not work with busybox's ping implemetation [#89](https://github.com/danielzzz/node-ping/issues/89)
Expand Down
4 changes: 2 additions & 2 deletions lib/builder/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ factory.isPlatformSupport = function (p) {
* Return a path to the ping executable in the system
* @param {string} platform - Name of the platform
* @param {bool} v6 - Ping via ipv6 or not
* @return {object} - Argument builder
* @return {string} - Executable path for system command ping
* @throw if given platform is not supported
*/
factory.getExecutablePath = function (platform, v6) {
Expand All @@ -75,7 +75,7 @@ factory.getExecutablePath = function (platform, v6) {
if (platform === 'aix') {
ret = '/usr/sbin/ping';
} else if (factory.isLinux(platform)) {
ret = v6 ? '/sbin/ping6' : '/bin/ping';
ret = v6 ? 'ping6' : 'ping';
} else if (factory.isWindow(platform)) {
ret = process.env.SystemRoot + '/system32/ping.exe';
} else if (factory.isMacOS(platform)) {
Expand Down
12 changes: 11 additions & 1 deletion lib/builder/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var defaultConfig = {
* @param {PingConfig} [config] - Configuration object for cmd line argument
* @return {string[]} - Command line argument according to the configuration
*/
builder.getResult = function (target, config) {
builder.getCommandArguments = function (target, config) {
var _config = config || {};

// Empty argument
Expand Down Expand Up @@ -83,4 +83,14 @@ builder.getResult = function (target, config) {
return ret;
};

/**
* Compute an option object for child_process.spawn
* @return {object} - Refer to document of child_process.spawn
*/
builder.getSpawnOptions = function () {
return {
shell: true,
};
};

module.exports = builder;
11 changes: 10 additions & 1 deletion lib/builder/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var defaultConfig = {
* @return {string[]} - Command line argument according to the configuration
* @throws If there are errors on building arguments with given inputs
*/
builder.getResult = function (target, config) {
builder.getCommandArguments = function (target, config) {
var _config = config || {};

// Empty argument
Expand Down Expand Up @@ -89,4 +89,13 @@ builder.getResult = function (target, config) {
return ret;
};

/**
* Compute an option object for child_process.spawn
* @return {object} - Refer to document of child_process.spawn
*/
builder.getSpawnOptions = function () {
return {};
};


module.exports = builder;
10 changes: 9 additions & 1 deletion lib/builder/win.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var defaultConfig = {
* @param {PingConfig} [config] - Configuration object for cmd line argument
* @return {string[]} - Command line argument according to the configuration
*/
builder.getResult = function (target, config) {
builder.getCommandArguments = function (target, config) {
var _config = config || {};

// Empty argument
Expand Down Expand Up @@ -88,4 +88,12 @@ builder.getResult = function (target, config) {
return ret;
};

/**
* Compute an option object for child_process.spawn
* @return {object} - Refer to document of child_process.spawn
*/
builder.getSpawnOptions = function () {
return {};
};

module.exports = builder;
14 changes: 11 additions & 3 deletions lib/parser/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ util.inherits(LinuxParser, base);
LinuxParser.prototype._processHeader = function (line) {
// Get host and numeric_host
var tokens = line.split(' ');

this._response.host = tokens[1];
this._response.numeric_host = tokens[2].slice(1, -1);
var isProbablyIPv4 = tokens[1].indexOf('(') === -1;

if (isProbablyIPv4) {
this._response.host = tokens[1];
this._response.numeric_host = tokens[2].slice(1, -1);
} else {
// Normalise into either a 2 or 3 element array
var foundAddresses = tokens.slice(1, -3).join('').match(/([^\s()]+)/g);
this._response.host = foundAddresses.shift();
this._response.numeric_host = foundAddresses.pop();
}

this._changeState(this.STATES.BODY);
};
Expand Down
8 changes: 5 additions & 3 deletions lib/ping-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ function probe(addr, config) {
var platform = os.platform();
try {
var argumentBuilder = builderFactory.createBuilder(platform);
ping = cp.spawn(
builderFactory.getExecutablePath(platform, _config.v6),
argumentBuilder.getResult(addr, _config)
var pingExecutablePath = builderFactory.getExecutablePath(
platform, _config.v6
);
var pingArgs = argumentBuilder.getCommandArguments(addr, _config);
var spawnOptions = argumentBuilder.getSpawnOptions();
ping = cp.spawn(pingExecutablePath, pingArgs, spawnOptions);
} catch (err) {
deferred.reject(err);
return deferred.promise;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions test/fixture/answer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@
"avg": "0.027",
"stddev": "0.003"
},
"linux_en_v6_sample1": {
"host": "2606:4700:4700::1111",
"numeric_host": "2606:4700:4700::1111",
"alive": true,
"output": "PING 2606:4700:4700::1111(2606:4700:4700::1111) 56 data bytes\n64 bytes from 2606:4700:4700::1111: icmp_seq=1 ttl=57 time=0.672 ms\n64 bytes from 2606:4700:4700::1111: icmp_seq=2 ttl=57 time=1.00 ms\n\n--- 2606:4700:4700::1111 ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1000ms\nrtt min/avg/max/mdev = 0.672/0.840/1.009/0.170 ms\n",
"time": 0.672,
"times": [
0.672,
1.00
],
"min": "0.672",
"max": "1.009",
"avg": "0.840",
"stddev": "0.170"
},
"linux_en_v6_sample2": {
"host": "one.one.one.one",
"numeric_host": "2606:4700:4700::1111",
"alive": true,
"output": "PING one.one.one.one(one.one.one.one (2606:4700:4700::1111)) 56 data bytes\n64 bytes from one.one.one.one (2606:4700:4700::1111): icmp_seq=1 ttl=60 time=1.66 ms\n64 bytes from one.one.one.one (2606:4700:4700::1111): icmp_seq=2 ttl=60 time=1.42 ms\n\n--- one.one.one.one ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1001ms\nrtt min/avg/max/mdev = 1.429/1.546/1.663/0.117 ms\n",
"time": 1.66,
"times": [
1.66,
1.42
],
"min": "1.429",
"max": "1.663",
"avg": "1.546",
"stddev": "0.117"
},
"window_en_sample1": {
"host": "www.some-domain.com",
"numeric_host": "127.0.0.1",
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/linux/en/v6_sample1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PING 2606:4700:4700::1111(2606:4700:4700::1111) 56 data bytes
64 bytes from 2606:4700:4700::1111: icmp_seq=1 ttl=57 time=0.672 ms
64 bytes from 2606:4700:4700::1111: icmp_seq=2 ttl=57 time=1.00 ms

--- 2606:4700:4700::1111 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.672/0.840/1.009/0.170 ms
7 changes: 7 additions & 0 deletions test/fixture/linux/en/v6_sample2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PING one.one.one.one(one.one.one.one (2606:4700:4700::1111)) 56 data bytes
64 bytes from one.one.one.one (2606:4700:4700::1111): icmp_seq=1 ttl=60 time=1.66 ms
64 bytes from one.one.one.one (2606:4700:4700::1111): icmp_seq=2 ttl=60 time=1.42 ms

--- one.one.one.one ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 1.429/1.546/1.663/0.117 ms