@aivin-labs/cli 1.0.50 → 1.0.57
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 +1 -1
- package/bin/cli.mjs +2214 -2211
- package/bin/lib/auth.mjs +1 -1
- package/bin/lib/context.mjs +3 -3
- package/bin/lib/openFile.mjs +3 -2
- package/bin/lib/remote/agentContract.mjs +370 -0
- package/bin/lib/remote/agentEvidence.mjs +409 -0
- package/bin/lib/remote/autoStart.mjs +640 -623
- package/bin/lib/remote/client.mjs +1454 -1418
- package/bin/lib/remote/codingAgents.mjs +594 -293
- package/bin/lib/remote/constants.mjs +1 -1
- package/bin/lib/remote/desktop/castAuth.mjs +259 -249
- package/bin/lib/remote/desktop/castConsent.mjs +154 -90
- package/bin/lib/remote/desktop/castInput.mjs +3 -0
- package/bin/lib/remote/desktop/castTrust.mjs +184 -0
- package/bin/lib/remote/desktop/desktopCast.mjs +483 -443
- package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +1 -1
- package/bin/lib/remote/desktop/desktopCastSource.mjs +250 -12
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +59 -4
- package/bin/lib/remote/desktop/strings.mjs +138 -132
- package/bin/lib/remote/desktop/traySource.mjs +1438 -1358
- package/bin/lib/remote/gui/index.mjs +567 -485
- package/bin/lib/remote/gui/windowsHostSource.mjs +630 -33
- package/bin/lib/remote/humanInput.mjs +7 -0
- package/bin/lib/remote/localConfirm.mjs +2 -2
- package/bin/lib/remote/toolHandlers.mjs +861 -859
- package/bin/lib/servers.mjs +31 -1
- package/docs/CLI.md +1606 -1602
- package/package.json +1 -1
|
@@ -111,7 +111,7 @@ export const DeviceEvent = Object.freeze({
|
|
|
111
111
|
|
|
112
112
|
/** Trạng thái phiên xem màn hình gửi BE (`device:desktop_cast_status`) — khớp BE `DesktopCastState`. */
|
|
113
113
|
export const CastState = Object.freeze({
|
|
114
|
-
READY: 'ready', CONNECTED: 'connected', DEGRADED: 'degraded', STOPPED: 'stopped', REFUSED: 'refused', INPUT_BLOCKED: 'input_blocked', AWAITING_CONSENT: 'awaiting_consent', PASTE_FAILED: 'paste_failed', DROP_REFUSED: 'drop_refused',
|
|
114
|
+
READY: 'ready', CONNECTED: 'connected', DEGRADED: 'degraded', STOPPED: 'stopped', REFUSED: 'refused', INPUT_BLOCKED: 'input_blocked', AWAITING_CONSENT: 'awaiting_consent', DEVICE_CHALLENGE: 'device_challenge', PASTE_FAILED: 'paste_failed', DROP_REFUSED: 'drop_refused',
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
/** Loại báo chủ động (`device:notify`). */
|
|
@@ -1,249 +1,259 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { spawn } from 'child_process';
|
|
4
|
-
import { REMOTE_CONFIG_DIR } from '../constants.mjs';
|
|
5
|
-
import { AIVIN_LOGO_PNG_BASE64 } from '../brand.mjs';
|
|
6
|
-
import { CastConsentDecision } from './castConsent.mjs';
|
|
7
|
-
import { lockHolder } from '../daemonLock.mjs';
|
|
8
|
-
import { CAST_CONSENT_WINDOW_TITLE, HUMAN_GUARD_PS, humanGuardEnv } from '../humanInput.mjs';
|
|
9
|
-
|
|
10
|
-
export const CAST_SESSION_FILE = path.join(REMOTE_CONFIG_DIR, 'desktop-cast-session.json');
|
|
11
|
-
export const CAST_ACTION_FILE = path.join(REMOTE_CONFIG_DIR, 'desktop-cast-action.json');
|
|
12
|
-
export const CAST_PERMISSIONS_FILE = path.join(REMOTE_CONFIG_DIR, 'cast-permissions.json');
|
|
13
|
-
|
|
14
|
-
const CAST_STOP_ACTION = Object.freeze({ action: 'stop_and_block' });
|
|
15
|
-
|
|
16
|
-
const b64 = (s) => Buffer.from(String(s ?? ''), 'utf8').toString('base64');
|
|
17
|
-
|
|
18
|
-
function loadPermissions() {
|
|
19
|
-
try {
|
|
20
|
-
const parsed = JSON.parse(fs.readFileSync(CAST_PERMISSIONS_FILE, 'utf8'));
|
|
21
|
-
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
22
|
-
} catch {
|
|
23
|
-
return {};
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function savePermissions(data) {
|
|
28
|
-
try {
|
|
29
|
-
fs.mkdirSync(path.dirname(CAST_PERMISSIONS_FILE), { recursive: true });
|
|
30
|
-
fs.writeFileSync(CAST_PERMISSIONS_FILE, JSON.stringify(data, null, 2), { encoding: 'utf8', mode: 0o600 });
|
|
31
|
-
} catch { }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Chủ máy vừa bấm ngắt: mọi phiên sau đều phải hỏi tới khi chủ máy bấm "Cho phép" một lần, kể cả chế độ không cần người ở máy.
|
|
36
|
-
* Khoá theo cả máy, không theo tên thiết bị: tên do trình duyệt tự khai và mã socket đổi sau mỗi lần nối nên chặn theo chúng lách được.
|
|
37
|
-
* Tệp từ bản cũ (`blockedViewers` còn phần tử) cũng tính là đang khoá.
|
|
38
|
-
*/
|
|
39
|
-
export function isCastLocked() {
|
|
40
|
-
const { locked, blockedViewers } = loadPermissions();
|
|
41
|
-
return locked === true || (Array.isArray(blockedViewers) && blockedViewers.length > 0);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function lockCast({ viewerDevice = '' } = {}) {
|
|
45
|
-
savePermissions({ locked: true, lockedAt: new Date().toISOString(), lastViewerDevice: String(viewerDevice || '') });
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function unlockCast() {
|
|
49
|
-
savePermissions({ locked: false });
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Ghi nhận thông tin phiên cast đang chạy để khay hệ thống (tray) đọc và hiển thị.
|
|
54
|
-
*/
|
|
55
|
-
export function recordCastSession({ active = true, sessionId = '', viewerId = '', viewerDevice = '' } = {}) {
|
|
56
|
-
try {
|
|
57
|
-
fs.mkdirSync(path.dirname(CAST_SESSION_FILE), { recursive: true });
|
|
58
|
-
const payload = {
|
|
59
|
-
active: !!active,
|
|
60
|
-
// Daemon đang giữ phiên: khay / `aivin remote disconnect` biết tệp còn sót sau khi daemon chết (không còn ai để ngắt).
|
|
61
|
-
pid: process.pid,
|
|
62
|
-
session_id: sessionId,
|
|
63
|
-
viewer_id: viewerId,
|
|
64
|
-
viewer_device: viewerDevice,
|
|
65
|
-
updated_at: new Date().toISOString(),
|
|
66
|
-
};
|
|
67
|
-
fs.writeFileSync(CAST_SESSION_FILE, JSON.stringify(payload, null, 2), 'utf8');
|
|
68
|
-
} catch { }
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Xoá thông tin phiên cast khi đã ngắt kết nối.
|
|
73
|
-
*/
|
|
74
|
-
export function clearCastSession() {
|
|
75
|
-
try {
|
|
76
|
-
if (fs.existsSync(CAST_SESSION_FILE)) {
|
|
77
|
-
fs.unlinkSync(CAST_SESSION_FILE);
|
|
78
|
-
}
|
|
79
|
-
} catch { }
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Kiểm tra xem khay hệ thống có gửi lệnh ngắt / chặn thiết bị cast không.
|
|
84
|
-
*/
|
|
85
|
-
export function checkCastAction() {
|
|
86
|
-
let content;
|
|
87
|
-
try {
|
|
88
|
-
if (!fs.existsSync(CAST_ACTION_FILE)) return null;
|
|
89
|
-
content = fs.readFileSync(CAST_ACTION_FILE, 'utf8');
|
|
90
|
-
fs.unlinkSync(CAST_ACTION_FILE);
|
|
91
|
-
} catch {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
// Có tệp nghĩa là chủ máy đã bấm ngắt: không đọc được nội dung thì vẫn phải ngắt, không được bỏ qua.
|
|
95
|
-
try {
|
|
96
|
-
const action = JSON.parse(content);
|
|
97
|
-
return action && typeof action === 'object' ? action : CAST_STOP_ACTION;
|
|
98
|
-
} catch {
|
|
99
|
-
return CAST_STOP_ACTION;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Gửi yêu cầu ngắt từ dòng lệnh, cùng đường với nút ở khay. Trả về `false` nếu không có phiên nào đang chạy — kể cả khi tệp phiên còn sót lại
|
|
105
|
-
* sau khi daemon chết (dọn luôn, không báo "đã ngắt" cho một phiên không tồn tại).
|
|
106
|
-
*/
|
|
107
|
-
export function requestCastStop({ daemonAlive = () => lockHolder() !== null } = {}) {
|
|
108
|
-
if (!fs.existsSync(CAST_SESSION_FILE)) return false;
|
|
109
|
-
if (!daemonAlive()) {
|
|
110
|
-
clearCastSession();
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
fs.mkdirSync(path.dirname(CAST_ACTION_FILE), { recursive: true });
|
|
114
|
-
fs.writeFileSync(CAST_ACTION_FILE, JSON.stringify(CAST_STOP_ACTION), 'utf8');
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export const CONSENT_XAML = `<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
119
|
-
Width="
|
|
120
|
-
Topmost="True" ShowInTaskbar="True" WindowStartupLocation="CenterScreen" FontFamily="Segoe UI" UseLayoutRounding="True">
|
|
121
|
-
<Window.Resources>
|
|
122
|
-
<Style x:Key="Btn" TargetType="Button">
|
|
123
|
-
<Setter Property="FontSize" Value="14"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Cursor" Value="Hand"/>
|
|
124
|
-
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
|
125
|
-
<Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button">
|
|
126
|
-
<Grid x:Name="root" RenderTransformOrigin="0.5,0.5" Background="Transparent">
|
|
127
|
-
<Border x:Name="b" CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
|
|
128
|
-
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
|
|
129
|
-
</Grid>
|
|
130
|
-
</ControlTemplate></Setter.Value></Setter>
|
|
131
|
-
</Style>
|
|
132
|
-
<Style x:Key="IconBtn" TargetType="Button">
|
|
133
|
-
<Setter Property="FontSize" Value="12"/><Setter Property="Cursor" Value="Hand"/><Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
|
134
|
-
<Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button">
|
|
135
|
-
<Border CornerRadius="8" Background="{TemplateBinding Background}">
|
|
136
|
-
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
|
137
|
-
</Border>
|
|
138
|
-
</ControlTemplate></Setter.Value></Setter>
|
|
139
|
-
</Style>
|
|
140
|
-
</Window.Resources>
|
|
141
|
-
<Border Margin="20" CornerRadius="18" Background="White" BorderBrush="#E5E6EB" BorderThickness="1">
|
|
142
|
-
<Border.Effect><DropShadowEffect BlurRadius="30" ShadowDepth="8" Direction="270" Opacity="0.18" Color="#0B1B3F"/></Border.Effect>
|
|
143
|
-
<StackPanel Margin="26,20,22,24">
|
|
144
|
-
<DockPanel x:Name="Head" Background="Transparent" LastChildFill="False">
|
|
145
|
-
<Image x:Name="Logo" Width="26" Height="26" RenderOptions.BitmapScalingMode="HighQuality" VerticalAlignment="Center"/>
|
|
146
|
-
<TextBlock Text="Aivin" FontSize="15" FontWeight="SemiBold" Foreground="#111827" Margin="9,0,0,0" VerticalAlignment="Center"/>
|
|
147
|
-
<TextBlock Text="• Yêu cầu chia sẻ màn hình" FontSize="13" Foreground="#6B7280" Margin="8,1,0,0" VerticalAlignment="Center"/>
|
|
148
|
-
<Button x:Name="Close" DockPanel.Dock="Right" Style="{StaticResource IconBtn}" Content="✕" Width="32" Height="32"
|
|
149
|
-
Background="Transparent" Foreground="#6B7280" ToolTip="Từ chối" IsTabStop="False"/>
|
|
150
|
-
</DockPanel>
|
|
151
|
-
<TextBlock Text="Cho phép xem và điều khiển màn hình máy tính?" Margin="0,20,0,0" FontSize="18" FontWeight="SemiBold" Foreground="#111827" TextWrapping="Wrap"/>
|
|
152
|
-
<Border x:Name="Warn" Margin="0,12,0,0" HorizontalAlignment="Left" CornerRadius="10" Background="#FEF3C7" Padding="12,6">
|
|
153
|
-
<TextBlock Text="⚠ Thiết bị này trước đó đã bị ngắt kết nối" Foreground="#92400E" FontSize="12.5" FontWeight="SemiBold" TextWrapping="Wrap"/>
|
|
154
|
-
</Border>
|
|
155
|
-
<Border Margin="0,14,0,0" CornerRadius="10" Background="#F4F6FA" BorderBrush="#E8ECF3" BorderThickness="1" Padding="14,12">
|
|
156
|
-
<StackPanel>
|
|
157
|
-
<TextBlock x:Name="DeviceText" FontSize="14" FontWeight="SemiBold" Foreground="#111827" TextWrapping="Wrap"/>
|
|
158
|
-
<TextBlock Text="Thiết bị này đang gửi yêu cầu xem màn hình và thao tác chuột, bàn phím trên máy tính của bạn." Margin="0,6,0,0" FontSize="12.5" Foreground="#4B5563" TextWrapping="Wrap"/>
|
|
159
|
-
</StackPanel>
|
|
160
|
-
</Border>
|
|
161
|
-
<DockPanel Margin="0,22,0,0" LastChildFill="True">
|
|
162
|
-
<Button x:Name="
|
|
163
|
-
Background="#4E46E5" Foreground="White" BorderThickness="0" Content="
|
|
164
|
-
<Button x:Name="
|
|
165
|
-
Background="White" Foreground="#
|
|
166
|
-
<TextBlock x:Name="Countdown" VerticalAlignment="Center"
|
|
167
|
-
</DockPanel>
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
</
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
'
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
"$w
|
|
179
|
-
"$w.
|
|
180
|
-
"
|
|
181
|
-
"
|
|
182
|
-
"$
|
|
183
|
-
"$
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
"
|
|
187
|
-
"$
|
|
188
|
-
"$w.FindName('
|
|
189
|
-
"
|
|
190
|
-
"$
|
|
191
|
-
|
|
192
|
-
'
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
...
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { spawn } from 'child_process';
|
|
4
|
+
import { REMOTE_CONFIG_DIR } from '../constants.mjs';
|
|
5
|
+
import { AIVIN_LOGO_PNG_BASE64 } from '../brand.mjs';
|
|
6
|
+
import { CastConsentDecision } from './castConsent.mjs';
|
|
7
|
+
import { lockHolder } from '../daemonLock.mjs';
|
|
8
|
+
import { CAST_CONSENT_WINDOW_TITLE, HUMAN_GUARD_PS, humanGuardEnv } from '../humanInput.mjs';
|
|
9
|
+
|
|
10
|
+
export const CAST_SESSION_FILE = path.join(REMOTE_CONFIG_DIR, 'desktop-cast-session.json');
|
|
11
|
+
export const CAST_ACTION_FILE = path.join(REMOTE_CONFIG_DIR, 'desktop-cast-action.json');
|
|
12
|
+
export const CAST_PERMISSIONS_FILE = path.join(REMOTE_CONFIG_DIR, 'cast-permissions.json');
|
|
13
|
+
|
|
14
|
+
const CAST_STOP_ACTION = Object.freeze({ action: 'stop_and_block' });
|
|
15
|
+
|
|
16
|
+
const b64 = (s) => Buffer.from(String(s ?? ''), 'utf8').toString('base64');
|
|
17
|
+
|
|
18
|
+
function loadPermissions() {
|
|
19
|
+
try {
|
|
20
|
+
const parsed = JSON.parse(fs.readFileSync(CAST_PERMISSIONS_FILE, 'utf8'));
|
|
21
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
22
|
+
} catch {
|
|
23
|
+
return {};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function savePermissions(data) {
|
|
28
|
+
try {
|
|
29
|
+
fs.mkdirSync(path.dirname(CAST_PERMISSIONS_FILE), { recursive: true });
|
|
30
|
+
fs.writeFileSync(CAST_PERMISSIONS_FILE, JSON.stringify(data, null, 2), { encoding: 'utf8', mode: 0o600 });
|
|
31
|
+
} catch { }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Chủ máy vừa bấm ngắt: mọi phiên sau đều phải hỏi tới khi chủ máy bấm "Cho phép" một lần, kể cả chế độ không cần người ở máy.
|
|
36
|
+
* Khoá theo cả máy, không theo tên thiết bị: tên do trình duyệt tự khai và mã socket đổi sau mỗi lần nối nên chặn theo chúng lách được.
|
|
37
|
+
* Tệp từ bản cũ (`blockedViewers` còn phần tử) cũng tính là đang khoá.
|
|
38
|
+
*/
|
|
39
|
+
export function isCastLocked() {
|
|
40
|
+
const { locked, blockedViewers } = loadPermissions();
|
|
41
|
+
return locked === true || (Array.isArray(blockedViewers) && blockedViewers.length > 0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function lockCast({ viewerDevice = '' } = {}) {
|
|
45
|
+
savePermissions({ locked: true, lockedAt: new Date().toISOString(), lastViewerDevice: String(viewerDevice || '') });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function unlockCast() {
|
|
49
|
+
savePermissions({ locked: false });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Ghi nhận thông tin phiên cast đang chạy để khay hệ thống (tray) đọc và hiển thị.
|
|
54
|
+
*/
|
|
55
|
+
export function recordCastSession({ active = true, sessionId = '', viewerId = '', viewerDevice = '' } = {}) {
|
|
56
|
+
try {
|
|
57
|
+
fs.mkdirSync(path.dirname(CAST_SESSION_FILE), { recursive: true });
|
|
58
|
+
const payload = {
|
|
59
|
+
active: !!active,
|
|
60
|
+
// Daemon đang giữ phiên: khay / `aivin remote disconnect` biết tệp còn sót sau khi daemon chết (không còn ai để ngắt).
|
|
61
|
+
pid: process.pid,
|
|
62
|
+
session_id: sessionId,
|
|
63
|
+
viewer_id: viewerId,
|
|
64
|
+
viewer_device: viewerDevice,
|
|
65
|
+
updated_at: new Date().toISOString(),
|
|
66
|
+
};
|
|
67
|
+
fs.writeFileSync(CAST_SESSION_FILE, JSON.stringify(payload, null, 2), 'utf8');
|
|
68
|
+
} catch { }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Xoá thông tin phiên cast khi đã ngắt kết nối.
|
|
73
|
+
*/
|
|
74
|
+
export function clearCastSession() {
|
|
75
|
+
try {
|
|
76
|
+
if (fs.existsSync(CAST_SESSION_FILE)) {
|
|
77
|
+
fs.unlinkSync(CAST_SESSION_FILE);
|
|
78
|
+
}
|
|
79
|
+
} catch { }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Kiểm tra xem khay hệ thống có gửi lệnh ngắt / chặn thiết bị cast không.
|
|
84
|
+
*/
|
|
85
|
+
export function checkCastAction() {
|
|
86
|
+
let content;
|
|
87
|
+
try {
|
|
88
|
+
if (!fs.existsSync(CAST_ACTION_FILE)) return null;
|
|
89
|
+
content = fs.readFileSync(CAST_ACTION_FILE, 'utf8');
|
|
90
|
+
fs.unlinkSync(CAST_ACTION_FILE);
|
|
91
|
+
} catch {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
// Có tệp nghĩa là chủ máy đã bấm ngắt: không đọc được nội dung thì vẫn phải ngắt, không được bỏ qua.
|
|
95
|
+
try {
|
|
96
|
+
const action = JSON.parse(content);
|
|
97
|
+
return action && typeof action === 'object' ? action : CAST_STOP_ACTION;
|
|
98
|
+
} catch {
|
|
99
|
+
return CAST_STOP_ACTION;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Gửi yêu cầu ngắt từ dòng lệnh, cùng đường với nút ở khay. Trả về `false` nếu không có phiên nào đang chạy — kể cả khi tệp phiên còn sót lại
|
|
105
|
+
* sau khi daemon chết (dọn luôn, không báo "đã ngắt" cho một phiên không tồn tại).
|
|
106
|
+
*/
|
|
107
|
+
export function requestCastStop({ daemonAlive = () => lockHolder() !== null } = {}) {
|
|
108
|
+
if (!fs.existsSync(CAST_SESSION_FILE)) return false;
|
|
109
|
+
if (!daemonAlive()) {
|
|
110
|
+
clearCastSession();
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
fs.mkdirSync(path.dirname(CAST_ACTION_FILE), { recursive: true });
|
|
114
|
+
fs.writeFileSync(CAST_ACTION_FILE, JSON.stringify(CAST_STOP_ACTION), 'utf8');
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const CONSENT_XAML = `<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
119
|
+
Width="540" SizeToContent="Height" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="NoResize"
|
|
120
|
+
Topmost="True" ShowInTaskbar="True" WindowStartupLocation="CenterScreen" FontFamily="Segoe UI" UseLayoutRounding="True">
|
|
121
|
+
<Window.Resources>
|
|
122
|
+
<Style x:Key="Btn" TargetType="Button">
|
|
123
|
+
<Setter Property="FontSize" Value="14"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Cursor" Value="Hand"/>
|
|
124
|
+
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
|
125
|
+
<Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button">
|
|
126
|
+
<Grid x:Name="root" RenderTransformOrigin="0.5,0.5" Background="Transparent">
|
|
127
|
+
<Border x:Name="b" CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
|
|
128
|
+
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
|
|
129
|
+
</Grid>
|
|
130
|
+
</ControlTemplate></Setter.Value></Setter>
|
|
131
|
+
</Style>
|
|
132
|
+
<Style x:Key="IconBtn" TargetType="Button">
|
|
133
|
+
<Setter Property="FontSize" Value="12"/><Setter Property="Cursor" Value="Hand"/><Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
|
134
|
+
<Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button">
|
|
135
|
+
<Border CornerRadius="8" Background="{TemplateBinding Background}">
|
|
136
|
+
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
|
137
|
+
</Border>
|
|
138
|
+
</ControlTemplate></Setter.Value></Setter>
|
|
139
|
+
</Style>
|
|
140
|
+
</Window.Resources>
|
|
141
|
+
<Border Margin="20" CornerRadius="18" Background="White" BorderBrush="#E5E6EB" BorderThickness="1">
|
|
142
|
+
<Border.Effect><DropShadowEffect BlurRadius="30" ShadowDepth="8" Direction="270" Opacity="0.18" Color="#0B1B3F"/></Border.Effect>
|
|
143
|
+
<StackPanel Margin="26,20,22,24">
|
|
144
|
+
<DockPanel x:Name="Head" Background="Transparent" LastChildFill="False">
|
|
145
|
+
<Image x:Name="Logo" Width="26" Height="26" RenderOptions.BitmapScalingMode="HighQuality" VerticalAlignment="Center"/>
|
|
146
|
+
<TextBlock Text="Aivin" FontSize="15" FontWeight="SemiBold" Foreground="#111827" Margin="9,0,0,0" VerticalAlignment="Center"/>
|
|
147
|
+
<TextBlock Text="• Yêu cầu chia sẻ màn hình" FontSize="13" Foreground="#6B7280" Margin="8,1,0,0" VerticalAlignment="Center"/>
|
|
148
|
+
<Button x:Name="Close" DockPanel.Dock="Right" Style="{StaticResource IconBtn}" Content="✕" Width="32" Height="32"
|
|
149
|
+
Background="Transparent" Foreground="#6B7280" ToolTip="Từ chối" IsTabStop="False" IsCancel="True" IsDefault="True"/>
|
|
150
|
+
</DockPanel>
|
|
151
|
+
<TextBlock Text="Cho phép xem và điều khiển màn hình máy tính?" Margin="0,20,0,0" FontSize="18" FontWeight="SemiBold" Foreground="#111827" TextWrapping="Wrap"/>
|
|
152
|
+
<Border x:Name="Warn" Margin="0,12,0,0" HorizontalAlignment="Left" CornerRadius="10" Background="#FEF3C7" Padding="12,6">
|
|
153
|
+
<TextBlock Text="⚠ Thiết bị này trước đó đã bị ngắt kết nối" Foreground="#92400E" FontSize="12.5" FontWeight="SemiBold" TextWrapping="Wrap"/>
|
|
154
|
+
</Border>
|
|
155
|
+
<Border Margin="0,14,0,0" CornerRadius="10" Background="#F4F6FA" BorderBrush="#E8ECF3" BorderThickness="1" Padding="14,12">
|
|
156
|
+
<StackPanel>
|
|
157
|
+
<TextBlock x:Name="DeviceText" FontSize="14" FontWeight="SemiBold" Foreground="#111827" TextWrapping="Wrap"/>
|
|
158
|
+
<TextBlock Text="Thiết bị này đang gửi yêu cầu xem màn hình và thao tác chuột, bàn phím trên máy tính của bạn." Margin="0,6,0,0" FontSize="12.5" Foreground="#4B5563" TextWrapping="Wrap"/>
|
|
159
|
+
</StackPanel>
|
|
160
|
+
</Border>
|
|
161
|
+
<DockPanel Margin="0,22,0,0" LastChildFill="True">
|
|
162
|
+
<Button x:Name="AllowAlways" DockPanel.Dock="Right" Style="{StaticResource Btn}" Height="40" MinWidth="116" Padding="20,0" Margin="10,0,0,0"
|
|
163
|
+
Background="#4E46E5" Foreground="White" BorderThickness="0" Content="_Luôn cho phép"/>
|
|
164
|
+
<Button x:Name="Allow" DockPanel.Dock="Right" Style="{StaticResource Btn}" Height="40" MinWidth="116" Padding="20,0" Margin="10,0,0,0"
|
|
165
|
+
Background="White" Foreground="#4E46E5" BorderBrush="#4E46E5" BorderThickness="1" Content="_Cho phép lần này"/>
|
|
166
|
+
<TextBlock x:Name="Countdown" VerticalAlignment="Center" FontSize="12" Foreground="#6B7280" TextWrapping="Wrap"/>
|
|
167
|
+
</DockPanel>
|
|
168
|
+
<TextBlock x:Name="Hint" Visibility="Collapsed" HorizontalAlignment="Right" Margin="0,10,0,0" FontSize="12" Foreground="#6B7280" TextWrapping="Wrap"/>
|
|
169
|
+
</StackPanel>
|
|
170
|
+
</Border>
|
|
171
|
+
</Window>`;
|
|
172
|
+
|
|
173
|
+
const CONSENT_SCRIPT = [
|
|
174
|
+
'Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase',
|
|
175
|
+
'function D($n) { [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String([Environment]::GetEnvironmentVariable($n))) }',
|
|
176
|
+
// Đồng ý cho xem màn hình chỉ từ phím / chuột thật trên máy — không nhận cả thao tác từ phiên điều khiển từ xa khác (humanInput.mjs).
|
|
177
|
+
...HUMAN_GUARD_PS,
|
|
178
|
+
"$w = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader ([xml](D 'AIVIN_CAST_XAML'))))",
|
|
179
|
+
"$w.Title = D 'AIVIN_CAST_TITLE'",
|
|
180
|
+
"$w.FindName('DeviceText').Text = D 'AIVIN_CAST_DEVICE'",
|
|
181
|
+
"if ($env:AIVIN_CAST_WARN -ne '1') { $w.FindName('Warn').Visibility = 'Collapsed' }",
|
|
182
|
+
"$ms = New-Object IO.MemoryStream(, [Convert]::FromBase64String($env:AIVIN_CAST_LOGO)); $img = New-Object Windows.Media.Imaging.BitmapImage",
|
|
183
|
+
"$img.BeginInit(); $img.StreamSource = $ms; $img.CacheOption = 'OnLoad'; $img.EndInit(); $w.FindName('Logo').Source = $img",
|
|
184
|
+
"$script:result = 'No'; $script:closing = $false",
|
|
185
|
+
'$tm = New-Object Windows.Threading.DispatcherTimer; $tm.Interval = [TimeSpan]::FromSeconds(1)',
|
|
186
|
+
"function Show-Hint { $script:hintLeft = 3; $hintText.Text = $script:hint; $hintText.Visibility = 'Visible' }",
|
|
187
|
+
"function Finish($r) { if ($script:closing) { return }; $script:closing = $true; $script:result = $r; $tm.Stop(); Remove-Guard; $w.Close() }",
|
|
188
|
+
"$w.FindName('Allow').Add_Click({ if (Test-Human) { Finish 'Yes' } else { Show-Hint } })",
|
|
189
|
+
// "Luôn cho phép" đi qua đúng chốt người thật như "Cho phép lần này": AI hay phiên điều khiển từ xa không tự cấp quyền lâu dài được.
|
|
190
|
+
"$w.FindName('AllowAlways').Add_Click({ if (Test-Human) { Finish 'Always' } else { Show-Hint } })",
|
|
191
|
+
// Không có khoá thiết bị thì không lưu được quyền lâu dài: chỉ còn "Cho phép lần này", thành nút chính.
|
|
192
|
+
"$allow = $w.FindName('Allow'); if ($env:AIVIN_CAST_CAN_TRUST -ne '1') { $w.FindName('AllowAlways').Visibility = 'Collapsed'; $allow.Background = (New-Object Windows.Media.BrushConverter).ConvertFromString('#4E46E5'); $allow.Foreground = [Windows.Media.Brushes]::White; $allow.BorderThickness = New-Object Windows.Thickness(0) }",
|
|
193
|
+
// Từ chối chỉ còn ở nút X: Enter / Esc vào nút này, không bao giờ thành đồng ý.
|
|
194
|
+
"$w.FindName('Close').Add_Click({ Finish 'No' })",
|
|
195
|
+
"$w.FindName('Head').Add_MouseLeftButtonDown({ $w.DragMove() })",
|
|
196
|
+
"$script:left = [int]$env:AIVIN_CAST_SECONDS; $fmt = D 'AIVIN_CAST_COUNTDOWN'; $countdown = $w.FindName('Countdown'); $hintText = $w.FindName('Hint'); $countdown.Text = $fmt -f $script:left",
|
|
197
|
+
"$tm.Add_Tick({ $script:left--; $countdown.Text = $fmt -f $script:left; if ($script:hintLeft -gt 0) { $script:hintLeft-- } else { $hintText.Visibility = 'Collapsed' }; if ($script:left -le 0) { Finish 'Timeout' } })",
|
|
198
|
+
"$w.Add_ContentRendered({ Install-Guard $w; $w.Activate(); [void]$w.FindName('Close').Focus(); $tm.Start() })",
|
|
199
|
+
'[void]$w.ShowDialog(); Remove-Guard; $tm.Stop(); $script:result',
|
|
200
|
+
].join('\n');
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Hỏi người ở máy trên chính màn hình Windows. Mặc định là từ chối: Enter, Esc, nút X, hết giờ đều không cho xem.
|
|
204
|
+
* `repeatedAfterBlock`: hiện thêm dòng cảnh báo thiết bị từng bị ngắt. `canTrust`: hiện thêm nút "Luôn cho phép".
|
|
205
|
+
* Trả về một giá trị của `CastConsentDecision`.
|
|
206
|
+
*/
|
|
207
|
+
export function askCastConsent({ viewerDevice = 'Thiết bị từ xa', timeoutSec = 45, repeatedAfterBlock = false, canTrust = false, signal } = {}) {
|
|
208
|
+
if (process.platform !== 'win32') return Promise.resolve(CastConsentDecision.UNAVAILABLE);
|
|
209
|
+
return new Promise((resolve) => {
|
|
210
|
+
let out = '';
|
|
211
|
+
let settled = false;
|
|
212
|
+
let timer = null;
|
|
213
|
+
|
|
214
|
+
const done = (answer) => {
|
|
215
|
+
if (settled) return;
|
|
216
|
+
settled = true;
|
|
217
|
+
if (timer) clearTimeout(timer);
|
|
218
|
+
resolve(answer);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const env = {
|
|
222
|
+
...process.env,
|
|
223
|
+
AIVIN_CAST_XAML: b64(CONSENT_XAML),
|
|
224
|
+
AIVIN_CAST_LOGO: AIVIN_LOGO_PNG_BASE64,
|
|
225
|
+
AIVIN_CAST_TITLE: b64(CAST_CONSENT_WINDOW_TITLE),
|
|
226
|
+
AIVIN_CAST_DEVICE: b64(`Thiết bị yêu cầu: ${viewerDevice}`),
|
|
227
|
+
AIVIN_CAST_COUNTDOWN: b64('Tự từ chối sau {0}s'),
|
|
228
|
+
AIVIN_CAST_SECONDS: String(timeoutSec),
|
|
229
|
+
AIVIN_CAST_WARN: repeatedAfterBlock ? '1' : '0',
|
|
230
|
+
AIVIN_CAST_CAN_TRUST: canTrust ? '1' : '0',
|
|
231
|
+
...humanGuardEnv(null),
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
let child = null;
|
|
235
|
+
try {
|
|
236
|
+
child = spawn('powershell.exe', ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', CONSENT_SCRIPT], {
|
|
237
|
+
env,
|
|
238
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
239
|
+
windowsHide: false,
|
|
240
|
+
});
|
|
241
|
+
} catch {
|
|
242
|
+
return done(CastConsentDecision.UNAVAILABLE);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const kill = () => { try { child.kill(); } catch { } };
|
|
246
|
+
signal?.addEventListener('abort', () => { kill(); done(CastConsentDecision.CANCELLED); }, { once: true });
|
|
247
|
+
timer = setTimeout(() => { kill(); done(CastConsentDecision.TIMEOUT); }, (timeoutSec + 5) * 1000);
|
|
248
|
+
|
|
249
|
+
child.stdout?.on('data', (chunk) => { out += chunk; });
|
|
250
|
+
child.on('error', () => done(CastConsentDecision.UNAVAILABLE));
|
|
251
|
+
child.on('close', (code) => {
|
|
252
|
+
if (code === 0 && /\bAlways\b/.test(out)) return done(CastConsentDecision.ALLOW_ALWAYS);
|
|
253
|
+
if (code === 0 && /\bYes\b/.test(out)) return done(CastConsentDecision.ALLOW);
|
|
254
|
+
if (/\bTimeout\b/.test(out)) return done(CastConsentDecision.TIMEOUT);
|
|
255
|
+
if (/\bNo\b/.test(out)) return done(CastConsentDecision.REJECTED);
|
|
256
|
+
return done(CastConsentDecision.UNAVAILABLE);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
}
|