build-mitm-hostname.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { readFileByLine } from './lib/fetch-text-by-line';
  2. import pathFn from 'path';
  3. import table from 'table';
  4. import { fdir as Fdir } from 'fdir';
  5. import { green, yellow } from 'picocolors';
  6. import { processLineFromReadline } from './lib/process-line';
  7. import { getHostname } from 'tldts';
  8. const PRESET_MITM_HOSTNAMES = [
  9. // '*baidu.com',
  10. '*.ydstatic.com',
  11. // '*snssdk.com',
  12. // '*musical.com',
  13. // '*musical.ly',
  14. // '*snssdk.ly',
  15. 'api.zhihu.com',
  16. 'www.zhihu.com',
  17. 'api.chelaile.net.cn',
  18. 'atrace.chelaile.net.cn',
  19. '*.meituan.net',
  20. 'ctrl.playcvn.com',
  21. 'ctrl.playcvn.net',
  22. 'ctrl.zmzapi.com',
  23. 'ctrl.zmzapi.net',
  24. 'api.zhuishushenqi.com',
  25. 'b.zhuishushenqi.com',
  26. 'ggic.cmvideo.cn',
  27. 'ggic2.cmvideo.cn',
  28. 'mrobot.pcauto.com.cn',
  29. 'mrobot.pconline.com.cn',
  30. 'home.umetrip.com',
  31. 'discardrp.umetrip.com',
  32. 'startup.umetrip.com',
  33. 'dsp-x.jd.com',
  34. 'bdsp-x.jd.com'
  35. ];
  36. (async () => {
  37. const folderListPath = pathFn.resolve(__dirname, '../List/');
  38. const rulesets = await new Fdir()
  39. .withFullPaths()
  40. .crawl(folderListPath)
  41. .withPromise();
  42. const urlRegexPaths: Array<{ origin: string, processed: string }> = [];
  43. await Promise.all(rulesets.map(async file => {
  44. const content = await processLineFromReadline(readFileByLine(file));
  45. urlRegexPaths.push(
  46. ...content
  47. .filter(i => (
  48. i.startsWith('URL-REGEX')
  49. && !i.includes('http://')
  50. ))
  51. .map(i => i.split(',')[1])
  52. .map(i => ({
  53. origin: i,
  54. processed: i
  55. .replaceAll('^https?://', '')
  56. .replaceAll('^https://', '')
  57. .replaceAll('^http://', '')
  58. .split('/')[0]
  59. .replaceAll(String.raw`\.`, '.')
  60. .replaceAll('.+', '*')
  61. .replaceAll(String.raw`\d`, '*')
  62. .replaceAll('([a-z])', '*')
  63. .replaceAll('[a-z]', '*')
  64. .replaceAll('([0-9])', '*')
  65. .replaceAll('[0-9]', '*')
  66. .replaceAll(/{.+?}/g, '')
  67. .replaceAll(/\*+/g, '*')
  68. }))
  69. );
  70. }));
  71. const mitmDomains = new Set(PRESET_MITM_HOSTNAMES); // Special case for parsed failed
  72. const parsedFailures = new Set();
  73. const dedupedUrlRegexPaths = [...new Set(urlRegexPaths)];
  74. dedupedUrlRegexPaths.forEach(i => {
  75. const result = getHostnameSafe(i.processed);
  76. if (result) {
  77. mitmDomains.add(result);
  78. } else {
  79. parsedFailures.add(`${i.origin} ${i.processed} ${result}`);
  80. }
  81. });
  82. const mitmDomainsRegExpArray = Array.from(mitmDomains)
  83. .slice()
  84. .filter(i => {
  85. return i.length > 3
  86. && !i.includes('.mp4') // Special Case
  87. && i !== '(www.)' // Special Case
  88. && !(i !== '*.meituan.net' && i.endsWith('.meituan.net'))
  89. && !i.startsWith('.')
  90. && !i.endsWith('.')
  91. && !i.endsWith('*');
  92. })
  93. .map(i => {
  94. return new RegExp(
  95. escapeRegExp(i)
  96. .replaceAll('{www or not}', '(www.)?')
  97. .replaceAll(String.raw`\*`, '(.*)')
  98. );
  99. });
  100. const parsedDomainsData: Array<[string, string]> = [];
  101. dedupedUrlRegexPaths.forEach(i => {
  102. const result = getHostnameSafe(i.processed);
  103. if (result) {
  104. if (matchWithRegExpArray(result, mitmDomainsRegExpArray)) {
  105. parsedDomainsData.push([green(result), i.origin]);
  106. } else {
  107. parsedDomainsData.push([yellow(result), i.origin]);
  108. }
  109. }
  110. });
  111. console.log('Mitm Hostnames:');
  112. console.log(`hostname = %APPEND% ${Array.from(mitmDomains).join(', ')}`);
  113. console.log('--------------------');
  114. console.log('Parsed Sucessed:');
  115. console.log(table.table(parsedDomainsData, {
  116. border: table.getBorderCharacters('void'),
  117. columnDefault: {
  118. paddingLeft: 0,
  119. paddingRight: 3
  120. },
  121. drawHorizontalLine: () => false
  122. }));
  123. console.log('--------------------');
  124. console.log('Parsed Failed');
  125. console.log(Array.from(parsedFailures).join('\n'));
  126. })();
  127. /** Util function */
  128. function getHostnameSafe(input: string) {
  129. const res = getHostname(input);
  130. if (res && /[^\s\w*.-]/.test(res)) return null;
  131. return res;
  132. }
  133. function matchWithRegExpArray(input: string, regexps: RegExp[] = []) {
  134. for (const r of regexps) {
  135. if (r.test(input)) return true;
  136. }
  137. return false;
  138. }
  139. function escapeRegExp(string = '') {
  140. const reRegExpChar = /[$()*+.?[\\\]^{|}]/g;
  141. const reHasRegExpChar = new RegExp(reRegExpChar.source);
  142. return string && reHasRegExpChar.test(string)
  143. ? string.replaceAll(reRegExpChar, String.raw`\$&`)
  144. : string;
  145. }