forked from dottgonzo/avahi-nodeutils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
157 lines (150 loc) · 16 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
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
"use strict";
var Promise = require("bluebird");
var child_process_1 = require("child_process");
function setHostname(hostname) {
return new Promise(function (resolve, reject) {
child_process_1.exec('avahi-set-host-name ' + hostname, function (err, stdout, stderr) {
if (err) {
reject(err);
}
else {
resolve(true);
}
});
});
}
exports.setHostname = setHostname;
function getHosts() {
return new Promise(function (resolve, reject) {
var results = [];
child_process_1.exec('avahi-browse -a -r -t', function (err, stdout, stderr) {
if (err) {
reject(err);
}
else {
var result = stdout.split('\n');
for (var i = 0; i < result.length; i++) {
var row = result[i];
if (row.includes("=") && (row.includes("IPv4") || row.includes("IPv6"))) {
var words = row.split(' ');
if (row.includes("IPv4")) {
results.push({ interface: words[1], ipv4: true, ipv6: false, mac: words[4].replace('[', '').replace(']', '') });
}
else if (row.includes("IPv6")) {
results.push({ interface: words[1], ipv6: true, ipv4: false, mac: words[4].replace('[', '').replace(']', '') });
}
}
else if (row.includes("=")) {
results[results.length - 1][row.split('=')[0].replace(/ /g, '')] = row.split('[')[1].split(']')[0];
}
}
var newones = [];
for (var i = 0; i < results.length; i++) {
if (results[i].ipv6) {
results[i].ipv6 = results[i].address;
results[i].port = parseInt(results[i].port);
for (var r = 0; r < results.length; r++) {
if (results[r].ipv4 && results[r].hostname === results[i].hostname) {
results[i].ipv4 = results[r].address;
results[r].delete = true;
}
}
}
}
for (var i = 0; i < results.length; i++) {
var newobj = Object['assign']({}, results[i]);
if (newobj.delete) {
}
else {
delete newobj.address;
newones.push(newobj);
}
}
resolve(newones);
}
});
});
}
exports.getHosts = getHosts;
function extractMacAddressFromScanLineArray(scanLineArray) {
if (!scanLineArray || scanLineArray.length < 5 || !scanLineArray[4] || scanLineArray[4].length === 0) {
return '';
}
const cleanedRawMacAddressCandidate = scanLineArray[4].replace('[', '').replace(']', '').trim();
const regex = /^[0-9a-f]{1,2}([.:-])[0-9a-f]{1,2}(?:\1[0-9a-f]{1,2}){4}$/g;
const regexMatch = cleanedRawMacAddressCandidate.match(regex);
if (regexMatch) {
return regexMatch[0];
}
return '';
}
exports.extractMacAddressFromScanLineArray = extractMacAddressFromScanLineArray;
function getHostsUnresolved() {
return new Promise(function (resolve, reject) {
var results = [];
child_process_1.exec('avahi-browse -a -t', function (err, stdout, stderr) {
if (err) {
reject(err);
}
else {
var result = stdout.split('\n');
for (var i = 0; i < result.length; i++) {
var row = result[i];
if (row.includes("+") && (row.includes("IPv4") || row.includes("IPv6"))) {
var words = row.split(' ');
if (row.includes("IPv4")) {
results.push({ interface: words[1], ipv4: true, ipv6: false, hostname: words[3], mac: extractMacAddressFromScanLineArray(words) });
}
else if (row.includes("IPv6")) {
results.push({ interface: words[1], ipv6: true, ipv4: false, hostname: words[3], mac: extractMacAddressFromScanLineArray(words) });
}
}
else if (row.includes("+")) {
results[results.length - 1][row.split('+')[0].replace(/ /g, '')] = row.split('[')[1].split(']')[0];
}
}
var newones = [];
for (var i = 0; i < results.length; i++) {
if (results[i].ipv6) {
results[i].ipv6 = results[i].address;
results[i].port = parseInt(results[i].port);
for (var r = 0; r < results.length; r++) {
if (results[r].ipv4 && results[r].hostname === results[i].hostname) {
results[i].ipv4 = results[r].address;
results[r].delete = true;
}
}
}
}
for (var i = 0; i < results.length; i++) {
var newobj = Object['assign']({}, results[i]);
if (newobj.delete) {
}
else {
delete newobj.address;
newones.push(newobj);
}
}
resolve(newones);
}
});
});
}
exports.getHostsUnresolved = getHostsUnresolved;
function whereHostNameIsLike(name) {
return new Promise(function (resolve, reject) {
getHosts().then(function (hosts) {
var hostlikes = [];
for (var i = 0; i < hosts.length; i++) {
if (hosts[i].hostname['includes'](name)) {
hostlikes.push(hosts[i]);
}
}
resolve(hostlikes);
}).catch(function (err) {
reject(err);
});
});
}
exports.whereHostNameIsLike = whereHostNameIsLike;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxJQUFZLE9BQU8sV0FBTSxVQUFVLENBQUMsQ0FBQTtBQUVwQyw4QkFBcUIsZUFJckIsQ0FBQyxDQUptQztBQWlCcEMscUJBQTRCLFFBQWdCO0lBQ3hDLE1BQU0sQ0FBQyxJQUFJLE9BQU8sQ0FBVSxVQUFDLE9BQU8sRUFBRSxNQUFNO1FBQ3hDLG9CQUFJLENBQUMsc0JBQXNCLEdBQUcsUUFBUSxFQUFFLFVBQUMsR0FBRyxFQUFFLE1BQU0sRUFBRSxNQUFNO1lBQ3hELEVBQUUsQ0FBQyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUM7Z0JBQ04sTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFBO1lBQ2YsQ0FBQztZQUFDLElBQUksQ0FBQyxDQUFDO2dCQUNKLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQTtZQUNqQixDQUFDO1FBQ0wsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUM7QUFWZSxtQkFBVyxjQVUxQixDQUFBO0FBR0Q7SUFDSSxNQUFNLENBQUMsSUFBSSxPQUFPLENBQWUsVUFBQyxPQUFPLEVBQUUsTUFBTTtRQUM3QyxJQUFNLE9BQU8sR0FBRyxFQUFFLENBQUE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gICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICByZXNvbHZlKHRydWUpXG4gICAgICAgICAgICB9XG4gICAgICAgIH0pXG4gICAgfSlcbn1cblxuXG5leHBvcnQgZnVuY3Rpb24gZ2V0SG9zdHMoKTogUHJvbWlzZTxJQXZhaGlIb3N0W10+IHtcbiAgICByZXR1cm4gbmV3IFByb21pc2U8SUF2YWhpSG9zdFtdPigocmVzb2x2ZSwgcmVqZWN0KSA9PiB7XG4gICAgICAgIGNvbnN0IHJlc3VsdHMgPSBbXVxuICAgICAgICBleGVjKCdhdmFoaS1icm93c2UgLWEgLXIgLXQnLCAoZXJyLCBzdGRvdXQsIHN0ZGVycikgPT4ge1xuICAgICAgICAgICAgaWYgKGVycikge1xuICAgICAgICAgICAgICAgIHJlamVjdChlcnIpXG4gICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgIGNvbnN0IHJlc3VsdCA9IHN0ZG91dC5zcGxpdCgnXFxuJylcbiAgICAgICAgICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHJlc3VsdC5sZW5ndGg7IGkrKykge1xuICAgICAgICAgICAgICAgICAgICBjb25zdCByb3c6IGFueSA9IHJlc3VsdFtpXVxuICAgICAgICAgICAgICAgICAgICBpZiAocm93LmluY2x1ZGVzKFwiPVwiKSAmJiAocm93LmluY2x1ZGVzKFwiSVB2NFwiKSB8fCByb3cuaW5jbHVkZXMoXCJJUHY2XCIpKSkge1xuICAgICAgICAgICAgICAgICAgICAgICAgY29uc3Qgd29yZHMgPSByb3cuc3BsaXQoJyAnKVxuXG4gICAgICAgICAgICAgICAgICAgICAgICBpZiAocm93LmluY2x1ZGVzKFwiSVB2NFwiKSkge1xuICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJlc3VsdHMucHVzaCh7IGludGVyZmFjZTogd29yZHNbMV0sIGlwdjQ6IHRydWUsIGlwdjY6IGZhbHNlLCBtYWM6d29yZHNbNF0ucmVwbGFjZSgnWycsJycpLnJlcGxhY2UoJ10nLCcnKSB9KVxuICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmIChyb3cuaW5jbHVkZXMoXCJJUHY2XCIpKSB7XG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgcmVzdWx0cy5wdXNoKHsgaW50ZXJmYWNlOiB3b3Jkc1sxXSwgaXB2NjogdHJ1ZSwgaXB2NDogZmFsc2UsIG1hYzp3b3Jkc1s0XS5yZXBsYWNlKCdbJywnJykucmVwbGFjZSgnXScsJycpIH0pXG4gICAgICAgICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAocm93LmluY2x1ZGVzKFwiPVwiKSkge1xuICAgICAgICAgICAgICAgICAgICAgICAgcmVzdWx0c1tyZXN1bHRzLmxlbmd0aCAtIDFdW3Jvdy5zcGxpdCgnPScpWzBdLnJlcGxhY2UoLyAvZywgJycpXSA9IHJvdy5zcGxpdCgnWycpWzFdLnNwbGl0KCddJylbMF1cbiAgICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICBjb25zdCBuZXdvbmVzID0gW11cbiAgICAgICAgICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHJlc3VsdHMubGVuZ3RoOyBpKyspIHtcbiAgICAgICAgICAgICAgICAgICAgaWYgKHJlc3VsdHNbaV0uaXB2Nikge1xuICAgICAgICAgICAgICAgICAgICAgICAgcmVzdWx0c1tpXS5pcHY2ID0gcmVzdWx0c1tpXS5hZGRyZXNzXG4gICAgICAgICAgICAgICAgICAgICAgICByZXN1bHRzW2ldLnBvcnQgPSBwYXJzZUludChyZXN1bHRzW2ldLnBvcnQpXG5cbiAgICAgICAgICAgICAgICAgICAgICAgIGZvciAobGV0IHIgPSAwOyByIDwgcmVzdWx0cy5sZW5ndGg7IHIrKykge1xuXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHJlc3VsdHNbcl0uaXB2NCAmJiByZXN1bHRzW3JdLmhvc3RuYW1lID09PSByZXN1bHRzW2ldLmhvc3RuYW1lKSB7XG5cblxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXN1bHRzW2ldLmlwdjQgPSByZXN1bHRzW3JdLmFkZHJlc3NcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmVzdWx0c1tyXS5kZWxldGUgPSB0cnVlXG5cblxuICAgICAgICAgICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICAgICAgICAgIH1cblxuXG4gICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICB9XG5cbiAgICAgICAgICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHJlc3VsdHMubGVuZ3RoOyBpKyspIHtcbiAgICAgICAgICAgICAgICAgICAgbGV0IG5ld29iaiA9IE9iamVjdFsnYXNzaWduJ10oe30sIHJlc3VsdHNbaV0pXG5cblxuICAgICAgICAgICAgICAgICAgICBpZiAobmV3b2JqLmRlbGV0ZSkge1xuICAgICAgICAgICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgICAgICAgICAgZGVsZXRlIG5ld29iai5hZGRyZXNzXG4gICAgICAgICAgICAgICAgICAgICAgICBuZXdvbmVzLnB1c2gobmV3b2JqKVxuXG4gICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgcmVzb2x2ZShuZXdvbmVzKVxuICAgICAgICAgICAgfVxuXG4gICAgICAgIH0pXG4gICAgfSlcblxufVxuZXhwb3J0IGZ1bmN0aW9uIHdoZXJlSG9zdE5hbWVJc0xpa2UobmFtZTogc3RyaW5nKTogUHJvbWlzZTxJQXZhaGlIb3N0W10+IHtcbiAgICByZXR1cm4gbmV3IFByb21pc2U8SUF2YWhpSG9zdFtdPigocmVzb2x2ZSwgcmVqZWN0KSA9PiB7XG5cbiAgICAgICAgZ2V0SG9zdHMoKS50aGVuKChob3N0cykgPT4geyBcbiAgICAgICAgICAgIGNvbnN0IGhvc3RsaWtlczogSUF2YWhpSG9zdFtdID0gW11cblxuICAgICAgICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBob3N0cy5sZW5ndGg7IGkrKykge1xuICAgICAgICAgICAgICAgIGlmIChob3N0c1tpXS5ob3N0bmFtZVsnaW5jbHVkZXMnXShuYW1lKSkge1xuICAgICAgICAgICAgICAgICAgICBob3N0bGlrZXMucHVzaChob3N0c1tpXSlcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICByZXNvbHZlKGhvc3RsaWtlcylcbiAgICAgICAgfSkuY2F0Y2goKGVycikgPT4ge1xuICAgICAgICAgICAgcmVqZWN0KGVycilcbiAgICAgICAgfSlcblxuICAgIH0pXG5cbn1cbiJdLCJzb3VyY2VSb290IjoiL3NvdXJjZS8ifQ==