send(data, cmd, id) { let sendBuf; if (this.tcp) { cmd = cmd || PacketType.COMMAND; id = id || this.rconId; let length = Buffer.byteLength(data); sendBuf = new Buffer(length + 16); sendBuf.writeInt32LE(length + 12, 0); sendBuf.writeInt32LE(id, 4); sendBuf.writeInt32LE(cmd, 8); sendBuf.write(data, 12); sendBuf.writeInt32LE(0, length + 12); } else { if (this.challenge && !this._challengeToken) { this.emit('error', new Error('Not authenticated')); return; } var str = "rcon "; if (this._challengeToken) str += this._challengeToken + " "; if (this.password) str += this.password + " "; str += data + "\n"; sendBuf = new Buffer(4 + Buffer.byteLength(str)); sendBuf.writeInt32LE(-1, 0); sendBuf.write(str, 4) } this._sendSocket(sendBuf); }
Rcon.prototype.send = function(data, cmd, id) { cmd = cmd || PacketType.COMMAND; id = id || this.rconId; var sendBuf = new Buffer(data.length + 16); sendBuf.writeInt32LE(data.length + 12, 0); sendBuf.writeInt32LE(id, 4); sendBuf.writeInt32LE(cmd, 8); sendBuf.write(data, 12); sendBuf.writeInt32LE(0, data.length + 12); this.socket.write(sendBuf.toString('binary'), 'binary'); };
socketOnConnect() { this.emit('connect'); if (this.tcp) { this.send(this.password, PacketType.AUTH); } else if (this.challenge) { let str = "challenge rcon\n"; let sendBuf = new Buffer(str.length + 4); sendBuf.writeInt32LE(-1, 0); sendBuf.write(str, 4); this._sendSocket(sendBuf); } else { let sendBuf = new Buffer(5); sendBuf.writeInt32LE(-1, 0); sendBuf.writeUInt8(0, 4); this._sendSocket(sendBuf); this.hasAuthed = true; this.emit('auth'); } }
NetWriter.prototype.writeInt32LE = function (value) { var buffer = new Buffer(4); buffer.writeInt32LE(value, 0, true); this.content.push(buffer); this.size += 4; };
function hashForSignature (inIndex, prevOutScript, hashType) { typeforce(types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number), arguments); // https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29 if (inIndex >= this.ins.length) return ONE; var txTmp = this.clone(); // in case concatenating two scripts ends up with two codeseparators, // or an extra one at the end, this prevents all those possible incompatibilities. var hashScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) { return x !== opcodes.OP_CODESEPARATOR; })); var i; // blank out other inputs' signatures txTmp.ins.forEach(function (input) { input.script = EMPTY_SCRIPT; }); txTmp.ins[inIndex].script = hashScript; // blank out some of the inputs if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) { // wildcard payee txTmp.outs = []; // let the others update at will txTmp.ins.forEach(function (input, i) { if (i !== inIndex) { input.sequence = 0; } }); } else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) { var nOut = inIndex; // only lock-in the txOut payee at same index as txIn // https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60 if (nOut >= this.outs.length) return ONE; txTmp.outs = txTmp.outs.slice(0, nOut + 1); // blank all other outputs (clear scriptPubKey, value === -1) var stubOut = { script: EMPTY_SCRIPT, valueBuffer: VALUE_UINT64_MAX }; for (i = 0; i < nOut; i++) { txTmp.outs[i] = stubOut; } // let the others update at will txTmp.ins.forEach(function (input, i) { if (i !== inIndex) { input.sequence = 0; } }); } // blank out other inputs completely, not recommended for open transactions if (hashType & Transaction.SIGHASH_ANYONECANPAY) { txTmp.ins[0] = txTmp.ins[inIndex]; txTmp.ins = txTmp.ins.slice(0, 1); } // serialize and hash var buffer = new Buffer(txTmp.signedByteLength(inIndex) + 4); buffer.writeInt32LE(hashType, buffer.length - 4); txTmp.toBufferForSigning(inIndex).copy(buffer, 0); return bcrypto.hash256(buffer); }