whatap 0.4.98 → 0.5.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.
@@ -0,0 +1,139 @@
1
+ const Pack = require('./pack');
2
+ const PackEnum = require('./packenum');
3
+ const MapValue = require('../value/map-value');
4
+ const DataOutputX = require('../io/data-outputx');
5
+ const DataInputX = require('../io/data-inputx');
6
+ const HashUtil = require('../util/hashutil');
7
+ const Value = require('../value/value');
8
+ const BooleanValue = require('../value/boolean-value');
9
+
10
+ function LogSinkPack() {
11
+ Pack.call(this);
12
+ this.category = '';
13
+ this.tagHash = 0;
14
+ this.tags = new MapValue();
15
+ this.line = 0;
16
+ this.content = '';
17
+ this.fields = new MapValue();
18
+ this.dropped = false;
19
+ }
20
+
21
+ LogSinkPack.prototype = Object.create(Pack.prototype);
22
+ LogSinkPack.prototype.constructor = LogSinkPack;
23
+
24
+ LogSinkPack.prototype.getPackType = function () {
25
+ return PackEnum.LOGSINK;
26
+ };
27
+
28
+ LogSinkPack.prototype.toString = function () {
29
+ return `${Pack.prototype.toString.call(this)} [category=${this.category}, tagHash=${this.tagHash}, tags=${this.tags}, content=${this.content}, fields=${this.fields}]`;
30
+ };
31
+
32
+ LogSinkPack.prototype.getTagAsBytes = function () {
33
+ const out = new DataOutputX();
34
+ out.writeValue(this.tags);
35
+ return out.toByteArray();
36
+ };
37
+
38
+ LogSinkPack.prototype.getContentBytes = function () {
39
+ const o = new DataOutputX();
40
+ o.writeByte(1);
41
+ o.writeText(this.content);
42
+ o.writeDecimal(this.line);
43
+ return o.toByteArray();
44
+ };
45
+
46
+ LogSinkPack.prototype.putCtr = function (key, value) {
47
+ if (key.charAt(0) === '!') {
48
+ this.fields.putValue(key, value);
49
+ }
50
+ };
51
+
52
+ LogSinkPack.prototype.getCtr = function (key) {
53
+ return this.fields.get(key);
54
+ };
55
+
56
+ LogSinkPack.prototype.getCtrBoolean = function (key) {
57
+ const v = this.fields.get(key);
58
+ if (v instanceof BooleanValue) {
59
+ return v.value;
60
+ }
61
+ return false;
62
+ };
63
+
64
+ LogSinkPack.prototype.content = function () {
65
+ return this.content || '';
66
+ };
67
+
68
+ LogSinkPack.prototype.setContent = function (str) {
69
+ this.content = str;
70
+ };
71
+
72
+ LogSinkPack.prototype.setContentBytes = function (d) {
73
+ try {
74
+ if (!d || d.length < 1) return;
75
+ const inStream = new DataInputX(d);
76
+ const ver = inStream.readByte();
77
+ if (ver === 1) {
78
+ this.content = inStream.readText();
79
+ this.line = inStream.readDecimal();
80
+ }
81
+ } catch (e) {
82
+ // Handle error
83
+ }
84
+ };
85
+
86
+ LogSinkPack.prototype.write = function (dout) {
87
+ Pack.prototype.write.call(this, dout);
88
+ dout.writeByte(0);
89
+ dout.writeText(this.category);
90
+ dout.writeDecimal(this.tagHash);
91
+ dout.writeValue(this.tags);
92
+ dout.writeDecimal(this.line);
93
+ dout.writeText(this.content);
94
+ if (this.fields.size() > 0) {
95
+ dout.writeBoolean(true);
96
+ dout.writeValue(this.fields);
97
+ } else {
98
+ dout.writeBoolean(false);
99
+ }
100
+ };
101
+
102
+ LogSinkPack.prototype.read = function (din) {
103
+ Pack.prototype.read.call(this, din);
104
+ const ver = din.readByte();
105
+ this.category = din.readText();
106
+ this.tagHash = din.readDecimal();
107
+ this.tags = din.readValue();
108
+ this.line = din.readDecimal();
109
+ this.content = din.readText();
110
+ if (din.readBoolean()) {
111
+ this.fields = din.readValue();
112
+ }
113
+ return this;
114
+ };
115
+
116
+ LogSinkPack.prototype.resetTagHash = function () {
117
+ const out = new DataOutputX();
118
+ out.writeValue(this.tags);
119
+ const tagBytes = out.toByteArray();
120
+ this.tagHash = HashUtil.hash64(tagBytes);
121
+ return tagBytes;
122
+ };
123
+
124
+ LogSinkPack.prototype.transferOidToTag = function () {
125
+ if (this.oid !== 0 && !this.tags.containsKey('oid')) {
126
+ this.tags.putValue('oid', this.oid);
127
+ this.tagHash = 0;
128
+ }
129
+ if (this.okind !== 0 && !this.tags.containsKey('okind')) {
130
+ this.tags.putValue('okind', this.okind);
131
+ this.tagHash = 0;
132
+ }
133
+ if (this.onode !== 0 && !this.tags.containsKey('onode')) {
134
+ this.tags.putValue('onode', this.onode);
135
+ this.tagHash = 0;
136
+ }
137
+ };
138
+
139
+ module.exports = LogSinkPack;
@@ -32,6 +32,7 @@ exports.HITMAP5M = 0x1700;
32
32
 
