ip.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import type { Span } from '../../trace';
  2. import { appendArrayFromSet } from '../misc';
  3. import type { SingboxSourceFormat } from '../singbox';
  4. import { RuleOutput } from './base';
  5. import { merge } from 'fast-cidr-tools';
  6. export class IPListOutput extends RuleOutput {
  7. protected type = 'ip' as const;
  8. constructor(span: Span, id: string, private readonly clashUseRule = true) {
  9. super(span, id);
  10. }
  11. private $merged: string[] | null = null;
  12. get merged() {
  13. if (!this.$merged) {
  14. this.$merged = merge(appendArrayFromSet([], [this.ipcidr, this.ipcidr6]));
  15. }
  16. return this.$merged;
  17. }
  18. private $surge: string[] | null = null;
  19. surge(): string[] {
  20. if (!this.$surge) {
  21. const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  22. appendArrayFromSet(results, this.ipcidr, i => `IP-CIDR,${i}`);
  23. appendArrayFromSet(results, this.ipcidr6, i => `IP-CIDR6,${i}`);
  24. this.$surge = results;
  25. }
  26. return this.$surge;
  27. }
  28. clash(): string[] {
  29. if (this.clashUseRule) {
  30. return this.surge();
  31. }
  32. return this.merged;
  33. }
  34. singbox(): string[] {
  35. const singbox: SingboxSourceFormat = {
  36. version: 2,
  37. rules: [{
  38. domain: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'],
  39. ip_cidr: this.merged
  40. }]
  41. };
  42. return RuleOutput.jsonToLines(singbox);
  43. }
  44. }