adguardhome.ts 3.3 KB

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