@sususgh/gamer-dsh-plugin 1.0.4 → 1.0.5

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.
@@ -1,4 +1,5 @@
1
1
  import http from 'node:http';
2
+ import { createHash, randomBytes } from 'node:crypto';
2
3
  import os from 'node:os';
3
4
  import path from 'node:path';
4
5
  import { readFile } from 'node:fs/promises';
@@ -9,7 +10,12 @@ import { PairingManager } from './pairing.js';
9
10
  const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
10
11
  const UI_ROOT = path.join(ROOT, 'desktop-ui');
11
12
  const PORT = Number.parseInt(process.env.GAMER_PORT || '8765', 10);
13
+ if (!Number.isInteger(PORT) || PORT < 1 || PORT > 65535) {
14
+ console.error('[gamer-desktop] Invalid GAMER_PORT. Use an integer from 1 to 65535.');
15
+ process.exit(1);
16
+ }
12
17
  const manager = new PairingManager();
18
+ const pairedSessions = new Map();
13
19
  const MIME = {
14
20
  '.html': 'text/html; charset=utf-8',
15
21
  '.js': 'text/javascript; charset=utf-8',
@@ -162,18 +168,61 @@ async function handle(req, res) {
162
168
  });
163
169
  }
164
170
 
171
+ if (url.pathname === '/api/pairing/claim' && req.method === 'POST') {
172
+ let body;
173
+ try { body = await readJson(req); }
174
+ catch (error) { return sendJson(res, 400, { error: error.message }); }
175
+ if (body.protocol !== 'gamer-runtime/1' || body.instance_id !== manager.instanceId
176
+ || typeof body.device_id !== 'string' || body.device_id.length < 8 || body.device_id.length > 128
177
+ || typeof body.device_name !== 'string' || body.device_name.length > 120) {
178
+ return sendJson(res, 400, { error: 'INVALID_PAIRING_REQUEST' });
179
+ }
180
+ if (!manager.verify(body.token, { consume: true })) {
181
+ return sendJson(res, 410, { error: 'PAIRING_TOKEN_INVALID_EXPIRED_OR_USED' });
182
+ }
183
+ const sessionId = randomBytes(18).toString('base64url');
184
+ const sessionToken = randomBytes(32).toString('base64url');
185
+ pairedSessions.set(sessionId, {
186
+ tokenHash: createHash('sha256').update(sessionToken).digest('hex'),
187
+ deviceId: body.device_id,
188
+ deviceName: body.device_name,
189
+ createdAt: Date.now(),
190
+ lastSeenAt: Date.now(),
191
+ });
192
+ return sendJson(res, 200, {
193
+ paired: true,
194
+ protocol: 'gamer-runtime/1',
195
+ instanceId: manager.instanceId,
196
+ sessionId,
197
+ sessionToken,
198
+ device: { id: body.device_id, name: body.device_name },
199
+ capabilities: { pairing: true, heartbeat: true, screenCapture: false, inputControl: false, modelInference: false },
200
+ });
201
+ }
202
+
203
+ if (url.pathname === '/api/pairing/heartbeat' && req.method === 'POST') {
204
+ const auth = req.headers.authorization || '';
205
+ const token = auth.startsWith('Bearer ') ? auth.slice(7) : '';
206
+ const hash = createHash('sha256').update(token).digest('hex');
207
+ const entry = [...pairedSessions.entries()].find(([, value]) => value.tokenHash === hash);
208
+ if (!entry) return sendJson(res, 401, { error: 'SESSION_UNAUTHORIZED' });
209
+ entry[1].lastSeenAt = Date.now();
210
+ return sendJson(res, 200, { connected: true, sessionId: entry[0], serverTime: Date.now() });
211
+ }
212
+
165
213
  if (url.pathname === '/api/pairing/stop' && req.method === 'POST') {
166
214
  if (!isLoopback(req.socket.remoteAddress)) {
167
215
  return sendJson(res, 403, { error: 'PAIRING_CONTROL_LOCAL_ONLY' });
168
216
  }
169
217
  manager.stop();
170
- return sendJson(res, 200, { active: false });
218
+ pairedSessions.clear();
219
+ return sendJson(res, 200, { active: false, sessionsRevoked: true });
171
220
  }
172
221
 
