3 var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
7 var Slot = function () {
8 function Slot(table, seqnum, machineid, prevhmac, hmac) {
9 _classCallCheck(this, Slot);
11 this.SLOT_SIZE = 2048;
12 this.RESERVED_SPACE = 64;
14 if (typeof seqnum === "number") {
17 throw new Error("seqnum should be a number");
19 if (typeof machineid === "number") {
20 this.machineid = machineid;
22 throw new Error("machine should be a number");
25 this.seqnumlive = true;
26 if (prevhmac && prevhmac instanceof Uint8Array) {
27 this.prevhmac = prevhmac;
29 throw new Error("prevhmac input not valid");
31 if (hmac && hmac instanceof Uint8Array) {
34 throw new Error("Hmac input not valid");
37 this.freespace = this.SLOT_SIZE - getBaseSize(); //???????
43 value: function getHMAC() {
48 value: function getPrevHmac() {
53 value: function addEntry(entry) {
54 if (entry && entry instanceof Entry) {
56 this.entries.push(_.extend(obj, entry));
58 this.freespace -= entry.getSize();
62 key: "addShallowEntry",
63 value: function addShallowEntry(entry) {
64 if (entry && entry instanceof Entry) {
65 this.entries.push(entry);
67 this.freespace -= entry.getSize();
72 value: function hasSpace(entry) {
73 var newfreespace = this.freespace - entry.getSize();
74 return newfreespace > this.RESERVED_SPACE;
78 value: function getEntries() {
83 value: function encode(key) {
84 var array = new Uint8Array(this.SLOT_SIZE);
85 var bb = new ByteBuffer().wrap(array.join([separator = '']));
86 //leave space for slot HMAC
87 bb.skip(this.HMAC_SIZE);
88 bb.writeIString(this.prevhmac.join([separator = '']));
89 bb.writeInt64(this.seqnum);
90 bb.writeInt64(this.machineid);
91 bb.writeByte(this.entries.length);
92 this.entries.forEach(function (entry) {
93 return entry.encode(bb);
96 var mac = crypto.HmacMD5(array.slice(this.HMAC_SIZE).join([separator = '']), key);
97 var realmac = new Uint8Array(mac.length);
98 for (var i = 0; i < mac.length; i++) {
99 realmac[i] = mac.charCodeAt(i);
103 bb.wrap(realmac.join([separator = '']));
104 return new Uint8Array(bb.toArrayBuffer());
107 * Returns the empty size of a Slot. Includes 2 HMACs, the machine
108 * identifier, the sequence number, and the number of entries.
113 value: function getBaseSize() {
114 return 2 * this.HMAC_SIZE + 2 * 8 + 2 * 4; //Problematic area
117 key: "getLiveEntries",
118 value: function getLiveEntries(resize) {
119 var liveEntries = [];
120 this.entries.forEach(function (entry) {
121 if (entry.isLive === true) {
122 if (!resize || entry.getType() !== entry.TypeTableStatus) {
123 liveEntries.push(entry);
128 if (this.seqnumlive && !resize) {
129 liveEntries.push(new LastMessage(this, this.machineid, this.seqnumlive));
135 key: "getSequenceNumber",
136 value: function getSequenceNumber() {
141 value: function getMachineID() {
142 return this.machineid;
146 value: function setDead() {
147 this.seqnumlive = false;
148 this.decrementLiveCount();
151 key: "decrementLiveCount",
152 value: function decrementLiveCount() {
154 if (this.livecount === 0) this.table.decrementLiveCount();
158 value: function isLive() {
159 return this.livecount > 0;
163 value: function toString() {
164 return '<' + this.getSequenceNumber() + '>';
168 value: function decode(table, array, key) {
169 var cond1 = array && array instanceof Uint8Array;
171 var mac = crypto.HmacMD5(array.slice(this.HMAC_SIZE).join([separator = '']), key);
173 var realmac = new Uint8Array(mac.length);
174 for (var i = 0; i < mac.length; i++) {
175 realmac[i] = mac.charCodeAt(i);
178 var bb = new ByteBuffer().wrap(array.join([separator = ''])).flip();
179 var hmac = new Uint8Array(this.HMAC_SIZE);
180 var prevhmac = new Uint8Array(this.HMAC_SIZE);
181 for (var i = 0; i < this.HMAC_SIZE; i++) {
182 hmac[i] = bb.readByte();
184 for (var j = 0; j < this.HMAC_SIZE; j++) {
185 prevhmac[j] = bb.readByte();
188 for (var k = 0; k < this.HMAC_SIZE; k++) {
189 if ((hmac[k] !== realmac[k])) {
190 throw new Error("Server Error: Invalid HMAC! Potential Attack!");
193 var _seqnum = bb.readLong();
194 var _machineid = bb.readLong();
195 var numentries = bb.readInt8();
197 var _slot = new Slot(table, _seqnum, _machineid, prevhmac, hmac);
199 for (var l = 0; l < numentries; l++) {
200 _slot.addShallowEntry(Entry.decode(_slot, bb));