This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2 - Object Introspection.js
220 lines (187 loc) · 6.04 KB
/
2 - Object Introspection.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
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
const { getData, setData, generateTag, runAsPkg } = require('./common'),
{ parseString } = require('xml2js'),
{ parse: parseGetAll } = require('./GetAll');
async function parseXML(xml) {
return new Promise((resolve, reject) => {
parseString(xml, {
mergeAttrs: true,
// explicitArray: false
}, (err, result) => {
if (err) {
return reject(err);
} else {
return resolve(result);
}
});
});
}
async function introspect(names, runner) {
/*
_root = {
bus.na.me: { // Level 1: Destination
/obj/ect: { // Level 2: Object
in.ter.face: { // Level 3: Interface
'method': { // Level 4: 'method'
MethodName: [
{name: "argument_name", type: "a{type}", direction: "in/out"},
...
],
...
},
'signal': { // Level 4: 'signal'
SignalName: [
{name: "argument_name", type: "a{type}"},
...
],
...
},
'property': { // Level 4: 'property'
PropertyName1: "property_value",
PropertyName2: null,
...
}
}
},
...
},
...
}
*/
const _root = {};
/*
shelf = [
{dest: "bus.na.me", object: "/", interface: null},
{dest: "bus.na.me", object: "/", interface: "in.ter.face"},
]
Temporary shelf for a batch of commands
- If interface is null, call Introspectable.Introspect
- If interface is not null, call Properties.GetAll
*/
let shelf = names.map(name => ({ dest: name, object: '/', interface: null }));
while (shelf.length) {
const l = shelf.length;
console.log(`+ Status: ${l} items are being processed.`);
// A random tag and delimiter for this iteration
const tag = generateTag(),
delimiter = `[:${tag}:]`;
const cmd = shelf.reduce((cmd, v, i) => {
const { dest, object, interface } = v;
// Delimiter
cmd += `\necho '${delimiter}';\n`;
// Metadata
const m = JSON.stringify(v);
cmd += `echo '${m}';\n`;
// dlog where we are
cmd += `echo -n -e '\\x03${tag}\\x00${i}/${l} ${m}\\x00' >> /dev/log_main;\n`;
// If interface is null, call Introspectable.Introspect
if (interface == null) {
return cmd + `dbus-send --system --type=method_call --print-reply --reply-timeout=5000 --dest=${dest} ${object} org.freedesktop.DBus.Introspectable.Introspect;\n`;
}
// If interface is not null, call Properties.GetAll
else {
return cmd + `dbus-send --system --type=method_call --print-reply --reply-timeout=5000 --dest=${dest} ${object} org.freedesktop.DBus.Properties.GetAll string:${interface};\n`;
}
}, '');
// Run the command
const res = await runner(cmd, tag);
// Flush shelf
shelf = [];
// Split by delimiter
for (const block of res.split(delimiter)) {
// Parse metadata
const [, metadata] = /^({.+})$/m.exec(block) || [, '{}'],
{ dest, object, interface } = JSON.parse(metadata);
if (!dest) {
continue;
}
// Parse the message
const [, string] = /\n\s+string "(.+)"[\n\s]*$/s.exec(block) || [], // Introspect
[, array] = /\n\s+(array \[.+\])[\n\s]*$/s.exec(block) || []; // GetAll
// If interface is null, Introspect is expected
if (interface == null && string) {
// Initialize _root[dest][object] only if any introspect is succeeded
// Initialize _root[dest] (Level 1)
if (!_root[dest]) {
_root[dest] = {};
}
// Initialize _root[dest][object] (Level 2)
if (!_root[dest][object]) {
_root[dest][object] = {};
}
// Parse XML, grab interfaces and children
const {
node: {
interface: interfaces,
node: children
} = {}
} = await parseXML(string) || {};
// interfaces: Enumerate and register
(interfaces || []).forEach(v => {
const {
name: [ interface ],
method: methods,
signal: signals,
property
} = v;
// Initialize _root[dest][object][interface] (Level 3)
_root[dest][object][interface] = { 'method': {}, 'signal': {}, 'property': {} };
// Found methods,
if (methods) {
// methods: Enumerate and register
methods.forEach(v => {
const {
name: [ method ],
arg
} = v;
_root[dest][object][interface]['method'][method] = arg || [];
});
}
// Found signals,
if (signals) {
// signals: Enumerate and register
signals.forEach(v => {
const {
name: [ signal ],
arg
} = v;
_root[dest][object][interface]['signal'][signal] = arg;
});
}
// Found property,
if (property) {
// Put it on shelf for later GetAll
shelf.push({dest, object, interface});
}
});
// children: Enumerate and register
(children || []).forEach(v => {
const {
name: [ child ]
} = v;
// Put it on shelf for later Introspect
shelf.push({
dest,
object: `${object}${object == '/' ? '' : '/'}${child}`,
interface: null
});
});
}
// If interface is not null, GetAll is expected
else if (array) {
// parseGetAll may contain non-JSON-compliant hex `0xFF` values
eval(`var properties = ${parseGetAll(array)};`);
_root[dest][object][interface]['property'] = properties;
}
// Else: Invalid
else {
console.log(`Invalid: ${block}`);
}
}
}
return _root;
}
async function main() {
// Introspect and acquire root object
setData('root', await introspect(getData('names'), runAsPkg));
}
main();