base.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type { Span } from '../../trace';
  2. import { compareAndWriteFile } from '../create-file';
  3. /**
  4. * The class is not about holding rule data, instead it determines how the
  5. * date is written to a file.
  6. */
  7. export abstract class BaseWriteStrategy {
  8. public abstract readonly name: string;
  9. /**
  10. * Sometimes a ruleset will create extra files (e.g. reject-url-regex w/ mitm.sgmodule),
  11. * and doesn't share the same filename and id. This property is used to overwrite the filename.
  12. */
  13. public overwriteFilename: string | null = null;
  14. public withFilename(filename: string) {
  15. this.overwriteFilename = filename;
  16. return this;
  17. }
  18. public abstract readonly type: 'domainset' | 'non_ip' | 'ip' | (string & {});
  19. abstract readonly fileExtension: 'conf' | 'txt' | 'json' | 'sgmodule'; /* | (string & {}) */
  20. constructor(public readonly outputDir: string) {}
  21. protected abstract result: string[] | null;
  22. abstract writeDomain(domain: string): void;
  23. abstract writeDomainSuffix(domain: string): void;
  24. abstract writeDomainKeywords(keyword: Set<string>): void;
  25. abstract writeDomainWildcard(wildcard: string): void;
  26. abstract writeUserAgents(userAgent: Set<string>): void;
  27. abstract writeProcessNames(processName: Set<string>): void;
  28. abstract writeProcessPaths(processPath: Set<string>): void;
  29. abstract writeUrlRegexes(urlRegex: Set<string>): void;
  30. abstract writeIpCidrs(ipCidr: string[], noResolve: boolean): void;
  31. abstract writeIpCidr6s(ipCidr6: string[], noResolve: boolean): void;
  32. abstract writeGeoip(geoip: Set<string>, noResolve: boolean): void;
  33. abstract writeIpAsns(asns: Set<string>, noResolve: boolean): void;
  34. abstract writeSourceIpCidrs(sourceIpCidr: string[]): void;
  35. abstract writeSourcePorts(port: Set<string>): void;
  36. abstract writeDestinationPorts(port: Set<string>): void;
  37. abstract writeProtocols(protocol: 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. public output(
  41. span: Span,
  42. title: string,
  43. description: string[] | readonly string[],
  44. date: Date,
  45. filePath: string
  46. ): void | Promise<void> {
  47. if (!this.result) {
  48. return;
  49. }
  50. return compareAndWriteFile(
  51. span,
  52. this.withPadding(
  53. title,
  54. description,
  55. date,
  56. this.result
  57. ),
  58. filePath
  59. );
  60. };
  61. public get content() {
  62. return this.result;
  63. }
  64. }