base.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import type { Span } from '../../trace';
  2. import { compareAndWriteFile } from '../create-file';
  3. import { compareAndWriteFileInWorker } from '../create-file.worker';
  4. /**
  5. * The class is not about holding rule data, instead it determines how the
  6. * date is written to a file.
  7. */
  8. export abstract class BaseWriteStrategy {
  9. public abstract readonly name: string;
  10. /**
  11. * Sometimes a ruleset will create extra files (e.g. reject-url-regex w/ mitm.sgmodule),
  12. * and doesn't share the same filename and id. This property is used to overwrite the filename.
  13. */
  14. public overwriteFilename: string | null = null;
  15. public withFilename(filename: string) {
  16. this.overwriteFilename = filename;
  17. return this;
  18. }
  19. public abstract readonly type: 'domainset' | 'non_ip' | 'ip' | (string & {});
  20. abstract readonly fileExtension: 'conf' | 'txt' | 'json' | 'sgmodule'; /* | (string & {}) */
  21. constructor(public readonly outputDir: string) {}
  22. protected abstract result: string[] | null;
  23. abstract writeDomain(domain: string): void;
  24. abstract writeDomainSuffix(domain: string): void;
  25. abstract writeDomainKeywords(keyword: Set<string>): void;
  26. abstract writeDomainWildcards(wildcard: Set<string>): void;
  27. abstract writeUserAgents(userAgent: Set<string>): void;
  28. abstract writeProcessNames(processName: Set<string>): void;
  29. abstract writeProcessPaths(processPath: Set<string>): void;
  30. abstract writeUrlRegexes(urlRegex: Set<string>): void;
  31. abstract writeIpCidrs(ipCidr: string[], noResolve: boolean): void;
  32. abstract writeIpCidr6s(ipCidr6: string[], noResolve: boolean): void;
  33. abstract writeGeoip(geoip: Set<string>, noResolve: boolean): void;
  34. abstract writeIpAsns(asns: Set<string>, noResolve: boolean): void;
  35. abstract writeSourceIpCidrs(sourceIpCidr: string[]): void;
  36. abstract writeSourcePorts(port: Set<string>): void;
  37. abstract writeDestinationPorts(port: Set<string>): void;
  38. abstract writeOtherRules(rule: string[]): void;
  39. protected abstract withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[]): string[];
  40. static readonly domainWildCardToRegex = (domain: string) => {
  41. let result = '^';
  42. for (let i = 0, len = domain.length; i < len; i++) {
  43. switch (domain[i]) {
  44. case '.':
  45. result += String.raw`\.`;
  46. break;
  47. case '*':
  48. result += String.raw`[\w.-]*?`;
  49. break;
  50. case '?':
  51. result += String.raw`[\w.-]`;
  52. break;
  53. default:
  54. result += domain[i];
  55. }
  56. }
  57. result += '$';
  58. return result;
  59. };
  60. public output(
  61. span: Span,
  62. title: string,
  63. description: string[] | readonly string[],
  64. date: Date,
  65. filePath: string
  66. ): void | Promise<void> {
  67. if (!this.result) {
  68. return;
  69. }
  70. if (this.result.length > 1000) {
  71. return compareAndWriteFileInWorker(
  72. span,
  73. this.withPadding(
  74. title,
  75. description,
  76. date,
  77. this.result
  78. ),
  79. filePath
  80. );
  81. }
  82. return compareAndWriteFile(
  83. span,
  84. this.withPadding(
  85. title,
  86. description,
  87. date,
  88. this.result
  89. ),
  90. filePath
  91. );
  92. };
  93. public get content() {
  94. return this.result;
  95. }
  96. }