33
33
  exports.TAG_COUNT = 0x1601;
34
34
  exports.ZIP = 0x170b;
35
+ exports.LOGSINK = 0x170a;
35
36
 
36
37
  exports.PACK_NAME = {
37
38
  0x0100 : 'PARAMETER',
@@ -58,6 +59,7 @@ exports.PACK_NAME = {
58
59
  0x1600 : 'COUNTER5M',
59
60
  0x1700 : 'HITMAP5M',
60
61
  0x1601 : 'TAG_COUNT',
61
- 0x170b : 'ZIP'
62
+ 0x170b : 'ZIP',
63
+ 0x170a : 'LOGSINK',
62
64
  };
63
65
 
@@ -10,7 +10,6 @@ var PackEnum = require('./packenum'),
10
10
  Step = require('../step/step'),
11
11
  DataOutputX = require('../io/data-outputx'),
12
12
  DataInputX = require('../io/data-inputx');
13
- var zlib = require('zlib');
14
13
 
15
14
  function ZipPack() {
16
15
  Pack.call(this);
@@ -0,0 +1,63 @@
1
+ var { InetAddress } = require('net');
2
+ var CompareUtil = require('../util/compare-util');
3
+ var HashUtil = require('../util/hashutil');
4
+
5
+ function LINK() {
6
+ this.ip = null;
7
+ this.port = 0;
8
+
9
+ this.toString = function () {
10
+ if (this.ip === null) return `0.0.0.0:${this.port}`;
11
+ try {
12
+ return `${InetAddress.getByAddress(this.ip).getHostAddress()}:${this.port}`;
13
+ } catch (e) {
14
+ return `0.0.0.0:${this.port}`;
15
+ }
16
+ };
17
+
18
+ this.include = function (k) {
19
+ if (!CompareUtil.equals(this.ip, k.ip)) return false;
20
+ if (this.port === 0) return true;
21
+ return this.port === k.port;
22
+ };
23
+
24
+ this.equals = function (o) {
25
+ if (o.constructor !== this.constructor) return false;
26
+ var k = o;
27
+ if (!CompareUtil.equals(this.ip, k.ip)) return false;
28
+ return this.port === k.port;
29
+ };
30
+
31
+ this.hashCode = function () {
32
+ return HashUtil.hash(this.ip) | this.port;
33
+ };
34
+
35
+ this.toBytes = function (out) {
36
+ out.writeBlob(this.ip);
37
+ out.writeInt(this.port);
38
+ return this;
39
+ };
40
+
41
+ this.toObject = function (din) {
42
+ this.ip = din.readBlob();
43
+ this.port = din.readInt();
44
+ return this;
45
+ };
46
+ }
47
+
48
+ LINK.create = function (ipStr, port) {
49
+ var k = new LINK();
50
+ try {
51
+ k.ip = InetAddress.getByName(ipStr).getAddress();
52
+ } catch (e) {
53
+ k.ip = new Buffer(4);
54
+ }
55
+ k.port = port;
56
+ return k;
57
+ };
58
+
59
+ LINK.prototype = new LINK();
60
+ LINK.prototype.constructor = LINK;
61
+
62
+ module.exports = LINK;
63
+
@@ -0,0 +1,123 @@
1
+ const DataOutputX = require('../io/data-outputx');
2
+ const DataInputX = require('../io/data-inputx');
3
+ const MapValue = require('../value/map-value');
4
+ const LINK = require('./link');
5
+ const net = require('net');
6
+
7
+ function NodeInfo() {
8
+ this.attr = new MapValue();
9
+ this.listen = new Set();
10
+ this.outter = new Set();
11
+
12
+ this.addListen = function (localIPs, address) {
13
+ const ipo = this.getIPPORT(address);
14
+ if (!ipo || this.isLocal127(ipo)) return;
15
+
16
+ if (ipo.ip === '*' || ipo.ip === '0.0.0.0' || ipo.ip === '::') {
17
+ localIPs.forEach(local_ip => {
18
+ const k = LINK.create(local_ip, parseInt(ipo.port, 10));
19
+ if (k) this.listen.add(k);
20
+ });
21
+ } else {
22
+ const k = LINK.create(ipo.ip, parseInt(ipo.port, 10));
23
+ if (k) this.listen.add(k);
24
+ }
25
+ };
26
+
27
+ this.addOutter = function (local, remote) {
28
+ const localIPO = this.getIPPORT(local);
29
+ if (!localIPO || this.isIPv6(localIPO)) return;
30
+ if (this.hasListen(localIPO.ip, localIPO.port)) return;
31
+
32
+ const remoteIPO = this.getIPPORT(remote);
33
+ if (!remoteIPO || this.isIPv6(remoteIPO) || this.isLocal127(remoteIPO)) return;
34
+
35
+ const k = LINK.create(remoteIPO.ip, parseInt(remoteIPO.port, 10));
36
+ if (k) this.outter.add(k);
37
+ };
38
+
39
+ this.hasListen = function (ip, port) {
40
+ const k = LINK.create(ip, parseInt(port, 10));
41
+ return this.listen.has(k);
42
+ };
43
+
44
+ this.getIPPORT = function (address) {
45
+ try {
46
+ const ipo = {};
47
+ const x = address.lastIndexOf(':');
48
+ ipo.ip = address.substring(0, x);
49
+ ipo.port = address.substring(x + 1);
50
+ return ipo;
51
+ } catch (e) {
52
+ return null;
53
+ }
54
+ };
55
+
56
+ this.toBytes = function () {
57
+ const out = new DataOutputX();
58
+ out.writeByte(0);
59
+ out.writeValue(this.attr);
60
+ this.toLinkBytes(this.listen, out);
61
+ this.toLinkBytes(this.outter, out);
62
+ return out.toByteArray();
63
+ };
64
+
65
+ this.toObject = function (b) {
66
+ const din = new DataInputX(b);
67
+ const ver = din.readByte();
68
+ this.attr = din.readValue();
69
+ this.listen = this.toLinkObject(din);
70
+ this.outter = this.toLinkObject(din);
71
+ return this;
72
+ };
73
+
74
+ this.toLinkObject = function (din) {
75
+ const data = new Set();
76
+ const sz = din.readDecimal();
77
+ for (let i = 0; i < sz; i++) {
78
+ data.add(new LINK().toObject(din));
79
+ }
80
+ return data;
81
+ };
82
+
83
+ this.toLinkBytes = function (data, out) {
84
+ out.writeDecimal(data.size);
85
+ if (data.size === 0) return;
86
+ data.forEach(k => k.toBytes(out));
87
+ };
88
+
89
+ this.toJSON = function () {
90
+ const o = new Map();
91
+ o.put('attr', JSON.stringify(this.attr));
92
+ o.put('listen', JSON.parse(this.listen));
93
+ o.put('outter', JSON.parse(this.outter));
94
+ return o;
95
+ };
96
+
97
+ this.toJSON = function (data) {
98
+ const out = new JSONArray();
99
+ data.forEach(k => out.put(k.toString()));
100
+ return out;
101
+ };
102
+
103
+ this.isIPv6 = function(data) {
104
+ var ip = data.ip;
105
+ if(ip){
106
+ return net.isIPv6(ip);
107
+ }
108
+ return false;
109
+ };
110
+
111
+ this.isLocal127 = function (data) {
112
+ var ip = data.ip;
113
+ if(ip){
114
+ return ip === '127.0.0.1';
115
+ }
116
+ return false;
117
+ }
118
+ }
119
+
120
+ NodeInfo.prototype = new NodeInfo();
121
+ NodeInfo.prototype.constructor = NodeInfo;
122
+
123
+ module.exports = NodeInfo;
@@ -0,0 +1,111 @@
1
+ var { exec } = require('child_process');
2
+ var os = require('os');
3
+ var NodeInfo = require('./nodeinfo');
4
+ var Logger = require('../logger');
5
+
6
+ function StatusDetector() {
7
+ this.process = async function () {
8
+ let node = new NodeInfo();
9
+ let stat = await this.netstat();
10
+
11
+ if (!stat) {
12
+ node = new NodeInfo();
13
+ } else {
14
+ try {
15
+ node = this.parse(stat);
16
+ } catch (e) {
17
+ node = new NodeInfo();
18
+ Logger.printError("WHATAP-203", "NodeInfo parse error", e, false);
19
+ }
20
+ }
21
+
22
+ node.attr.type = 'nodejs';
23
+ node.attr.time = Date.now();
24
+ node.attr.ip = this.getLocalIpAddress();
25
+ node.attr.pid = process.pid;
26
+ node.attr.pname = process.title;
27
+
28
+ return node;
29
+ };
30
+
31
+ this.netstat = function () {
32
+ return new Promise((resolve, reject) => {
33
+ let cmd = 'netstat -an -t';
34
+ if (os.platform() === 'darwin') {
35
+ cmd = 'netstat -an -p tcp';
36
+ } else if (os.platform() === 'win32') {
37
+ resolve('');
38
+ return;
39
+ }
40
+
41
+ exec(cmd, (error, stdout, stderr) => {
42
+ if (error) {
43
+ reject(error);
44
+ return;
45
+ }
46
+ resolve(stdout);
47
+ });
48
+ });
49
+ };
50
+
51
+ this.parse = function (netstat) {
52
+ var node = new NodeInfo();
53
+ var localIPs = this.getLocalIpSet();
54
+
55
+ var lines = netstat.split('\n');
56
+ lines.forEach(line => {
57
+ if (line.startsWith('tcp')) {
58
+ this.parseLine(node, line, localIPs);
59
+ }
60
+ });
61
+
62
+ return node;
63
+ };
64
+
65
+ this.parseLine = function (node, line, localIPs) {
66
+ var tokens = line.trim().split(/\s+/);
67
+ if (!tokens[0].startsWith('tcp')) return;
68
+
69
+ var localAddress = tokens[3].replace(/(\d+\.\d+\.\d+\.\d+)\.(\d+)/, '$1:$2');
70
+ var remoteAddress = tokens[4].replace(/(\d+\.\d+\.\d+\.\d+)\.(\d+)/, '$1:$2');
71
+
72
+ if (tokens[5] === 'LISTEN') {
73
+ node.addListen(localIPs, localAddress);
74
+ } else {
75
+ node.addOutter(localAddress, remoteAddress);
76
+ }
77
+ };
78
+
79
+ this.getLocalIpSet = function () {
80
+ var ipSet = new Set();
81
+ var interfaces = os.networkInterfaces();
82
+ for (var devName in interfaces) {
83
+ var iface = interfaces[devName];
84
+ iface.forEach(alias => {
85
+ if (alias.family === 'IPv4' && !alias.internal) {
86
+ ipSet.add(alias.address);
87
+ }
88
+ });
89
+ }
90
+ return ipSet;
91
+ };
92
+
93
+ this.getLocalIpAddress = function () {
94
+ var interfaces = os.networkInterfaces();
95
+ for (var devName in interfaces) {
96
+ var iface = interfaces[devName];
97
+ for (let i = 0; i < iface.length; i++) {
98
+ var alias = iface[i];
99
+ if (alias.family === 'IPv4' && !alias.internal) {
100
+ return alias.address;
101
+ }
102
+ }
103
+ }
104
+ return '0.0.0.0';
105
+ };
106
+ }
107
+
108
+ StatusDetector.prototype = new StatusDetector();
109
+ StatusDetector.prototype.constructor = StatusDetector;
110
+
111
+ module.exports = StatusDetector;
@@ -0,0 +1,131 @@
1
+ var CompareUtil = {
2
+ compareTo: function (l, r) {
3
+ if (Array.isArray(l) && Array.isArray(r)) {
4
+ return this.compareArrays(l, r);
5
+ }
6
+ if (typeof l === 'string' && typeof r === 'string') {
7
+ return l.localeCompare(r);
8
+ }
9
+ if (typeof l === 'number' && typeof r === 'number') {
10
+ return l === r ? 0 : l > r ? 1 : -1;
11
+ }
12
+ if (l && typeof l.compareTo === 'function' && r && typeof r.compareTo === 'function') {
13
+ return l.compareTo(r);
14
+ }
15
+ throw new Error('Unsupported comparison types');
16
+ },
17
+
18
+ compareArrays: function (l, r) {
19
+ if (l == null && r == null) return 0;
20
+ if (l == null) return -1;
21
+ if (r == null) return 1;
22
+ for (let i = 0; i < l.length && i < r.length; i++) {
23
+ let comp = this.compareTo(l[i], r[i]);
24
+ if (comp !== 0) return comp;
25
+ }
26
+ return l.length - r.length;
27
+ },
28
+
29
+ equals: function (l, r) {
30
+ return this.compareTo(l, r) === 0;
31
+ },
32
+
33
+ compareByteArrays: function (l, r) {
34
+ return this.compareArrays(l, r);
35
+ },
36
+
37
+ compareShortArrays: function (l, r) {
38
+ return this.compareArrays(l, r);
39
+ },
40
+
41
+ compareIntArrays: function (l, r) {
42
+ return this.compareArrays(l, r);
43
+ },
44
+
45
+ compareFloatArrays: function (l, r) {
46
+ return this.compareArrays(l, r);
47
+ },
48
+
49
+ compareLongArrays: function (l, r) {
50
+ return this.compareArrays(l, r);
51
+ },
52
+
53
+ compareDoubleArrays: function (l, r) {
54
+ return this.compareArrays(l, r);
55
+ },
56
+
57
+ compareStringArrays: function (l, r) {
58
+ return this.compareArrays(l, r);
59
+ },
60
+
61
+ compareComparableArrays: function (l, r) {
62
+ return this.compareArrays(l, r);
63
+ },
64
+
65
+ compareToValues: function (l, r) {
66
+ if (l == null && r == null) return 0;
67
+ if (l == null) return -1;
68
+ if (r == null) return 1;
69
+ return l.compareTo(r);
70
+ },
71
+
72
+ equalsByteArrays: function (l, r) {
73
+ return this.compareByteArrays(l, r) === 0;
74
+ },
75
+
76
+ equalsShortArrays: function (l, r) {
77
+ return this.compareShortArrays(l, r) === 0;
78
+ },
79
+
80
+ equalsIntArrays: function (l, r) {
81
+ return this.compareIntArrays(l, r) === 0;
82
+ },
83
+
84
+ equalsFloatArrays: function (l, r) {
85
+ return this.compareFloatArrays(l, r) === 0;
86
+ },
87
+
88
+ equalsLongArrays: function (l, r) {
89
+ return this.compareLongArrays(l, r) === 0;
90
+ },
91
+
92
+ equalsDoubleArrays: function (l, r) {
93
+ return this.compareDoubleArrays(l, r) === 0;
94
+ },
95
+
96
+ equalsStringArrays: function (l, r) {
97
+ return this.compareStringArrays(l, r) === 0;
98
+ },
99
+
100
+ equalsComparableArrays: function (l, r) {
101
+ return this.compareComparableArrays(l, r) === 0;
102
+ },
103
+
104
+ equalsValues: function (l, r) {
105
+ return this.compareToValues(l, r) === 0;
106
+ },
107
+
108
+ equalsString: function (l, r) {
109
+ if (l == null) return r == null;
110
+ else return l === r;
111
+ },
112
+
113
+ equalsObject: function (l, r) {
114
+ if (l == null) return r == null;
115
+ else return l === r;
116
+ },
117
+
118
+ equalsLong: function (l, r) {
119
+ return l === r;
120
+ },
121
+
122
+ equalsInt: function (l, r) {
123
+ return l === r;
124
+ },
125
+
126
+ equalsFloat: function (l, r) {
127
+ return l === r;
128
+ }
129
+ };
130
+
131
+ module.exports = CompareUtil;