ip.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type { Span } from '../../trace';
  2. import { appendArrayInPlace } from '../append-array-in-place';
  3. import { appendArrayFromSet } from '../misc';
  4. import type { SingboxSourceFormat } from '../singbox';
  5. import { RuleOutput } from './base';
  6. import { merge } from 'fast-cidr-tools';
  7. type Preprocessed = string[];
  8. export class IPListOutput extends RuleOutput<Preprocessed> {
  9. protected type = 'ip' as const;
  10. constructor(span: Span, id: string, private readonly clashUseRule = true) {
  11. super(span, id);
  12. }
  13. mitmSgmodule = undefined;
  14. protected preprocess() {
  15. const results: string[] = [];
  16. appendArrayInPlace(
  17. results,
  18. merge(
  19. appendArrayInPlace(Array.from(this.ipcidrNoResolve), Array.from(this.ipcidr)),
  20. true
  21. )
  22. );
  23. appendArrayFromSet(results, this.ipcidr6NoResolve);
  24. appendArrayFromSet(results, this.ipcidr6);
  25. return results;
  26. }
  27. private $surge: string[] | null = null;
  28. surge(): string[] {
  29. if (!this.$surge) {
  30. const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  31. appendArrayInPlace(
  32. results,
  33. merge(Array.from(this.ipcidrNoResolve)).map(i => `IP-CIDR,${i},no-resolve`, true)
  34. );
  35. appendArrayFromSet(results, this.ipcidr6NoResolve, i => `IP-CIDR6,${i},no-resolve`);
  36. appendArrayInPlace(
  37. results,
  38. merge(Array.from(this.ipcidr)).map(i => `IP-CIDR,${i}`, true)
  39. );
  40. appendArrayFromSet(results, this.ipcidr6, i => `IP-CIDR6,${i}`);
  41. this.$surge = results;
  42. }
  43. return this.$surge;
  44. }
  45. clash(): string[] {
  46. if (this.clashUseRule) {
  47. return this.surge();
  48. }
  49. return this.$preprocessed;
  50. }
  51. singbox(): string[] {
  52. const singbox: SingboxSourceFormat = {
  53. version: 2,
  54. rules: [{
  55. domain: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'],
  56. ip_cidr: this.$preprocessed
  57. }]
  58. };
  59. return RuleOutput.jsonToLines(singbox);
  60. }
  61. }