-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathnetwork.js
39 lines (32 loc) · 872 Bytes
/
network.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
/* @flow */
const os = require('os');
const IGNORE_INTERFACES = ['lo0', 'awdl0', 'bridge0'];
const LOCAL_IPS = ['127.0.0.1', '::1'];
export function isOffline(): boolean {
let interfaces;
try {
interfaces = os.networkInterfaces();
} catch (e) {
// As of October 2016, Windows Subsystem for Linux (WSL) does not support
// the os.networkInterfaces() call and throws instead. For this platform,
// assume we are online.
if (e.syscall === 'uv_interface_addresses') {
return false;
} else {
throw e;
}
}
for (const name in interfaces) {
if (IGNORE_INTERFACES.indexOf(name) >= 0) {
continue;
}
const addrs = interfaces[name];
for (const addr of addrs) {
if (LOCAL_IPS.indexOf(addr.address) < 0) {
// found a possible remote ip
return false;
}
}
}
return true;
}