@sunshine-star/dsh-access-pc 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,8 +16,9 @@ PC 客户端外壳:把 hub 的控制台(即 dsh web 本体)装进一个**
16
16
  ```bash
17
17
  cd clients/pc-electron
18
18
  npm install # 首次,下载 Electron(国内可用 ELECTRON_MIRROR 镜像)
19
- npm start # 打开窗口
20
- npm start -- --hub https://xrzhang.online
19
+ npm start # 打开窗口(默认 hub: http://127.0.0.1:8080)
20
+ npm start -- --hub http://10.168.1.185:8080
21
+ npm start -- --settings # 直接打开 Hub 地址设置
21
22
  node_modules/.bin/electron . --check # 只验 hub 可达,不开窗口
22
23
  ```
23
24
 
@@ -54,6 +55,16 @@ dsh-access-pc --create-shortcut --desktop # 只建桌面
54
55
  包内含 Electron(约 100MB);国内可设
55
56
  `ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/` 加速下载。
56
57
 
58
+ ## 设置 Hub 地址
59
+
60
+ - 进入界面后按 **Ctrl+,**(macOS 为 Cmd+,)或菜单“导航 → 设置 Hub 地址…”;
61
+ - 也可以在“右键菜单 → 设置 Hub 地址…”里打开;
62
+ - 启动参数 `--settings` 会在打开界面时直接弹出设置;
63
+ - 设置窗口会从配置文件加载当前值(首次默认 `http://127.0.0.1:8080`),保存后写入配置并立即连接新地址;
64
+ - hub 加载失败时,错误对话框也提供“设置 Hub 地址…”按钮。
65
+
66
+ 配置文件:Windows `%APPDATA%\dsh-access\config.json`,Linux `~/.config/dsh-access/config.json`。
67
+
57
68
  ## 窗口行为
58
69
 
59
70
  - **默认单窗口**:点“打开”在同一个窗口加载 DSH 页面;主机页**右侧边缘有悬浮“←”按钮**,点击回主机列表
package/main.js CHANGED
@@ -9,13 +9,13 @@
9
9
  // - 支持 Windows / Linux / macOS;
10
10
  // - 支持 --hub / --check / --version,并复用 ~/.dsh-access 或
11
11
  // %APPDATA%\dsh-access\config.json 里的配置(与 Go 客户端同一份)。
12
- const { app, BrowserWindow, Menu, clipboard, shell, dialog } = require('electron')
12
+ const { app, BrowserWindow, Menu, clipboard, ipcMain, shell, dialog } = require('electron')
13
13
  const fs = require('node:fs')
14
14
  const path = require('node:path')
15
15
  const os = require('node:os')
16
16
 
17
17
  const APP_NAME = 'dsh-access-pc'
18
- const DEFAULT_HUB = 'https://xrzhang.online'
18
+ const DEFAULT_HUB = 'http://127.0.0.1:8080'
19
19
  const DEFAULT_WINDOW = '1440,900'
20
20
 
21
21
  // 虚拟机/远程桌面/受限显卡环境下,硬件加速常常导致进程在 ready 前崩溃。
