sysiddr5 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sysiddr5 might be problematic. Click here for more details.

@@ -0,0 +1,327 @@
1
+ 'use strict';
2
+ // @ts-check
3
+ // ==================================================================================
4
+ // dockerSockets.js
5
+ // ----------------------------------------------------------------------------------
6
+ // Description: System Information - library
7
+ // for Node.js
8
+ // Copyright: (c) 2014 - 2023
9
+ // Author: Sebastian Hildebrandt
10
+ // ----------------------------------------------------------------------------------
11
+ // License: MIT
12
+ // ==================================================================================
13
+ // 13. DockerSockets
14
+ // ----------------------------------------------------------------------------------
15
+
16
+ const net = require('net');
17
+ const isWin = require('os').type() === 'Windows_NT';
18
+ const socketPath = isWin ? '//./pipe/docker_engine' : '/var/run/docker.sock';
19
+
20
+ class DockerSocket {
21
+
22
+ getInfo(callback) {
23
+ try {
24
+
25
+ let socket = net.createConnection({ path: socketPath });
26
+ let alldata = '';
27
+ let data;
28
+
29
+ socket.on('connect', () => {
30
+ socket.write('GET http:/info HTTP/1.0\r\n\r\n');
31
+ });
32
+
33
+ socket.on('data', data => {
34
+ alldata = alldata + data.toString();
35
+ });
36
+
37
+ socket.on('error', () => {
38
+ socket = false;
39
+ callback({});
40
+ });
41
+
42
+ socket.on('end', () => {
43
+ let startbody = alldata.indexOf('\r\n\r\n');
44
+ alldata = alldata.substring(startbody + 4);
45
+ socket = false;
46
+ try {
47
+ data = JSON.parse(alldata);
48
+ callback(data);
49
+ } catch (err) {
50
+ callback({});
51
+ }
52
+ });
53
+ } catch (err) {
54
+ callback({});
55
+ }
56
+ }
57
+
58
+ listImages(all, callback) {
59
+ try {
60
+
61
+ let socket = net.createConnection({ path: socketPath });
62
+ let alldata = '';
63
+ let data;
64
+
65
+ socket.on('connect', () => {
66
+ socket.write('GET http:/images/json' + (all ? '?all=1' : '') + ' HTTP/1.0\r\n\r\n');
67
+ });
68
+
69
+ socket.on('data', data => {
70
+ alldata = alldata + data.toString();
71
+ });
72
+
73
+ socket.on('error', () => {
74
+ socket = false;
75
+ callback({});
76
+ });
77
+
78
+ socket.on('end', () => {
79
+ let startbody = alldata.indexOf('\r\n\r\n');
80
+ alldata = alldata.substring(startbody + 4);
81
+ socket = false;
82
+ try {
83
+ data = JSON.parse(alldata);
84
+ callback(data);
85
+ } catch (err) {
86
+ callback({});
87
+ }
88
+ });
89
+ } catch (err) {
90
+ callback({});
91
+ }
92
+ }
93
+
94
+ inspectImage(id, callback) {
95
+ id = id || '';
96
+ if (id) {
97
+ try {
98
+ let socket = net.createConnection({ path: socketPath });
99
+ let alldata = '';
100
+ let data;
101
+
102
+ socket.on('connect', () => {
103
+ socket.write('GET http:/images/' + id + '/json?stream=0 HTTP/1.0\r\n\r\n');
104
+ });
105
+
106
+ socket.on('data', data => {
107
+ alldata = alldata + data.toString();
108
+ });
109
+
110
+ socket.on('error', () => {
111
+ socket = false;
112
+ callback({});
113
+ });
114
+
115
+ socket.on('end', () => {
116
+ let startbody = alldata.indexOf('\r\n\r\n');
117
+ alldata = alldata.substring(startbody + 4);
118
+ socket = false;
119
+ try {
120
+ data = JSON.parse(alldata);
121
+ callback(data);
122
+ } catch (err) {
123
+ callback({});
124
+ }
125
+ });
126
+ } catch (err) {
127
+ callback({});
128
+ }
129
+ } else {
130
+ callback({});
131
+ }
132
+ }
133
+
134
+ listContainers(all, callback) {
135
+ try {
136
+
137
+ let socket = net.createConnection({ path: socketPath });
138
+ let alldata = '';
139
+ let data;
140
+
141
+ socket.on('connect', () => {
142
+ socket.write('GET http:/containers/json' + (all ? '?all=1' : '') + ' HTTP/1.0\r\n\r\n');
143
+ });
144
+
145
+ socket.on('data', data => {
146
+ alldata = alldata + data.toString();
147
+ });
148
+
149
+ socket.on('error', () => {
150
+ socket = false;
151
+ callback({});
152
+ });
153
+
154
+ socket.on('end', () => {
155
+ let startbody = alldata.indexOf('\r\n\r\n');
156
+ alldata = alldata.substring(startbody + 4);
157
+ socket = false;
158
+ try {
159
+ data = JSON.parse(alldata);
160
+ callback(data);
161
+ } catch (err) {
162
+ callback({});
163
+ }
164
+ });
165
+ } catch (err) {
166
+ callback({});
167
+ }
168
+ }
169
+
170
+ getStats(id, callback) {
171
+ id = id || '';
172
+ if (id) {
173
+ try {
174
+ let socket = net.createConnection({ path: socketPath });
175
+ let alldata = '';
176
+ let data;
177
+
178
+ socket.on('connect', () => {
179
+ socket.write('GET http:/containers/' + id + '/stats?stream=0 HTTP/1.0\r\n\r\n');
180
+ });
181
+
182
+ socket.on('data', data => {
183
+ alldata = alldata + data.toString();
184
+ });
185
+
186
+ socket.on('error', () => {
187
+ socket = false;
188
+ callback({});
189
+ });
190
+
191
+ socket.on('end', () => {
192
+ let startbody = alldata.indexOf('\r\n\r\n');
193
+ alldata = alldata.substring(startbody + 4);
194
+ socket = false;
195
+ try {
196
+ data = JSON.parse(alldata);
197
+ callback(data);
198
+ } catch (err) {
199
+ callback({});
200
+ }
201
+ });
202
+ } catch (err) {
203
+ callback({});
204
+ }
205
+ } else {
206
+ callback({});
207
+ }
208
+ }
209
+
210
+ getInspect(id, callback) {
211
+ id = id || '';
212
+ if (id) {
213
+ try {
214
+ let socket = net.createConnection({ path: socketPath });
215
+ let alldata = '';
216
+ let data;
217
+
218
+ socket.on('connect', () => {
219
+ socket.write('GET http:/containers/' + id + '/json?stream=0 HTTP/1.0\r\n\r\n');
220
+ });
221
+
222
+ socket.on('data', data => {
223
+ alldata = alldata + data.toString();
224
+ });
225
+
226
+ socket.on('error', () => {
227
+ socket = false;
228
+ callback({});
229
+ });
230
+
231
+ socket.on('end', () => {
232
+ let startbody = alldata.indexOf('\r\n\r\n');
233
+ alldata = alldata.substring(startbody + 4);
234
+ socket = false;
235
+ try {
236
+ data = JSON.parse(alldata);
237
+ callback(data);
238
+ } catch (err) {
239
+ callback({});
240
+ }
241
+ });
242
+ } catch (err) {
243
+ callback({});
244
+ }
245
+ } else {
246
+ callback({});
247
+ }
248
+ }
249
+
250
+ getProcesses(id, callback) {
251
+ id = id || '';
252
+ if (id) {
253
+ try {
254
+ let socket = net.createConnection({ path: socketPath });
255
+ let alldata = '';
256
+ let data;
257
+
258
+ socket.on('connect', () => {
259
+ socket.write('GET http:/containers/' + id + '/top?ps_args=-opid,ppid,pgid,vsz,time,etime,nice,ruser,user,rgroup,group,stat,rss,args HTTP/1.0\r\n\r\n');
260
+ });
261
+
262
+ socket.on('data', data => {
263
+ alldata = alldata + data.toString();
264
+ });
265
+
266
+ socket.on('error', () => {
267
+ socket = false;
268
+ callback({});
269
+ });
270
+
271
+ socket.on('end', () => {
272
+ let startbody = alldata.indexOf('\r\n\r\n');
273
+ alldata = alldata.substring(startbody + 4);
274
+ socket = false;
275
+ try {
276
+ data = JSON.parse(alldata);
277
+ callback(data);
278
+ } catch (err) {
279
+ callback({});
280
+ }
281
+ });
282
+ } catch (err) {
283
+ callback({});
284
+ }
285
+ } else {
286
+ callback({});
287
+ }
288
+ }
289
+
290
+ listVolumes(callback) {
291
+ try {
292
+
293
+ let socket = net.createConnection({ path: socketPath });
294
+ let alldata = '';
295
+ let data;
296
+
297
+ socket.on('connect', () => {
298
+ socket.write('GET http:/volumes HTTP/1.0\r\n\r\n');
299
+ });
300
+
301
+ socket.on('data', data => {
302
+ alldata = alldata + data.toString();
303
+ });
304
+
305
+ socket.on('error', () => {
306
+ socket = false;
307
+ callback({});
308
+ });
309
+
310
+ socket.on('end', () => {
311
+ let startbody = alldata.indexOf('\r\n\r\n');
312
+ alldata = alldata.substring(startbody + 4);
313
+ socket = false;
314
+ try {
315
+ data = JSON.parse(alldata);
316
+ callback(data);
317
+ } catch (err) {
318
+ callback({});
319
+ }
320
+ });
321
+ } catch (err) {
322
+ callback({});
323
+ }
324
+ }
325
+ }
326
+
327
+ module.exports = DockerSocket;