DomainLockTemplate.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @returns {string}
  3. */
  4. export function DomainLockTemplate (): string {
  5. return `
  6. const {domainLockFunctionName} = {callControllerFunctionName}(this, function () {
  7. {globalVariableTemplate}
  8. const regExp = new RegExp("[{domainsStringDiff}]", "g");
  9. const domains = "{domains}".replace(regExp, "").split(";");
  10. let document;
  11. let domain;
  12. let location;
  13. let hostname;
  14. const isName = function(name, length, cs) {
  15. if (name.length != length) {
  16. return false;
  17. }
  18. for (let i = 0; i < length; i++) {
  19. for (let j = 0; j < cs.length; j += 2) {
  20. if (i == cs[j] && name.charCodeAt(i) != cs[j+1]) {
  21. return false;
  22. }
  23. }
  24. }
  25. return true;
  26. };
  27. const isNameVariant1 = function(cs, name, length) {
  28. return isName(name, length, cs);
  29. };
  30. const isNameVariant2 = function(name, cs, length) {
  31. return isNameVariant1(cs, name, length);
  32. };
  33. const isNameVariant3 = function(length, name, cs) {
  34. return isNameVariant2(name, cs, length);
  35. };
  36. for (let d in that) {
  37. if (isName(d, 8, [7, 116, 5, 101, 3, 117, 0, 100])) {
  38. document = d;
  39. break;
  40. }
  41. }
  42. for (let d1 in that[document]) {
  43. if (isNameVariant3(6, d1, [5, 110, 0, 100])) {
  44. domain = d1;
  45. break;
  46. }
  47. }
  48. for (let d2 in that[document]) {
  49. if (isNameVariant2(d2, [7, 110, 0, 108], 8)) {
  50. location = d2;
  51. break;
  52. }
  53. }
  54. if (!("~" > domain)) {
  55. for (let d3 in that[document][location]) {
  56. if (isNameVariant1([7, 101, 0, 104], d3, 8)) {
  57. hostname = d3;
  58. break;
  59. }
  60. }
  61. }
  62. if (!document || !that[document]) {
  63. return;
  64. }
  65. const documentDomain = that[document][domain];
  66. const documentLocationHostName = !!that[document][location] && that[document][location][hostname];
  67. const currentDomain = documentDomain || documentLocationHostName;
  68. if (!currentDomain) {
  69. return;
  70. }
  71. let ok = false;
  72. for (let i = 0; i < domains.length; i++) {
  73. const domain = domains[i];
  74. const domainNormalized = domain[0] === String.fromCharCode(46)
  75. ? domain.slice(1)
  76. : domain;
  77. const position = currentDomain.length - domainNormalized.length;
  78. const lastIndex = currentDomain.indexOf(domainNormalized, position);
  79. const endsWith = lastIndex !== -1 && lastIndex === position;
  80. if (endsWith) {
  81. if (currentDomain.length == domain.length || domain.indexOf(".") === 0) {
  82. ok = true;
  83. }
  84. }
  85. }
  86. if (!ok) {
  87. const regExp2 = new RegExp("[{domainLockRedirectUrlDiff}]", "g");
  88. const domainLockRedirectUrl = "{hiddenDomainLockRedirectUrl}".replace(regExp2, "");
  89. that[document][location] = domainLockRedirectUrl;
  90. }
  91. });
  92. {domainLockFunctionName}();
  93. `;
  94. }