mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Refactor: adjust output
This commit is contained in:
53
Build/lib/rules/ip.ts
Normal file
53
Build/lib/rules/ip.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { Span } from '../../trace';
|
||||
import { appendArrayFromSet } from '../misc';
|
||||
import type { SingboxSourceFormat } from '../singbox';
|
||||
import { RuleOutput } from './base';
|
||||
|
||||
export class IPListOutput extends RuleOutput {
|
||||
protected type = 'ip' as const;
|
||||
|
||||
constructor(span: Span, id: string, private readonly clashUseRule = true) {
|
||||
super(span, id);
|
||||
}
|
||||
|
||||
private $merged: string[] | null = null;
|
||||
get merged() {
|
||||
if (!this.$merged) {
|
||||
this.$merged = appendArrayFromSet(appendArrayFromSet([], this.ipcidr), this.ipcidr6);
|
||||
}
|
||||
return this.$merged;
|
||||
}
|
||||
|
||||
private $surge: string[] | null = null;
|
||||
|
||||
surge(): string[] {
|
||||
if (!this.$surge) {
|
||||
const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
|
||||
appendArrayFromSet(results, this.ipcidr, i => `IP-CIDR,${i}`);
|
||||
appendArrayFromSet(results, this.ipcidr6, i => `IP-CIDR6,${i}`);
|
||||
|
||||
this.$surge = results;
|
||||
}
|
||||
return this.$surge;
|
||||
}
|
||||
|
||||
clash(): string[] {
|
||||
if (this.clashUseRule) {
|
||||
return this.surge();
|
||||
}
|
||||
|
||||
return this.merged;
|
||||
}
|
||||
|
||||
singbox(): string[] {
|
||||
const singbox: SingboxSourceFormat = {
|
||||
version: 2,
|
||||
rules: [{
|
||||
domain: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'],
|
||||
ip_cidr: this.merged
|
||||
}]
|
||||
};
|
||||
return RuleOutput.jsonToLines(singbox);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user