fix: handle port value zero correctly and prevent NaN from being written to config

This commit is contained in:
xmk23333 2026-01-15 18:23:46 +08:00
parent 727eceb0cf
commit 36027cecea
2 changed files with 22 additions and 14 deletions

View File

@ -43,6 +43,14 @@ export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>):
controledMihomoWriteQueue = controledMihomoWriteQueue.then(async () => { controledMihomoWriteQueue = controledMihomoWriteQueue.then(async () => {
const { controlDns = true, controlSniff = true } = await getAppConfig() const { controlDns = true, controlSniff = true } = await getAppConfig()
// 过滤端口字段中的 NaN 值,防止写入无效配置
const portFields = ['mixed-port', 'socks-port', 'port', 'redir-port', 'tproxy-port'] as const
for (const field of portFields) {
if (field in patch && (typeof patch[field] !== 'number' || Number.isNaN(patch[field]))) {
delete patch[field]
}
}
if (patch.hosts) { if (patch.hosts) {
controledMihomoConfig.hosts = patch.hosts controledMihomoConfig.hosts = patch.hosts
} }

View File

@ -130,11 +130,11 @@ const Mihomo: React.FC = () => {
const { 'store-selected': storeSelected, 'store-fake-ip': storeFakeIp } = profile const { 'store-selected': storeSelected, 'store-fake-ip': storeFakeIp } = profile
const [isManualPortChange, setIsManualPortChange] = useState(false) const [isManualPortChange, setIsManualPortChange] = useState(false)
const [mixedPortInput, setMixedPortInput] = useState(showMixedPort || mixedPort) const [mixedPortInput, setMixedPortInput] = useState(showMixedPort ?? mixedPort)
const [socksPortInput, setSocksPortInput] = useState(showSocksPort || socksPort) const [socksPortInput, setSocksPortInput] = useState(showSocksPort ?? socksPort)
const [httpPortInput, setHttpPortInput] = useState(showHttpPort || httpPort) const [httpPortInput, setHttpPortInput] = useState(showHttpPort ?? httpPort)
const [redirPortInput, setRedirPortInput] = useState(showRedirPort || redirPort) const [redirPortInput, setRedirPortInput] = useState(showRedirPort ?? redirPort)
const [tproxyPortInput, setTproxyPortInput] = useState(showTproxyPort || tproxyPort) const [tproxyPortInput, setTproxyPortInput] = useState(showTproxyPort ?? tproxyPort)
const [externalControllerInput, setExternalControllerInput] = useState(externalController) const [externalControllerInput, setExternalControllerInput] = useState(externalController)
const [secretInput, setSecretInput] = useState(secret) const [secretInput, setSecretInput] = useState(secret)
const [lanAllowedIpsInput, setLanAllowedIpsInput] = useState(lanAllowedIps) const [lanAllowedIpsInput, setLanAllowedIpsInput] = useState(lanAllowedIps)
@ -702,7 +702,7 @@ const Mihomo: React.FC = () => {
size="sm" size="sm"
type="number" type="number"
className="w-[100px]" className="w-[100px]"
value={showMixedPort?.toString()} value={(showMixedPort ?? mixedPort ?? '').toString()}
max={65535} max={65535}
min={0} min={0}
onValueChange={(v) => { onValueChange={(v) => {
@ -763,7 +763,7 @@ const Mihomo: React.FC = () => {
size="sm" size="sm"
type="number" type="number"
className="w-[100px]" className="w-[100px]"
value={showSocksPort?.toString()} value={(showSocksPort ?? socksPort ?? '').toString()}
max={65535} max={65535}
min={0} min={0}
onValueChange={(v) => { onValueChange={(v) => {
@ -796,7 +796,7 @@ const Mihomo: React.FC = () => {
onValueChange={(value) => { onValueChange={(value) => {
patchAppConfig({ enableSocksPort: value }) patchAppConfig({ enableSocksPort: value })
if (value) { if (value) {
const port = appConfig?.showSocksPort || socksPort const port = appConfig?.showSocksPort ?? socksPort
onChangeNeedRestart({ 'socks-port': port }) onChangeNeedRestart({ 'socks-port': port })
} else { } else {
onChangeNeedRestart({ 'socks-port': 0 }) onChangeNeedRestart({ 'socks-port': 0 })
@ -824,7 +824,7 @@ const Mihomo: React.FC = () => {
size="sm" size="sm"
type="number" type="number"
className="w-[100px]" className="w-[100px]"
value={showHttpPort?.toString()} value={(showHttpPort ?? httpPort ?? '').toString()}
max={65535} max={65535}
min={0} min={0}
onValueChange={(v) => { onValueChange={(v) => {
@ -857,7 +857,7 @@ const Mihomo: React.FC = () => {
onValueChange={(value) => { onValueChange={(value) => {
patchAppConfig({ enableHttpPort: value }) patchAppConfig({ enableHttpPort: value })
if (value) { if (value) {
const port = appConfig?.showHttpPort || httpPort const port = appConfig?.showHttpPort ?? httpPort
onChangeNeedRestart({ port: port }) onChangeNeedRestart({ port: port })
} else { } else {
onChangeNeedRestart({ port: 0 }) onChangeNeedRestart({ port: 0 })
@ -886,7 +886,7 @@ const Mihomo: React.FC = () => {
size="sm" size="sm"
type="number" type="number"
className="w-[100px]" className="w-[100px]"
value={showRedirPort?.toString()} value={(showRedirPort ?? redirPort ?? '').toString()}
max={65535} max={65535}
min={0} min={0}
onValueChange={(v) => { onValueChange={(v) => {
@ -919,7 +919,7 @@ const Mihomo: React.FC = () => {
onValueChange={(value) => { onValueChange={(value) => {
patchAppConfig({ enableRedirPort: value }) patchAppConfig({ enableRedirPort: value })
if (value) { if (value) {
const port = appConfig?.showRedirPort || redirPort const port = appConfig?.showRedirPort ?? redirPort
onChangeNeedRestart({ 'redir-port': port }) onChangeNeedRestart({ 'redir-port': port })
} else { } else {
onChangeNeedRestart({ 'redir-port': 0 }) onChangeNeedRestart({ 'redir-port': 0 })
@ -949,7 +949,7 @@ const Mihomo: React.FC = () => {
size="sm" size="sm"
type="number" type="number"
className="w-[100px]" className="w-[100px]"
value={showTproxyPort?.toString()} value={(showTproxyPort ?? tproxyPort ?? '').toString()}
max={65535} max={65535}
min={0} min={0}
onValueChange={(v) => { onValueChange={(v) => {
@ -982,7 +982,7 @@ const Mihomo: React.FC = () => {
onValueChange={(value) => { onValueChange={(value) => {
patchAppConfig({ enableTproxyPort: value }) patchAppConfig({ enableTproxyPort: value })
if (value) { if (value) {
const port = appConfig?.showTproxyPort || tproxyPort const port = appConfig?.showTproxyPort ?? tproxyPort
onChangeNeedRestart({ 'tproxy-port': port }) onChangeNeedRestart({ 'tproxy-port': port })
} else { } else {
onChangeNeedRestart({ 'tproxy-port': 0 }) onChangeNeedRestart({ 'tproxy-port': 0 })