@trezub/ts-rcon
Version:
Rcon client implemented in Node.js with TypeScript
3 lines (2 loc) • 11.2 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("events"),e=require("net"),r=require("dgram"),n=require("buffer"),o=require("tiny-typed-emitter");function i(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function a(t,e){return(a=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}var c,u,s=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var o=Object.create((e&&e.prototype instanceof f?e:f).prototype),i=new S(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=_(a,r);if(c){if(c===l)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,i),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var l={};function f(){}function h(){}function p(){}var d={};d[o]=function(){return this};var v=Object.getPrototypeOf,g=v&&v(v(L([])));g&&g!==e&&r.call(g,o)&&(d=g);var y=p.prototype=f.prototype=Object.create(d);function m(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=s(t[o],t,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(f).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function _(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,_(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function E(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(E,this),this.reset(!0)}function L(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:b}}function b(){return{value:void 0,done:!0}}return h.prototype=y.constructor=p,p.constructor=h,h.displayName=c(p,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,c(t,a,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(u(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(y),c(y,a,"Generator"),y[o]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=L,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),k(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;k(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:L(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(u={exports:{}}),u.exports);!function(t){t[t.COMMAND=2]="COMMAND",t[t.AUTH=3]="AUTH",t[t.RESPONSE_VALUE=0]="RESPONSE_VALUE",t[t.RESPONSE_AUTH=2]="RESPONSE_AUTH"}(c||(c={})),exports.default=function(o){var u,l;function f(a,u,l,f){var h;return(h=o.call(this)||this).send=function(){var t,e=(t=s.mark((function t(e,r){var o,i,a,u,l;return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i=Math.round(2147483647*Math.random()),!h.tcp){t.next=15;break}return r=r||c.COMMAND,a=n.Buffer.byteLength(e),(o=n.Buffer.alloc(a+14)).writeInt32LE(a+10,0),o.writeInt32LE(i,4),o.writeInt32LE(r,8),o.write(e,12),o.writeInt16LE(0,a+12),h._sendSocket(o),u=new Promise((function(t,e){h._runningCommands.set(i,{reject:e,resolve:t})})),t.abrupt("return",u);case 15:if(!h.challenge||h._challengeToken){t.next=18;break}return h.emit("error",new Error("Not authenticated")),t.abrupt("return");case 18:return l="rcon ",h._challengeToken&&(l+=h._challengeToken+" "),h.password&&(l+=h.password+" "),(o=n.Buffer.alloc(4+n.Buffer.byteLength(l+=e+"\n"))).writeInt32LE(-1,0),o.write(l,4),h._sendSocket(o),t.abrupt("return",Promise.resolve(void 0));case 27:case"end":return t.stop()}}),t)})),function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function c(t){i(a,n,o,c,u,"next",t)}function u(t){i(a,n,o,c,u,"throw",t)}c(void 0)}))});return function(t,r){return e.apply(this,arguments)}}(),h._sendSocket=function(t){h._tcpSocket?h._tcpSocket.write(t.toString("binary"),"binary"):h._udpSocket&&h._udpSocket.send(t,0,t.length,h.port,h.host)},h.connect=function(){h.tcp?(h._tcpSocket=e.createConnection(h.port,h.host),h._tcpSocket.on("data",(function(t){h._tcpSocketOnData(t)})).on("connect",(function(){h.socketOnConnect()})).on("error",(function(t){h.emit("error",t)})).on("end",(function(){h.socketOnEnd()}))):(h._udpSocket=r.createSocket("udp4"),h._udpSocket.on("message",(function(t){h._udpSocketOnData(t)})).on("listening",(function(){h.socketOnConnect()})).on("error",(function(t){h.emit("error",t)})).on("close",(function(){h.socketOnEnd()})),h._udpSocket.bind(0))},h.disconnect=function(){h._tcpSocket&&h._tcpSocket.end(),h._udpSocket&&h._udpSocket.close()},h.setTimeout=function(t,e){h._tcpSocket&&h._tcpSocket.setTimeout(t,(function(){h._tcpSocket.end(),e&&e()}))},h._udpSocketOnData=function(t){if(4294967295===t.readUInt32LE(0)){var e=t.toString("utf-8",4),r=e.split(" ");3===r.length&&"challenge"===r[0]&&"rcon"===r[1]?(h._challengeToken=r[2].substr(0,r[2].length-1).trim(),h.hasAuthed=!0,h.emit("auth")):h.emit("response",e.substr(1,e.length-2))}else h.emit("error",new Error("Received malformed packet"))},h._tcpSocketOnData=function(t){for(null!=h.outstandingData&&(t=n.Buffer.concat([h.outstandingData,t],h.outstandingData.length+t.length),h.outstandingData=null);t.length;){var e=t.readInt32LE(0);if(!e)return;var r=t.readInt32LE(4),o=t.readInt32LE(8);if(!(e>=10&&t.length>=e+4)){h.outstandingData=t;break}if(h._runningCommands.has(r))if(h.hasAuthed||o!==c.RESPONSE_AUTH){if(o===c.RESPONSE_VALUE){var i,a=t.toString("utf8",12,12+e-10);"\n"===a.charAt(a.length-1)&&(a=a.substring(0,a.length-1)),null==(i=h._runningCommands.get(r))||i.resolve(a),h._runningCommands.delete(r)}}else h.hasAuthed=!0,h.emit("auth");else if(-1===r)h.emit("error",new Error("Authentication failed"));else{var u=t.toString("utf8",12,12+e-10);"\n"===u.charAt(u.length-1)&&(u=u.substring(0,u.length-1)),h.emit("server",u)}t=t.slice(12+e-8)}},h.socketOnConnect=function(){if(h.emit("connect"),h.tcp)h.send(h.password,c.AUTH);else if(h.challenge){var t="challenge rcon\n",e=n.Buffer.alloc(t.length+4);e.writeInt32LE(-1,0),e.write(t,4),h._sendSocket(e)}else{var r=n.Buffer.alloc(5);r.writeInt32LE(-1,0),r.writeUInt8(0,4),h._sendSocket(r),h.hasAuthed=!0,h.emit("auth")}},h.socketOnEnd=function(){h.emit("end"),h.hasAuthed=!1},f=f||{},h.host=a,h.port=u,h.password=l,h.hasAuthed=!1,h.outstandingData=null,h.tcp=!f.tcp||f.tcp,h.challenge=!f.challenge||f.challenge,h._challengeToken="",h._runningCommands=new Map,t.EventEmitter.call(function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(h)),h}return l=o,(u=f).prototype=Object.create(l.prototype),u.prototype.constructor=u,a(u,l),f}(o.TypedEmitter);
//# sourceMappingURL=ts-rcon.cjs.production.min.js.map