Repository navigation
Expand file tree
/
Copy pathinterface.js
More file actions
90 lines (74 loc) · 2.37 KB
/
Copy pathinterface.js
File metadata and controls
90 lines (74 loc) · 2.37 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import network from 'network'
import arp from 'node-arp'
import fs from 'fs'
import os from 'os'
import path from 'path'
export default (callback, needToCache = null, noNeedPublicIp = false) => {
if(typeof callback !== 'function') return
let active = {
ip: null,
mac: null,
netmask: null,
gateway: {
ip: null,
mac: null
},
vendor: null,
model: null,
name: null,
type: null,
external: null
}
let interfacePath = path.resolve(`${__dirname}/../data/interface.json`)
let currentInterfaces = os.networkInterfaces()
if(fs.existsSync(interfacePath)){
let savedData = JSON.parse(fs.readFileSync(interfacePath))
if(typeof savedData['os'] !== 'undefined'
&& typeof savedData['active'] !== 'undefined'){
let savedInterfaces = JSON.stringify(savedData['os'])
if(savedInterfaces == JSON.stringify(currentInterfaces)){
callback(savedData['active'])
return
}
}
}
let cacheIt = (active)=>{
let cache = JSON.stringify({
os: currentInterfaces,
active
}, null, 4)
fs.writeFileSync(interfacePath, cache)
}
network.get_active_interface((error, resource) => {
if(error !== null){
callback(null, 'No Network Card')
return
}
active.ip = resource.ip_address
active.mac = resource.mac_address
active.netmask = resource.netmask
active.vendor = resource.vendor
active.model = resource.model
active.name = resource.name
active.type = resource.type
arp.getMAC(resource.gateway_ip, (error, gatewayMAC) => {
if(error !== false){
callback(null, 'No Gateway')
return
}
active.gateway.ip = resource.gateway_ip
active.gateway.mac = gatewayMAC
if(noNeedPublicIp){
callback(active)
cacheIt(active)
}else{
network.get_public_ip((error, externalIp) => {
if(error === null)
active.external = externalIp
callback(active)
cacheIt(active)
})
}
})
})
}