1
+ "use strict" ;
2
+
3
+ //taken from iobroker.discovery
4
+
5
+ var cp = require ( 'child_process' ) ;
6
+ var p = require ( 'os' ) . platform ( ) . toLowerCase ( ) ;
7
+
8
+ var isWin = false ;
9
+ var init ;
10
+ var xfamily = [ 'linux' , 'sunos' , 'unix' ] ;
11
+ //var regex = /=.*[<|=]([0-9]*).*TTL|ttl..*=([0-9\.]*)/;
12
+ var regex = / = .* [ < | = ] ( [ 0 - 9 ] * ) .* ?t t l .* ?= ( [ 0 - 9 \. ] * ) / im;
13
+
14
+
15
+ exports . reset = function ( ) {
16
+ // if config changed
17
+ init = null ;
18
+ } ;
19
+
20
+ function probe ( addr , config , callback ) {
21
+ config = config || { } ;
22
+
23
+ var ls = null ;
24
+ var log = config . log || console . log ;
25
+ var outstring = '' ;
26
+
27
+ if ( ! init ) init = function ( ip ) {
28
+
29
+ var args = [ ] ;
30
+ config = {
31
+ numeric : config . numeric === undefined ? true : config . numeric ,
32
+ timeout : parseInt ( config . timeout === undefined ? 2 : config . timeout , 10 ) ,
33
+ minReply : parseInt ( config . minReply === undefined ? 1 : config . minReply , 10 ) ,
34
+ extra : config . extra || [ ]
35
+ } ;
36
+
37
+ //var args = [];
38
+
39
+ if ( xfamily . indexOf ( p ) !== - 1 ) {
40
+ //linux
41
+ //args = [];
42
+ if ( config . numeric !== false ) args . push ( '-n' ) ;
43
+
44
+ if ( config . timeout !== false ) args . push ( '-w ' + config . timeout ) ;
45
+
46
+ if ( config . minReply !== false ) args . push ( '-c ' + config . minReply ) ;
47
+
48
+ if ( config . extra !== false ) args = args . concat ( config . extra ) ;
49
+
50
+ args . push ( addr ) ;
51
+ //log('System command: /bin/ping ' + args.join(' '));
52
+ init = function ( ip ) {
53
+ args [ args . length - 1 ] = ip ;
54
+ return cp . spawn ( '/bin/ping' , args ) ;
55
+ }
56
+ } else if ( p . match ( / ^ w i n / ) ) {
57
+ //windows
58
+ isWin = true ;
59
+ var _args = [ ] ;
60
+ if ( config . minReply !== false ) _args . push ( '-n ' + config . minReply ) ;
61
+
62
+ if ( config . timeout !== false ) _args . push ( '-w ' + config . timeout * 1 /*000*/ ) ;
63
+
64
+ if ( config . extra !== false ) _args = _args . concat ( config . extra ) ;
65
+
66
+ _args . push ( addr ) ;
67
+
68
+ args = [
69
+ '/s' , // leave quotes as they are
70
+ '/c' , // run and exit
71
+ // !!! order of c and s is important - c must come last!!!
72
+ '"' , // enforce starting quote
73
+ process . env . SystemRoot + '\\system32\\ping.exe' // command itself. Notice that you'll have to pass it quoted if it contains spaces
74
+ ] . concat ( _args )
75
+ . concat ( '"' ) ; // enforce closing quote
76
+
77
+ //log('System command: ' + (process.env.comspec || 'cmd.exe') + ' ' + allArgs.join(' '));
78
+ // switch the command to cmd shell instead of the original command
79
+ init = function ( ip ) {
80
+ args [ args . length - 2 ] = ip ;
81
+ return cp . spawn ( process . env . comspec || 'cmd.exe' , args , { windowsVerbatimArguments : true } ) ;
82
+ }
83
+ } else if ( p === 'darwin' || p === 'freebsd' ) {
84
+ //mac osx or freebsd
85
+ //args = [];
86
+ if ( config . numeric !== false ) args . push ( '-n' ) ;
87
+
88
+ if ( config . timeout !== false ) args . push ( '-t ' + config . timeout ) ;
89
+
90
+ if ( config . minReply !== false ) args . push ( '-c ' + config . minReply ) ;
91
+
92
+ if ( config . extra !== false ) args = args . concat ( config . extra ) ;
93
+
94
+ args . push ( addr ) ;
95
+ //log('System command: /sbin/ping ' + args.join(' '));
96
+ init = function ( ip ) {
97
+ args [ args . length - 1 ] = ip ;
98
+ return cp . spawn ( '/sbin/ping' , args ) ;
99
+ } ;
100
+ } else {
101
+ return callback && callback ( 'Your platform "' + p + '" is not supported' ) ;
102
+ }
103
+ return init ( addr ) ;
104
+ } ;
105
+
106
+ ls = init ( addr ) ;
107
+
108
+ ls . on ( 'error' , function ( e ) {
109
+ callback && callback ( new Error ( 'ping.probe: there was an error while executing the ping program. check the path or permissions...' ) ) ;
110
+ callback = null ;
111
+ } ) ;
112
+
113
+ ls . stdout . on ( 'data' , function ( data ) {
114
+ outstring += String ( data ) ;
115
+ } ) ;
116
+
117
+ ls . on ( 'exit' , function ( code ) {
118
+ var ms , m ; //, result = 1;
119
+ if ( ( m = regex . exec ( outstring ) ) && m . length >= 2 ) {
120
+ ms = ~ ~ m [ 1 ] ;
121
+ //result = 0;
122
+ }
123
+
124
+ // var lines = outstring.split('\n');
125
+ // for (var t = 0; t < lines.length; t++) {
126
+ // var m = regex.exec(lines[t]) || '';
127
+ // if (m !== '') {
128
+ // ms = m[1];
129
+ // result = 0;
130
+ // break;
131
+ // }
132
+ // }
133
+
134
+ if ( callback ) callback ( null , {
135
+ host : addr ,
136
+ alive : isWin ? ( ms !== undefined ) : ! code ,
137
+ ms : ms
138
+ } ) ;
139
+ callback = null ;
140
+ } ) ;
141
+ }
142
+
143
+ exports . probe = probe ;
0 commit comments