@@ -130,8 +130,58 @@ if (args.version) {
130
130
  if (args['multi-window'] === true) cfg.singleWindow = false
131
131
  saveConfig(file, cfg)
132
132
 
133
- const hubPrefix = cfg.hub.replace(/\/$/, '')
133
+ const hubPrefix = () => cfg.hub.replace(/\/$/, '')
134
134
  const singleWindow = cfg.singleWindow === true
135
+ let settingsWindow = null
136
+ function openSettings() {
137
+ if (settingsWindow && !settingsWindow.isDestroyed()) {
138
+ settingsWindow.focus()
139
+ return
140
+ }
141
+ settingsWindow = new BrowserWindow({
142
+ width: 480,
143
+ height: 250,
144
+ parent: mainWindow,
145
+ modal: false,
146
+ resizable: false,
147
+ minimizable: false,
148
+ maximizable: false,
149
+ fullscreenable: false,
150
+ title: '设置 Hub 地址',
151
+ backgroundColor: '#0b1220',
152
+ autoHideMenuBar: true,
153
+ webPreferences: {
154
+ preload: path.join(__dirname, 'settings-preload.js'),
155
+ contextIsolation: true,
156
+ nodeIntegration: false,
157
+ sandbox: true,
158
+ },
159
+ })
160
+ void settingsWindow.loadFile(path.join(__dirname, 'settings.html'))
161
+ settingsWindow.on('closed', () => { settingsWindow = null })
162
+ }
163
+
164
+ ipcMain.handle('settings:get', () => cfg.hub)
165
+ ipcMain.handle('settings:save', (_event, value) => {
166
+ const hub = String(value ?? '').trim().replace(/\/$/, '')
167
+ let parsed
168
+ try {
169
+ parsed = new URL(hub)
170
+ } catch {
171
+ throw new Error('地址格式不正确,需要 http:// 或 https://')
172
+ }
173
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
174
+ throw new Error('只支持 http/https 地址')
175
+ }
176
+ cfg.hub = hub
177
+ saveConfig(file, cfg)
178
+ if (mainWindow && !mainWindow.isDestroyed()) void mainWindow.loadURL(cfg.hub)
179
+ if (settingsWindow && !settingsWindow.isDestroyed()) settingsWindow.close()
180
+ return true
181
+ })
182
+ ipcMain.on('settings:cancel', () => {
183
+ if (settingsWindow && !settingsWindow.isDestroyed()) settingsWindow.close()
184
+ })
135
185
 
136
186
  // 站内链接:默认新窗口(独立于打开者);--single-window 时在同一窗口加载。
137
187
  const HOST_BUTTON_ID = '__dsh_access_back'
