Skip to content

Commit

Permalink
default listen on 127.0.0.1, listen on demand (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlongzt authored Nov 5, 2024
1 parent f20f15d commit d024214
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
29 changes: 20 additions & 9 deletions src/main/resolve/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,32 @@ export function findAvailablePort(startPort: number): Promise<number> {
})
}

let pacServer: http.Server

export async function startPacServer(): Promise<void> {
await stopPacServer()
const { sysProxy } = await getAppConfig()
const { mode = 'manual', host: cHost, pacScript } = sysProxy
if (mode !== 'auto') {
return
}
const host = cHost || '127.0.0.1'
let script = pacScript || defaultPacScript
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig()
script = script.replaceAll('%mixed-port%', port.toString())
pacPort = await findAvailablePort(10000)
const server = http
pacServer = http
.createServer(async (_req, res) => {
const {
sysProxy: { pacScript }
} = await getAppConfig()
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig()
let script = pacScript || defaultPacScript
script = script.replaceAll('%mixed-port%', port.toString())
res.writeHead(200, { 'Content-Type': 'application/x-ns-proxy-autoconfig' })
res.end(script)
})
.listen(pacPort)
server.unref()
.listen(pacPort, host)
}

export async function stopPacServer(): Promise<void> {
if (pacServer) {
pacServer.close()
}
}

export async function startSubStoreFrontendServer(): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion src/main/sys/sysproxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { triggerAutoProxy, triggerManualProxy } from '@mihomo-party/sysproxy'
import { getAppConfig, getControledMihomoConfig } from '../config'
import { pacPort } from '../resolve/server'
import { pacPort, startPacServer, stopPacServer } from '../resolve/server'
import { promisify } from 'util'
import { execFile } from 'child_process'
import path from 'path'
Expand Down Expand Up @@ -63,6 +63,7 @@ export async function triggerSysProxy(enable: boolean): Promise<void> {
}

async function enableSysProxy(): Promise<void> {
await startPacServer()
const { sysProxy } = await getAppConfig()
const { mode, host, bypass = defaultBypass } = sysProxy
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig()
Expand Down Expand Up @@ -105,6 +106,7 @@ async function enableSysProxy(): Promise<void> {
}

async function disableSysProxy(): Promise<void> {
await stopPacServer()
const execFilePromise = promisify(execFile)
if (process.platform === 'win32') {
try {
Expand Down
4 changes: 3 additions & 1 deletion src/main/utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,13 @@ export async function init(): Promise<void> {
await migration()
await initFiles()
await cleanup()
await startPacServer()
await startSubStoreFrontendServer()
await startSubStoreBackendServer()
const { sysProxy } = await getAppConfig()
try {
if (sysProxy.enable) {
await startPacServer()
}
await triggerSysProxy(sysProxy.enable)
} catch {
// ignore
Expand Down
15 changes: 13 additions & 2 deletions src/renderer/src/pages/syspeoxy.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Input, Tab, Tabs } from '@nextui-org/react'
import { Button, Input, Tab, Tabs, Tooltip } from '@nextui-org/react'
import BasePage from '@renderer/components/base/base-page'
import SettingCard from '@renderer/components/base/base-setting-card'
import SettingItem from '@renderer/components/base/base-setting-item'
Expand All @@ -9,6 +9,7 @@ import { openUWPTool, triggerSysProxy } from '@renderer/utils/ipc'
import { Key, useState } from 'react'
import React from 'react'
import { MdDeleteForever } from 'react-icons/md'
import { IoIosHelpCircle } from 'react-icons/io'

const defaultBypass: string[] =
platform === 'linux'
Expand Down Expand Up @@ -135,7 +136,17 @@ const Sysproxy: React.FC = () => {
}}
/>
</SettingItem>
<SettingItem title="代理模式" divider>
<SettingItem
actions={
<Tooltip content="PAC监听 代理主机:端口(10000开始的可用)">
<Button isIconOnly size="sm" variant="light">
<IoIosHelpCircle className="text-lg" />
</Button>
</Tooltip>
}
title="代理模式"
divider
>
<Tabs
size="sm"
color="primary"
Expand Down

0 comments on commit d024214

Please sign in to comment.