This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrespondd
executable file
·224 lines (201 loc) · 5.29 KB
/
respondd
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env node
const dgram = require('dgram')
const socket = dgram.createSocket('udp6')
const fs = require('fs')
const zlib = require('zlib')
const os = require('os')
const process = require('child_process')
const myproc = require('process')
const defConfig = {
"bat_iface": "bat0",
"mcast_group": "ff02::2:1001",
"mcast_iface": "bat0",
"port": "1001",
"site_code": undefined
}
function getNodeinfo() {
return {
"software": {
"autoupdater": {
"enabled": false
},
"batman-adv": {
"version": getFileContents("/sys/module/batman_adv/version")
},
"fastd": {
"enabled": isProcessRunning("fastd")
},
"tunneldigger": {
"enabled": isProcessRunning("l2tp_broker")
}
},
"network": {
"addresses": getV6AddrForInterface(getConfig("mcast_iface")),
"mesh": getMeshAddresses(),
"mac": getPrimaryMac()
},
"system": {
"site_code": getConfig("site_code")
},
"hostname": getConfig("hostname"),
"hardware": {
"model": os.cpus()[0].model,
"nproc": os.cpus().length
}
}
}
function getStatistics() {
return {
"traffic": getTrafficStats(),
"memory": {
"total": os.totalmem(),
"free": os.freemem()
},
"uptime": os.uptime(),
"loadavg": os.loadavg()[0],
"processes": {
"running": parseInt(evalExec("ps ax | awk '{print $3}' | grep R | wc -l")),
"total": parseInt(evalExec("ps ax | wc -l")) - 1,
}
}
}
function getNeighbours() {
return {
"batadv": JSON.parse(evalExec("sudo ./bat-list-neighbours"))
}
}
function getV6AddrForInterface(iface) {
try {
return os.networkInterfaces()[iface].filter((a) => a.family == "IPv6").map((a) => a.address)
} catch (e) {
return undefined
}
}
function isProcessRunning(name) {
try {
return evalExec("ps ax | grep "+name+" | grep -v grep").length > 0
} catch (e) {
return false
}
}
function evalExec(line) {
try {
return process.execSync(line).toString().trim()
} catch (e) {
return undefined
}
}
function getFileContents(name) {
try {
return fs.readFileSync(name).toString().trim()
} catch (e) {
return undefined
}
}
function getTrafficStats() {
try {
var res = {}
var vs = evalExec("/sbin/ethtool -S " + getConfig("bat_iface") + " | head -n 12 | tail -n 11").split("\n")
vs.forEach((v) => {
var x = v.split(": ")
var group = x[0].trim()
var type = "packets"
var val = parseInt(x[1].trim())
if (group.indexOf("_bytes") > -1) {
group = group.substring(0,group.length-6)
type = "bytes"
}
if (group.indexOf("_dropped") > -1) {
group = group.substring(0,group.length-8)
type = "dropped"
}
if (!(group in res)) res[group] = {}
res[group][type] = val
})
return res
} catch (e) {
return undefined
}
}
function getMeshAddresses() {
var bat = getConfig("bat_iface")
var data = {
"interfaces": {
"tunnel": evalExec("cat /sys/class/net/"+bat+"/lower_*/address").split("\n"),
"other": [getPrimaryMac()]
}
}
var res = {}
res[bat] = data
return res
}
function getNodeId() {
return getPrimaryMac().replace(/:/g,"")
}
function getPrimaryMac() {
return getMac(getConfig("mcast_iface"));
}
function getMac(iface) {
try {
return os.networkInterfaces()[iface][0].mac
} catch (e) {
return "00:00:00:00:00:00"
}
}
function getData(what) {
var data = {}
switch(what) {
case "nodeinfo": data = getNodeinfo(); break
case "statistics": data = getStatistics(); break
case "neighbours": data = getNeighbours(); break
default: console.log("Unknown data type: " + what)
}
data.node_id = getNodeId()
var res = {}
res[what] = data
return res
}
var config = {}
try {
config = JSON.parse(fs.readFileSync("./config.json"))
} catch (err) {
console.log("no config file found")
}
function getConfig(opt) {
if (opt == "listen")
return getV6AddrForInterface(getConfig("mcast_iface")).filter((a) => a.startsWith("fe80:"))[0] + "%" + getConfig("mcast_iface")
if (opt in config) return config[opt]
if (opt == "hostname") return os.hostname()
else return defConfig[opt]
}
socket.on('error', (err) => {
console.log("socket error: " + err)
socket.close()
})
socket.on('message', (msg, rinfo) => {
if (!rinfo.address.startsWith("f")) return
msg = msg.toString()
console.log("message from: "+rinfo.address+": "+msg)
if (msg.startsWith("GET ")) {
// Multi case
var whats = msg.substring(4).split(" ")
whats.forEach((what) => {
zlib.deflateRaw(new Buffer(JSON.stringify(getData(what.trim()))), (_, data) => {
socket.send(data, 0, data.length, rinfo.port, rinfo.address + "%" + getConfig("mcast_iface"), (err) => { if (err) console.log(err) })
})
})
} else {
// Single case
var res = new Buffer(JSON.stringify(getData(msg.trim())))
socket.send(res, 0, res.length, rinfo.port, rinfo.address + "%" + getConfig("mcast_iface"), (err) => { if (err) console.log(err) })
}
})
socket.on('listening', () => {
var addr = socket.address()
console.log("socket listening on " + addr.address + ":" + addr.port)
})
socket.bind(getConfig("port"), () => {
socket.addMembership(getConfig("mcast_group"), getConfig("listen"))
myproc.setgid('nogroup')
myproc.setuid('nobody')
})