173
222
  if (url.pathname === '/pair' && req.method === 'GET') {
174
223
  const valid = url.searchParams.get('protocol') === 'gamer-runtime/1'
175
224
  && url.searchParams.get('instance_id') === manager.instanceId
176
- && manager.verify(url.searchParams.get('token'), { consume: true });
225
+ && manager.verify(url.searchParams.get('token'));
177
226
  res.writeHead(valid ? 200 : 410, {
178
227
  'content-type': 'text/html; charset=utf-8',
179
228
  'cache-control': 'no-store',
package/dsh/client.js CHANGED
@@ -7,7 +7,7 @@ window.__ModuleLoader__.load({
7
7
 
8
8
  const COPY = {
9
9
  zh: {
10
- title: 'Gamer 游戏智能体', subtitle: 'PC 游戏控制中心 · Android 手机伴侣', version: '版本 1.0.1',
10
+ title: 'Gamer 游戏智能体', subtitle: 'PC 游戏控制中心 · Android 手机伴侣', version: '版本 1.0.5',
11
11
  overview: '运行状态', ready: 'DSH 插件入口已加载',
12
12
  desktop: '桌面端控制台', desktopValue: '界面框架已提供,游戏运行时尚未接入',
13
13
  android: 'Android 手机伴侣', androidValue: '通信服务尚未接入',
@@ -23,7 +23,7 @@ window.__ModuleLoader__.load({
23
23
  status: '未连接', noGameControl: '当前版本不会控制游戏或发送键鼠/触控指令。'
24
24
  },
25
25
  en: {
26
- title: 'Gamer AI Agent', subtitle: 'PC game control center · Android companion', version: 'Version 1.0.1',
26
+ title: 'Gamer AI Agent', subtitle: 'PC game control center · Android companion', version: 'Version 1.0.5',
27
27
  overview: 'Runtime status', ready: 'Native DSH plugin entry loaded',
28
28
  desktop: 'Desktop control center', desktopValue: 'UI shell is present; game runtime is not connected',
29
29
  android: 'Android companion', androidValue: 'Communication service is not connected',
@@ -31,7 +31,7 @@ window.__ModuleLoader__.load({
31
31
  automation: 'Automatic gameplay', automationValue: 'Not implemented; no game actions are executed',
32
32
  pairing: 'QR device pairing', pairingHint: 'Pair an Android phone securely after the companion service is available.',
33
33
  qrUnavailable: 'QR pairing service is not connected',
34
- qrDetail: 'This release has no desktop pairing server yet, so it cannot generate a real, scannable connection QR code.',
34
+ qrDetail: 'The desktop pairing service must be running locally. Set the same custom port here and in the startup script to generate a real QR code.',
35
35
  qrNote: 'No fake QR code is shown and no device is reported as connected.',
36
36
  roadmap: 'Next steps', roadmapText: 'Implement the desktop pairing service and Android companion before enabling QR generation, two-sided authentication and device status sync.',
37
37
  repo: 'View GitHub project and development progress', language: 'Language', chinese: '简体中文', english: 'English',
@@ -60,8 +60,10 @@ window.__ModuleLoader__.load({
60
60
  const [error, setError] = React.useState('');
61
61
  const [busy, setBusy] = React.useState(false);
62
62
  const [addresses, setAddresses] = React.useState([]);
63
+ const [port, setPort] = React.useState(() => { try { return localStorage.getItem('gamer.desktop.port') || '8765'; } catch { return '8765'; } });
63
64
  const c = COPY[language];
64
- const base = 'http://127.0.0.1:8765';
65
+ const validPort = /^([1-9][0-9]{0,4})$/.test(port) && Number(port) <= 65535;
66
+ const base = 'http://127.0.0.1:' + (validPort ? port : '8765');
65
67
 
66
68
  const generateQr = React.useCallback(async (selectedHost) => {
67
69
  if (!selectedHost) return;
@@ -82,7 +84,7 @@ window.__ModuleLoader__.load({
82
84
  } catch (e) {
83
85
  setQr(''); setPairState('二维码生成失败'); setError(e.message || String(e));
84
86
  } finally { setBusy(false); }
85
- }, []);
87
+ }, [base]);
86
88
 
87
89
  const detectAndGenerate = React.useCallback(async () => {
88
90
  setBusy(true); setError(''); setPairState('正在检测桌面配对服务…');
@@ -97,10 +99,10 @@ window.__ModuleLoader__.load({
97
99
  await generateQr(selected);
98
100
  } catch (e) {
99
101
  setQr(''); setPairState('桌面配对服务不可用');
100
- setError((e.message || String(e)) + '。请先在电脑运行 Gamer 桌面端启动脚本,并确认端口 8765 可用。');
102
+ setError((e.message || String(e)) + '。请先在电脑运行 Gamer 桌面端启动脚本,并确认本机端口 ' + port + ' 可用;如修改端口,请确保启动脚本与此处填写一致。');
101
103
  setBusy(false);
102
104
  }
103
- }, [generateQr]);
105
+ }, [generateQr, base]);
104
106
 
105
107
  React.useEffect(() => {
106
108
  let alive = true;
@@ -150,8 +152,14 @@ window.__ModuleLoader__.load({
150
152
  h('section', { style: { display: 'grid', gridTemplateColumns: 'minmax(0,1fr) minmax(180px,220px)', gap: '18px', alignItems: 'center', padding: '16px', borderRadius: '14px', border: '1px solid var(--dsw-alias-border-l2, rgba(127,127,127,.22))' } },
151
153
  h('div', { style: { minWidth: 0 } },
152
154
  h('div', { style: { fontSize: '15px', fontWeight: 700 } }, c.pairing),
153
- h('p', { style: { fontSize: '12px', lineHeight: 1.65, color: 'var(--dsw-alias-label-secondary, #777)' } }, '页面加载后会自动连接本机 Gamer 桌面服务并生成真实二维码。电脑与手机需要处于同一局域网。'),
154
- h('div', { style: { fontSize: '13px', fontWeight: 650 } }, pairState),
155
+ h('p', { style: { fontSize: '12px', lineHeight: 1.65, color: 'var(--dsw-alias-label-secondary, #777)' } }, '页面加载后会自动连接本机 Gamer 桌面服务并生成真实二维码。电脑与手机需要处于同一局域网。可自定义桌面服务端口,并与启动脚本中的端口保持一致。'),
156
+ h('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'wrap', margin: '10px 0' } },
157
+ h('label', { htmlFor: 'gamer-local-port', style: { fontSize: '12px' } }, '桌面服务端口 / Desktop port'),
158
+ h('input', { id: 'gamer-local-port', type: 'number', min: 1, max: 65535, step: 1, value: port, onChange: e => setPort(e.target.value), onBlur: () => { try { localStorage.setItem('gamer.desktop.port', port); } catch {} }, style: { width: '92px', padding: '7px 9px', borderRadius: '8px', border: '1px solid var(--dsw-alias-border-l2, #aaa)', background: 'var(--dsw-alias-bg-layer-1, transparent)', color: 'inherit' } }),
159
+ h('button', { type: 'button', disabled: busy || !validPort, onClick: () => { try { localStorage.setItem('gamer.desktop.port', port); } catch {} detectAndGenerate(); }, style: { borderRadius: '8px', padding: '7px 10px' } }, '应用端口并重连')
160
+ ),
161
+ !validPort ? h('p', { role: 'alert', style: { color: '#c45b50', fontSize: '12px' } }, '端口必须是 1–65535 的整数。') : null,
162
+ h('div', { style: { fontSize: '13px', fontWeight: 650 } }, pairState),
155
163
  host ? h('p', { style: { fontSize: '12px', color: 'var(--dsw-alias-label-secondary, #777)' } }, '电脑地址:' + host) : null,
156
164
  h('p', { style: { fontSize: '12px', color: 'var(--dsw-alias-label-secondary, #777)' } }, qr ? ('有效期:' + Math.floor(remaining / 60) + ':' + String(remaining % 60).padStart(2, '0')) : '有效期:未生成'),
157
165
  error ? h('p', { role: 'alert', style: { color: '#c45b50', fontSize: '12px', lineHeight: 1.6 } }, error) : null,
@@ -173,7 +181,7 @@ window.__ModuleLoader__.load({
173
181
  ),
174
182
  h('section', { style: { padding: '14px', borderRadius: '12px', background: 'var(--dsw-alias-bg-layer-2, rgba(127,127,127,.06))' } },
175
183
  h('div', { style: { fontSize: '13px', fontWeight: 700, marginBottom: '5px' } }, c.overview),
176
- h('div', { style: { fontSize: '12px', lineHeight: 1.65, color: 'var(--dsw-alias-label-secondary, #777)' } }, 'DSH 插件界面与桌面配对服务通过本机回环地址通信。若服务未启动,请先运行桌面端启动脚本。游戏控制、视觉识别和 Android 双端认证仍需各自运行时实现。')
184
+ h('div', { style: { fontSize: '12px', lineHeight: 1.65, color: 'var(--dsw-alias-label-secondary, #777)' } }, 'DSH 插件界面与桌面配对服务通过本机回环地址通信。若服务未启动,请先运行桌面端启动脚本。端口可在上方自定义;若仍出现 Failed to fetch,请检查服务是否运行、端口是否一致以及 DSH 是否允许本地网络访问。游戏控制、视觉识别和 Android 双端认证仍需各自运行时实现。')
177
185
  ),
178
186
  h('a', { href: 'https://github.com/sususgh/Gamer-DSH-Plugin', target: '_blank', rel: 'noreferrer',
179
187
  style: { fontSize: '12px', color: 'var(--dsw-alias-label-link, #4b91d1)', textDecoration: 'none' } }, c.repo + ' ↗')
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sususgh/gamer-dsh-plugin",
3
- "version": "1.0.4",
4
- "description": "Native DeepSeek Harness plugin with automatic desktop QR pairing and Gamer runtime UI",
3
+ "version": "1.0.5",
4
+ "description": "Gamer 游戏智能体:PC 游戏控制中心与 Android 手机伴侣,支持本地配对服务和自定义端口。 | Gamer AI game agent for PC and Android with local QR pairing and a configurable desktop port.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
7
7
  "author": "sususgh",
@@ -29,7 +29,9 @@
29
29
  "LICENSE",
30
30
  "docs/DSH-INSTALL.zh-CN.md",
31
31
  "docs/runtime-protocol-v1.md",
32
- "locale"
32
+ "locale",
33
+ "start-windows.bat",
34
+ "start-linux.sh"
33
35
  ],
34
36
  "exports": {
35
37
  ".": "./dsh/index.js",
package/start-linux.sh ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ DEFAULT_PORT=8765
4
+ read -r -p "Gamer desktop port [${DEFAULT_PORT}]: " GAMER_PORT
5
+ GAMER_PORT="${GAMER_PORT:-$DEFAULT_PORT}"
6
+ if ! [[ "$GAMER_PORT" =~ ^[0-9]+$ ]] || (( GAMER_PORT < 1 || GAMER_PORT > 65535 )); then
7
+ echo "Invalid port. Enter an integer from 1 to 65535."
8
+ exit 1
9
+ fi
10
+ cd "$(dirname "$0")/desktop-runtime"
11
+ if [[ ! -d node_modules/qrcode ]]; then
12
+ echo "Installing desktop runtime dependencies..."
13
+ npm install
14
+ fi
15
+ export GAMER_PORT
16
+ echo "Starting Gamer desktop service on port $GAMER_PORT..."
17
+ echo "Keep this terminal open while using the DSH plugin."
18
+ npm start
@@ -0,0 +1,30 @@
1
+ @echo off
2
+ setlocal
3
+ title Gamer Desktop Pairing Service
4
+ echo ==========================================
5
+ echo Gamer Desktop - QR Pairing Service
6
+ echo ==========================================
7
+ set "GAMER_PORT=8765"
8
+ set /p "GAMER_PORT=Enter port (1-65535) [8765]: "
9
+ if "%GAMER_PORT%"=="" set "GAMER_PORT=8765"
10
+ powershell -NoProfile -Command "if ([int]::TryParse('%GAMER_PORT%', [ref]$p) -and $p -ge 1 -and $p -le 65535) { exit 0 } else { exit 1 }"
11
+ if errorlevel 1 (
12
+ echo Invalid port. Please enter a number from 1 to 65535.
13
+ pause
14
+ exit /b 1
15
+ )
16
+ cd /d "%~dp0desktop-runtime"
17
+ if not exist "node_modules\qrcode" (
18
+ echo Installing desktop runtime dependencies...
19
+ call npm install
20
+ if errorlevel 1 (
21
+ echo Dependency installation failed.
22
+ pause
23
+ exit /b 1
24
+ )
25
+ )
26
+ set "GAMER_PORT=%GAMER_PORT%"
27
+ echo Starting Gamer on port %GAMER_PORT%...
28
+ echo Keep this window open while using the DSH plugin.
29
+ call npm start
30
+ pause