Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

184 rindas
4.9 KiB

  1. const { routerConfig: { signatures, allowed, allowTargetId, secondaryAllowed, globalAllowed, secondaryAllowTargetId } } = require('./routerConfig');
  2. module.exports = {
  3. allowedCpn: function (msg) {
  4. const { cpn, method, data: { cpn: secondaryCpn, method: secondaryMethod, targetId } } = msg;
  5. const valid = allowed[cpn] && allowed[cpn].includes(method);
  6. if (!valid)
  7. return false;
  8. if (!secondaryCpn) {
  9. if (targetId !== undefined) {
  10. const canHaveTargetId = allowTargetId?.[cpn]?.includes(method);
  11. if (!canHaveTargetId)
  12. return false;
  13. }
  14. return true;
  15. }
  16. const secondaryValid = secondaryAllowed?.[secondaryCpn]?.includes(secondaryMethod);
  17. if (!secondaryValid)
  18. return false;
  19. if (targetId !== undefined) {
  20. const canHaveTargetId = secondaryAllowTargetId?.[secondaryCpn]?.includes(secondaryMethod);
  21. if (!canHaveTargetId)
  22. return false;
  23. }
  24. return true;
  25. },
  26. allowedGlobal: function (msg) {
  27. const result = globalAllowed[msg.module] && globalAllowed[msg.module].includes(msg.method);
  28. return result;
  29. },
  30. allowedGlobalCall: function (threadModule, method) {
  31. const result = globalAllowed[threadModule] && globalAllowed[threadModule].includes(method);
  32. return result;
  33. },
  34. keysCorrect: function (obj, keys) {
  35. const foundIncorrect = keys.some(({ key, dataType, optional, spec }) => {
  36. if (!Object.hasOwnProperty.call(obj, key)) {
  37. if (optional)
  38. return false;
  39. return true;
  40. }
  41. const value = obj[key];
  42. if (dataType === 'string' || dataType === 'boolean')
  43. return dataType !== typeof(value);
  44. else if (dataType === 'numberOrString')
  45. return (typeof(value) !== 'string' && !Number.isFinite(value));
  46. else if (dataType === 'integerOrString')
  47. return (typeof(value) !== 'string' && !Number.isInteger(value));
  48. else if (dataType === 'integer')
  49. return !Number.isInteger(value);
  50. else if (dataType === 'integerNullOrObject') {
  51. const isCorrect = (
  52. Number.isInteger(value) ||
  53. value === null ||
  54. (
  55. typeof(value) === 'object' &&
  56. this.keysCorrect(value, spec)
  57. )
  58. );
  59. return !isCorrect;
  60. } else if (dataType === 'integerNullObjectOrString') {
  61. const isCorrect = (
  62. Number.isInteger(value) ||
  63. typeof(dataType) === 'string' ||
  64. value === null ||
  65. (
  66. typeof(value) === 'object' &&
  67. this.keysCorrect(value, spec)
  68. )
  69. );
  70. return !isCorrect;
  71. } else if (dataType === 'arrayOfStrings')
  72. return (!Array.isArray(value) || value.some(v => typeof(v) !== 'string'));
  73. else if (dataType === 'arrayOfIntegers')
  74. return (!Array.isArray(value) || value.some(v => !Number.isInteger(v)));
  75. else if (dataType === 'arrayOfObjects') {
  76. if (!Array.isArray(value) || value.some(v => v === null || typeof(v) !== 'object'))
  77. return true;
  78. const foundIncorrectObject = value.some(v => !this.keysCorrect(v, spec));
  79. if (foundIncorrectObject)
  80. return true;
  81. return foundIncorrectObject;
  82. } else if (dataType === 'object') {
  83. if (typeof(value) !== 'object' || value === null)
  84. return true;
  85. if (!spec)
  86. return false;
  87. const foundIncorrectObject = !this.keysCorrect(value, spec);
  88. if (foundIncorrectObject)
  89. return true;
  90. return foundIncorrectObject;
  91. } else if (dataType === 'stringOrNull')
  92. return (typeof(value) !== 'string' && value !== null);
  93. return true;
  94. });
  95. if (foundIncorrect)
  96. return false;
  97. const foundInvalid = Object.keys(obj).some(o => !keys.some(k => k.key === o));
  98. return !foundInvalid;
  99. },
  100. signatureCorrect: function (msg, config) {
  101. if (config.callback !== 'deferred') {
  102. if (config.callback === true && !msg.callback)
  103. return false;
  104. else if (config.callback === false && !!msg.callback)
  105. return false;
  106. }
  107. const expectKeys = config.data;
  108. const keysCorrect = this.keysCorrect(msg.data, expectKeys);
  109. return keysCorrect;
  110. },
  111. isMsgValid: function (msg, source) {
  112. let signature;
  113. if (msg.module) {
  114. if (msg.threadModule !== undefined || msg.cpn !== undefined || msg.data.cpn !== undefined)
  115. return false;
  116. signature = signatures.global[msg.module]?.[msg.method];
  117. } else if (msg.threadModule) {
  118. if (msg.module !== undefined || msg.cpn !== undefined || msg.data.cpn !== undefined)
  119. return false;
  120. signature = signatures.threadGlobal[msg.threadModule]?.[msg.method];
  121. } else if (msg.cpn) {
  122. if (msg.module !== undefined || msg.threadModule !== undefined)
  123. return false;
  124. signature = signatures.cpnMethods[msg.cpn]?.[msg.method];
  125. }
  126. if (!signature)
  127. return false;
  128. const result = this.signatureCorrect(msg, signature);
  129. if (!result || msg.cpn !== 'player' || msg.method !== 'performAction') {
  130. if (result && signature.allowWhenIngame === false && source?.name !== undefined)
  131. return false;
  132. return result;
  133. }
  134. const signatureThreadMsg = signatures.threadCpnMethods[msg.data.cpn]?.[msg.data.method];
  135. if (!signatureThreadMsg)
  136. return false;
  137. const resultSub = this.signatureCorrect(msg.data, signatureThreadMsg);
  138. return resultSub;
  139. }
  140. };