From e60c2f5f19490d84fed5e041253a18bfb693d76f Mon Sep 17 00:00:00 2001 From: lyswhut <lyswhut@qq.com> Date: Tue, 2 Apr 2024 11:07:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B3=A8=E5=86=8C=E7=9A=84?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=9C=AA=E8=A2=AB=E6=B8=85=E7=90=86=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 2 +- .../utils/lyric-font-player/line-player.js | 2 +- src/common/utils/nodejs.ts | 18 ++++++++++++++++ src/lang/en-us.json | 2 +- src/lang/zh-cn.json | 2 +- src/lang/zh-tw.json | 2 +- src/main/modules/openApi/index.ts | 7 +++++-- src/main/modules/sync/client/client.ts | 3 ++- src/main/modules/sync/server/server/server.ts | 2 +- src/main/modules/sync/server/utils/tools.ts | 18 ---------------- src/main/modules/sync/utils.ts | 21 ++----------------- .../Setting/components/SettingOpenAPI.vue | 4 +++- 12 files changed, 36 insertions(+), 47 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index 4570bd22..db860ddf 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -2,7 +2,7 @@ - 主题编辑器添加“深色字体”选项,启用后将减少字体颜色梯度,各类字体(正文、标签字体等)颜色将更接近,这有助于解决创建全透明主题时可能出现的字体配色问题(#1799) - 新增在线自定义源导入功能,允许通过http/https链接导入自定义源 -- 新增HTTP开放API服务,默认关闭,该服务可以为第三方软件提供调用LX的能力,可用API看说明文档(#1824) +- 新增HTTP开放API服务,默认关闭,该服务可以为第三方软件提供调用LX的能力,可用API看[说明文档](https://lyswhut.github.io/lx-music-doc/desktop/open-api)(#1824) - 托盘菜单新增播放、切歌、收藏控制 ### 优化 diff --git a/src/common/utils/lyric-font-player/line-player.js b/src/common/utils/lyric-font-player/line-player.js index 1d0e0f42..a557b2d3 100644 --- a/src/common/utils/lyric-font-player/line-player.js +++ b/src/common/utils/lyric-font-player/line-player.js @@ -231,7 +231,7 @@ export default class LinePlayer { timeoutTools.cancelNextTick = window.cancelAnimationFrame.bind(window) } else { timeoutTools.nextTick = (handler) => { - return setTimeout(handler, 80) + return setTimeout(handler, 20) } timeoutTools.cancelNextTick = clearTimeout.bind(global) } diff --git a/src/common/utils/nodejs.ts b/src/common/utils/nodejs.ts index 71423439..ce279cfc 100644 --- a/src/common/utils/nodejs.ts +++ b/src/common/utils/nodejs.ts @@ -2,6 +2,7 @@ import fs from 'node:fs' import crypto from 'node:crypto' import { gzip, gunzip } from 'node:zlib' import path from 'node:path' +import { networkInterfaces } from 'node:os' import { log } from '@common/utils' export const joinPath = (...paths: string[]): string => path.join(...paths) @@ -185,3 +186,20 @@ export const copyFile = async(sourcePath: string, distPath: string) => { export const moveFile = async(sourcePath: string, distPath: string) => { return fs.promises.rename(sourcePath, distPath) } + +export const getAddress = (): string[] => { + const nets = networkInterfaces() + const results: string[] = [] + // console.log(nets) + + for (const interfaceInfos of Object.values(nets)) { + if (!interfaceInfos) continue + // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses + for (const interfaceInfo of interfaceInfos) { + if (interfaceInfo.family === 'IPv4' && !interfaceInfo.internal) { + results.push(interfaceInfo.address) + } + } + } + return results +} diff --git a/src/lang/en-us.json b/src/lang/en-us.json index feb8e92e..2b574cd2 100644 --- a/src/lang/en-us.json +++ b/src/lang/en-us.json @@ -461,7 +461,7 @@ "setting__odc_clear_search_input": "Clear the search box 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_address": "Service address: {address}", + "setting__open_api_address": "Service address:", "setting__open_api_bind_lan": "Allow access from LAN", "setting__open_api_enable": "Enable open API service", "setting__open_api_port": "Service port", diff --git a/src/lang/zh-cn.json b/src/lang/zh-cn.json index 2ecb6829..bf143890 100644 --- a/src/lang/zh-cn.json +++ b/src/lang/zh-cn.json @@ -461,7 +461,7 @@ "setting__odc_clear_search_input": "离开搜索界面时清空搜索框", "setting__odc_clear_search_list": "离开搜索界面时清空搜索列表", "setting__open_api": "开放API", - "setting__open_api_address": "服务地址:{address}", + "setting__open_api_address": "服务地址:", "setting__open_api_bind_lan": "允许来自局域网的访问", "setting__open_api_enable": "启用开放API服务", "setting__open_api_port": "服务端口", diff --git a/src/lang/zh-tw.json b/src/lang/zh-tw.json index ffbebc41..8f369946 100644 --- a/src/lang/zh-tw.json +++ b/src/lang/zh-tw.json @@ -461,7 +461,7 @@ "setting__odc_clear_search_input": "離開搜尋介面時清空搜尋框", "setting__odc_clear_search_list": "離開搜尋介面時清空搜尋列表", "setting__open_api": "開放API", - "setting__open_api_address": "服務地址:{address}", + "setting__open_api_address": "服務地址:", "setting__open_api_bind_lan": "允許來自區域網路的訪問", "setting__open_api_enable": "啟用開放API服務", "setting__open_api_port": "服務連接埠", diff --git a/src/main/modules/openApi/index.ts b/src/main/modules/openApi/index.ts index 1e78ffca..ee668ddd 100644 --- a/src/main/modules/openApi/index.ts +++ b/src/main/modules/openApi/index.ts @@ -1,6 +1,7 @@ import http from 'node:http' import querystring from 'node:querystring' import type { Socket } from 'node:net' +import { getAddress } from '@common/utils/nodejs' let status: LX.OpenAPI.Status = { status: false, @@ -198,11 +199,13 @@ export const stopServer = async() => { return status } export const startServer = async(port: number, bindLan: boolean) => { - if (status.status) await handleStopServer() + if (status.status) await stopServer() await handleStartServer(port, bindLan ? '0.0.0.0' : '127.0.0.1').then(() => { status.status = true status.message = '' - status.address = `http://localhost${port == 80 ? '' : ':' + port}` + let address = ['127.0.0.1'] + if (bindLan) address = [...address, ...getAddress()] + status.address = address.join(', ') }).catch(err => { console.log(err) status.status = false diff --git a/src/main/modules/sync/client/client.ts b/src/main/modules/sync/client/client.ts index 97dbeb27..1cecacfb 100644 --- a/src/main/modules/sync/client/client.ts +++ b/src/main/modules/sync/client/client.ts @@ -6,10 +6,11 @@ import { callObj } from './sync' // import registerSyncListHandler from './syncList' import log from '../log' import { dateFormat } from '@common/utils/common' -import { aesEncrypt, getAddress } from '../utils' +import { aesEncrypt } from '../utils' import { sendClientStatus } from '@main/modules/winMain' import { createMsg2call } from 'message2call' import { SYNC_CLOSE_CODE, SYNC_CODE } from '@common/constants_sync' +import { getAddress } from '@common/utils/nodejs' let status: LX.Sync.ClientStatus = { status: false, diff --git a/src/main/modules/sync/server/server/server.ts b/src/main/modules/sync/server/server/server.ts index 037cc592..ea53b3ab 100644 --- a/src/main/modules/sync/server/server/server.ts +++ b/src/main/modules/sync/server/server/server.ts @@ -2,7 +2,6 @@ import http, { type IncomingMessage } from 'node:http' import { WebSocketServer } from 'ws' import { registerLocalSyncEvent, callObj, sync, unregisterLocalSyncEvent } from './sync' import { authCode, authConnect } from './auth' -import { getAddress } from '../../utils' import { SYNC_CLOSE_CODE, SYNC_CODE } from '@common/constants_sync' import { getUserSpace, releaseUserSpace, getServerId, initServerInfo } from '../user' import { createMsg2call } from 'message2call' @@ -11,6 +10,7 @@ import { sendServerStatus } from '@main/modules/winMain' import { decryptMsg, encryptMsg, generateCode as handleGenerateCode } from '../utils/tools' import migrateData from '../../migrate' import type { Socket } from 'node:net' +import { getAddress } from '@common/utils/nodejs' let status: LX.Sync.ServerStatus = { diff --git a/src/main/modules/sync/server/utils/tools.ts b/src/main/modules/sync/server/utils/tools.ts index 3a18faf8..eb1a9694 100644 --- a/src/main/modules/sync/server/utils/tools.ts +++ b/src/main/modules/sync/server/utils/tools.ts @@ -1,4 +1,3 @@ -import { networkInterfaces } from 'node:os' import { createCipheriv, createDecipheriv, publicEncrypt, privateDecrypt, constants } from 'node:crypto' // import { join } from 'node:path' import zlib from 'node:zlib' @@ -8,23 +7,6 @@ import type http from 'node:http' // import { getUserName } from '../user/data' // import { saveClientKeyInfo } from './data' -export const getAddress = (): string[] => { - const nets = networkInterfaces() - const results: string[] = [] - // console.log(nets) - - for (const interfaceInfos of Object.values(nets)) { - if (!interfaceInfos) continue - // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses - for (const interfaceInfo of interfaceInfos) { - if (interfaceInfo.family === 'IPv4' && !interfaceInfo.internal) { - results.push(interfaceInfo.address) - } - } - } - return results -} - export const generateCode = (): string => { return Math.random().toString().substring(2, 8) } diff --git a/src/main/modules/sync/utils.ts b/src/main/modules/sync/utils.ts index 444477b4..4ac392d2 100644 --- a/src/main/modules/sync/utils.ts +++ b/src/main/modules/sync/utils.ts @@ -1,28 +1,10 @@ import { createCipheriv, createDecipheriv, publicEncrypt, privateDecrypt, constants } from 'node:crypto' -import os, { networkInterfaces } from 'node:os' +import os from 'node:os' import fs from 'node:fs' import zlib from 'node:zlib' import cp from 'node:child_process' -export const getAddress = (): string[] => { - const nets = networkInterfaces() - const results: string[] = [] - // console.log(nets) - - for (const interfaceInfos of Object.values(nets)) { - if (!interfaceInfos) continue - // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses - for (const interfaceInfo of interfaceInfos) { - if (interfaceInfo.family === 'IPv4' && !interfaceInfo.internal) { - results.push(interfaceInfo.address) - } - } - } - return results -} - - // https://stackoverflow.com/a/75309339 export const getComputerName = () => { let name: string | undefined @@ -40,6 +22,7 @@ export const getComputerName = () => { try { name = cp.execSync('hostnamectl --pretty').toString().trim() } catch {} + break } if (!name) name = os.hostname() return name diff --git a/src/renderer/views/Setting/components/SettingOpenAPI.vue b/src/renderer/views/Setting/components/SettingOpenAPI.vue index eafbc5fe..623b89b7 100644 --- a/src/renderer/views/Setting/components/SettingOpenAPI.vue +++ b/src/renderer/views/Setting/components/SettingOpenAPI.vue @@ -6,7 +6,9 @@ dd.gap-top 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') }} + span.select {{ openAPI.address }} .p.small(v-if="openAPI.message") {{ openAPI.message }} .p .p.small {{ $t('setting__open_api_port') }}