mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-15 23:42:27 +08:00
chore: trim excessive comments
This commit is contained in:
@@ -132,7 +132,6 @@ function parseList(str: string): string[] {
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
// 默认DNS配置
|
||||
const DEFAULT_DNS_CONFIG = {
|
||||
enable: true,
|
||||
listen: ':53',
|
||||
@@ -251,13 +250,12 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
hosts: '',
|
||||
})
|
||||
|
||||
// 用于YAML编辑模式, 初始化标记:阻止 updateValuesFromYaml 在配置加载前误执行
|
||||
// Prevent YAML-to-form sync before configuration loads.
|
||||
const [yamlContent, setYamlContent] = useReducer(
|
||||
(_: string, next: string) => next,
|
||||
'{ isInitValue: true }',
|
||||
)
|
||||
|
||||
// 从配置对象更新表单值
|
||||
const updateValuesFromConfig = useCallback((config: any) => {
|
||||
if (!config) return
|
||||
|
||||
@@ -388,7 +386,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
setYamlContent(yaml.dump(config, { forceQuotes: true }))
|
||||
}, [generateDnsConfig, values.hosts])
|
||||
|
||||
// 重置为默认值
|
||||
const resetToDefaults = useCallback(() => {
|
||||
setValues({
|
||||
enable: DEFAULT_DNS_CONFIG.enable,
|
||||
@@ -425,7 +422,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
updateYamlFromValues()
|
||||
}, [updateYamlFromValues])
|
||||
|
||||
// 从YAML更新表单值
|
||||
const updateValuesFromYaml = useCallback(() => {
|
||||
try {
|
||||
const parsedYaml = yaml.load(yamlContent) as any
|
||||
@@ -503,14 +499,11 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
[initDnsConfig],
|
||||
)
|
||||
|
||||
// 生成DNS配置对象
|
||||
// 处理保存操作
|
||||
const onSave = useLockFn(async () => {
|
||||
try {
|
||||
let config: Record<string, any>
|
||||
|
||||
if (visualization) {
|
||||
// 使用表单值生成配置
|
||||
config = {}
|
||||
|
||||
const dnsConfig = generateDnsConfig()
|
||||
@@ -523,7 +516,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
config.hosts = hosts
|
||||
}
|
||||
} else {
|
||||
// 使用YAML编辑器的值
|
||||
const parsedConfig = yaml.load(yamlContent)
|
||||
if (typeof parsedConfig !== 'object' || parsedConfig === null) {
|
||||
throw new Error(t('settings.modals.dns.errors.invalid'))
|
||||
@@ -531,10 +523,8 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
config = parsedConfig as Record<string, any>
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
await invoke('save_dns_config', { dnsConfig: config })
|
||||
|
||||
// 验证配置
|
||||
const validation = await invoke<ValidationOutcome>(
|
||||
'validate_dns_config',
|
||||
{},
|
||||
@@ -547,7 +537,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
: 'Configuration validation skipped'
|
||||
let cleanErrorMsg = errorMsg
|
||||
|
||||
// 提取关键错误信息
|
||||
if (errorMsg.includes('level=error')) {
|
||||
const errorLines = errorMsg
|
||||
.split('\n')
|
||||
@@ -575,7 +564,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
return
|
||||
}
|
||||
|
||||
// 如果DNS开关当前是打开的,则需要应用新的DNS配置
|
||||
if (clash?.dns?.enable) {
|
||||
await invoke('apply_dns_config', { apply: true })
|
||||
mutateClash()
|
||||
@@ -588,12 +576,10 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
}
|
||||
})
|
||||
|
||||
// YAML编辑器内容变更处理
|
||||
const handleYamlChange = (value?: string) => {
|
||||
setYamlContent(value || '')
|
||||
}
|
||||
|
||||
// 处理表单值变化
|
||||
const handleChange = (field: string) => (event: any) => {
|
||||
const value =
|
||||
event.target.type === 'checkbox'
|
||||
@@ -606,7 +592,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
[field]: value,
|
||||
}
|
||||
|
||||
// 当可视化编辑模式下的值变化时,自动更新YAML
|
||||
if (visualization) {
|
||||
setTimeout(() => {
|
||||
updateYamlFromValues()
|
||||
@@ -667,7 +652,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
onCancel={() => setOpen(false)}
|
||||
onOk={onSave}
|
||||
>
|
||||
{/* Warning message */}
|
||||
<Typography
|
||||
variant="body2"
|
||||
color="warning.main"
|
||||
@@ -1053,7 +1037,6 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
/>
|
||||
</Item>
|
||||
|
||||
{/* Hosts 配置部分 */}
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
sx={{ mt: 3, mb: 0, fontWeight: 'bold' }}
|
||||
|
||||
@@ -9,30 +9,23 @@ import { useClash } from '@/hooks/use-clash'
|
||||
import { restartCore } from '@/services/cmds'
|
||||
import { showNotice } from '@/services/notice-service'
|
||||
|
||||
// 定义开发环境的URL列表
|
||||
// 这些URL在开发模式下会被自动包含在允许的来源中
|
||||
// 在生产环境中,这些URL会被过滤掉
|
||||
// 这样可以确保在生产环境中不会意外暴露开发环境的URL
|
||||
// Development origins must never be persisted into production configuration.
|
||||
const DEV_URLS = [
|
||||
'tauri://localhost',
|
||||
'http://tauri.localhost',
|
||||
'http://localhost:3000',
|
||||
]
|
||||
|
||||
// 获取完整的源列表,包括开发URL
|
||||
const getFullOrigins = (origins: string[]) => {
|
||||
// 合并现有源和开发URL,并去重
|
||||
const allOrigins = [...origins, ...DEV_URLS]
|
||||
const uniqueOrigins = [...new Set(allOrigins)]
|
||||
return uniqueOrigins
|
||||
}
|
||||
|
||||
// 过滤基础URL(确保后续添加)
|
||||
const filterBaseOriginsForUI = (origins: string[]) => {
|
||||
return origins.filter((origin: string) => !DEV_URLS.includes(origin.trim()))
|
||||
}
|
||||
|
||||
// 统一使用的按钮样式
|
||||
const buttonStyle = {
|
||||
borderRadius: '8px',
|
||||
textTransform: 'none',
|
||||
@@ -47,7 +40,6 @@ const buttonStyle = {
|
||||
},
|
||||
}
|
||||
|
||||
// 添加按钮样式
|
||||
const addButtonStyle = {
|
||||
...buttonStyle,
|
||||
backgroundColor: '#4CAF50',
|
||||
@@ -57,7 +49,6 @@ const addButtonStyle = {
|
||||
},
|
||||
}
|
||||
|
||||
// 删除按钮样式
|
||||
const deleteButtonStyle = {
|
||||
...buttonStyle,
|
||||
backgroundColor: '#FF5252',
|
||||
@@ -85,7 +76,6 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
|
||||
|
||||
const lastKeyRef = useRef(0) // 用于生成唯一的key
|
||||
|
||||
// CORS配置状态管理
|
||||
const [corsConfig, setCorsConfig] = useState<{
|
||||
allowPrivateNetwork: boolean
|
||||
allowOrigins: AllowOriginItem[]
|
||||
@@ -101,7 +91,6 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
|
||||
}
|
||||
})
|
||||
|
||||
// 处理CORS配置变更
|
||||
const handleCorsConfigChange = (
|
||||
key: 'allowPrivateNetwork' | 'allowOrigins',
|
||||
value: boolean | AllowOriginItem[],
|
||||
@@ -112,7 +101,6 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
|
||||
}))
|
||||
}
|
||||
|
||||
// 添加新的允许来源
|
||||
const handleAddOrigin = () => {
|
||||
lastKeyRef.current += 1
|
||||
handleCorsConfigChange('allowOrigins', [
|
||||
@@ -121,24 +109,20 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
|
||||
])
|
||||
}
|
||||
|
||||
// 更新允许来源列表中的某一项
|
||||
const handleUpdateOrigin = (index: number, value: string) => {
|
||||
const newOrigins = [...corsConfig.allowOrigins]
|
||||
newOrigins[index] = { ...newOrigins[index], value }
|
||||
handleCorsConfigChange('allowOrigins', newOrigins)
|
||||
}
|
||||
|
||||
// 删除允许来源列表中的某一项
|
||||
const handleDeleteOrigin = (index: number) => {
|
||||
const newOrigins = [...corsConfig.allowOrigins]
|
||||
newOrigins.splice(index, 1)
|
||||
handleCorsConfigChange('allowOrigins', newOrigins)
|
||||
}
|
||||
|
||||
// 保存配置请求
|
||||
const { loading, run: saveConfig } = useRequest(
|
||||
async () => {
|
||||
// 保存时使用完整的源列表(包括开发URL)
|
||||
const fullOrigins = getFullOrigins(
|
||||
corsConfig.allowOrigins.map((origin) => origin.value),
|
||||
)
|
||||
|
||||
@@ -40,18 +40,15 @@ export function GuardState<T>(props: Props<T>) {
|
||||
|
||||
childProps[valueProps] = value
|
||||
childProps[onChangeProps] = async (...args: any[]) => {
|
||||
// 多次操作无效
|
||||
if (lockRef.current) return
|
||||
lockRef.current = true
|
||||
|
||||
try {
|
||||
const newValue = onFormat ? onFormat(...args) : (args[0] as T)
|
||||
// 先在ui上响应操作
|
||||
onChange(newValue)
|
||||
|
||||
const now = Date.now()
|
||||
|
||||
// save the old value
|
||||
if (waitTime <= 0 || now - lastRef.current >= waitTime) {
|
||||
saveRef.current = value
|
||||
}
|
||||
@@ -62,14 +59,12 @@ export function GuardState<T>(props: Props<T>) {
|
||||
await onGuard(newValue, value!)
|
||||
lockRef.current = false
|
||||
} else {
|
||||
// debounce guard
|
||||
clearTimeout(timeRef.current)
|
||||
|
||||
timeRef.current = setTimeout(async () => {
|
||||
try {
|
||||
await onGuard(newValue, saveRef.current!)
|
||||
} catch (err: any) {
|
||||
// 状态回退
|
||||
onChange(saveRef.current!)
|
||||
onCatch(err)
|
||||
} finally {
|
||||
@@ -78,7 +73,6 @@ export function GuardState<T>(props: Props<T>) {
|
||||
}, waitTime)
|
||||
}
|
||||
} catch (err: any) {
|
||||
// 状态回退
|
||||
onChange(saveRef.current!)
|
||||
onCatch(err)
|
||||
lockRef.current = false
|
||||
|
||||
@@ -191,7 +191,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
const { systemProxyAddress } = useSystemData()
|
||||
const displayedMixedPort = useDisplayedMixedPort()
|
||||
|
||||
// 为当前状态计算系统代理地址
|
||||
const getSystemProxyAddress = useMemo(() => {
|
||||
const isPacMode = value.pac ?? false
|
||||
|
||||
@@ -250,14 +249,11 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
close: () => setOpen(false),
|
||||
}))
|
||||
|
||||
// 获取网络接口和主机名
|
||||
const fetchNetworkInterfaces = async () => {
|
||||
try {
|
||||
// 获取系统网络接口信息
|
||||
const interfaces = await getNetworkInterfacesInfo()
|
||||
const ipAddresses: string[] = []
|
||||
|
||||
// 从interfaces中提取IPv4和IPv6地址
|
||||
interfaces.forEach((iface) => {
|
||||
iface.addr.forEach((address) => {
|
||||
if (address.V4 && address.V4.ip) {
|
||||
@@ -269,7 +265,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
})
|
||||
})
|
||||
|
||||
// 获取当前系统的主机名
|
||||
let hostname = ''
|
||||
try {
|
||||
hostname = await getSystemHostname()
|
||||
@@ -278,12 +273,9 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
console.error('获取主机名失败:', err)
|
||||
}
|
||||
|
||||
// 构建选项列表
|
||||
const options = ['127.0.0.1', 'localhost']
|
||||
|
||||
// 确保主机名添加到列表,即使它是空字符串也记录下来
|
||||
if (hostname) {
|
||||
// 如果主机名不是localhost或127.0.0.1,则添加它
|
||||
if (hostname !== 'localhost' && hostname !== '127.0.0.1') {
|
||||
hostname = hostname + '.local'
|
||||
options.push(hostname)
|
||||
@@ -295,16 +287,13 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
debugLog('主机名为空')
|
||||
}
|
||||
|
||||
// 添加IP地址
|
||||
options.push(...ipAddresses)
|
||||
|
||||
// 去重
|
||||
const uniqueOptions = Array.from(new Set(options))
|
||||
debugLog('最终选项列表:', uniqueOptions)
|
||||
setHostOptions(uniqueOptions)
|
||||
} catch (error) {
|
||||
console.error('获取网络接口失败:', error)
|
||||
// 失败时至少提供基本选项
|
||||
setHostOptions(['127.0.0.1', 'localhost'])
|
||||
}
|
||||
}
|
||||
@@ -325,7 +314,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
return
|
||||
}
|
||||
|
||||
// 修改验证规则,允许IP和主机名
|
||||
const ipv4Regex =
|
||||
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
|
||||
const ipv6Regex =
|
||||
@@ -369,7 +357,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
let pacContent = value.pac_content
|
||||
if (pacContent) {
|
||||
pacContent = pacContent.replace(/%proxy_host%/g, value.proxy_host)
|
||||
// 将 mixed-port 转换为字符串
|
||||
const mixedPortStr = displayedMixedPort.toString()
|
||||
pacContent = pacContent.replace(/%mixed-port%/g, mixedPortStr)
|
||||
}
|
||||
@@ -378,7 +365,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
patch.pac_file_content = pacContent
|
||||
}
|
||||
|
||||
// 处理IPv6地址,如果是IPv6地址但没有被方括号包围,则添加方括号
|
||||
let proxyHost = value.proxy_host
|
||||
if (
|
||||
ipv6Regex.test(proxyHost) &&
|
||||
@@ -392,7 +378,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
patch.proxy_host = proxyHost
|
||||
}
|
||||
|
||||
// 判断是否需要重置系统代理
|
||||
const needResetProxy =
|
||||
value.pac !== proxy_auto_config ||
|
||||
proxyHost !== proxy_host ||
|
||||
@@ -402,7 +387,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
|
||||
Promise.resolve().then(async () => {
|
||||
try {
|
||||
// 乐观更新本地状态
|
||||
if (Object.keys(patch).length > 0) {
|
||||
mutateVerge({ ...verge, ...patch }, false)
|
||||
}
|
||||
@@ -413,7 +397,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
try {
|
||||
await invalidateProxyState()
|
||||
|
||||
// 如果需要重置代理且代理当前启用
|
||||
if (needResetProxy && enabled) {
|
||||
const [currentSysProxy, currentAutoProxy] = await Promise.all([
|
||||
getSystemProxy(),
|
||||
@@ -439,7 +422,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
console.error('配置保存失败:', err)
|
||||
mutateVerge()
|
||||
showNotice.error(err)
|
||||
// setOpen(true);
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -587,7 +569,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
setValue((v) => ({
|
||||
...v,
|
||||
use_default: e,
|
||||
// 当取消选择use_default且当前bypass为空时,填充默认值
|
||||
bypass: nextBypass,
|
||||
}))
|
||||
return
|
||||
@@ -697,6 +678,5 @@ const FlexBox = styled('div')`
|
||||
|
||||
.label {
|
||||
flex: none;
|
||||
//width: 85px;
|
||||
}
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user