@@ -162,6 +212,7 @@ if (args.version) {
162
212
  app.on('web-contents-created', (_event, contents) => {
163
213
  // 右键菜单:导航、链接、编辑、检查元素。
164
214
  contents.on('context-menu', (_event, params) => {
215
+ if (contents.getURL().startsWith('file://')) return
165
216
  const window = BrowserWindow.fromWebContents(contents)
166
217
  if (!window || window.isDestroyed()) return
167
218
  const items = [
@@ -176,6 +227,7 @@ if (args.version) {
176
227
  enabled: Boolean(contents.navigationHistory?.canGoBack?.()),
177
228
  click: () => contents.navigationHistory?.goBack?.(),
178
229
  },
230
+ { label: '设置 Hub 地址…', click: () => openSettings() },
179
231
  { label: '刷新', click: () => contents.reload() },
180
232
  { type: 'separator' },
181
233
  ]
@@ -205,12 +257,18 @@ if (args.version) {
205
257
  contents.on('did-navigate-in-page', () => installHostBackButton(contents))
206
258
  // 单窗口模式下没有浏览器工具栏,Alt+← 用来回主机列表。
207
259
  contents.on('before-input-event', (event, input) => {
208
- if (input.type !== 'keyDown' || !input.alt || input.key !== 'ArrowLeft') return
260
+ if (input.type !== 'keyDown') return
261
+ if ((input.control || input.meta) && input.key === ',') {
262
+ event.preventDefault()
263
+ openSettings()
264
+ return
265
+ }
266
+ if (!input.alt || input.key !== 'ArrowLeft') return
209
267
  event.preventDefault()
210
268
  if (contents.navigationHistory?.canGoBack?.()) contents.navigationHistory.goBack()
211
269
  })
212
270
  contents.setWindowOpenHandler(({ url }) => {
213
- if (!url.startsWith(hubPrefix)) {
271
+ if (!url.startsWith(hubPrefix())) {
214
272
  void shell.openExternal(url)
215
273
  return { action: 'deny' }
216
274
  }
@@ -259,7 +317,11 @@ if (args.version) {
259
317
  title: '无法打开 hub',
260
318
  message: `加载失败:${description}(${code})`,
261
319
  detail: `${url}\n\n请检查网络、hub 地址与证书。配置文件:${file}`,
262
- buttons: ['知道了'],
320
+ buttons: ['设置 Hub 地址…', '知道了'],
321
+ defaultId: 0,
322
+ cancelId: 1,
323
+ }).then(result => {
324
+ if (result.response === 0) openSettings()
263
325
  })
264
326
  console.error(`[${APP_NAME}] 加载失败 ${code} ${description} ${url}`)
265
327
  })
@@ -277,6 +339,7 @@ if (args.version) {
277
339
  submenu: [
278
340
  { label: '返回', accelerator: 'Alt+Left', click: goBack },
279
341
  { label: '主机列表', accelerator: 'Alt+H', click: goHome },
342
+ { label: '设置 Hub 地址…', accelerator: 'CmdOrCtrl+,', click: openSettings },
280
343
  { type: 'separator' },
281
344
  { role: 'reload' },
282
345
  { role: 'forceReload' },
@@ -287,6 +350,7 @@ if (args.version) {
287
350
  }]))
288
351
 
289
352
  void mainWindow.loadURL(cfg.hub)
353
+ if (args.settings === true) openSettings()
290
354
  console.log(`[${APP_NAME}] 已打开 ${cfg.hub}(配置 ${file})`)
291
355
 
292
356
  app.on('window-all-closed', () => app.quit())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sunshine-star/dsh-access-pc",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "DSH Access PC client (Electron shell around the hub console)",
5
5
  "main": "main.js",
6
6
  "bin": {
@@ -8,6 +8,8 @@
8
8
  },
9
9
  "files": [
10
10
  "main.js",
11
+ "settings.html",
12
+ "settings-preload.js",
11
13
  "bin",
12
14
  "scripts/start.js",
13
15
  "scripts/doctor.js",
@@ -0,0 +1,7 @@
1
+ const { contextBridge, ipcRenderer } = require('electron')
2
+
3
+ contextBridge.exposeInMainWorld('settings', {
4
+ get: () => ipcRenderer.invoke('settings:get'),
5
+ save: hub => ipcRenderer.invoke('settings:save', hub),
6
+ cancel: () => ipcRenderer.send('settings:cancel'),
7
+ })
package/settings.html ADDED
@@ -0,0 +1,63 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>设置 Hub 地址</title>
6
+ <style>
7
+ :root { color-scheme: dark; }
8
+ * { box-sizing: border-box; }
9
+ body {
10
+ margin: 0; padding: 20px 22px;
11
+ font: 14px/1.6 -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif;
12
+ background: #0b1220; color: #e8ecf7;
13
+ }
14
+ h1 { margin: 0 0 4px; font-size: 16px; font-weight: 600; }
15
+ .hint { margin: 0 0 14px; color: #94a0bd; font-size: 12px; }
16
+ input {
17
+ width: 100%; padding: 9px 11px; border-radius: 8px;
18
+ border: 1px solid #3a4a75; background: #0a1226; color: #e8ecf7;
19
+ font-size: 14px; outline: none;
20
+ }
21
+ input:focus { border-color: #7c9cff; box-shadow: 0 0 0 3px rgba(124,156,255,.25); }
22
+ .err { min-height: 18px; margin: 6px 0 0; color: #ff8d8d; font-size: 12px; }
23
+ .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
24
+ button {
25
+ padding: 7px 16px; border-radius: 8px; border: 1px solid #27304a;
26
+ background: transparent; color: #e8ecf7; font-size: 13px; cursor: pointer;
27
+ }
28
+ button.primary { background: #7c9cff; border-color: #7c9cff; color: #08122b; font-weight: 600; }
29
+ button:hover { filter: brightness(1.08); }
30
+ </style>
31
+ </head>
32
+ <body>
33
+ <h1>Hub 地址</h1>
34
+ <p class="hint">例如 http://127.0.0.1:8080 或 https://hub.example.com</p>
35
+ <form id="form">
36
+ <input id="hub" type="text" spellcheck="false" autocomplete="off" placeholder="http://127.0.0.1:8080">
37
+ <p class="err" id="err"></p>
38
+ <div class="actions">
39
+ <button type="button" id="cancel">取消</button>
40
+ <button type="submit" class="primary">保存并连接</button>
41
+ </div>
42
+ </form>
43
+ <script>
44
+ const input = document.getElementById('hub')
45
+ const err = document.getElementById('err')
46
+ window.settings.get().then(value => {
47
+ input.value = value || ''
48
+ input.focus()
49
+ input.select()
50
+ })
51
+ document.getElementById('form').addEventListener('submit', async event => {
52
+ event.preventDefault()
53
+ err.textContent = ''
54
+ try {
55
+ await window.settings.save(input.value.trim())
56
+ } catch (error) {
57
+ err.textContent = error?.message || String(error)
58
+ }
59
+ })
60
+ document.getElementById('cancel').addEventListener('click', () => window.settings.cancel())
61
+ </script>
62
+ </body>
63
+ </html>