UNPKG

@arbytez/cod4-rcon-commands

Version:

A simple wrapper library to the rcon commands you can send to a cod4 server. Response data for commands '_status_', '_rconStatus_' and '_info_' is parsed into custom type object, so it is possible to easily manage the server status/info.

3 lines (2 loc) 9.54 kB
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var n=e(require("dgram")),r=e(require("iconv-lite"));function t(){return(t=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e}).apply(this,arguments)}var o=function(e,t,o,i,u){return void 0===o&&(o=""),void 0===u&&(u=1e3),new Promise((function(s,a){try{var c="",m=n.createSocket("udp4");m.send(function(e,n){void 0===n&&(n="");var t=r.encode(n?"rcon "+n+" "+e:""+e,"ascii"),o=new Uint8Array(t.length+5);o[0]=r.encode("255","ascii"),o[1]=r.encode("255","ascii"),o[2]=r.encode("255","ascii"),o[3]=r.encode("255","ascii"),o[4]=r.encode("02","ascii");for(var i=4,u=0;u<t.length;u++)o[i++]=t[u];return o[o.length-1]=r.encode("00","ascii"),o}(i,o),t,e),m.on("message",(function(e,n){var r=Buffer.from(e).toString().replace("����print","").replace("����infoResponse","").replace("����statusResponse","").replace("\n","");c+=r})),setTimeout((function(){m.close(),s(c)}),u)}catch(e){a(e.message)}}))},i=function(e,n,r,t,i){void 0===i&&(i=1e3);try{return Promise.resolve(o(e,n,r,t,i))}catch(e){return Promise.reject(e)}},u=function(e,n,r,t){void 0===t&&(t=1e3);try{return Promise.resolve(o(e,n,"",r,t))}catch(e){return Promise.reject(e)}},s=function(e,n,r,t){void 0===t&&(t=1e3);try{return Promise.resolve(i(e,n,r,"status",t)).then((function(e){var n=e.split("\n"),r=[];return n.forEach((function(e,n){var t=e.match(/^\s*(\d+)\s+(-?\d+)\s+(\d+)\s+(\d+)\s+([a-fA-F0-9]{16,32}|\d+) (.+?)\s+(\d+) (\d+\.\d+\.\d+\.\d+):(\-?\d+)\s+(\-?\d+)\s+(\d+)$/);if(t){var o={num:parseInt(t[1]),score:parseInt(t[2]),ping:parseInt(t[3]),id:t[4].trim(),steamId:t[5].trim(),name:t[6].trim(),lastmsg:parseInt(t[7]),ip:t[8].trim(),port:parseInt(t[9]),qport:parseInt(t[10]),rate:parseInt(t[11])};o.name&&o.name.endsWith("^7")&&(o.name=o.name.slice(0,o.name.length-2)),r.push(o)}})),r=r.sort((function(e,n){return(n.score||0)-(e.score||0)}))}))}catch(e){return Promise.reject(e)}},a=function(e,n,r){void 0===r&&(r=500);try{return Promise.resolve(u(e,n,"getstatus",r)).then((function(r){for(var t,o=r.split("\n"),i=o[0].split("\\"),u={},s=[],a=2;a<i.length;a+=2)u[i[a-1]]=i[a];for(var c=1;c<o.length;c++){var m=o[c].match(/^(-?\d+)\s+(-?\d+)\s+"(.*)"$/);m&&s.push({score:Number(m[1]),ping:m[2],name:m[3].trim()})}s=s.sort((function(e,n){return(n.score||0)-(e.score||0)}));var d=Date.parse(u.g_mapStartTime);return isNaN(d)||(u.g_mapStartTime=new Date(d)),"[object Date]"===Object.prototype.toString.call(u.g_mapStartTime)&&(t=u.g_mapStartTime),{online:Boolean(u.sv_hostname),ip:String(e),port:String(n),sv_hostname:u.sv_hostname||"",g_gametype:u.g_gametype||"",sv_maxclients:Number(u.sv_maxclients||0),uptime:u.uptime||"",g_mapStartTime:t||new Date,sv_privateClients:Number(u.sv_privateClients||0),fs_game:u._Mod||u.fs_game||"",version:u.version||"",mapname:u.mapname||"",onlinePlayers:s||[],branch:u.branch||"",build:u.build||"",g_compassShowEnemies:Number(u.g_compassShowEnemies||NaN),gamename:u.gamename||"",protocol:Number(u.protocol||NaN),revision:u.revision||"",shortversion:u.shortversion||"",sv_disableClientConsole:Number(u.sv_disableClientConsole||NaN),sv_floodprotect:Number(u.sv_floodprotect||NaN),sv_maxPing:Number(u.sv_maxPing||NaN),sv_maxRate:Number(u.sv_maxRate||NaN),sv_minPing:Number(u.sv_minPing||NaN),sv_pure:Number(u.sv_pure||NaN),sv_voice:Number(u.sv_voice||NaN),type:Number(u.type||NaN)}}))}catch(e){return Promise.reject(e)}};exports.createRconCommands=function(e,n,r){return void 0===r&&(r=""),{execRconCmd:function(t,o){return void 0===o&&(o=500),i(e,n,r,t,o)},rconStatus:function(o){return void 0===o&&(o=500),function(e,n,r,o){void 0===o&&(o=500);try{return Promise.resolve(Promise.all([a(e,n,200),s(e,n,r,o)])).then((function(e){return t({},e[0],{onlinePlayers:e[1]})}))}catch(e){return Promise.reject(e)}}(e,n,r,o)},info:function(r){return void 0===r&&(r=200),function(e,n,r){void 0===r&&(r=200);try{return Promise.resolve(u(e,n,"getinfo",r)).then((function(e){for(var n=e.split("\\"),r={},t=2;t<n.length;t+=2)r[n[t-1]]=n[t];return{build:r.build||"",clients:Number(r.clients||0),ff:Number(r.ff||NaN),g_humanplayers:Number(r.g_humanplayers||0),game:r.game||"",gametype:r.gametype||"",hc:Number(r.hc||NaN),hostname:r.hostname||"",hw:Number(r.hw||NaN),ki:Number(r.ki||NaN),mapname:r.mapname||"",mod:Number(r.mod||NaN),od:Number(r.od||NaN),protocol:Number(r.protocol||NaN),pswrd:r.pswrd||"",pure:Number(r.pure||NaN),shortversion:r.shortversion||"",sv_maxclients:Number(r.sv_maxclients||0),type:Number(r.type||NaN),voice:Number(r.voice||NaN)}}))}catch(e){return Promise.reject(e)}}(e,n,r)},status:function(r){return void 0===r&&(r=500),a(e,n,r)},cmdlist:function(t){return void 0===t&&(t=100),i(e,n,r,"cmdlist",t)},cvarlist:function(t){return void 0===t&&(t=1e3),i(e,n,r,"cvarlist",t)},fast_restart:function(t){return void 0===t&&(t=100),i(e,n,r,"fast_restart",t)},tell:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"tell "+t+" "+o,u)},say:function(t,o){return void 0===o&&(o=100),i(e,n,r,"say "+t,o)},set:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"set "+t+" "+o,u)},exec:function(t,o){return void 0===o&&(o=500),i(e,n,r,"exec "+t,o)},getss:function(t,o,u){return void 0===o&&(o=""),void 0===u&&(u=200),i(e,n,r,o?"getss "+t+" "+o:"getss "+t,u)},which:function(t,o){return void 0===o&&(o=100),i(e,n,r,"which "+t,o)},path:function(t){return void 0===t&&(t=100),i(e,n,r,"path",t)},map_rotate:function(t){return void 0===t&&(t=100),i(e,n,r,"map_rotate",t)},kick:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"kick "+t+" "+o,u)},dumpbanlist:function(t){return void 0===t&&(t=1e3),i(e,n,r,"dumpbanlist",t)},loadPlugin:function(t,o){return void 0===o&&(o=100),i(e,n,r,"loadPlugin "+t,o)},unloadPlugin:function(t,o){return void 0===o&&(o=100),i(e,n,r,"unloadPlugin "+t,o)},ministatus:function(t){return void 0===t&&(t=500),i(e,n,r,"ministatus",t)},tempban:function(t,o,u,s){return void 0===s&&(s=100),i(e,n,r,"tempban "+t+" "+o+" "+u,s)},dumpuser:function(t,o){return void 0===o&&(o=100),i(e,n,r,"dumpuser "+t,o)},map:function(t,o){return void 0===o&&(o=100),i(e,n,r,"map "+t,o)},permban:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"permban "+t+" "+o,u)},record:function(t,o,u){return void 0===o&&(o=""),void 0===u&&(u=100),i(e,n,r,o?"record "+t+" "+o:"record "+t,u)},screentell:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"screentell "+t+" "+o,u)},screensay:function(t,o){return void 0===o&&(o=100),i(e,n,r,"screensay "+t,o)},map_restart:function(t){return void 0===t&&(t=100),i(e,n,r,"map_restart",t)},sets:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"sets "+t+" "+o,u)},unban:function(t,o){return void 0===o&&(o=100),i(e,n,r,"unban "+t,o)},getmodules:function(t,o,u){return void 0===o&&(o=""),void 0===u&&(u=100),i(e,n,r,o?"getmodules "+t+" "+o:"getmodules "+t,u)},systeminfo:function(t){return void 0===t&&(t=100),i(e,n,r,"systeminfo",t)},serverinfo:function(t){return void 0===t&&(t=100),i(e,n,r,"serverinfo",t)},undercover:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"undercover "+t+" "+o,u)},AdminChangeCommandPower:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"AdminChangeCommandPower "+t+" "+o,u)},addCommand:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"addCommand "+t+" "+o,u)},seta:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"seta "+t+" "+o,u)},XAssetUsage:function(t){return void 0===t&&(t=200),i(e,n,r,"XAssetUsage",t)},stoprecord:function(t,o){return void 0===o&&(o=100),i(e,n,r,"stoprecord "+t,o)},killserver:function(t){return void 0===t&&(t=100),i(e,n,r,"killserver",t)},ChangePassword:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"ChangePassword "+t+" "+o,u)},AdminChangePassword:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"AdminChangePassword "+t+" "+o,u)},AdminListAdmins:function(t){return void 0===t&&(t=200),i(e,n,r,"AdminListAdmins",t)},AdminAddAdmin:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"AdminAddAdmin "+t+" "+o,u)},AdminRemoveAdmin:function(t,o){return void 0===o&&(o=100),i(e,n,r,"AdminRemoveAdmin "+t,o)},gametype:function(t,o){return void 0===o&&(o=100),i(e,n,r,"gametype "+t,o)},writenvcfg:function(t){return void 0===t&&(t=100),i(e,n,r,"writenvcfg",t)},pluginInfo:function(t,o){return void 0===o&&(o=100),i(e,n,r,"pluginInfo "+t,o)},plugins:function(t){return void 0===t&&(t=100),i(e,n,r,"plugins",t)},writeconfig:function(t,o){return void 0===o&&(o=100),i(e,n,r,"writeconfig "+t,o)},quit:function(t){return void 0===t&&(t=100),i(e,n,r,"quit",t)},net_restart:function(t){return void 0===t&&(t=100),i(e,n,r,"net_restart",t)},zonememinfo:function(t){return void 0===t&&(t=100),i(e,n,r,"zonememinfo",t)},setu:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"setu "+t+" "+o,u)},reset:function(t,o){return void 0===o&&(o=100),i(e,n,r,"reset "+t,o)},setcvartotime:function(t,o){return void 0===o&&(o=100),i(e,n,r,"setcvartotime "+t,o)},setfromcvar:function(t,o,u){return void 0===u&&(u=100),i(e,n,r,"setfromcvar "+t+" "+o,u)},toggle:function(t,o){return void 0===o&&(o=100),i(e,n,r,"toggle "+t,o)},echo:function(t,o){return void 0===o&&(o=100),i(e,n,r,"echo "+t,o)},vstr:function(t,o){return void 0===o&&(o=100),i(e,n,r,"vstr "+t,o)},meminfo:function(t){return void 0===t&&(t=200),i(e,n,r,"meminfo",t)},AdminListCommands:function(t){return void 0===t&&(t=200),i(e,n,r,"AdminListCommands",t)}}}; //# sourceMappingURL=cod4-rcon-commands.cjs.production.min.js.map