build-mitm-hostname.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { readFileByLine } from './lib/fetch-text-by-line';
  2. import pathFn from 'path';
  3. import table from 'table';
  4. import listDir from '@sukka/listdir';
  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 listDir(folderListPath);
  39. const urlRegexPaths: Array<{ origin: string, processed: string }> = [];
  40. await Promise.all(rulesets.map(async file => {
  41. const content = await processLineFromReadline(readFileByLine(pathFn.join(folderListPath, file)));
  42. urlRegexPaths.push(
  43. ...content
  44. .filter(i => (
  45. i.startsWith('URL-REGEX')
  46. && !i.includes('http://')
  47. ))
  48. .map(i => i.split(',')[1])
  49. .map(i => ({
  50. origin: i,
  51. processed: i
  52. .replaceAll('^https?://', '')
  53. .replaceAll('^https://', '')
  54. .replaceAll('^http://', '')
  55. .split('/')[0]
  56. .replaceAll('\\.', '.')
  57. .replaceAll('.+', '*')
  58. .replaceAll('\\d', '*')
  59. .replaceAll('([a-z])', '*')
  60. .replaceAll('[a-z]', '*')
  61. .replaceAll('([0-9])', '*')
  62. .replaceAll('[0-9]', '*')
  63. .replaceAll(/{.+?}/g, '')
  64. .replaceAll(/\*+/g, '*')
  65. }))
  66. );
  67. }));
  68. const mitmDomains = new Set(PRESET_MITM_HOSTNAMES); // Special case for parsed failed
  69. const parsedFailures = new Set();
  70. const dedupedUrlRegexPaths = [...new Set(urlRegexPaths)];
  71. dedupedUrlRegexPaths.forEach(i => {
  72. const result = getHostnameSafe(i.processed);
  73. if (result) {
  74. mitmDomains.add(result);
  75. } else {
  76. parsedFailures.add(`${i.origin} ${i.processed} ${result}`);
  77. }
  78. });
  79. const mitmDomainsRegExpArray = Array.from(mitmDomains)
  80. .slice()
  81. .filter(i => {
  82. return i.length > 3
  83. && !i.includes('.mp4') // Special Case
  84. && i !== '(www.)' // Special Case
  85. && !(i !== '*.meituan.net' && i.endsWith('.meituan.net'))
  86. && !i.startsWith('.')
  87. && !i.endsWith('.')
  88. && !i.endsWith('*');
  89. })
  90. .map(i => {
  91. return new RegExp(
  92. escapeRegExp(i)
  93. .replaceAll('{www or not}', '(www.)?')
  94. .replaceAll('\\*', '(.*)')
  95. );
  96. });
  97. const parsedDomainsData: Array<[string, string]> = [];
  98. dedupedUrlRegexPaths.forEach(i => {
  99. const result = getHostnameSafe(i.processed);
  100. if (result) {
  101. if (matchWithRegExpArray(result, mitmDomainsRegExpArray)) {
  102. parsedDomainsData.push([green(result), i.origin]);
  103. } else {
  104. parsedDomainsData.push([yellow(result), i.origin]);
  105. }
  106. }
  107. });
  108. console.log('Mitm Hostnames:');
  109. console.log(`hostname = %APPEND% ${Array.from(mitmDomains).join(', ')}`);
  110. console.log('--------------------');
  111. console.log('Parsed Sucessed:');
  112. console.log(table.table(parsedDomainsData, {
  113. border: table.getBorderCharacters('void'),
  114. columnDefault: {
  115. paddingLeft: 0,
  116. paddingRight: 3
  117. },
  118. drawHorizontalLine: () => false
  119. }));
  120. console.log('--------------------');
  121. console.log('Parsed Failed');
  122. console.log([...parsedFailures].join('\n'));
  123. })();
  124. /** Util function */
  125. function getHostnameSafe(input: string) {
  126. const res = getHostname(input);
  127. if (res && /[^\s\w*.-]/.test(res)) return null;
  128. return res;
  129. }
  130. function matchWithRegExpArray(input: string, regexps: RegExp[] = []) {
  131. for (const r of regexps) {
  132. if (r.test(input)) return true;
  133. }
  134. return false;
  135. }
  136. function escapeRegExp(string = '') {
  137. const reRegExpChar = /[$()*+.?[\\\]^{|}]/g;
  138. const reHasRegExpChar = new RegExp(reRegExpChar.source);
  139. return string && reHasRegExpChar.test(string)
  140. ? string.replaceAll(reRegExpChar, '\\$&')
  141. : string;
  142. }