base.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. 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. return compareAndWriteFile(
  71. span,
  72. this.withPadding(
  73. title,
  74. description,
  75. date,
  76. this.result
  77. ),
  78. filePath
  79. );
  80. };
  81. public get content() {
  82. return this.result;
  83. }
  84. }