forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.js
More file actions
31 lines (27 loc) · 930 Bytes
/
ip.js
File metadata and controls
31 lines (27 loc) · 930 Bytes
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
const logger = require('./logger');
module.exports = function(remote) {
const interfaces = require('os').networkInterfaces();
if (remote && 'utun1' in interfaces) {
for (const key in interfaces) {
if (interfaces.hasOwnProperty(key) && key.match(/utun/)) {
const ip = getIP(interfaces[key]);
if (ip) {
logger.warn('检测到vpn环境, 优先获取 utun1 ip:', ip);
return ip;
}
}
}
}
for (const key in interfaces) {
if (interfaces.hasOwnProperty(key)) {
const ip = getIP(interfaces[key]);
if (ip) {
return ip;
}
}
}
};
function getIP(list) {
const targetItem = list.find(item => item.family === 'IPv4' && item.address !== '127.0.0.1' && !item.internal);
return targetItem ? targetItem.address : null;
}