From f93d2a556aa1112c74fb3f3a1616b4de16dbf67d Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:10:38 +0800 Subject: [PATCH] feat(connections): allow editing IP rule ranges --- Changelog.md | 2 +- .../connection/connection-rule-dialog.tsx | 68 +++++++++++-------- src/components/connection/connection-rule.ts | 11 ++- src/locales/ar/connections.json | 7 +- src/locales/de/connections.json | 7 +- src/locales/en/connections.json | 7 +- src/locales/es/connections.json | 7 +- src/locales/fa/connections.json | 7 +- src/locales/id/connections.json | 7 +- src/locales/jp/connections.json | 7 +- src/locales/ko/connections.json | 7 +- src/locales/pt/connections.json | 7 +- src/locales/ru/connections.json | 7 +- src/locales/tr/connections.json | 7 +- src/locales/tt/connections.json | 7 +- src/locales/zh/connections.json | 7 +- src/locales/zhtw/connections.json | 7 +- src/types/generated/i18n-keys.ts | 1 + src/types/generated/i18n-resources.ts | 1 + 19 files changed, 109 insertions(+), 72 deletions(-) diff --git a/Changelog.md b/Changelog.md index db32f327b..9f8ca6065 100644 --- a/Changelog.md +++ b/Changelog.md @@ -51,7 +51,7 @@ ✨ 新增功能 - 新增连接页面按网络协议(TCP/UDP)筛选连接 -- 新增从连接详情设置域名、进程或源/目标 IP 规则,支持自动识别主域、匹配子域及修改已有规则 +- 新增从连接详情设置域名、进程或源/目标 IP 规则,支持识别主域、匹配子域、自定义 IP 网段及修改已有规则 diff --git a/src/components/connection/connection-rule-dialog.tsx b/src/components/connection/connection-rule-dialog.tsx index e6d8fb874..7839041e5 100644 --- a/src/components/connection/connection-rule-dialog.tsx +++ b/src/components/connection/connection-rule-dialog.tsx @@ -27,6 +27,7 @@ import { mutate } from '@/services/mutate' import { showNotice } from '@/services/notice-service' import { + getConnectionIPRule, getConnectionRuleTargets, getConnectionRules, getConnectionRootDomain, @@ -72,7 +73,9 @@ export function ConnectionRuleDialog({ ) const [policy, setPolicy] = useState(null) const [scope, setScope] = useState('DOMAIN') - const [domainValue, setDomainValue] = useState(targets.domain?.value ?? '') + const [matchingInput, setMatchingInput] = useState( + () => targets[getInitialKind(target)]?.value ?? '', + ) const [rules, setRules] = useState(null) const [loadAttempt, setLoadAttempt] = useState(0) const [loading, setLoading] = useState(true) @@ -81,25 +84,33 @@ export function ConnectionRuleDialog({ const selected = targets[kind] const matchingRoot = kind === 'domain' && scope === 'ROOT-DOMAIN' const editingDomain = kind === 'domain' && scope === 'DOMAIN-SUFFIX' + const editingIP = kind === 'source' || kind === 'destination' const matchingValue = matchingRoot ? (rootDomain ?? '') - : editingDomain - ? domainValue + : editingDomain || editingIP + ? matchingInput : (selected?.value ?? '') const invalidDomain = editingDomain && - !isConnectionRuleDomain(domainValue, targets.domain?.value ?? '') - const condition = selected - ? { - type: - kind === 'domain' && scope !== 'DOMAIN' - ? 'DOMAIN-SUFFIX' - : selected.type, - value: kind === 'domain' ? matchingValue.trim() : selected.value, - } + !isConnectionRuleDomain(matchingInput, targets.domain?.value ?? '') + const ipRule = editingIP + ? getConnectionIPRule(matchingInput, kind === 'source') : undefined + const invalidIP = editingIP && !ipRule + const invalidValue = invalidDomain || invalidIP + const condition = editingIP + ? ipRule + : selected + ? { + type: + kind === 'domain' && scope !== 'DOMAIN' + ? 'DOMAIN-SUFFIX' + : selected.type, + value: kind === 'domain' ? matchingValue.trim() : selected.value, + } + : undefined const existing = - rules && condition && !invalidDomain + rules && condition && !invalidValue ? findConnectionRule(rules, condition) : undefined const modifying = Boolean(existing) @@ -166,7 +177,7 @@ export function ConnectionRuleDialog({ if ( blocked || !condition || - invalidDomain || + invalidValue || !policy || !policies.includes(policy) ) @@ -244,7 +255,7 @@ export function ConnectionRuleDialog({ }) : undefined)) : undefined - setDomainValue(targets.domain?.value ?? '') + setMatchingInput(nextTarget?.value ?? '') setKind(next) setPolicy(rule?.policy ?? null) setScope( @@ -303,21 +314,22 @@ export function ConnectionRuleDialog({ : kindLabels[kind] } value={matchingValue} - disabled={editingDomain && blocked} + disabled={(editingDomain || editingIP) && blocked} onChange={(event) => { const value = event.target.value - setDomainValue(value) + setMatchingInput(value) + const nextCondition = editingIP + ? getConnectionIPRule(value, kind === 'source') + : isConnectionRuleDomain(value, targets.domain?.value ?? '') + ? { type: 'DOMAIN-SUFFIX', value: value.trim() } + : undefined const match = - rules && - isConnectionRuleDomain(value, targets.domain?.value ?? '') - ? findConnectionRule(rules, { - type: 'DOMAIN-SUFFIX', - value: value.trim(), - }) + rules && nextCondition + ? findConnectionRule(rules, nextCondition) : undefined if (match) setPolicy(match.policy) }} - error={invalidDomain} + error={invalidValue} helperText={ editingDomain || matchingRoot ? ( <> @@ -331,10 +343,12 @@ export function ConnectionRuleDialog({ )} + ) : invalidIP ? ( + t('connections.ruleDialog.invalidIp') ) : undefined } slotProps={{ - input: { readOnly: !editingDomain }, + input: { readOnly: !editingDomain && !editingIP }, htmlInput: { title: condition?.value, }, @@ -350,7 +364,7 @@ export function ConnectionRuleDialog({ onChange={(_, value) => { const nextScope = value as DomainScope setScope(nextScope) - setDomainValue(targets.domain?.value ?? '') + setMatchingInput(targets.domain?.value ?? '') const match = rules && targets.domain ? findConnectionRule(rules, { @@ -493,7 +507,7 @@ export function ConnectionRuleDialog({ disabled={ blocked || !condition || - invalidDomain || + invalidValue || !policy || !policies.includes(policy) } diff --git a/src/components/connection/connection-rule.ts b/src/components/connection/connection-rule.ts index 90954010a..22fe372da 100644 --- a/src/components/connection/connection-rule.ts +++ b/src/components/connection/connection-rule.ts @@ -1,16 +1,23 @@ import { dump, loadAll } from 'js-yaml' import isFQDN from 'validator/es/lib/isFQDN' import isIP from 'validator/es/lib/isIP' +import isIPRange from 'validator/es/lib/isIPRange' export type ConnectionRuleKind = 'domain' | 'process' | 'source' | 'destination' function getAddressRule(address: string | undefined, source: boolean) { const ip = address?.trim() if (!ip || !isIP(ip)) return undefined - const ipv4 = isIP(ip, 4) + return getConnectionIPRule(`${ip}/${isIP(ip, 4) ? 32 : 128}`, source) +} + +export function getConnectionIPRule(value: string, source: boolean) { + const cidr = value.trim() + if (cidr.includes('%') || !isIPRange(cidr)) return undefined + const ipv4 = isIP(cidr.split('/')[0], 4) return { type: source ? 'SRC-IP-CIDR' : ipv4 ? 'IP-CIDR' : 'IP-CIDR6', - value: `${ip}/${ipv4 ? 32 : 128}`, + value: cidr, } } diff --git a/src/locales/ar/connections.json b/src/locales/ar/connections.json index a56cc0c19..ad0bd1a91 100644 --- a/src/locales/ar/connections.json +++ b/src/locales/ar/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/de/connections.json b/src/locales/de/connections.json index 737f3dead..b7d71ac66 100644 --- a/src/locales/de/connections.json +++ b/src/locales/de/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/en/connections.json b/src/locales/en/connections.json index 2fd34172c..59b805648 100644 --- a/src/locales/en/connections.json +++ b/src/locales/en/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/es/connections.json b/src/locales/es/connections.json index 32c3fbb41..8513d7c9c 100644 --- a/src/locales/es/connections.json +++ b/src/locales/es/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/fa/connections.json b/src/locales/fa/connections.json index 2eb4305bf..631da6cd1 100644 --- a/src/locales/fa/connections.json +++ b/src/locales/fa/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/id/connections.json b/src/locales/id/connections.json index d2fe9aeba..0c2722dca 100644 --- a/src/locales/id/connections.json +++ b/src/locales/id/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/jp/connections.json b/src/locales/jp/connections.json index 328ffb82d..375fb277d 100644 --- a/src/locales/jp/connections.json +++ b/src/locales/jp/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/ko/connections.json b/src/locales/ko/connections.json index 222c6ef28..08d78156e 100644 --- a/src/locales/ko/connections.json +++ b/src/locales/ko/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/pt/connections.json b/src/locales/pt/connections.json index 7caeb03bb..47d10af19 100644 --- a/src/locales/pt/connections.json +++ b/src/locales/pt/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/ru/connections.json b/src/locales/ru/connections.json index 3f3527a95..e7e7ff3e3 100644 --- a/src/locales/ru/connections.json +++ b/src/locales/ru/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/tr/connections.json b/src/locales/tr/connections.json index e61c85bfb..5f9d2f372 100644 --- a/src/locales/tr/connections.json +++ b/src/locales/tr/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/tt/connections.json b/src/locales/tt/connections.json index bdf8f09ba..39a84800b 100644 --- a/src/locales/tt/connections.json +++ b/src/locales/tt/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/locales/zh/connections.json b/src/locales/zh/connections.json index 491dbb8c8..c81584a76 100644 --- a/src/locales/zh/connections.json +++ b/src/locales/zh/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "保存并应用", "sourceIp": "源 IP", "destinationIp": "目标 IP", - "sourceHint": "匹配来自此 IP 的所有请求,可能包含其他应用,不限定端口。", - "destinationHint": "匹配访问此目标 IP 的请求,覆盖所有域名,不限定端口。", + "sourceHint": "匹配来自此 IP 或网段的所有请求,可能包含其他应用,不限定端口。", + "destinationHint": "匹配访问此目标 IP 或网段的请求,覆盖所有域名,不限定端口。", "fullPath": "完整路径", "connectionHost": "当前连接域名:{{host}}", "invalidDomain": "请输入当前连接域名或其父域名,父域名至少包含两级。", - "matchingDomain": "匹配域名" + "matchingDomain": "匹配域名", + "invalidIp": "请输入有效的 IP/前缀长度,IPv4 为 0–32,IPv6 为 0–128。" } } diff --git a/src/locales/zhtw/connections.json b/src/locales/zhtw/connections.json index 91939b9c3..fd3a67849 100644 --- a/src/locales/zhtw/connections.json +++ b/src/locales/zhtw/connections.json @@ -59,11 +59,12 @@ "saveAndApply": "Save and apply", "sourceIp": "Source IP", "destinationIp": "Destination IP", - "sourceHint": "Matches all requests from this IP, including requests from other applications. No port filtering.", - "destinationHint": "Matches this destination IP across all domains. No port filtering.", + "sourceHint": "Matches all requests from this IP or subnet, including requests from other applications. No port filtering.", + "destinationHint": "Matches this destination IP or subnet across all domains. No port filtering.", "fullPath": "Full path", "connectionHost": "Current connection domain: {{host}}", "invalidDomain": "Enter the current domain or a parent domain with at least two labels.", - "matchingDomain": "Matching domain" + "matchingDomain": "Matching domain", + "invalidIp": "Enter a valid IP/prefix length: 0–32 for IPv4 or 0–128 for IPv6." } } diff --git a/src/types/generated/i18n-keys.ts b/src/types/generated/i18n-keys.ts index c3825fc40..cc805eef0 100644 --- a/src/types/generated/i18n-keys.ts +++ b/src/types/generated/i18n-keys.ts @@ -55,6 +55,7 @@ export const translationKeys = [ 'connections.ruleDialog.connectionHost', 'connections.ruleDialog.invalidDomain', 'connections.ruleDialog.matchingDomain', + 'connections.ruleDialog.invalidIp', 'home.page.tooltips.lightweightMode', 'home.page.tooltips.manual', 'home.page.tooltips.settings', diff --git a/src/types/generated/i18n-resources.ts b/src/types/generated/i18n-resources.ts index 2506e6183..6ab576472 100644 --- a/src/types/generated/i18n-resources.ts +++ b/src/types/generated/i18n-resources.ts @@ -51,6 +51,7 @@ export interface TranslationResources { exactDomain: string fullPath: string invalidDomain: string + invalidIp: string matchBy: string matchingDomain: string modifyTitle: string