fix: 代码审查修复 - 代理密码编码、类型一致性、主机名验证

This commit is contained in:
VirtualHotBar
2026-06-02 19:02:03 +08:00
parent 8ebcdfd2c9
commit 92fab410d5
7 changed files with 32 additions and 3 deletions

View File

@@ -769,6 +769,7 @@
"no_proxy": "No Proxy",
"proxy_host": "Proxy Host",
"proxy_host_placeholder": "e.g. 127.0.0.1",
"proxy_host_invalid": "Invalid hostname format. Please enter a valid IP address or domain name",
"proxy_port": "Proxy Port",
"proxy_port_placeholder": "e.g. 7890",
"proxy_username": "Proxy Username",

View File

@@ -782,6 +782,7 @@
"no_proxy": "不使用代理",
"proxy_host": "代理主机",
"proxy_host_placeholder": "例如127.0.0.1",
"proxy_host_invalid": "主机名格式无效请输入有效的IP地址或域名",
"proxy_port": "代理端口",
"proxy_port_placeholder": "例如7890",
"proxy_username": "代理用户名",

View File

@@ -736,6 +736,7 @@
"no_proxy": "不使用代理",
"proxy_host": "代理主機",
"proxy_host_placeholder": "例如127.0.0.1",
"proxy_host_invalid": "主機名格式無效請輸入有效的IP地址或域名",
"proxy_port": "代理端口",
"proxy_port_placeholder": "例如7890",
"proxy_username": "代理用戶名",

View File

@@ -56,10 +56,11 @@ function getMountStorage(mountPath: string): MountListItem | undefined {
}
/**
* 检查是否已挂载
* 检查挂载配置是否存在(同步版本,仅检查配置,不检查实际挂载状态)
* 注意此函数检查的是配置是否存在而非实际的rclone挂载状态
* 如需检查实际挂载状态,请使用 mountRepository.isMounted()(异步版本)
*/
function isMounted(mountPath: string): boolean {
// 同步版本,用于 UI 判断
const mountList = mountRepository.getMountConfig(mountPath)
return mountList !== undefined
}

View File

@@ -11,6 +11,16 @@ import { clearAllCache } from '../../../utils/tempCleanup'
const FormItem = Form.Item
/**
* 验证主机名格式允许IP地址、域名、localhost
*/
function isValidHostname(hostname: string): boolean {
if (!hostname) return false
// 允许: IP地址、域名、localhost
const hostnameRegex = /^[a-zA-Z0-9]([a-zA-Z0-9\-\.]*[a-zA-Z0-9])?$/
return hostnameRegex.test(hostname)
}
export function AdvancedSettings(): JSX.Element {
const { t } = useTranslation()
const { increment: incrementSettings } = useSettingsStore()
@@ -59,6 +69,12 @@ export function AdvancedSettings(): JSX.Element {
incrementSettings()
}
}}
onBlur={e => {
const value = e.target.value
if (value && !isValidHostname(value)) {
Message.warning(t('proxy_host_invalid'))
}
}}
/>
</FormItem>
<FormItem label={t('proxy_port')}>

View File

@@ -164,6 +164,10 @@ class ConfigService {
if (this.config.framework?.openlist?.password) {
this.config.framework.openlist.password = decodePassword(this.config.framework.openlist.password)
}
// 解码代理密码
if (this.config.settings?.proxy?.password) {
this.config.settings.proxy.password = decodePassword(this.config.settings.proxy.password)
}
logger.info('Config loaded from disk', 'ConfigService')
} catch (error) {
@@ -185,6 +189,10 @@ class ConfigService {
if (configToSave.framework?.openlist?.password) {
configToSave.framework.openlist.password = encodePassword(configToSave.framework.openlist.password)
}
// 编码代理密码
if (configToSave.settings?.proxy?.password) {
configToSave.settings.proxy.password = encodePassword(configToSave.settings.proxy.password)
}
await invoke('update_config', {
data: configToSave,

View File

@@ -11,13 +11,14 @@ import { LOCALHOST_URLS } from '../../constants'
import { netmountLogDir, rcloneConfigFile, rcloneLogFile } from '../netmountPaths'
import { restartSidecar, startSidecarAndWait, stopSidecarGracefully } from '../sidecarService'
import { parseExtraCliArgs } from '../cliArgs'
import type { NMConfig } from '../../type/config'
/**
* 构建代理URL
* 根据代理配置生成 rclone --http-proxy 参数值
* 支持 HTTP 和 SOCKS5 代理格式protocol://[user:pass@]host:port
*/
function buildProxyUrl(proxy: { type: string; host?: string; port?: number; username?: string; password?: string }): string | undefined {
function buildProxyUrl(proxy: NonNullable<NMConfig['settings']['proxy']>): string | undefined {
if (proxy.type === 'no_proxy' || !proxy.host || !proxy.port) {
return undefined
}