ip.ts 1.3 KB

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