transformice.js
Version:
Node.js client for Transformice with full Typescript support.
123 lines (122 loc) • 4.66 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var net_1 = __importDefault(require("net"));
var events_1 = require("events");
var _1 = require(".");
var enums_1 = require("../enums");
/**
* Represents a client that connects to Transformice.
*
* @hidden
*/
var Connection = /** @class */ (function (_super) {
__extends(Connection, _super);
/**
* Constructor.
* @example
* ```js
* const conn = new Connection(client, 'connectionName');
* ```
*/
function Connection(identificationKeys, messageKeys) {
var _this = _super.call(this) || this;
_this.open = false;
_this.fingerprint = 0;
_this.buffer = Buffer.alloc(0);
_this.length = 0;
_this.identificationKeys = identificationKeys;
_this.messageKeys = messageKeys;
return _this;
}
/**
* Connects the socket.
*/
Connection.prototype.connect = function (host, port) {
var _this = this;
this.socket = net_1.default.createConnection({ port: port, host: host }, function () {
_this.open = true;
_this.emit("connect");
});
this.socket.on("data", function (data) {
_this.buffer = Buffer.concat([_this.buffer, data]);
while (_this.buffer.length > _this.length) {
if (_this.length === 0) {
for (var i = 0; i < 5; i++) {
var byte = _this.buffer.slice(0, 1)[0];
_this.buffer = _this.buffer.slice(1);
_this.length |= (byte & 127) << (i * 7);
if (!(byte & 0x80))
break;
}
}
if (_this.buffer.length >= _this.length) {
_this.emit("data", _this, new _1.ByteArray(_this.buffer.slice(0, _this.length)));
_this.buffer = _this.buffer.slice(_this.length);
_this.length = 0;
}
}
});
this.socket.once("close", function () {
_this.emit("close");
});
this.socket.on("error", function (err) {
_this.emit("error", err);
});
};
/**
* Sends a packet to the connection.
* @param {ByteArray} packet - The packet.
* @param {enums.cipherMethod} [method=enums.cipherMethod.none] - The algorithm method to cipher the packet with it.
*/
Connection.prototype.send = function (identifier, packet, method) {
if (method === void 0) { method = enums_1.cipherMethods.none; }
if (method === enums_1.cipherMethods.xor) {
packet = packet.xorCipher(this.messageKeys, this.fingerprint);
}
else if (method === enums_1.cipherMethods.xxtea) {
packet = packet.blockCipher(this.identificationKeys);
}
packet = new _1.ByteArray().writeUnsignedShort(identifier).writeBytes(packet);
var m = new _1.ByteArray();
var size = packet.length;
var sizeType = size >>> 7;
while (sizeType !== 0) {
m.writeUnsignedByte((size & 0x7f) | 0x80);
size = sizeType;
sizeType >>>= 7;
}
m.writeUnsignedByte(size & 0x7f);
m.writeByte(this.fingerprint);
m.writeBytes(packet);
this.socket.write(m.buffer);
this.fingerprint = (this.fingerprint + 1) % 100;
};
/**
* Close the connection.
*/
Connection.prototype.close = function () {
this.open = false;
if (!this.socket.destroyed) {
this.socket.removeAllListeners();
this.socket.destroy();
}
};
return Connection;
}(events_1.EventEmitter));
exports.default = Connection;
;