添加开发API允许局域网访问选项(#1828)

pull/1834/head
lyswhut 2024-04-01 22:44:34 +08:00
parent 703327ed41
commit cd7d5ed665
10 changed files with 31 additions and 11 deletions

View File

@ -138,6 +138,7 @@ const defaultSetting: LX.AppSetting = {
'openAPI.enable': false, 'openAPI.enable': false,
'openAPI.port': '23330', 'openAPI.port': '23330',
'openAPI.bindLan': false,
// 'theme.id': 'blue_plus', // 'theme.id': 'blue_plus',
'theme.id': 'green', 'theme.id': 'green',

View File

@ -626,9 +626,14 @@ declare global {
'openAPI.enable': boolean 'openAPI.enable': boolean
/** /**
* * API
*/ */
'openAPI.port': '23333' | string 'openAPI.port': '23330' | string
/**
*
*/
'openAPI.bindLan': boolean
/** /**
* *

View File

@ -8,6 +8,7 @@ declare namespace LX {
interface EnableServer { interface EnableServer {
enable: boolean enable: boolean
port: string port: string
bindLan: boolean
} }
interface ActionBase <A> { interface ActionBase <A> {

View File

@ -462,6 +462,7 @@
"setting__odc_clear_search_list": "Clear the search list when you are not searching", "setting__odc_clear_search_list": "Clear the search list when you are not searching",
"setting__open_api": "Open API", "setting__open_api": "Open API",
"setting__open_api_address": "Service address: {address}", "setting__open_api_address": "Service address: {address}",
"setting__open_api_bind_lan": "Allow access from LAN",
"setting__open_api_enable": "Enable open API service", "setting__open_api_enable": "Enable open API service",
"setting__open_api_port": "Service port", "setting__open_api_port": "Service port",
"setting__open_api_port_tip": "Please enter the open API service port", "setting__open_api_port_tip": "Please enter the open API service port",

View File

@ -462,6 +462,7 @@
"setting__odc_clear_search_list": "离开搜索界面时清空搜索列表", "setting__odc_clear_search_list": "离开搜索界面时清空搜索列表",
"setting__open_api": "开放API", "setting__open_api": "开放API",
"setting__open_api_address": "服务地址:{address}", "setting__open_api_address": "服务地址:{address}",
"setting__open_api_bind_lan": "允许来自局域网的访问",
"setting__open_api_enable": "启用开放API服务", "setting__open_api_enable": "启用开放API服务",
"setting__open_api_port": "服务端口", "setting__open_api_port": "服务端口",
"setting__open_api_port_tip": "请输入开放API服务端口", "setting__open_api_port_tip": "请输入开放API服务端口",

View File

@ -462,6 +462,7 @@
"setting__odc_clear_search_list": "離開搜尋介面時清空搜尋列表", "setting__odc_clear_search_list": "離開搜尋介面時清空搜尋列表",
"setting__open_api": "開放API", "setting__open_api": "開放API",
"setting__open_api_address": "服務地址:{address}", "setting__open_api_address": "服務地址:{address}",
"setting__open_api_bind_lan": "允許來自區域網路的訪問",
"setting__open_api_enable": "啟用開放API服務", "setting__open_api_enable": "啟用開放API服務",
"setting__open_api_port": "服務連接埠", "setting__open_api_port": "服務連接埠",
"setting__open_api_port_tip": "請輸入開放API服務端口", "setting__open_api_port_tip": "請輸入開放API服務端口",

View File

@ -41,7 +41,7 @@ const handleSubscribePlayerStatus = (req: http.IncomingMessage, res: http.Server
} }
} }
const handleStartServer = async(port = 9000, ip = '127.0.0.1') => new Promise<void>((resolve, reject) => { const handleStartServer = async(port: number, ip: string) => new Promise<void>((resolve, reject) => {
httpServer = http.createServer((req, res): void => { httpServer = http.createServer((req, res): void => {
const [endUrl, query] = `/${req.url?.split('/').at(-1) ?? ''}`.split('?') const [endUrl, query] = `/${req.url?.split('/').at(-1) ?? ''}`.split('?')
let code let code
@ -197,9 +197,9 @@ export const stopServer = async() => {
}) })
return status return status
} }
export const startServer = async(port: number) => { export const startServer = async(port: number, bindLan: boolean) => {
if (status.status) await handleStopServer() if (status.status) await handleStopServer()
await handleStartServer(port).then(() => { await handleStartServer(port, bindLan ? '0.0.0.0' : '127.0.0.1').then(() => {
status.status = true status.status = true
status.message = '' status.message = ''
status.address = `http://localhost${port == 80 ? '' : ':' + port}` status.address = `http://localhost${port == 80 ? '' : ':' + port}`

View File

@ -11,7 +11,7 @@ export default () => {
mainHandle<LX.OpenAPI.Actions, any>(WIN_MAIN_RENDERER_EVENT_NAME.open_api_action, async({ params: data }) => { mainHandle<LX.OpenAPI.Actions, any>(WIN_MAIN_RENDERER_EVENT_NAME.open_api_action, async({ params: data }) => {
switch (data.action) { switch (data.action) {
case 'enable': case 'enable':
return data.data.enable ? await startServer(parseInt(data.data.port)) : await stopServer() return data.data.enable ? await startServer(parseInt(data.data.port), data.data.bindLan) : await stopServer()
case 'status': return getStatus() case 'status': return getStatus()
} }
}) })

View File

@ -5,12 +5,13 @@ import { openAPI } from '@renderer/store'
import { setAutoPause } from '@renderer/core/lyric' import { setAutoPause } from '@renderer/core/lyric'
export default () => { export default () => {
const handleEnable = async(enable: boolean, port: string) => { const handleEnable = async(enable: boolean, port: string, bindLan: boolean) => {
await sendOpenAPIAction({ await sendOpenAPIAction({
action: 'enable', action: 'enable',
data: { data: {
enable, enable,
port, port,
bindLan,
}, },
}).then((status) => { }).then((status) => {
openAPI.address = status.address openAPI.address = status.address
@ -27,16 +28,22 @@ export default () => {
}) })
} }
watch(() => appSetting['openAPI.enable'], enable => { watch(() => appSetting['openAPI.enable'], enable => {
void handleEnable(enable, appSetting['openAPI.port']) void handleEnable(enable, appSetting['openAPI.port'], appSetting['openAPI.bindLan'])
}) })
watch(() => appSetting['openAPI.port'], port => { watch(() => appSetting['openAPI.port'], port => {
void handleEnable(appSetting['openAPI.enable'], port) if (!appSetting['openAPI.enable']) return
void handleEnable(appSetting['openAPI.enable'], port, appSetting['openAPI.bindLan'])
})
watch(() => appSetting['openAPI.bindLan'], bindLan => {
if (!appSetting['openAPI.enable']) return
void handleEnable(appSetting['openAPI.enable'], appSetting['openAPI.port'], bindLan)
}) })
return async() => { return async() => {
if (appSetting['openAPI.enable']) { if (appSetting['openAPI.enable']) {
void handleEnable(true, appSetting['openAPI.port']) void handleEnable(true, appSetting['openAPI.port'], appSetting['openAPI.bindLan'])
} }
} }
} }

View File

@ -2,7 +2,10 @@
dt#sync {{ $t('setting__open_api') }} dt#sync {{ $t('setting__open_api') }}
dd.gap-top dd.gap-top
div div
base-checkbox.gap-top(id="setting_open_api_enable" :model-value="appSetting['openAPI.enable']" :label="$t('setting__open_api_enable')" @update:model-value="updateSetting({ 'openAPI.enable': $event })") .p
base-checkbox(id="setting_open_api_enable" :model-value="appSetting['openAPI.enable']" :label="$t('setting__open_api_enable')" @update:model-value="updateSetting({ 'openAPI.enable': $event })")
.p.gap-top
base-checkbox(id="setting_open_api_bind_lan" :model-value="appSetting['openAPI.bindLan']" :label="$t('setting__open_api_bind_lan')" @update:model-value="updateSetting({ 'openAPI.bindLan': $event })")
.p.gap-top.small {{ $t('setting__open_api_address', { address: openAPI.address || '' }) }} .p.gap-top.small {{ $t('setting__open_api_address', { address: openAPI.address || '' }) }}
.p.small(v-if="openAPI.message") {{ openAPI.message }} .p.small(v-if="openAPI.message") {{ openAPI.message }}
.p .p