whatap 0.4.79 → 0.4.80
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/.vscode/keep-context.json +3 -3
- package/README.md +51 -51
- package/bindings/darwin/x64/whatap.node +0 -0
- package/bindings/linux/ia32/whatap.node +0 -0
- package/bindings/linux/x64/whatap.node +0 -0
- package/help.txt +10 -10
- package/index.js +8 -8
- package/lib/conf/conf-sys-mon.js +100 -100
- package/lib/conf/config-default.js +238 -238
- package/lib/conf/configure.js +423 -423
- package/lib/conf/license.js +41 -41
- package/lib/control/cmd-config.js +23 -23
- package/lib/control/control-handler.js +344 -344
- package/lib/control/packagectr-helper.js +149 -149
- package/lib/core/agent.js +329 -328
- package/lib/core/interceptor.js +142 -142
- package/lib/core/request-agent.js +26 -26
- package/lib/counter/counter-manager.js +127 -127
- package/lib/counter/meter/meter-activex.js +66 -66
- package/lib/counter/meter/meter-httpc.js +57 -57
- package/lib/counter/meter/meter-resource.js +9 -9
- package/lib/counter/meter/meter-service.js +167 -167
- package/lib/counter/meter/meter-socket.io.js +50 -50
- package/lib/counter/meter/meter-sql.js +70 -70
- package/lib/counter/meter/meter-users.js +57 -57
- package/lib/counter/meter.js +183 -183
- package/lib/counter/task/activetransaction.js +93 -93
- package/lib/counter/task/agentinfo.js +105 -105
- package/lib/counter/task/counter-task.js +9 -9
- package/lib/counter/task/gcstat.js +34 -34
- package/lib/counter/task/heapmem.js +24 -24
- package/lib/counter/task/httpc.js +75 -75
- package/lib/counter/task/proc-cpu.js +28 -28
- package/lib/counter/task/realtimeuser.js +30 -30
- package/lib/counter/task/res/systemECSTask.js +54 -54
- package/lib/counter/task/res/systemKubeTask.js +52 -52
- package/lib/counter/task/res/util/awsEcsClientThread.js +166 -166
- package/lib/counter/task/res/util/linuxProcStatUtil.js +13 -13
- package/lib/counter/task/res-sys-cpu.js +61 -61
- package/lib/counter/task/service.js +201 -201
- package/lib/counter/task/socketio.js +29 -29
- package/lib/counter/task/sql.js +104 -104
- package/lib/counter/task/systemperf.js +42 -42
- package/lib/data/datapack-sender.js +256 -256
- package/lib/data/dataprofile-agent.js +136 -136
- package/lib/data/datatext-agent.js +135 -135
- package/lib/data/event-level.js +15 -15
- package/lib/data/test.js +48 -48
- package/lib/env/constants.js +20 -20
- package/lib/kube/kube-client.js +143 -143
- package/lib/lang/text-types.js +58 -58
- package/lib/logger.js +319 -319
- package/lib/net/netflag.js +54 -54
- package/lib/net/paramdef.js +40 -40
- package/lib/net/receiver.js +65 -65
- package/lib/net/security-master.js +182 -173
- package/lib/net/sender.js +140 -140
- package/lib/net/tcp-return.js +17 -17
- package/lib/net/tcp-session.js +285 -285
- package/lib/net/tcpreq-client-proxy.js +69 -69
- package/lib/net/tcprequest-mgr.js +57 -57
- package/lib/observers/cluster-observer.js +21 -21
- package/lib/observers/express-observer.js +214 -214
- package/lib/observers/file-observer.js +184 -184
- package/lib/observers/global-observer.js +30 -30
- package/lib/observers/http-observer.js +704 -704
- package/lib/observers/maria-observer.js +382 -382
- package/lib/observers/memcached-observer.js +55 -55
- package/lib/observers/mongo-observer.js +262 -265
- package/lib/observers/mongodb-observer.js +197 -197
- package/lib/observers/mongoose-observer.js +83 -83
- package/lib/observers/mssql-observer.js +205 -205
- package/lib/observers/mysql-observer.js +436 -394
- package/lib/observers/net-observer.js +72 -72
- package/lib/observers/pgsql-observer.js +295 -295
- package/lib/observers/process-observer.js +25 -25
- package/lib/observers/promise-observer.js +31 -31
- package/lib/observers/redis-observer.js +156 -109
- package/lib/observers/schedule-observer.js +66 -66
- package/lib/observers/socket.io-observer.js +54 -54
- package/lib/observers/stream-observer.js +19 -19
- package/lib/observers/thrift-observer.js +196 -196
- package/lib/pack/activestack-pack.js +54 -54
- package/lib/pack/counter-pack.js +649 -649
- package/lib/pack/errorsnap-pack.js +68 -68
- package/lib/pack/event-pack.js +53 -53
- package/lib/pack/hitmap-pack.js +62 -62
- package/lib/pack/hitmap-pack1.js +146 -146
- package/lib/pack/netstat.js +14 -14
- package/lib/pack/pack.js +49 -49
- package/lib/pack/packenum.js +60 -60
- package/lib/pack/param-pack.js +213 -213
- package/lib/pack/profile-pack.js +48 -48
- package/lib/pack/realtimeuser-pack.js +40 -40
- package/lib/pack/stat-general-pack.js +95 -95
- package/lib/pack/staterror-pack.js +119 -119
- package/lib/pack/stathttpc-pack.js +66 -66
- package/lib/pack/stathttpc-rec.js +78 -78
- package/lib/pack/statremote-pack.js +45 -45
- package/lib/pack/statservice-pack.js +62 -62
- package/lib/pack/statservice-pack1.js +87 -87
- package/lib/pack/statservice-rec.js +292 -292
- package/lib/pack/statservice-rec_dep.js +151 -151
- package/lib/pack/statsql-pack.js +69 -69
- package/lib/pack/statsql-rec.js +100 -100
- package/lib/pack/statuseragent-pack.js +43 -43
- package/lib/pack/tagcount-pack.js +398 -398
- package/lib/pack/tagctr.js +14 -14
- package/lib/pack/text-pack.js +49 -49
- package/lib/pack/time-count.js +25 -25
- package/lib/pack/websocket.js +14 -14
- package/lib/plugin/plugin-loadermanager.js +56 -56
- package/lib/plugin/plugin.js +75 -75
- package/lib/requestlog.js +326 -326
- package/lib/service/tx-record.js +288 -288
- package/lib/stat/stat-error.js +116 -116
- package/lib/stat/stat-httpc.js +97 -97
- package/lib/stat/stat-remoteip.js +46 -46
- package/lib/stat/stat-sql.js +112 -112
- package/lib/stat/stat-tranx.js +57 -57
- package/lib/stat/stat-tx-caller.js +159 -159
- package/lib/stat/stat-tx-domain.js +110 -110
- package/lib/stat/stat-tx-referer.js +111 -111
- package/lib/stat/stat-useragent.js +48 -48
- package/lib/stat/timingsender.js +72 -72
- package/lib/step/activestack-step.js +37 -37
- package/lib/step/dbc-step.js +35 -35
- package/lib/step/http-stepx.js +66 -66
- package/lib/step/message-step.js +39 -39
- package/lib/step/method-stepx.js +46 -46
- package/lib/step/resultset-step.js +39 -39
- package/lib/step/securemsg-step.js +43 -43
- package/lib/step/socket-step.js +46 -46
- package/lib/step/sql-stepx.js +67 -67
- package/lib/step/sqlxtype.js +15 -15
- package/lib/step/step.js +65 -65
- package/lib/step/stepenum.js +53 -53
- package/lib/trace/local-context.js +13 -13
- package/lib/trace/parsed-sql.js +13 -13
- package/lib/trace/profile-collector.js +70 -70
- package/lib/trace/serviceurl-pattern-detector.js +117 -117
- package/lib/trace/trace-context-manager.js +195 -195
- package/lib/trace/trace-context.js +134 -134
- package/lib/util/anylist.js +102 -102
- package/lib/util/array-util.js +100 -100
- package/lib/util/bitutil.js +27 -27
- package/lib/util/cardinality/hyperloglog.js +105 -105
- package/lib/util/cardinality/murmurhash.js +31 -31
- package/lib/util/cardinality/registerset.js +74 -74
- package/lib/util/config-util.js +17 -17
- package/lib/util/cypher.js +89 -89
- package/lib/util/datetimehelper.js +237 -229
- package/lib/util/dateutil.js +110 -106
- package/lib/util/errordata.js +20 -20
- package/lib/util/escape-literal-sql.js +342 -342
- package/lib/util/hashutil2.js +126 -126
- package/lib/util/hexa32.js +57 -57
- package/lib/util/index.js +78 -78
- package/lib/util/intint-map.js +47 -47
- package/lib/util/intkey-linkedmap.js +26 -26
- package/lib/util/intkey-map.js +25 -25
- package/lib/util/intset.js +82 -82
- package/lib/util/iputil.js +119 -119
- package/lib/util/iputil_x.js +526 -526
- package/lib/util/keygen.js +17 -17
- package/lib/util/kube-util.js +72 -72
- package/lib/util/longint-linkedmap.js +35 -35
- package/lib/util/longkey-linkedmap.js +25 -25
- package/lib/util/longlong-linkedmap.js +37 -37
- package/lib/util/nodeutil.js +67 -67
- package/lib/util/oidutil.js +96 -93
- package/lib/util/paramsecurity.js +78 -78
- package/lib/util/pathtree.js +172 -172
- package/lib/util/pre-process.js +13 -13
- package/lib/util/process-seq.js +165 -165
- package/lib/util/property-util.js +35 -35
- package/lib/util/request-queue.js +42 -42
- package/lib/util/requestdouble-queue.js +72 -72
- package/lib/util/resourceprofile.js +156 -156
- package/lib/util/seedrandom.js +242 -242
- package/lib/util/stop-watch.js +29 -29
- package/lib/util/string-util.js +9 -9
- package/lib/util/stringkey-linkedmap.js +28 -28
- package/lib/util/stringnum-linkedmap.js +31 -31
- package/lib/util/stringset.js +65 -65
- package/lib/util/system-util.js +9 -9
- package/lib/util/userid-util.js +57 -57
- package/lib/util/utils.js +67 -67
- package/lib/value/blob-value.js +61 -61
- package/lib/value/boolean-value.js +51 -51
- package/lib/value/decimal-value.js +71 -71
- package/lib/value/double-summary.js +105 -105
- package/lib/value/double-value.js +50 -50
- package/lib/value/float-array.js +59 -59
- package/lib/value/float-value.js +64 -64
- package/lib/value/int-array.js +59 -59
- package/lib/value/int-map-value.js +151 -151
- package/lib/value/int-value.js +64 -64
- package/lib/value/ip4-value.js +83 -83
- package/lib/value/list-value.js +136 -136
- package/lib/value/long-array.js +59 -59
- package/lib/value/long-summary.js +105 -105
- package/lib/value/map-value.js +175 -175
- package/lib/value/metric-value.js +157 -157
- package/lib/value/null-value.js +42 -42
- package/lib/value/number-value.js +34 -34
- package/lib/value/summary-value.js +29 -29
- package/lib/value/text-array.js +117 -117
- package/lib/value/text-value.js +59 -59
- package/lib/value/texthash-value.js +49 -49
- package/lib/value/value.js +14 -14
- package/lib/value/valueenum.js +99 -99
- package/logs/whatap-20230906.log +54 -0
- package/package.json +28 -28
- package/whatap.conf +1 -1
- package/logs/whatap-20230616.log +0 -13
package/lib/net/tcp-return.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2016 the WHATAP project authors. All rights reserved.
|
|
3
|
-
* Use of this source code is governed by a license that
|
|
4
|
-
* can be found in the LICENSE file.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
function TcpReturn() {
|
|
8
|
-
this.code = 0;
|
|
9
|
-
this.data = null; /*Buffer*/
|
|
10
|
-
this.transfer_key = 0;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
TcpReturn.prototype.toString = function () {
|
|
14
|
-
return "TcpReturn [code=" + this.code + ", transfer_key=" + this.transfer_key + ", data="
|
|
15
|
-
+ (this.data != null ? this.data.toString('hex') : null) + "]";
|
|
16
|
-
};
|
|
17
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2016 the WHATAP project authors. All rights reserved.
|
|
3
|
+
* Use of this source code is governed by a license that
|
|
4
|
+
* can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
function TcpReturn() {
|
|
8
|
+
this.code = 0;
|
|
9
|
+
this.data = null; /*Buffer*/
|
|
10
|
+
this.transfer_key = 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
TcpReturn.prototype.toString = function () {
|
|
14
|
+
return "TcpReturn [code=" + this.code + ", transfer_key=" + this.transfer_key + ", data="
|
|
15
|
+
+ (this.data != null ? this.data.toString('hex') : null) + "]";
|
|
16
|
+
};
|
|
17
|
+
|
|
18
18
|
module.exports = TcpReturn;
|
package/lib/net/tcp-session.js
CHANGED
|
@@ -1,286 +1,286 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2016 the WHATAP project authors. All rights reserved.
|
|
3
|
-
* Use of this source code is governed by a license that
|
|
4
|
-
* can be found in the LICENSE file.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
var net = require('net'),
|
|
8
|
-
netflag = require('./netflag').NetFlag,
|
|
9
|
-
netsrc = require('./netflag').NetSrc,
|
|
10
|
-
secu = require('./security-master'),
|
|
11
|
-
conf = require('./../conf/configure'),
|
|
12
|
-
DataOutputX = require('./../io/data-outputx'),
|
|
13
|
-
DataInputX = require('./../io/data-inputx'),
|
|
14
|
-
IPUtil = require('./../util/iputil'),
|
|
15
|
-
TcpReturn = require('./tcp-return'),
|
|
16
|
-
Receiver = require('./receiver'),
|
|
17
|
-
OidUtil = require('../util/oidutil'),
|
|
18
|
-
Logger = require('../logger'),
|
|
19
|
-
Long = require('long');
|
|
20
|
-
|
|
21
|
-
function TcpSession() {
|
|
22
|
-
if(typeof TcpSession.instance === 'object') {
|
|
23
|
-
return TcpSession.instance;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
this.in = null;
|
|
27
|
-
this.out = null;
|
|
28
|
-
this.receiver = new Receiver();
|
|
29
|
-
this._cb = null;
|
|
30
|
-
this.reconnectCnt = 0;
|
|
31
|
-
TcpSession.instance = this;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
TcpSession.prototype.open = function(cb){
|
|
35
|
-
var self = this;
|
|
36
|
-
|
|
37
|
-
if(cb) this._cb = cb;
|
|
38
|
-
|
|
39
|
-
self.auto_oname_reset = conf.auto_oname_reset;
|
|
40
|
-
if(secu.PCODE == null || secu.PCODE === 0) {
|
|
41
|
-
if(cb) return cb(new Error('PCODE is not defined..'));
|
|
42
|
-
}
|
|
43
|
-
if(this.isOpen()) {
|
|
44
|
-
++self.reconnectCnt;
|
|
45
|
-
if(self.reconnectCnt >= 3) {
|
|
46
|
-
self.close();
|
|
47
|
-
}
|
|
48
|
-
return cb(new Error('Already connected..'));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
var host = conf.getWhatapServerHost();
|
|
52
|
-
var port = conf.whatap_server_port || 6600;
|
|
53
|
-
var connection_timeout = conf.tcp_connection_timeout;
|
|
54
|
-
|
|
55
|
-
var options = {
|
|
56
|
-
host: host,
|
|
57
|
-
port: port
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
Logger.print('WHATAP-180', "Try to connect to " + JSON.stringify(options), false);
|
|
61
|
-
this.out = net.connect(options, function() {
|
|
62
|
-
var localAddr = (this.localAddress || IPUtil.getIp());
|
|
63
|
-
|
|
64
|
-
if(conf.auto_oname_enabled === true) {
|
|
65
|
-
var oname = conf["whatap.oname"];
|
|
66
|
-
if((oname == null || oname.length === 0)
|
|
67
|
-
&& self.auto_oname_reset === conf.auto_oname_reset) {
|
|
68
|
-
self.prepareAutoOnameByServer(localAddr);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
secu.decideAgentOnameOid(localAddr);
|
|
74
|
-
if(conf.cipher_net_enabled){
|
|
75
|
-
try {
|
|
76
|
-
self.out.write( self.keyReset() );
|
|
77
|
-
} catch(e) {
|
|
78
|
-
Logger.printError("WHATAP-125", "KeyReset " + host + ":" + port, e, true);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
this.out.setKeepAlive(true , 60000);
|
|
84
|
-
this.out.setTimeout(connection_timeout);
|
|
85
|
-
|
|
86
|
-
this.out.on('error', function (err) {
|
|
87
|
-
Logger.printError("WHATAP-127", 'Failed to connect to ' + JSON.stringify(options), err);
|
|
88
|
-
self.close();
|
|
89
|
-
cb(err);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
this.out.on('timeout' , function () {
|
|
93
|
-
Logger.print("WHATAP-184", "Socket timeout. WhaTap will retry connect after " + (connection_timeout/1000) + " sec.", false);
|
|
94
|
-
self.close();
|
|
95
|
-
setTimeout(function() {
|
|
96
|
-
self.open(cb);
|
|
97
|
-
} , 500);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
var firstReceived = false;
|
|
101
|
-
this.out.on('data', function(data) {
|
|
102
|
-
if(self.PREPARE_AGENT) {
|
|
103
|
-
var din = new DataInputX(data);
|
|
104
|
-
var source = din.readByte();
|
|
105
|
-
var code = din.readByte();
|
|
106
|
-
var pcode = din.readLong();
|
|
107
|
-
var oid = din.readInt();
|
|
108
|
-
var transfer_key = din.readInt();
|
|
109
|
-
var _data = din.readIntBytes(8 * 1024 * 1024);
|
|
110
|
-
if (conf.cypher_level > 0) {
|
|
111
|
-
_data = secu.cypher.decrypt(_data);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
var oname = new DataInputX(_data).readText();
|
|
115
|
-
|
|
116
|
-
var localAddr = (this.localAddress || IPUtil.getIp());
|
|
117
|
-
secu.setAgentOnameOid(oname, localAddr);
|
|
118
|
-
self.PREPARE_AGENT = false;
|
|
119
|
-
if(conf.cipher_net_enabled) {
|
|
120
|
-
try {
|
|
121
|
-
self.out.write( self.keyReset() );
|
|
122
|
-
} catch(e) {
|
|
123
|
-
Logger.printError("WHATAP-125", "KeyReset " + host + ":" + port, e, true);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if(firstReceived) {
|
|
130
|
-
self.read(data);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
var d = self.readKeyReset(data);
|
|
134
|
-
if( d && d.length != 0 ) {
|
|
135
|
-
conf.updateNetCypherKey(d);
|
|
136
|
-
firstReceived = true;
|
|
137
|
-
if(cb) cb();
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
this.out.on('end', function(){
|
|
142
|
-
Logger.print("WHATAP-183", "Disconnected from server, please check your network or Whatap Server!!!!!", false);
|
|
143
|
-
self.close();
|
|
144
|
-
setTimeout(function() {
|
|
145
|
-
cb(new Error('Socket end Event!!!!!'));
|
|
146
|
-
return;
|
|
147
|
-
},500);
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
TcpSession.prototype.close = function(){
|
|
152
|
-
if(this.out){
|
|
153
|
-
this.out.destroy();
|
|
154
|
-
this.out = null;
|
|
155
|
-
this.reconnectCnt = 0;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
TcpSession.prototype.send = function(code , b ){
|
|
160
|
-
try{
|
|
161
|
-
if(this.out == null ){
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
var out = new DataOutputX();
|
|
165
|
-
out.writeByte(netsrc.AGENT_JAVA_EMBED);
|
|
166
|
-
out.writeByte(code);
|
|
167
|
-
out.writeLong(secu.PCODE);
|
|
168
|
-
out.writeInt(secu.OID);
|
|
169
|
-
out.writeInt(conf.TRANSFER_KEY);
|
|
170
|
-
out.writeIntBytes(b);
|
|
171
|
-
/*if(this.out === null ){
|
|
172
|
-
if(this._cb) {
|
|
173
|
-
setTimeout(function() {
|
|
174
|
-
this._cb(new Error('[Send Error] Socket is null !! TCP Session Reconnection......'));
|
|
175
|
-
return;
|
|
176
|
-
},100);
|
|
177
|
-
}
|
|
178
|
-
} */
|
|
179
|
-
this.out.write(out.toByteArray());
|
|
180
|
-
return true;
|
|
181
|
-
} catch(e) {
|
|
182
|
-
Logger.printError('WHATAP-126', 'TcpSession Error', e);
|
|
183
|
-
}
|
|
184
|
-
this.close();
|
|
185
|
-
return false;
|
|
186
|
-
};
|
|
187
|
-
TcpSession.prototype.readKeyReset = function( inPS /* DataInputX */){
|
|
188
|
-
try {
|
|
189
|
-
if( inPS.constructor === Buffer){
|
|
190
|
-
inPS = new DataInputX(inPS);
|
|
191
|
-
}
|
|
192
|
-
var source = inPS.readByte();
|
|
193
|
-
var code = inPS.readByte();
|
|
194
|
-
var pcode = inPS.readLong();
|
|
195
|
-
var oid = inPS.readInt();
|
|
196
|
-
var transfer_key = inPS.readInt();
|
|
197
|
-
var data = inPS.readIntBytes(1024);
|
|
198
|
-
|
|
199
|
-
if( pcode != secu.PCODE || oid != secu.OID){
|
|
200
|
-
return null;
|
|
201
|
-
} else {
|
|
202
|
-
return data;
|
|
203
|
-
}
|
|
204
|
-
} catch(e) {
|
|
205
|
-
Logger.printError("WHATAP-189", 'License key or invalid agent version error(fail key)', e, true);
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
TcpSession.prototype.keyReset = function(){
|
|
210
|
-
var msg = new DataOutputX().writeText("hello").writeText(secu.ONAME).writeInt8(secu.IP).toByteArray();
|
|
211
|
-
if( conf.cypher_level > 0 ){
|
|
212
|
-
msg = secu.cypher.encrypt(msg);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
var out = new DataOutputX();
|
|
216
|
-
out.writeByte(netsrc.AGENT_JAVA_EMBED);
|
|
217
|
-
out.writeByte(netflag.KEY_RESET);
|
|
218
|
-
out.writeLong(Long.fromValue(secu.PCODE));
|
|
219
|
-
out.writeInt(secu.OID);
|
|
220
|
-
out.writeInt(0);
|
|
221
|
-
out.writeIntBytes(msg);
|
|
222
|
-
return out.toByteArray();
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
TcpSession.prototype.isOpen = function(){
|
|
226
|
-
return this.out != null;
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
TcpSession.prototype.read = function (data) {
|
|
230
|
-
var din = new DataInputX(data);
|
|
231
|
-
var out = new TcpReturn();
|
|
232
|
-
var source = din.readByte();
|
|
233
|
-
|
|
234
|
-
out.code = din.readByte();
|
|
235
|
-
var pcode = din.readLong();
|
|
236
|
-
var oid = din.readInt();
|
|
237
|
-
out.transfer_key = din.readInt();
|
|
238
|
-
out.data = din.readIntBytes();
|
|
239
|
-
|
|
240
|
-
if(pcode != secu.PCODE || oid != secu.OID) {
|
|
241
|
-
Logger.printError('WHATAP-190', 'Received data is not mine..', new Error('Receive data error'));
|
|
242
|
-
return null;
|
|
243
|
-
}
|
|
244
|
-
this.receiver.read(out);
|
|
245
|
-
};
|
|
246
|
-
TcpSession.prototype.prepareAutoOnameByServer = function (localAddr) {
|
|
247
|
-
var self = this;
|
|
248
|
-
try {
|
|
249
|
-
var auto_oname_prefix = conf.auto_oname_prefix;
|
|
250
|
-
|
|
251
|
-
if(auto_oname_prefix.indexOf('{') >= 0) {
|
|
252
|
-
OidUtil.setIp(localAddr);
|
|
253
|
-
OidUtil.setPort(port);
|
|
254
|
-
OidUtil.setPid(process.pid);
|
|
255
|
-
OidUtil.setHostName(require('os').hostname());
|
|
256
|
-
auto_oname_prefix = OidUtil.mkOname(conf.auto_oname_prefix);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
var pout = new DataOutputX();
|
|
260
|
-
pout.writeText(auto_oname_prefix);
|
|
261
|
-
pout.writeInt(IPUtil.stringToInt(localAddr));
|
|
262
|
-
var msg = pout.toByteArray();
|
|
263
|
-
|
|
264
|
-
var trkey = 0;
|
|
265
|
-
if(conf.cypher_level > 0) {
|
|
266
|
-
msg = secu.cypher.encrypt(msg);
|
|
267
|
-
var b0 = 1;
|
|
268
|
-
var b1 = (conf.cypher_level / 8);
|
|
269
|
-
trkey = DataInputX.toInt([b0, b1, 0, 0], 0);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
var out = new DataOutputX();
|
|
273
|
-
out.writeByte(netsrc.AGENT_JAVA_EMBED);
|
|
274
|
-
out.writeByte(netflag.PREPARE_AGENT);
|
|
275
|
-
out.writeLong(secu.PCODE);
|
|
276
|
-
out.writeInt(0);
|
|
277
|
-
out.writeInt(trkey);
|
|
278
|
-
out.writeIntBytes(msg);
|
|
279
|
-
self.PREPARE_AGENT = true;
|
|
280
|
-
self.out.write(out.toByteArray());
|
|
281
|
-
} catch(e) {
|
|
282
|
-
Logger.printError("WHATAP-003", "Prepare oname error", e, false);
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2016 the WHATAP project authors. All rights reserved.
|
|
3
|
+
* Use of this source code is governed by a license that
|
|
4
|
+
* can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
var net = require('net'),
|
|
8
|
+
netflag = require('./netflag').NetFlag,
|
|
9
|
+
netsrc = require('./netflag').NetSrc,
|
|
10
|
+
secu = require('./security-master'),
|
|
11
|
+
conf = require('./../conf/configure'),
|
|
12
|
+
DataOutputX = require('./../io/data-outputx'),
|
|
13
|
+
DataInputX = require('./../io/data-inputx'),
|
|
14
|
+
IPUtil = require('./../util/iputil'),
|
|
15
|
+
TcpReturn = require('./tcp-return'),
|
|
16
|
+
Receiver = require('./receiver'),
|
|
17
|
+
OidUtil = require('../util/oidutil'),
|
|
18
|
+
Logger = require('../logger'),
|
|
19
|
+
Long = require('long');
|
|
20
|
+
|
|
21
|
+
function TcpSession() {
|
|
22
|
+
if(typeof TcpSession.instance === 'object') {
|
|
23
|
+
return TcpSession.instance;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this.in = null;
|
|
27
|
+
this.out = null;
|
|
28
|
+
this.receiver = new Receiver();
|
|
29
|
+
this._cb = null;
|
|
30
|
+
this.reconnectCnt = 0;
|
|
31
|
+
TcpSession.instance = this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
TcpSession.prototype.open = function(cb){
|
|
35
|
+
var self = this;
|
|
36
|
+
|
|
37
|
+
if(cb) this._cb = cb;
|
|
38
|
+
|
|
39
|
+
self.auto_oname_reset = conf.auto_oname_reset;
|
|
40
|
+
if(secu.PCODE == null || secu.PCODE === 0) {
|
|
41
|
+
if(cb) return cb(new Error('PCODE is not defined..'));
|
|
42
|
+
}
|
|
43
|
+
if(this.isOpen()) {
|
|
44
|
+
++self.reconnectCnt;
|
|
45
|
+
if(self.reconnectCnt >= 3) {
|
|
46
|
+
self.close();
|
|
47
|
+
}
|
|
48
|
+
return cb(new Error('Already connected..'));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
var host = conf.getWhatapServerHost();
|
|
52
|
+
var port = conf.whatap_server_port || 6600;
|
|
53
|
+
var connection_timeout = conf.tcp_connection_timeout;
|
|
54
|
+
|
|
55
|
+
var options = {
|
|
56
|
+
host: host,
|
|
57
|
+
port: port
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
Logger.print('WHATAP-180', "Try to connect to " + JSON.stringify(options), false);
|
|
61
|
+
this.out = net.connect(options, function() {
|
|
62
|
+
var localAddr = (this.localAddress || IPUtil.getIp());
|
|
63
|
+
|
|
64
|
+
if(conf.auto_oname_enabled === true) {
|
|
65
|
+
var oname = conf["whatap.oname"];
|
|
66
|
+
if((oname == null || oname.length === 0)
|
|
67
|
+
&& self.auto_oname_reset === conf.auto_oname_reset) {
|
|
68
|
+
self.prepareAutoOnameByServer(localAddr);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
secu.decideAgentOnameOid(localAddr);
|
|
74
|
+
if(conf.cipher_net_enabled){
|
|
75
|
+
try {
|
|
76
|
+
self.out.write( self.keyReset() );
|
|
77
|
+
} catch(e) {
|
|
78
|
+
Logger.printError("WHATAP-125", "KeyReset " + host + ":" + port, e, true);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
this.out.setKeepAlive(true , 60000);
|
|
84
|
+
this.out.setTimeout(connection_timeout);
|
|
85
|
+
|
|
86
|
+
this.out.on('error', function (err) {
|
|
87
|
+
Logger.printError("WHATAP-127", 'Failed to connect to ' + JSON.stringify(options), err);
|
|
88
|
+
self.close();
|
|
89
|
+
cb(err);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
this.out.on('timeout' , function () {
|
|
93
|
+
Logger.print("WHATAP-184", "Socket timeout. WhaTap will retry connect after " + (connection_timeout/1000) + " sec.", false);
|
|
94
|
+
self.close();
|
|
95
|
+
setTimeout(function() {
|
|
96
|
+
self.open(cb);
|
|
97
|
+
} , 500);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
var firstReceived = false;
|
|
101
|
+
this.out.on('data', function(data) {
|
|
102
|
+
if(self.PREPARE_AGENT) {
|
|
103
|
+
var din = new DataInputX(data);
|
|
104
|
+
var source = din.readByte();
|
|
105
|
+
var code = din.readByte();
|
|
106
|
+
var pcode = din.readLong();
|
|
107
|
+
var oid = din.readInt();
|
|
108
|
+
var transfer_key = din.readInt();
|
|
109
|
+
var _data = din.readIntBytes(8 * 1024 * 1024);
|
|
110
|
+
if (conf.cypher_level > 0) {
|
|
111
|
+
_data = secu.cypher.decrypt(_data);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
var oname = new DataInputX(_data).readText();
|
|
115
|
+
|
|
116
|
+
var localAddr = (this.localAddress || IPUtil.getIp());
|
|
117
|
+
secu.setAgentOnameOid(oname, localAddr);
|
|
118
|
+
self.PREPARE_AGENT = false;
|
|
119
|
+
if(conf.cipher_net_enabled) {
|
|
120
|
+
try {
|
|
121
|
+
self.out.write( self.keyReset() );
|
|
122
|
+
} catch(e) {
|
|
123
|
+
Logger.printError("WHATAP-125", "KeyReset " + host + ":" + port, e, true);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if(firstReceived) {
|
|
130
|
+
self.read(data);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
var d = self.readKeyReset(data);
|
|
134
|
+
if( d && d.length != 0 ) {
|
|
135
|
+
conf.updateNetCypherKey(d);
|
|
136
|
+
firstReceived = true;
|
|
137
|
+
if(cb) cb();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
this.out.on('end', function(){
|
|
142
|
+
Logger.print("WHATAP-183", "Disconnected from server, please check your network or Whatap Server!!!!!", false);
|
|
143
|
+
self.close();
|
|
144
|
+
setTimeout(function() {
|
|
145
|
+
cb(new Error('Socket end Event!!!!!'));
|
|
146
|
+
return;
|
|
147
|
+
},500);
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
TcpSession.prototype.close = function(){
|
|
152
|
+
if(this.out){
|
|
153
|
+
this.out.destroy();
|
|
154
|
+
this.out = null;
|
|
155
|
+
this.reconnectCnt = 0;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
TcpSession.prototype.send = function(code , b ){
|
|
160
|
+
try{
|
|
161
|
+
if(this.out == null ){
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
var out = new DataOutputX();
|
|
165
|
+
out.writeByte(netsrc.AGENT_JAVA_EMBED);
|
|
166
|
+
out.writeByte(code);
|
|
167
|
+
out.writeLong(secu.PCODE);
|
|
168
|
+
out.writeInt(secu.OID);
|
|
169
|
+
out.writeInt(conf.TRANSFER_KEY);
|
|
170
|
+
out.writeIntBytes(b);
|
|
171
|
+
/*if(this.out === null ){
|
|
172
|
+
if(this._cb) {
|
|
173
|
+
setTimeout(function() {
|
|
174
|
+
this._cb(new Error('[Send Error] Socket is null !! TCP Session Reconnection......'));
|
|
175
|
+
return;
|
|
176
|
+
},100);
|
|
177
|
+
}
|
|
178
|
+
} */
|
|
179
|
+
this.out.write(out.toByteArray());
|
|
180
|
+
return true;
|
|
181
|
+
} catch(e) {
|
|
182
|
+
Logger.printError('WHATAP-126', 'TcpSession Error', e);
|
|
183
|
+
}
|
|
184
|
+
this.close();
|
|
185
|
+
return false;
|
|
186
|
+
};
|
|
187
|
+
TcpSession.prototype.readKeyReset = function( inPS /* DataInputX */){
|
|
188
|
+
try {
|
|
189
|
+
if( inPS.constructor === Buffer){
|
|
190
|
+
inPS = new DataInputX(inPS);
|
|
191
|
+
}
|
|
192
|
+
var source = inPS.readByte();
|
|
193
|
+
var code = inPS.readByte();
|
|
194
|
+
var pcode = inPS.readLong();
|
|
195
|
+
var oid = inPS.readInt();
|
|
196
|
+
var transfer_key = inPS.readInt();
|
|
197
|
+
var data = inPS.readIntBytes(1024);
|
|
198
|
+
|
|
199
|
+
if( pcode != secu.PCODE || oid != secu.OID){
|
|
200
|
+
return null;
|
|
201
|
+
} else {
|
|
202
|
+
return data;
|
|
203
|
+
}
|
|
204
|
+
} catch(e) {
|
|
205
|
+
Logger.printError("WHATAP-189", 'License key or invalid agent version error(fail key)', e, true);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
TcpSession.prototype.keyReset = function(){
|
|
210
|
+
var msg = new DataOutputX().writeText("hello").writeText(secu.ONAME).writeInt8(secu.IP).toByteArray();
|
|
211
|
+
if( conf.cypher_level > 0 ){
|
|
212
|
+
msg = secu.cypher.encrypt(msg);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
var out = new DataOutputX();
|
|
216
|
+
out.writeByte(netsrc.AGENT_JAVA_EMBED);
|
|
217
|
+
out.writeByte(netflag.KEY_RESET);
|
|
218
|
+
out.writeLong(Long.fromValue(secu.PCODE));
|
|
219
|
+
out.writeInt(secu.OID);
|
|
220
|
+
out.writeInt(0);
|
|
221
|
+
out.writeIntBytes(msg);
|
|
222
|
+
return out.toByteArray();
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
TcpSession.prototype.isOpen = function(){
|
|
226
|
+
return this.out != null;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
TcpSession.prototype.read = function (data) {
|
|
230
|
+
var din = new DataInputX(data);
|
|
231
|
+
var out = new TcpReturn();
|
|
232
|
+
var source = din.readByte();
|
|
233
|
+
|
|
234
|
+
out.code = din.readByte();
|
|
235
|
+
var pcode = din.readLong();
|
|
236
|
+
var oid = din.readInt();
|
|
237
|
+
out.transfer_key = din.readInt();
|
|
238
|
+
out.data = din.readIntBytes();
|
|
239
|
+
|
|
240
|
+
if(pcode != secu.PCODE || oid != secu.OID) {
|
|
241
|
+
Logger.printError('WHATAP-190', 'Received data is not mine..', new Error('Receive data error'));
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
this.receiver.read(out);
|
|
245
|
+
};
|
|
246
|
+
TcpSession.prototype.prepareAutoOnameByServer = function (localAddr) {
|
|
247
|
+
var self = this;
|
|
248
|
+
try {
|
|
249
|
+
var auto_oname_prefix = conf.auto_oname_prefix;
|
|
250
|
+
|
|
251
|
+
if(auto_oname_prefix.indexOf('{') >= 0) {
|
|
252
|
+
OidUtil.setIp(localAddr);
|
|
253
|
+
OidUtil.setPort(port);
|
|
254
|
+
OidUtil.setPid(process.pid);
|
|
255
|
+
OidUtil.setHostName(require('os').hostname());
|
|
256
|
+
auto_oname_prefix = OidUtil.mkOname(conf.auto_oname_prefix);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
var pout = new DataOutputX();
|
|
260
|
+
pout.writeText(auto_oname_prefix);
|
|
261
|
+
pout.writeInt(IPUtil.stringToInt(localAddr));
|
|
262
|
+
var msg = pout.toByteArray();
|
|
263
|
+
|
|
264
|
+
var trkey = 0;
|
|
265
|
+
if(conf.cypher_level > 0) {
|
|
266
|
+
msg = secu.cypher.encrypt(msg);
|
|
267
|
+
var b0 = 1;
|
|
268
|
+
var b1 = (conf.cypher_level / 8);
|
|
269
|
+
trkey = DataInputX.toInt([b0, b1, 0, 0], 0);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
var out = new DataOutputX();
|
|
273
|
+
out.writeByte(netsrc.AGENT_JAVA_EMBED);
|
|
274
|
+
out.writeByte(netflag.PREPARE_AGENT);
|
|
275
|
+
out.writeLong(secu.PCODE);
|
|
276
|
+
out.writeInt(0);
|
|
277
|
+
out.writeInt(trkey);
|
|
278
|
+
out.writeIntBytes(msg);
|
|
279
|
+
self.PREPARE_AGENT = true;
|
|
280
|
+
self.out.write(out.toByteArray());
|
|
281
|
+
} catch(e) {
|
|
282
|
+
Logger.printError("WHATAP-003", "Prepare oname error", e, false);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
|
|
286
286
|
module.exports = new TcpSession();
|