UNPKG

node-insim

Version:

An InSim library for NodeJS with TypeScript support

142 lines (141 loc) 5.98 kB
"use strict"; 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) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IS_IPB = void 0; var decorators_1 = require("../decorators"); var errors_1 = require("../errors"); var lfspack_1 = require("../lfspack"); var ip_1 = require("../utils/ip"); var base_1 = require("./base"); var enums_1 = require("./enums"); /** * IP Bans * * You can set up to 120 IP addresses that are not allowed to join a host */ var IS_IPB = /** @class */ (function (_super) { __extends(IS_IPB, _super); function IS_IPB(data) { var _this = _super.call(this) || this; /** 8 + NumB * 4 */ _this.Size = 8; _this.Type = enums_1.PacketType.ISP_IPB; /** 0 unless this is a reply to a {@link TINY_IPB} request */ _this.ReqI = 0; /** Number of bans in this packet */ _this.NumB = 0; _this.Sp0 = 0; _this.Sp1 = 0; _this.Sp2 = 0; _this.Sp3 = 0; /** IP addresses, 0 to {@link MAX_BANS} ({@link NumB}) */ _this.BanIPs = []; _this.banIPsOffset = 8; _this.banIPSize = 4; _this.initialize(data); return _this; } IS_IPB.prototype.unpack = function (buffer) { _super.prototype.unpack.call(this, buffer); this.BanIPs = []; for (var i = 0; i < this.NumB; i++) { var start = this.banIPsOffset + this.banIPSize * i; var ipAddressBuffer = (0, lfspack_1.copyBuffer)(buffer.slice(start, start + this.banIPSize)); var ipAddress = ipAddressBuffer.join('.'); this.BanIPs.push(ipAddress); } return this; }; IS_IPB.prototype.pack = function () { if (this.BanIPs.length > IS_IPB.MAX_BANS) { throw new RangeError("IS_IPB - Too many BanIPs set (max is ".concat(IS_IPB.MAX_BANS)); } var invalidIPs = this.BanIPs.filter(function (ipAddress) { return !(0, ip_1.isValidIPv4)(ipAddress); }); if (invalidIPs.length > 0) { throw new RangeError("IS_IPB - The following IP addresses are not valid IPv4: ".concat(invalidIPs.join())); } this.NumB = this.BanIPs.length; this.Size = this.banIPsOffset + this.BanIPs.length * this.banIPSize; var dataBuffer = _super.prototype.pack.call(this); var maybeIPBuffer = this.BanIPs.map(function (ipAddress) { return (0, lfspack_1.pack)('L', [(0, ip_1.ipToUnsignedInteger)(ipAddress)]); }); if (maybeIPBuffer.some(function (buffer) { return buffer === null; })) { throw new errors_1.InSimError('IS_IPB - Failed to pack all BanIPs'); } var banIPsBuffer = maybeIPBuffer.reduce(function (acc, banIPs) { return new Uint8Array(__spreadArray(__spreadArray([], __read(acc), false), __read(banIPs), false)); }, new Uint8Array()); return new Uint8Array(__spreadArray(__spreadArray([], __read(dataBuffer), false), __read(banIPsBuffer), false)); }; IS_IPB.MAX_BANS = 120; __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "Size", void 0); __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "Type", void 0); __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "ReqI", void 0); __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "NumB", void 0); __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "Sp0", void 0); __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "Sp1", void 0); __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "Sp2", void 0); __decorate([ (0, decorators_1.byte)() ], IS_IPB.prototype, "Sp3", void 0); return IS_IPB; }(base_1.SendablePacket)); exports.IS_IPB = IS_IPB;