adguardhome.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { escapeStringRegexp } from 'foxts/escape-string-regexp';
  2. import { BaseWriteStrategy } from './base';
  3. import { noop } from 'foxts/noop';
  4. import { notSupported } from '../misc';
  5. export class AdGuardHome extends BaseWriteStrategy {
  6. // readonly type = 'domainset';
  7. readonly fileExtension = 'txt';
  8. readonly type = '';
  9. protected result: string[] = [];
  10. // eslint-disable-next-line @typescript-eslint/class-methods-use-this -- abstract method
  11. withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[]): string[] {
  12. return [
  13. `! Title: ${title}`,
  14. '! Last modified: ' + date.toUTCString(),
  15. '! Expires: 6 hours',
  16. '! License: https://github.com/SukkaW/Surge/blob/master/LICENSE',
  17. '! Homepage: https://github.com/SukkaW/Surge',
  18. `! Description: ${description.join(' ')}`,
  19. '!',
  20. ...content,
  21. '! EOF'
  22. ];
  23. }
  24. writeDomain(domain: string): void {
  25. this.result.push(`|${domain}^`);
  26. }
  27. // const whitelistArray = sortDomains(Array.from(whitelist));
  28. // for (let i = 0, len = whitelistArray.length; i < len; i++) {
  29. // const domain = whitelistArray[i];
  30. // if (domain[0] === '.') {
  31. // results.push(`@@||${domain.slice(1)}^`);
  32. // } else {
  33. // results.push(`@@|${domain}^`);
  34. // }
  35. // }
  36. writeDomainSuffix(domain: string): void {
  37. this.result.push(`||${domain}^`);
  38. }
  39. writeDomainKeywords(keywords: Set<string>): void {
  40. for (const keyword of keywords) {
  41. // Use regex to match keyword
  42. this.result.push(`/${escapeStringRegexp(keyword)}/`);
  43. }
  44. }
  45. writeDomainWildcards(wildcards: Set<string>): void {
  46. for (const wildcard of wildcards) {
  47. const processed = wildcard.replaceAll('?', '*');
  48. if (processed.startsWith('*.')) {
  49. this.result.push(`||${processed.slice(2)}^`);
  50. } else {
  51. this.result.push(`|${processed}^`);
  52. }
  53. }
  54. }
  55. writeUserAgents = noop;
  56. writeProcessNames = noop;
  57. writeProcessPaths = noop;
  58. writeUrlRegexes = noop;
  59. writeIpCidrs(ipGroup: string[], noResolve: boolean): void {
  60. if (noResolve) {
  61. // When IP is provided to AdGuardHome, any domain resolve to those IP will be blocked
  62. // So we can't do noResolve
  63. return;
  64. }
  65. for (const ipcidr of ipGroup) {
  66. if (ipcidr.endsWith('/32')) {
  67. this.result.push(`||${ipcidr.slice(0, -3)}`);
  68. /* else if (ipcidr.endsWith('.0/24')) {
  69. results.push(`||${ipcidr.slice(0, -6)}.*`);
  70. } */
  71. } else {
  72. this.result.push(`||${ipcidr}^`);
  73. }
  74. }
  75. }
  76. writeIpCidr6s(ipGroup: string[], noResolve: boolean): void {
  77. if (noResolve) {
  78. // When IP is provided to AdGuardHome, any domain resolve to those IP will be blocked
  79. // So we can't do noResolve
  80. return;
  81. }
  82. for (const ipcidr of ipGroup) {
  83. if (ipcidr.endsWith('/128')) {
  84. this.result.push(`||${ipcidr.slice(0, -4)}`);
  85. } else {
  86. this.result.push(`||${ipcidr}`);
  87. }
  88. }
  89. };
  90. writeGeoip = notSupported('writeGeoip');
  91. writeIpAsns = notSupported('writeIpAsns');
  92. writeSourceIpCidrs = notSupported('writeSourceIpCidrs');
  93. writeSourcePorts = notSupported('writeSourcePorts');
  94. writeDestinationPorts = noop;
  95. writeOtherRules = noop;
  96. }