forked from AtuyL/my-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const
os = require( 'os' ),
interfaces = os.networkInterfaces();
let version, internal;
function getMyIP( version, internal ) {
version = version || 'IPv4';
internal = internal || false;
for( const key in interfaces ) {
const addresses = interfaces[ key ];
for( let i = 0; i < addresses.length; i++ ) {
const address = addresses[ i ];
if( address.internal === internal && address.family === version ) {
return address.address;
}
}
}
return 'localhost';
}
if( !module.parent ) {
version = 'IPv4';
internal = false;
let
argv = process.argv,
length = argv.length;
if( length >= 3 ) {
for( let i = 2; i < length; i++ ) {
if( argv[ i ] === '--version6' || argv[ i ] === '-v6' )
version = 'IPv6';
else if( argv[ i ] === '--internal' || argv[ i ] === '-i' )
internal = true;
}
}
return getMyIP( version, internal );
} else {
module.exports = getMyIP;
}