@sunshine-star/dsh-access-pc 0.1.1 → 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
 
@@ -38,9 +39,32 @@ dsh-access-pc --hub http://10.168.1.185:8080
38
39
  npx -y @sunshine-star/dsh-access-pc --hub http://10.168.1.185:8080
39
40
  ```
40
41
 
42
+ 安装时可顺便创建桌面图标(Windows 桌面 + 开始菜单;Linux 应用菜单 + 桌面):
43
+
44
+ ```bash
45
+ DSH_ACCESS_PC_SHORTCUT=1 npm i -g @sunshine-star/dsh-access-pc
46
+ ```
47
+
48
+ 装完也可以手动创建:
49
+
50
+ ```bash
51
+ dsh-access-pc --create-shortcut # 桌面 + 开始菜单
52
+ dsh-access-pc --create-shortcut --desktop # 只建桌面
53
+ ```
54
+
41
55
  包内含 Electron(约 100MB);国内可设
42
56
  `ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/` 加速下载。
43
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
+
44
68
  ## 窗口行为
45
69
 
46
70
  - **默认单窗口**:点“打开”在同一个窗口加载 DSH 页面;主机页**右侧边缘有悬浮“←”按钮**,点击回主机列表
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -8,6 +8,11 @@
8
8
  const { spawn } = require('node:child_process')
9
9
  const path = require('node:path')
10
10
 
11
+ if (process.argv.includes('--create-shortcut')) {
12
+ const { main } = require('../scripts/shortcut.js')
13
+ process.exit(main(process.argv.slice(2)))
14
+ }
15
+
11
16
  const electronPath = require('electron')
12
17
  const appDir = path.join(__dirname, '..')
13
18
  const env = { ...process.env }
package/main.js CHANGED
@@ -9,18 +9,19 @@
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 前崩溃。
22
22
  // 客户端只渲染一个网页,软件渲染足够,先关掉更稳。
23
23
  app.disableHardwareAcceleration()
24
+ if (process.platform === 'win32') app.setAppUserModelId('com.sunshine-star.dsh-access')
24
25
 
25
26
  /** 解析 --key value 形式的参数。 */
26
27
  function parseArgs(argv) {
@@ -129,8 +130,58 @@ if (args.version) {
129
130
  if (args['multi-window'] === true) cfg.singleWindow = false
130
131
  saveConfig(file, cfg)
131
132
 
132
- const hubPrefix = cfg.hub.replace(/\/$/, '')
133
+ const hubPrefix = () => cfg.hub.replace(/\/$/, '')
133
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
+ })
134
185
 
135
186
  // 站内链接:默认新窗口(独立于打开者);--single-window 时在同一窗口加载。
136
187
  const HOST_BUTTON_ID = '__dsh_access_back'
@@ -161,6 +212,7 @@ if (args.version) {
161
212
  app.on('web-contents-created', (_event, contents) => {
162
213
  // 右键菜单:导航、链接、编辑、检查元素。
163
214
  contents.on('context-menu', (_event, params) => {
215
+ if (contents.getURL().startsWith('file://')) return
164
216
  const window = BrowserWindow.fromWebContents(contents)
165
217
  if (!window || window.isDestroyed()) return
166
218
  const items = [
@@ -175,6 +227,7 @@ if (args.version) {
175
227
  enabled: Boolean(contents.navigationHistory?.canGoBack?.()),
176
228
  click: () => contents.navigationHistory?.goBack?.(),
177
229
  },
230
+ { label: '设置 Hub 地址…', click: () => openSettings() },
178
231
  { label: '刷新', click: () => contents.reload() },
179
232
  { type: 'separator' },
180
233
  ]
@@ -204,12 +257,18 @@ if (args.version) {
204
257
  contents.on('did-navigate-in-page', () => installHostBackButton(contents))
205
258
  // 单窗口模式下没有浏览器工具栏,Alt+← 用来回主机列表。
206
259
  contents.on('before-input-event', (event, input) => {
207
- 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
208
267
  event.preventDefault()
209
268
  if (contents.navigationHistory?.canGoBack?.()) contents.navigationHistory.goBack()
210
269
  })
211
270
  contents.setWindowOpenHandler(({ url }) => {
212
- if (!url.startsWith(hubPrefix)) {
271
+ if (!url.startsWith(hubPrefix())) {
213
272
  void shell.openExternal(url)
214
273
  return { action: 'deny' }
215
274
  }
@@ -241,6 +300,7 @@ if (args.version) {
241
300
  minWidth: 960,
242
301
  minHeight: 640,
243
302
  title: 'DSH Access',
303
+ icon: path.join(__dirname, 'assets', process.platform === 'win32' ? 'icon.ico' : 'icon.png'),
244
304
  backgroundColor: '#0b1220',
245
305
  autoHideMenuBar: true,
246
306
  webPreferences: {
@@ -257,7 +317,11 @@ if (args.version) {
257
317
  title: '无法打开 hub',
258
318
  message: `加载失败:${description}(${code})`,
259
319
  detail: `${url}\n\n请检查网络、hub 地址与证书。配置文件:${file}`,
260
- buttons: ['知道了'],
320
+ buttons: ['设置 Hub 地址…', '知道了'],
321
+ defaultId: 0,
322
+ cancelId: 1,
323
+ }).then(result => {
324
+ if (result.response === 0) openSettings()
261
325
  })
262
326
  console.error(`[${APP_NAME}] 加载失败 ${code} ${description} ${url}`)
263
327
  })
@@ -275,6 +339,7 @@ if (args.version) {
275
339
  submenu: [
276
340
  { label: '返回', accelerator: 'Alt+Left', click: goBack },
277
341
  { label: '主机列表', accelerator: 'Alt+H', click: goHome },
342
+ { label: '设置 Hub 地址…', accelerator: 'CmdOrCtrl+,', click: openSettings },
278
343
  { type: 'separator' },
279
344
  { role: 'reload' },
280
345
  { role: 'forceReload' },
@@ -285,6 +350,7 @@ if (args.version) {
285
350
  }]))
286
351
 
287
352
  void mainWindow.loadURL(cfg.hub)
353
+ if (args.settings === true) openSettings()
288
354
  console.log(`[${APP_NAME}] 已打开 ${cfg.hub}(配置 ${file})`)
289
355
 
290
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.1",
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,15 +8,21 @@
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",
16
+ "scripts/shortcut.js",
17
+ "scripts/postinstall.js",
18
+ "assets",
14
19
  "README.md"
15
20
  ],
16
21
  "scripts": {
17
22
  "start": "node scripts/start.js",
18
23
  "check": "node scripts/start.js --check",
19
- "doctor": "node scripts/doctor.js"
24
+ "doctor": "node scripts/doctor.js",
25
+ "postinstall": "node scripts/postinstall.js"
20
26
  },
21
27
  "dependencies": {
22
28
  "electron": "^33.2.0"
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * 可选:npm 全局安装时创建桌面/开始菜单快捷方式。
4
+ *
5
+ * 默认不创建,只提示;显式设置 DSH_ACCESS_PC_SHORTCUT=1 才创建:
6
+ * DSH_ACCESS_PC_SHORTCUT=1 npm i -g @sunshine-star/dsh-access-pc
7
+ * 设 0 或 DSH_ACCESS_PC_NO_SHORTCUT=1 可关闭提示。
8
+ */
9
+ const globalInstall = process.env.npm_config_global === 'true'
10
+ if (!globalInstall) process.exit(0)
11
+ if (process.env.DSH_ACCESS_PC_SHORTCUT === '0' || process.env.DSH_ACCESS_PC_NO_SHORTCUT === '1') process.exit(0)
12
+
13
+ if (process.env.DSH_ACCESS_PC_SHORTCUT === '1' || process.env.DSH_ACCESS_PC_SHORTCUT === 'desktop') {
14
+ try {
15
+ const { createShortcuts } = require('./shortcut.js')
16
+ const created = createShortcuts({ desktop: true, menu: true })
17
+ for (const item of created) console.log(`[dsh-access-pc] 已创建 ${item}`)
18
+ } catch (error) {
19
+ console.error(`[dsh-access-pc] 创建快捷方式失败:${error.message}`)
20
+ console.error('[dsh-access-pc] 可稍后运行:dsh-access-pc --create-shortcut')
21
+ }
22
+ } else {
23
+ console.log('[dsh-access-pc] 想创建桌面图标:dsh-access-pc --create-shortcut(安装时设 DSH_ACCESS_PC_SHORTCUT=1 可自动创建)')
24
+ }
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * 创建桌面 / 开始菜单快捷方式(无需管理员权限)。
4
+ *
5
+ * 用法:
6
+ * dsh-access-pc --create-shortcut # 桌面 + 开始菜单
7
+ * dsh-access-pc --create-shortcut --desktop # 只建桌面
8
+ *
9
+ * 也支持安装时自动创建:DSH_ACCESS_PC_SHORTCUT=1 npm i -g ...
10
+ */
11
+ const { spawnSync } = require('node:child_process')
12
+ const fs = require('node:fs')
13
+ const os = require('node:os')
14
+ const path = require('node:path')
15
+
16
+ const APP_NAME = 'DSH Access'
17
+ const appDir = path.join(__dirname, '..')
18
+ const iconIco = path.join(appDir, 'assets', 'icon.ico')
19
+ const iconPng = path.join(appDir, 'assets', 'icon.png')
20
+
21
+ function electronBinary() {
22
+ try {
23
+ return require('electron')
24
+ } catch (error) {
25
+ throw new Error(`找不到 Electron:${error.message}`)
26
+ }
27
+ }
28
+
29
+ function desktopDir() {
30
+ return process.env.DSH_ACCESS_SHORTCUT_DIR || path.join(os.homedir(), 'Desktop')
31
+ }
32
+
33
+ function startMenuDir() {
34
+ return process.env.DSH_ACCESS_START_MENU_DIR ||
35
+ path.join(process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming'),
36
+ 'Microsoft', 'Windows', 'Start Menu', 'Programs')
37
+ }
38
+
39
+ function createWindowsLink(lnkPath) {
40
+ fs.mkdirSync(path.dirname(lnkPath), { recursive: true })
41
+ const script = `
42
+ $ws = New-Object -ComObject WScript.Shell
43
+ $sc = $ws.CreateShortcut($env:SHORTCUT_LNK)
44
+ $sc.TargetPath = $env:SHORTCUT_TARGET
45
+ $sc.Arguments = $env:SHORTCUT_ARGS
46
+ $sc.WorkingDirectory = $env:SHORTCUT_WD
47
+ $sc.IconLocation = $env:SHORTCUT_ICON
48
+ $sc.Description = 'DSH Access - remote DSH client'
49
+ $sc.Save()
50
+ `
51
+ const env = {
52
+ ...process.env,
53
+ SHORTCUT_LNK: lnkPath,
54
+ SHORTCUT_TARGET: electronBinary(),
55
+ SHORTCUT_ARGS: `"${appDir}"`,
56
+ SHORTCUT_WD: appDir,
57
+ SHORTCUT_ICON: iconIco,
58
+ }
59
+ const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
60
+ env,
61
+ encoding: 'utf8',
62
+ })
63
+ if (result.status !== 0) throw new Error((result.stderr || result.stdout || '创建快捷方式失败').trim())
64
+ return lnkPath
65
+ }
66
+
67
+ function desktopEntry() {
68
+ const electron = electronBinary()
69
+ return `[Desktop Entry]
70
+ Type=Application
71
+ Name=${APP_NAME}
72
+ Comment=Remote DSH client
73
+ Exec="${electron}" "${appDir}"
74
+ Icon=${iconPng}
75
+ Terminal=false
76
+ Categories=Network;RemoteAccess;
77
+ StartupWMClass=dsh-access-pc
78
+ `
79
+ }
80
+
81
+ function createLinuxEntry(targetPath) {
82
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true })
83
+ fs.writeFileSync(targetPath, desktopEntry())
84
+ fs.chmodSync(targetPath, 0o755)
85
+ return targetPath
86
+ }
87
+
88
+ /**
89
+ * @param {{ desktop?: boolean, menu?: boolean }} options
90
+ * @returns {string[]} 创建的路径。
91
+ */
92
+ function createShortcuts({ desktop = true, menu = true } = {}) {
93
+ const created = []
94
+ if (process.platform === 'win32') {
95
+ if (desktop) created.push(createWindowsLink(path.join(desktopDir(), `${APP_NAME}.lnk`)))
96
+ if (menu) created.push(createWindowsLink(path.join(startMenuDir(), `${APP_NAME}.lnk`)))
97
+ return created
98
+ }
99
+ if (process.platform === 'linux') {
100
+ const base = path.join(os.homedir(), '.local', 'share', 'applications')
101
+ const entry = path.join(base, 'dsh-access-pc.desktop')
102
+ created.push(createLinuxEntry(entry))
103
+ if (desktop) {
104
+ const desktopPath = path.join(desktopDir(), 'dsh-access-pc.desktop')
105
+ try {
106
+ fs.copyFileSync(entry, desktopPath)
107
+ fs.chmodSync(desktopPath, 0o755)
108
+ created.push(desktopPath)
109
+ } catch {
110
+ // 没有桌面目录就只留应用菜单项。
111
+ }
112
+ }
113
+ return created
114
+ }
115
+ throw new Error(`暂不支持 ${process.platform} 的快捷方式;macOS 可把 Electron 应用拖入 Applications`)
116
+ }
117
+
118
+ function main(argv = process.argv.slice(2)) {
119
+ const desktop = argv.includes('--desktop') || !argv.includes('--menu-only')
120
+ const menu = !argv.includes('--desktop')
121
+ try {
122
+ const created = createShortcuts({ desktop, menu })
123
+ for (const item of created) console.log(`已创建 ${item}`)
124
+ return 0
125
+ } catch (error) {
126
+ console.error(`[${APP_NAME}] ${error.message}`)
127
+ return 1
128
+ }
129
+ }
130
+
131
+ if (require.main === module) process.exit(main())
132
+
133
+ module.exports = { createShortcuts, main }
@@ -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>