forked from soulman-is-good/node-ssp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.js
189 lines (176 loc) · 6.34 KB
/
commands.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
"use strict";
var aesjs = require("aes-js")
var Class = require('./class');
var Commands = Class.extend({
command_list: null,
exec_stack: [],
keys: null,
count: 0,
setKeys: function (keys) {
var self = this;
self.keys = keys
console.log(keys)
},
initialize: function (socket, type, ID, sequence) {
var self = this;
this.exec_stack = [];
this.client = socket;
this.command_list = require('./commands/' + type);
//map commands for better use
for (var cmd in this.command_list) {
(function (command) {
self[command] = function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(command);
return self.stack.apply(self, args);
};
}(cmd));
}
this.ID = ID || 0;
this.sequence = this.sequenceNumber = sequence || 0x80;
},
getSequence: function () {
var seq = this.ID | (this.sequence = (this.sequence === this.sequenceNumber ? 0x00 : this.sequenceNumber));
return seq
},
CRC16: function (command) {
var length = command.length,
seed = 0xFFFF,
poly = 0x8005,
crc = seed;
for (var i = 0; i < length; i++) {
crc ^= (command[i] << 8);
for (var j = 0; j < 8; j++) {
if (crc & 0x8000) {
crc = ((crc << 1) & 0xffff) ^ poly;
} else {
crc <<= 1;
}
}
}
return [(crc & 0xFF), ((crc >> 8) & 0xFF)];
},
stack: function (commandName) {
var self = this;
var command,
commandLine,
args = Array.prototype.slice.call(arguments, 1);
if (!this.command_list.hasOwnProperty(commandName)) {
throw new Error("Unknown command '" + commandName + "'");
}
if (commandName instanceof Array) {
console.log("commandName", commandName)
for (var i in commandName) {
this.stack(commandName[i]);
}
} else {
commandName = commandName.toLowerCase();
if ("function" === typeof this.command_list[commandName]) {
command = this.command_list[commandName].call(this);
} else {
command = this.command_list[commandName];
}
var STX = 0x7F
var LENGTH = args.length + 1
var SEQ_SLAVE_ID = this.getSequence()
var DATA = [command].concat(args)
commandLine = [SEQ_SLAVE_ID, LENGTH].concat(DATA);
var crc = this.CRC16(commandLine);
commandLine = [STX].concat(commandLine, crc);
var hex = commandLine.map(function (item) {
return item.toString(16).toUpperCase()
})
console.log("COM1 => ", hex, "| UNENCRYPTED |", arguments[0])
if (self.keys != null) {
var STEX = 0x7E
var eLENGTH = DATA.length;
self.count++
var eCOUNT = this.parseHexString(this.count.toString(16), 4)
var eDATA = DATA
var ePACKING = 0x00
var eCommandLine = [eLENGTH].concat(eCOUNT, eDATA, ePACKING)
var eCRC = this.CRC16(eCommandLine);
eCommandLine = eCommandLine.concat(eCRC)
var parse = function (a, count) {
for (var i = a.length; i < count; i++) {
a.push(0)
}
return a;
}
var key = parse(Array.prototype.slice.call(self.keys.fixedKey, 0).reverse(), 8).concat(this.parseHexString(self.keys.key, 8))
var aesCtr = new aesjs.ModeOfOperation.ctr(key);
var uint8Array = aesCtr.encrypt(eCommandLine);
eCommandLine = [STEX].concat([].slice.call(uint8Array))
DATA = eCommandLine
LENGTH = DATA.length
}
commandLine = [SEQ_SLAVE_ID, LENGTH].concat(DATA);
crc = this.CRC16(commandLine);
commandLine = [STX].concat(commandLine, crc);
if (self.keys != null) {
var hex = commandLine.map(function (item) {
return item.toString(16).toUpperCase()
})
console.log("COM1 =>", hex, "| ENCRYPTED |", arguments[0])
}
this.exec_stack.push(commandLine);
}
return this;
},
exec: function (command, cb) {
var typeCmd = typeof command;
if ("function" === typeCmd) {
console.log("command function", command)
cb = command;
command = null;
} else if ("string" === typeCmd) {
console.log("command String", command)
this.stack(command);
} else if (command instanceof Array) {
console.log("command array", command)
this.exec_stack.push(command);
}
if (this.exec_stack.length === 0) {
cb && cb();
} else {
var buf = new Buffer(this.exec_stack.shift()), self = this;
this.client.write(buf, function () {
self.client.drain(function () {
setTimeout(function () {
self.exec(cb);
}, 100);
});
});
}
},
byteToHexString: function (uint8arr) {
var hexStr = '';
for (var i = 0; i < uint8arr.length; i++) {
var hex = (uint8arr[i] & 0xff).toString(16);
hex = (hex.length === 1) ? '0' + hex : hex;
hexStr += hex;
}
return hexStr.toUpperCase();
},
parseHexString: function (str, count) {
var a = [];
for (var i = str.length; i > 0; i -= 2) {
a.push(parseInt(str.substr(i - 2, 2), 16));
}
for (var i = a.length; i < count; i++) {
a.push(0)
}
return a;
},
parseHexStringReverse: function (str, count) {
var a = [];
for (var i = str.length; i > 0; i -= 2) {
a.push(parseInt(str.substr(i - 2, 2), 16));
}
for (var i = a.length; i < count; i++) {
a.push(0)
}
return a;
}
});
module.exports = Commands;