Files
sproutlink/web/src/ipRuleHelpers.ts

27 lines
862 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { IpRule } from './api';
const MAX_IPS = 200;
/** 从表单生成 IpRule未选模式或内容为空时返回 null */
export function buildIpRule(mode: '' | 'allow' | 'block', text: string): IpRule | null {
if (mode !== 'allow' && mode !== 'block') return null;
const ips = text
.split(/[\n,;]+/u)
.map(s => s.trim())
.filter(Boolean)
.slice(0, MAX_IPS);
if (ips.length === 0) return null;
return { mode, ips };
}
export function parseIpRuleText(json: string | null | undefined): { mode: '' | 'allow' | 'block'; text: string } {
if (!json) return { mode: '', text: '' };
try {
const r = JSON.parse(json) as IpRule;
if (r.mode !== 'allow' && r.mode !== 'block') return { mode: '', text: '' };
return { mode: r.mode, text: (r.ips ?? []).join('\n') };
} catch {
return { mode: '', text: '' };
}
}