feat: add LAN access configuration for desktop app, allowing users to enable public API access with corresponding UI updates and translations

This commit is contained in:
wizardchen
2026-04-11 16:34:40 +08:00
committed by lyingbug
parent d99e2cbac3
commit dfaaa62f14
14 changed files with 421 additions and 558 deletions
+12
View File
@@ -1987,6 +1987,18 @@ export default {
desktopPortSaved: 'Saved. Restart the app for it to take effect.',
desktopPortSaveFailed: 'Save failed',
desktopPortInvalid: 'Enter a port between 0 and 65535',
desktopBindPublicLabel: 'Allow LAN access to the API',
desktopBindPublicDescription:
'When enabled, the embedded server listens on 0.0.0.0 so other devices on your LAN can reach the API via the address below. This increases exposure—use host firewalls and routing rules as needed. Restart the app after changing this.',
desktopBindPublicSaved: 'Saved. Restart the app for it to take effect.',
desktopBindPublicSaveFailed: 'Save failed',
lanUrlLabel: 'LAN API base URL',
lanUrlDescription:
'Shown after “Allow LAN access” is on and the app has restarted. If the IP is wrong, replace the host with your machine’s IPv4 from system network settings.',
lanUrlCopyTitle: 'Copy LAN API URL',
lanUrlCopySuccess: 'LAN API URL copied',
lanUrlUnavailable:
'The API is listening on all interfaces, but no LAN IPv4 could be detected automatically. Find your IPv4 in network settings and build http://YOUR_IP:PORT/api/v1 manually.',
}
},
system: {
+13
View File
@@ -1435,8 +1435,21 @@ export default {
desktopPortSaved: "저장되었습니다. 적용하려면 앱을 다시 시작하세요.",
desktopPortSaveFailed: "저장 실패",
desktopPortInvalid: "0~65535 사이의 포트를 입력하세요",
desktopBindPublicLabel: "Allow LAN access to the API",
desktopBindPublicDescription:
"When enabled, the embedded server listens on 0.0.0.0 so other devices on your LAN can reach the API via the address below. This increases exposure—use host firewalls and routing rules as needed. Restart the app after changing this.",
desktopBindPublicSaved: "Saved. Restart the app for it to take effect.",
desktopBindPublicSaveFailed: "Save failed",
lanUrlLabel: "LAN API base URL",
lanUrlDescription:
"Shown after \"Allow LAN access\" is on and the app has restarted. If the IP is wrong, replace the host with your machine IPv4 from system network settings.",
lanUrlCopyTitle: "Copy LAN API URL",
lanUrlCopySuccess: "LAN API URL copied",
lanUrlUnavailable:
"The API is listening on all interfaces, but no LAN IPv4 could be detected automatically. Find your IPv4 in network settings and build http://YOUR_IP:PORT/api/v1 manually.",
},
},
system: {
title: "시스템 정보",
sectionDescription: "시스템 버전 정보 및 사용자 계정 설정 보기",
+12
View File
@@ -1282,6 +1282,18 @@ export default {
desktopPortSaved: 'Сохранено. Перезапустите приложение, чтобы применить.',
desktopPortSaveFailed: 'Не удалось сохранить',
desktopPortInvalid: 'Введите порт от 0 до 65535',
desktopBindPublicLabel: 'Разрешить доступ к API в LAN',
desktopBindPublicDescription:
'Встроенный сервер слушает 0.0.0.0 — другие устройства в локальной сети смогут обращаться к API по адресу ниже. Риски доступа выше; настройте брандмауэр и маршрутизацию. После изменения перезапустите приложение.',
desktopBindPublicSaved: 'Сохранено. Перезапустите приложение, чтобы применить.',
desktopBindPublicSaveFailed: 'Не удалось сохранить',
lanUrlLabel: 'Базовый URL API для LAN',
lanUrlDescription:
'Отображается после включения доступа в LAN и перезапуска. Если IP неверный, подставьте IPv4 из сетевых настроек системы.',
lanUrlCopyTitle: 'Копировать LAN URL API',
lanUrlCopySuccess: 'LAN URL API скопирован',
lanUrlUnavailable:
'API слушает все интерфейсы, но LAN IPv4 не удалось определить автоматически. Узнайте IPv4 в настройках сети и соберите http://ВАШ_IP:ПОРТ/api/v1 вручную.',
}
},
system: {
+12
View File
@@ -1429,6 +1429,18 @@ export default {
desktopPortSaved: "已保存,请重启应用后生效",
desktopPortSaveFailed: "保存失败",
desktopPortInvalid: "请输入 0–65535 之间的端口",
desktopBindPublicLabel: "允许局域网访问 API",
desktopBindPublicDescription:
"开启后内嵌服务监听 0.0.0.0,同一局域网内其他设备可通过下方地址调用 API。存在数据暴露风险,请在防火墙与路由侧自行做好访问控制;修改后需重启应用。",
desktopBindPublicSaved: "已保存,请重启应用后生效",
desktopBindPublicSaveFailed: "保存失败",
lanUrlLabel: "局域网 API 地址",
lanUrlDescription:
"在已开启「允许局域网访问」且应用已重启后显示;若 IP 不准确,请在本机网络设置中查看实际 IPv4 后替换主机部分。",
lanUrlCopyTitle: "复制局域网 API 地址",
lanUrlCopySuccess: "局域网 API 地址已复制",
lanUrlUnavailable:
"当前已监听所有网卡,但未能自动识别本机 IPv4。请在本机网络设置中查看局域网地址,并手动拼接为 http://你的IP:端口/api/v1。",
},
},
system: {
+174 -27
View File
@@ -81,28 +81,69 @@
</div>
</div>
<!-- Desktop (Wails): fixed local API port for Chrome extension etc. -->
<div v-if="showDesktopPortSetting" class="setting-row">
<div class="setting-info">
<label>{{ $t('tenant.api.desktopPortLabel') }}</label>
<p class="desc">{{ $t('tenant.api.desktopPortDescription') }}</p>
</div>
<div class="setting-control">
<div class="api-key-control">
<div class="desktop-port-input-wrap">
<t-input-number
v-model="desktopPortInput"
:min="0"
:max="65535"
theme="normal"
/>
<!-- Desktop (Wails): fixed local API port + optional LAN/public listen -->
<template v-if="showDesktopPortSetting || showDesktopBindPublicSetting">
<div v-if="showDesktopPortSetting" class="setting-row">
<div class="setting-info">
<label>{{ $t('tenant.api.desktopPortLabel') }}</label>
<p class="desc">{{ $t('tenant.api.desktopPortDescription') }}</p>
</div>
<div class="setting-control">
<div class="api-key-control">
<div class="desktop-port-input-wrap">
<t-input-number
v-model="desktopPortInput"
:min="0"
:max="65535"
theme="normal"
/>
</div>
<t-button size="small" variant="text" @click="saveDesktopPort">
{{ $t('tenant.api.desktopPortSave') }}
</t-button>
</div>
<t-button size="small" variant="text" @click="saveDesktopPort">
{{ $t('tenant.api.desktopPortSave') }}
</t-button>
</div>
</div>
</div>
<div v-if="showDesktopBindPublicSetting" class="setting-row">
<div class="setting-info">
<label>{{ $t('tenant.api.desktopBindPublicLabel') }}</label>
<p class="desc">{{ $t('tenant.api.desktopBindPublicDescription') }}</p>
</div>
<div class="setting-control desktop-bind-public-control">
<t-switch v-model="desktopBindPublicInput" @change="onDesktopBindPublicChange" />
</div>
</div>
<div v-if="wailsApiLanBaseURL" class="setting-row">
<div class="setting-info">
<label>{{ $t('tenant.api.lanUrlLabel') }}</label>
<p class="desc">{{ $t('tenant.api.lanUrlDescription') }}</p>
</div>
<div class="setting-control">
<div class="api-key-control">
<t-input
:model-value="wailsApiLanBaseURL"
readonly
type="text"
style="width: 100%; font-family: monospace; font-size: 12px;"
/>
<t-button
size="small"
variant="text"
@click="copyLanApiUrl"
:title="$t('tenant.api.lanUrlCopyTitle')"
>
<t-icon name="file-copy" />
</t-button>
</div>
</div>
</div>
<div v-if="showLanUrlUnavailableHint" class="setting-row lan-url-hint-row">
<t-alert theme="warning" :message="$t('tenant.api.lanUrlUnavailable')" />
</div>
</template>
<!-- API docs -->
<div class="setting-row">
@@ -187,7 +228,11 @@ const showApiKey = ref(false)
/** WeKnora Lite (Wails): real API origin is loopback + dynamic port, not window.location.origin */
const wailsApiBaseURL = ref<string | null>(null)
const showDesktopPortSetting = ref(false)
const showDesktopBindPublicSetting = ref(false)
const desktopPortInput = ref<number | undefined>(0)
const desktopBindPublicInput = ref(false)
const wailsApiLanBaseURL = ref<string | null>(null)
const desktopListenPublicActive = ref(false)
// Computed
const displayApiKey = computed(() => {
@@ -202,6 +247,13 @@ const displayApiKey = computed(() => {
return masked
})
const showLanUrlUnavailableHint = computed(
() =>
showDesktopBindPublicSetting.value &&
desktopListenPublicActive.value &&
!wailsApiLanBaseURL.value
)
const apiBaseUrlDisplay = computed(() => {
if (wailsApiBaseURL.value) {
return wailsApiBaseURL.value
@@ -217,12 +269,17 @@ const apiBaseUrlDisplay = computed(() => {
type WeKnoraDesktopWindow = Window & {
__WEKNORA_API_BASE__?: string
__WEKNORA_API_LAN_BASE__?: string
go?: {
main?: {
App?: {
GetAPIBaseURL?: () => Promise<string> | string
GetAPILanBaseURL?: () => Promise<string> | string
GetDesktopHTTPPortSetting?: () => Promise<number> | number
GetDesktopHTTPBindPublicSetting?: () => Promise<boolean> | boolean
GetDesktopListenPublicActive?: () => Promise<boolean> | boolean
SetDesktopHTTPPortSetting?: (port: number) => Promise<void> | void
SetDesktopHTTPBindPublicSetting?: (v: boolean) => Promise<void> | void
}
}
}
@@ -234,6 +291,7 @@ async function tryLoadWailsApiBaseURL() {
const injected = win.__WEKNORA_API_BASE__
if (typeof injected === 'string' && injected.trim()) {
wailsApiBaseURL.value = injected.trim().replace(/\/$/, '')
await tryLoadWailsLanHints(win)
return
}
const fn = win.go?.main?.App?.GetAPIBaseURL
@@ -246,10 +304,38 @@ async function tryLoadWailsApiBaseURL() {
} catch {
/* binding error */
}
await tryLoadWailsLanHints(win)
return
}
await new Promise((r) => setTimeout(r, 50))
}
await tryLoadWailsLanHints(win)
}
async function tryLoadWailsLanHints(win: WeKnoraDesktopWindow) {
const injectedLan = win.__WEKNORA_API_LAN_BASE__
if (typeof injectedLan === 'string' && injectedLan.trim()) {
wailsApiLanBaseURL.value = injectedLan.trim().replace(/\/$/, '')
}
const fnLan = win.go?.main?.App?.GetAPILanBaseURL
if (typeof fnLan === 'function' && !wailsApiLanBaseURL.value) {
try {
const raw = await Promise.resolve(fnLan())
if (typeof raw === 'string' && raw.trim()) {
wailsApiLanBaseURL.value = raw.trim().replace(/\/$/, '')
}
} catch {
/* binding error */
}
}
const fnAct = win.go?.main?.App?.GetDesktopListenPublicActive
if (typeof fnAct === 'function') {
try {
desktopListenPublicActive.value = !!(await Promise.resolve(fnAct()))
} catch {
desktopListenPublicActive.value = false
}
}
}
function desktopPortBindingsAvailable(win: WeKnoraDesktopWindow) {
@@ -257,15 +343,47 @@ function desktopPortBindingsAvailable(win: WeKnoraDesktopWindow) {
return typeof app?.GetDesktopHTTPPortSetting === 'function' && typeof app?.SetDesktopHTTPPortSetting === 'function'
}
async function loadDesktopPortPrefs() {
function desktopBindPublicBindingsAvailable(win: WeKnoraDesktopWindow) {
const app = win.go?.main?.App
return (
typeof app?.GetDesktopHTTPBindPublicSetting === 'function' &&
typeof app?.SetDesktopHTTPBindPublicSetting === 'function'
)
}
async function loadDesktopApiPrefs() {
const win = window as WeKnoraDesktopWindow
if (!desktopPortBindingsAvailable(win)) return
showDesktopPortSetting.value = true
if (desktopPortBindingsAvailable(win)) {
showDesktopPortSetting.value = true
try {
const p = await Promise.resolve(win.go!.main!.App!.GetDesktopHTTPPortSetting!())
desktopPortInput.value = typeof p === 'number' ? p : 0
} catch {
desktopPortInput.value = 0
}
}
if (desktopBindPublicBindingsAvailable(win)) {
showDesktopBindPublicSetting.value = true
try {
const b = await Promise.resolve(win.go!.main!.App!.GetDesktopHTTPBindPublicSetting!())
desktopBindPublicInput.value = !!b
} catch {
desktopBindPublicInput.value = false
}
}
}
const onDesktopBindPublicChange = async (value: boolean) => {
const v = value === true
const win = window as WeKnoraDesktopWindow
const fn = win.go?.main?.App?.SetDesktopHTTPBindPublicSetting
if (typeof fn !== 'function') return
try {
const p = await Promise.resolve(win.go!.main!.App!.GetDesktopHTTPPortSetting!())
desktopPortInput.value = typeof p === 'number' ? p : 0
} catch {
desktopPortInput.value = 0
await Promise.resolve(fn(v))
MessagePlugin.success(t('tenant.api.desktopBindPublicSaved'))
} catch (err: unknown) {
MessagePlugin.error(err instanceof Error ? err.message : t('tenant.api.desktopBindPublicSaveFailed'))
desktopBindPublicInput.value = !v
}
}
@@ -342,6 +460,22 @@ const copyApiKey = async () => {
}
}
const copyLanApiUrl = async () => {
const text = wailsApiLanBaseURL.value
if (!text) return
try {
if (navigator.clipboard && navigator.clipboard.writeText) {
await navigator.clipboard.writeText(text)
} else {
fallbackCopyText(text)
}
MessagePlugin.success(t('tenant.api.lanUrlCopySuccess'))
} catch {
fallbackCopyText(text)
MessagePlugin.success(t('tenant.api.lanUrlCopySuccess'))
}
}
const copyApiUrl = async () => {
const text = apiBaseUrlDisplay.value
try {
@@ -378,7 +512,7 @@ const formatDate = (dateStr: string | undefined) => {
// Lifecycle
onMounted(async () => {
await tryLoadWailsApiBaseURL()
await loadDesktopPortPrefs()
await loadDesktopApiPrefs()
loadInfo()
})
</script>
@@ -531,5 +665,18 @@ onMounted(async () => {
font-size: 12px;
}
}
.desktop-bind-public-control {
padding-top: 4px;
}
.lan-url-hint-row {
padding-top: 0;
border-bottom: 1px solid var(--td-component-stroke);
:deep(.t-alert) {
width: 100%;
}
}
</style>
+8
View File
@@ -3,6 +3,14 @@
export function GetAPIBaseURL():Promise<string>;
export function GetAPILanBaseURL():Promise<string>;
export function GetDesktopHTTPBindPublicSetting():Promise<boolean>;
export function GetDesktopHTTPPortSetting():Promise<number>;
export function GetDesktopListenPublicActive():Promise<boolean>;
export function SetDesktopHTTPBindPublicSetting(arg1:boolean):Promise<void>;
export function SetDesktopHTTPPortSetting(arg1:number):Promise<void>;
+16
View File
@@ -6,10 +6,26 @@ export function GetAPIBaseURL() {
return window['go']['main']['App']['GetAPIBaseURL']();
}
export function GetAPILanBaseURL() {
return window['go']['main']['App']['GetAPILanBaseURL']();
}
export function GetDesktopHTTPBindPublicSetting() {
return window['go']['main']['App']['GetDesktopHTTPBindPublicSetting']();
}
export function GetDesktopHTTPPortSetting() {
return window['go']['main']['App']['GetDesktopHTTPPortSetting']();
}
export function GetDesktopListenPublicActive() {
return window['go']['main']['App']['GetDesktopListenPublicActive']();
}
export function SetDesktopHTTPBindPublicSetting(arg1) {
return window['go']['main']['App']['SetDesktopHTTPBindPublicSetting'](arg1);
}
export function SetDesktopHTTPPortSetting(arg1) {
return window['go']['main']['App']['SetDesktopHTTPPortSetting'](arg1);
}