parse-filter.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const { isIP } = require('net');
  2. const { fetchWithRetry } = require('./fetch-retry');
  3. const rDomain = /^(((?!\-))(xn\-\-)?[a-z0-9\-_]{0,61}[a-z0-9]{1,1}\.)*(xn\-\-)?([a-z0-9\-]{1,61}|[a-z0-9\-]{1,30})\.[a-z]{2,}$/m
  4. const DEBUG_DOMAIN_TO_FIND = null; // example.com | null
  5. const warnOnceUrl = new Set();
  6. const warnOnce = (url, isWhite, ...message) => {
  7. const key = `${url}${isWhite ? 'white' : 'black'}`;
  8. if (warnOnceUrl.has(key)) {
  9. return;
  10. }
  11. warnOnceUrl.add(key);
  12. console.warn(url, isWhite ? '(white)' : '(black)', ...message);
  13. }
  14. /**
  15. * @param {string | URL} domainListsUrl
  16. */
  17. async function processDomainLists (domainListsUrl) {
  18. if (typeof domainListsUrl === 'string') {
  19. domainListsUrl = new URL(domainListsUrl);
  20. }
  21. /** @type Set<string> */
  22. const domainSets = new Set();
  23. /** @type string[] */
  24. const domains = (await (await fetchWithRetry(domainListsUrl)).text()).split('\n');
  25. domains.forEach(line => {
  26. if (
  27. line.startsWith('#')
  28. || line.startsWith('!')
  29. || line.startsWith(' ')
  30. || line === ''
  31. || line.startsWith('\r')
  32. || line.startsWith('\n')
  33. ) {
  34. return;
  35. }
  36. const domainToAdd = line.trim();
  37. if (DEBUG_DOMAIN_TO_FIND && domainToAdd.includes(DEBUG_DOMAIN_TO_FIND)) {
  38. warnOnce(domainListsUrl.toString(), false, DEBUG_DOMAIN_TO_FIND);
  39. }
  40. domainSets.add(domainToAdd);
  41. });
  42. return [...domainSets];
  43. }
  44. /**
  45. * @param {string | URL} hostsUrl
  46. */
  47. async function processHosts (hostsUrl, includeAllSubDomain = false) {
  48. if (typeof hostsUrl === 'string') {
  49. hostsUrl = new URL(hostsUrl);
  50. }
  51. /** @type Set<string> */
  52. const domainSets = new Set();
  53. /** @type string[] */
  54. const hosts = (await (await fetchWithRetry(hostsUrl)).text()).split('\n');
  55. hosts.forEach(line => {
  56. if (line.includes('#')) {
  57. return;
  58. }
  59. if (line.startsWith(' ') || line.startsWith('\r') || line.startsWith('\n') || line.trim() === '') {
  60. return;
  61. }
  62. const [, ...domains] = line.split(' ');
  63. const domain = domains.join(' ').trim();
  64. if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
  65. warnOnce(hostsUrl.toString(), false, DEBUG_DOMAIN_TO_FIND);
  66. }
  67. if (rDomain.test(domain)) {
  68. if (includeAllSubDomain) {
  69. domainSets.add(`.${domain}`);
  70. } else {
  71. domainSets.add(domain);
  72. }
  73. }
  74. });
  75. return [...domainSets];
  76. }
  77. /**
  78. * @param {string | URL} filterRulesUrl
  79. * @returns {Promise<{ white: Set<string>, black: Set<string> }>}
  80. */
  81. async function processFilterRules (filterRulesUrl) {
  82. if (typeof filterRulesUrl === 'string') {
  83. filterRulesUrl = new URL(filterRulesUrl);
  84. }
  85. /** @type Set<string> */
  86. const whitelistDomainSets = new Set();
  87. /** @type Set<string> */
  88. const blacklistDomainSets = new Set();
  89. /** @type string[] */
  90. const filterRules = (await (await fetchWithRetry(filterRulesUrl)).text()).split('\n').map(line => line.trim());
  91. filterRules.forEach(line => {
  92. const lineStartsWithDoubleVerticalBar = line.startsWith('||');
  93. if (
  94. line === ''
  95. || line.includes('#')
  96. || line.includes('!')
  97. || line.includes('*')
  98. || line.includes('/')
  99. || line.includes('$') && !lineStartsWithDoubleVerticalBar
  100. || line === ''
  101. || isIP(line) !== 0
  102. ) {
  103. return;
  104. }
  105. const lineEndsWithCaret = line.endsWith('^');
  106. const lineEndsWithCaretVerticalBar = line.endsWith('^|');
  107. if (lineStartsWithDoubleVerticalBar && line.endsWith('^$badfilter')) {
  108. const domain = line.replace('||', '').replace('^$badfilter', '').trim();
  109. if (rDomain.test(domain)) {
  110. if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
  111. warnOnce(filterRulesUrl.toString(), true, DEBUG_DOMAIN_TO_FIND);
  112. }
  113. whitelistDomainSets.add(domain);
  114. }
  115. } else if (line.startsWith('@@||')
  116. && (
  117. lineEndsWithCaret
  118. || lineEndsWithCaretVerticalBar
  119. || line.endsWith('^$badfilter')
  120. || line.endsWith('^$1p')
  121. )
  122. ) {
  123. const domain = line
  124. .replaceAll('@@||', '')
  125. .replaceAll('^$badfilter', '')
  126. .replaceAll('^$1p', '')
  127. .replaceAll('^|', '')
  128. .replaceAll('^', '')
  129. .trim();
  130. if (rDomain.test(domain)) {
  131. if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
  132. warnOnce(filterRulesUrl.toString(), true, DEBUG_DOMAIN_TO_FIND);
  133. }
  134. whitelistDomainSets.add(domain);
  135. }
  136. } else if (
  137. lineStartsWithDoubleVerticalBar
  138. && (
  139. lineEndsWithCaret
  140. || lineEndsWithCaretVerticalBar
  141. || line.endsWith('^$all')
  142. )
  143. ) {
  144. const domain = line
  145. .replaceAll('||', '')
  146. .replaceAll('^|', '')
  147. .replaceAll('^$all', '')
  148. .replaceAll('^', '')
  149. .trim();
  150. if (rDomain.test(domain)) {
  151. if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
  152. warnOnce(filterRulesUrl.toString(), false, DEBUG_DOMAIN_TO_FIND);
  153. }
  154. blacklistDomainSets.add(`.${domain}`);
  155. }
  156. } else if (line.startsWith('://')
  157. && (
  158. lineEndsWithCaret
  159. || lineEndsWithCaretVerticalBar
  160. )
  161. ) {
  162. const domain = `${line.replaceAll('://', '').replaceAll('^|', '').replaceAll('^', '')}`.trim();
  163. if (rDomain.test(domain)) {
  164. if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
  165. warnOnce(filterRulesUrl.toString(), false, DEBUG_DOMAIN_TO_FIND);
  166. }
  167. blacklistDomainSets.add(domain);
  168. }
  169. }
  170. });
  171. return {
  172. white: whitelistDomainSets,
  173. black: blacklistDomainSets
  174. };
  175. }
  176. module.exports.processDomainLists = processDomainLists;
  177. module.exports.processHosts = processHosts;
  178. module.exports.processFilterRules = processFilterRules;