feat(happ): add routing editor, optional ad blocking, and LAN bypass preset (#6545)

* feat(happ): make ad blocking optional in routing presets

Add an independent AdBlock toggle for Iran, China, and global presets, applied only when generating routing rules. Update the China preset to Bypass-CN and cover preset behavior and localized controls.

* feat: add visual routing editor with JSON support and localization updates

- Implemented a new modal for editing routing profiles with basic and advanced tabs.
- Added functionality to load, parse, and generate routing profiles in JSON format.
- Enhanced user experience with validation and error handling for JSON input.
- Updated translations for Russian, Turkish, Ukrainian, Vietnamese, Chinese (Simplified and Traditional) to include new routing editor terms.
- Created helper functions for managing routing profiles and generating deep links.
- Added unit tests for routing editor functionalities and JSON handling.

* feat: update routing editor to preserve null lists in profiles and enhance validation messages
This commit is contained in:
NgaiYeanCoi
2026-09-27 04:57:01 +08:00
committed by GitHub
parent 94fa317e76
commit 5e15120cec
22 changed files with 1363 additions and 209 deletions
@@ -1,6 +1,6 @@
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, Input, Modal, Select, Space, Switch, Tabs, message } from 'antd';
import { Button, Input, Select, Space, Switch, Tabs, message } from 'antd';
import {
BranchesOutlined,
BuildOutlined,
@@ -13,7 +13,8 @@ import {
} from '@ant-design/icons';
import type { AllSetting } from '@/models/setting';
import { SettingListItem } from '@/components/ui';
import { buildHappPresetDeeplink, parseList, toBase64Utf8 } from './happPresets';
import { buildHappPresetDeeplink } from './happPresets';
import HappRoutingEditorModal from './HappRoutingEditorModal';
import { catTabLabel } from './catTabLabel';
interface HappSettingsContentProps {
@@ -32,38 +33,20 @@ export default function HappSettingsContent({
defaultActiveTab = 'routing',
}: HappSettingsContentProps) {
const { t } = useTranslation();
// Generator choices stay local until Apply updates the draft; page Save persists it.
const [selectedPreset, setSelectedPreset] = useState<string>('iran-bypass');
const [includeAdblock, setIncludeAdblock] = useState(false);
const [isModalOpen, setIsModalOpen] = useState(false);
const [directDomains, setDirectDomains] = useState('');
const [proxyDomains, setProxyDomains] = useState('');
const [blockDomains, setBlockDomains] = useState('');
const [directIPs, setDirectIPs] = useState('');
const [proxyIPs, setProxyIPs] = useState('');
const [blockIPs, setBlockIPs] = useState('');
const applyPreset = () => {
const payload = buildHappPresetDeeplink(selectedPreset);
const payload = buildHappPresetDeeplink(selectedPreset, includeAdblock);
if (payload) {
updateSetting({ subRoutingRules: payload });
message.success(t('pages.settings.subHappPresetApplied'));
}
};
const handleBuildDeeplink = () => {
const profile = {
Name: 'Custom Rules',
GlobalProxy: 'true',
DirectSites: parseList(directDomains),
DirectIp: parseList(directIPs),
ProxySites: parseList(proxyDomains),
ProxyIp: parseList(proxyIPs),
BlockSites: parseList(blockDomains),
BlockIp: parseList(blockIPs),
DomainStrategy: 'IPIfNonMatch',
};
const deeplink = 'happ://routing/onadd/' + toBase64Utf8(JSON.stringify(profile));
const handleBuildDeeplink = (deeplink: string) => {
updateSetting({ subRoutingRules: deeplink });
setIsModalOpen(false);
message.success(t('pages.settings.subHappDeeplinkGenerated'));
@@ -112,21 +95,31 @@ export default function HappSettingsContent({
title={t('pages.settings.subHappPresets')}
description={t('pages.settings.subHappPresetsDesc')}
>
<Space orientation="horizontal" style={{ width: '100%' }}>
<Space orientation="horizontal" wrap style={{ width: '100%' }}>
<Select
aria-label={t('pages.settings.subHappPresets')}
value={selectedPreset}
style={{ minWidth: 170 }}
onChange={setSelectedPreset}
options={[
{ value: 'iran-bypass', label: t('pages.settings.subHappPresetIran') },
{ value: 'china-direct', label: t('pages.settings.subHappPresetChina') },
{ value: 'adblock', label: t('pages.settings.subHappPresetAdblock') },
{ value: 'global', label: t('pages.settings.subHappPresetGlobal') },
{ value: 'lan-bypass', label: t('pages.settings.subHappPresetLocal') },
{ value: 'off', label: t('pages.settings.subHappPresetOff') },
]}
/>
<Space size="small">
<Switch
aria-label={t('pages.settings.subHappIncludeAdblock')}
checked={includeAdblock}
disabled={selectedPreset === 'off'}
onChange={setIncludeAdblock}
/>
<span>{t('pages.settings.subHappIncludeAdblock')}</span>
</Space>
<Button type="primary" onClick={applyPreset}>
{t('pages.settings.subHappPresets')}
{t('pages.settings.subHappApplyPreset')}
</Button>
</Space>
</SettingListItem>
@@ -589,83 +582,14 @@ export default function HappSettingsContent({
]}
/>
<Modal
title={t('pages.settings.subHappModalTitle')}
open={isModalOpen}
onCancel={() => setIsModalOpen(false)}
onOk={handleBuildDeeplink}
okText={t('pages.settings.subHappBuildDeeplink')}
width={650}
>
<Space orientation="vertical" style={{ width: '100%', marginTop: 12 }} size="middle">
<div>
<div style={{ fontWeight: 600, marginBottom: 4 }}>
{t('pages.settings.subHappDirectDomains')}
</div>
<Input.TextArea
rows={2}
value={directDomains}
placeholder="domain:ir, domain:cn, example.local"
onChange={(e) => setDirectDomains(e.target.value)}
/>
</div>
<div>
<div style={{ fontWeight: 600, marginBottom: 4 }}>
{t('pages.settings.subHappProxyDomains')}
</div>
<Input.TextArea
rows={2}
value={proxyDomains}
placeholder="geosite:google, youtube.com"
onChange={(e) => setProxyDomains(e.target.value)}
/>
</div>
<div>
<div style={{ fontWeight: 600, marginBottom: 4 }}>
{t('pages.settings.subHappBlockDomains')}
</div>
<Input.TextArea
rows={2}
value={blockDomains}
placeholder="geosite:category-ads-all, analytics.google.com"
onChange={(e) => setBlockDomains(e.target.value)}
/>
</div>
<div>
<div style={{ fontWeight: 600, marginBottom: 4 }}>
{t('pages.settings.subHappDirectIPs')}
</div>
<Input.TextArea
rows={2}
value={directIPs}
placeholder="geoip:ir, 192.168.0.0/16, 10.0.0.0/8"
onChange={(e) => setDirectIPs(e.target.value)}
/>
</div>
<div>
<div style={{ fontWeight: 600, marginBottom: 4 }}>
{t('pages.settings.subHappProxyIPs')}
</div>
<Input.TextArea
rows={2}
value={proxyIPs}
placeholder="1.1.1.1/32, 8.8.8.8/32"
onChange={(e) => setProxyIPs(e.target.value)}
/>
</div>
<div>
<div style={{ fontWeight: 600, marginBottom: 4 }}>
{t('pages.settings.subHappBlockIPs')}
</div>
<Input.TextArea
rows={2}
value={blockIPs}
placeholder="geoip:phishing, 0.0.0.0/8"
onChange={(e) => setBlockIPs(e.target.value)}
/>
</div>
</Space>
</Modal>
{/* Mount per opening so canceled edits are discarded and the latest parent draft is loaded. */}
{isModalOpen ? (
<HappRoutingEditorModal
input={allSetting.subRoutingRules}
onCancel={() => setIsModalOpen(false)}
onGenerate={handleBuildDeeplink}
/>
) : null}
</>
);
}