@aivin-labs/cli 1.0.11 → 1.0.13
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 +16 -0
- package/bin/cli.mjs +67 -0
- package/bin/lib/remote/browserTunnel.mjs +47 -8
- package/bin/lib/remote/callLedger.mjs +73 -14
- package/bin/lib/remote/client.mjs +89 -8
- package/bin/lib/remote/cloudFiles.mjs +53 -0
- package/bin/lib/remote/constants.mjs +17 -0
- package/bin/lib/remote/dailyBrowser.mjs +8 -2
- package/bin/lib/remote/desktop/desktopCast.mjs +198 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +514 -0
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +95 -0
- package/bin/lib/remote/desktop/index.mjs +44 -7
- package/bin/lib/remote/desktop/linux.mjs +1 -1
- package/bin/lib/remote/desktop/linuxTraySource.mjs +434 -427
- package/bin/lib/remote/desktop/mac.mjs +2 -2
- package/bin/lib/remote/desktop/macTraySource.mjs +295 -292
- package/bin/lib/remote/desktop/traySource.mjs +541 -527
- package/bin/lib/remote/deviceProfile.mjs +1 -0
- package/bin/lib/remote/docText.mjs +85 -0
- package/bin/lib/remote/fileIndexer.mjs +757 -399
- package/bin/lib/remote/folderRules.mjs +140 -0
- package/bin/lib/remote/gui/index.mjs +126 -17
- package/bin/lib/remote/gui/windowsHost.mjs +9 -2
- package/bin/lib/remote/gui/windowsHostSource.mjs +194 -51
- package/bin/lib/remote/idempotency.mjs +49 -20
- package/bin/lib/remote/localPolicy.mjs +8 -2
- package/bin/lib/remote/mascot/askHostSource.mjs +18 -0
- package/bin/lib/remote/mascot/bot.mjs +29 -4
- package/bin/lib/remote/mascot/hostSource.mjs +4 -4
- package/bin/lib/remote/mascot/rigSource.mjs +12 -7
- package/bin/lib/remote/osSearch.mjs +107 -0
- package/bin/lib/remote/sqliteDb.mjs +51 -0
- package/bin/lib/remote/toolHandlers.mjs +788 -741
- package/bin/lib/remoteControl.mjs +26 -26
- package/docs/AIVIN_MASCOT.md +14 -0
- package/docs/CLI.md +1504 -1484
- package/docs/media/aivin-mascot-live.gif +0 -0
- package/docs/media/aivin-mascot-live.png +0 -0
- package/docs/media/aivin-remote-architecture.svg +102 -0
- package/package.json +71 -71
- package/docs/media/aivin-mascot-states.mp4 +0 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiến trình phát hình ảnh màn hình trực tiếp và điều khiển từ xa trên Windows (Desktop Scenecast).
|
|
3
|
+
* Tương thích C# 5 / .NET Framework 4.5+ / PowerShell 5.1:
|
|
4
|
+
* - Chụp màn hình qua Graphics.CopyFromScreen.
|
|
5
|
+
* - Lồng con trỏ chuột thật của Windows vào khung hình (GetCursorInfo + DrawIcon).
|
|
6
|
+
* - Hỗ trợ đa màn hình: lấy danh sách màn hình và chuyển đổi màn hình cần chụp theo thời gian thực.
|
|
7
|
+
* - Co ảnh về chiều rộng tối đa (giữ tỉ lệ), nén ảnh sang định dạng JPEG nhẹ.
|
|
8
|
+
* - So sánh ảnh thu nhỏ (64x36) để chỉ gửi khung hình khi có chuyển động hoặc giữ nhịp định kỳ (1 FPS).
|
|
9
|
+
* - Nhận lệnh chuột, bàn phím và phím tắt hệ thống (hotkeys) qua stdin và thực thi qua Win32 API.
|
|
10
|
+
* - Hỗ trợ thay đổi độ phân giải, chất lượng nén JPEG, giới hạn FPS và chọn màn hình theo thời gian thực.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const DESKTOP_CAST_CSHARP = String.raw`
|
|
14
|
+
using System;
|
|
15
|
+
using System.Collections.Generic;
|
|
16
|
+
using System.Drawing;
|
|
17
|
+
using System.Drawing.Imaging;
|
|
18
|
+
using System.IO;
|
|
19
|
+
using System.Runtime.InteropServices;
|
|
20
|
+
using System.Text;
|
|
21
|
+
using System.Threading;
|
|
22
|
+
using System.Web.Script.Serialization;
|
|
23
|
+
using System.Windows.Forms;
|
|
24
|
+
|
|
25
|
+
namespace AivinDesktopCast {
|
|
26
|
+
static class Native {
|
|
27
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
28
|
+
[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
|
|
29
|
+
[DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
|
|
30
|
+
[DllImport("user32.dll")] public static extern void keybd_event(byte bVk, ushort bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
31
|
+
[DllImport("user32.dll")] public static extern short VkKeyScan(char ch);
|
|
32
|
+
|
|
33
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
34
|
+
public struct POINT { public int x; public int y; }
|
|
35
|
+
|
|
36
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
37
|
+
public struct CURSORINFO {
|
|
38
|
+
public int cbSize;
|
|
39
|
+
public int flags;
|
|
40
|
+
public IntPtr hCursor;
|
|
41
|
+
public POINT ptScreenPos;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
[DllImport("user32.dll")] public static extern bool GetCursorInfo(out CURSORINFO pci);
|
|
45
|
+
[DllImport("user32.dll")] public static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);
|
|
46
|
+
|
|
47
|
+
public const int CURSOR_SHOWING = 0x00000001;
|
|
48
|
+
|
|
49
|
+
public const uint MOUSEEVENTF_MOVE = 0x0001;
|
|
50
|
+
public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
|
|
51
|
+
public const uint MOUSEEVENTF_LEFTUP = 0x0004;
|
|
52
|
+
public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
|
53
|
+
public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
|
|
54
|
+
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
|
|
55
|
+
public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
|
|
56
|
+
public const uint MOUSEEVENTF_WHEEL = 0x0800;
|
|
57
|
+
|
|
58
|
+
public const uint KEYEVENTF_KEYUP = 0x0002;
|
|
59
|
+
public const uint KEYEVENTF_UNICODE = 0x0004;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static class Out {
|
|
63
|
+
static readonly object gate = new object();
|
|
64
|
+
static readonly JavaScriptSerializer json = new JavaScriptSerializer { MaxJsonLength = int.MaxValue };
|
|
65
|
+
|
|
66
|
+
public static void Send(Dictionary<string, object> msg) {
|
|
67
|
+
string line = json.Serialize(msg);
|
|
68
|
+
lock (gate) { Console.Out.WriteLine(line); Console.Out.Flush(); }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public static Dictionary<string, object> Parse(string text) {
|
|
72
|
+
if (string.IsNullOrEmpty(text)) return new Dictionary<string, object>();
|
|
73
|
+
return json.Deserialize<Dictionary<string, object>>(text);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public static Dictionary<string, object> Msg(string t) {
|
|
77
|
+
var d = new Dictionary<string, object>();
|
|
78
|
+
d["t"] = t;
|
|
79
|
+
return d;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public static class CastScreen {
|
|
84
|
+
public static Rectangle GetBounds(int monitor) {
|
|
85
|
+
if (monitor < 0 || monitor >= Screen.AllScreens.Length) {
|
|
86
|
+
return SystemInformation.VirtualScreen;
|
|
87
|
+
}
|
|
88
|
+
return Screen.AllScreens[monitor].Bounds;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public static List<Dictionary<string, object>> GetScreens() {
|
|
92
|
+
var list = new List<Dictionary<string, object>>();
|
|
93
|
+
var all = Screen.AllScreens;
|
|
94
|
+
for (int i = 0; i < all.Length; i++) {
|
|
95
|
+
var s = all[i];
|
|
96
|
+
var d = new Dictionary<string, object>();
|
|
97
|
+
d["index"] = i;
|
|
98
|
+
d["name"] = "Màn hình " + (i + 1) + (s.Primary ? " (Chính)" : "");
|
|
99
|
+
d["primary"] = s.Primary;
|
|
100
|
+
d["width"] = s.Bounds.Width;
|
|
101
|
+
d["height"] = s.Bounds.Height;
|
|
102
|
+
d["x"] = s.Bounds.X;
|
|
103
|
+
d["y"] = s.Bounds.Y;
|
|
104
|
+
list.Add(d);
|
|
105
|
+
}
|
|
106
|
+
return list;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public static Bitmap Grab(Rectangle r) {
|
|
110
|
+
var bmp = new Bitmap(r.Width, r.Height, PixelFormat.Format24bppRgb);
|
|
111
|
+
using (var g = Graphics.FromImage(bmp)) {
|
|
112
|
+
g.CopyFromScreen(r.Left, r.Top, 0, 0, r.Size);
|
|
113
|
+
|
|
114
|
+
// Lồng con trỏ chuột thật của Windows vào ảnh
|
|
115
|
+
try {
|
|
116
|
+
Native.CURSORINFO pci;
|
|
117
|
+
pci.cbSize = Marshal.SizeOf(typeof(Native.CURSORINFO));
|
|
118
|
+
if (Native.GetCursorInfo(out pci) && (pci.flags & Native.CURSOR_SHOWING) != 0) {
|
|
119
|
+
int cursorX = pci.ptScreenPos.x - r.Left;
|
|
120
|
+
int cursorY = pci.ptScreenPos.y - r.Top;
|
|
121
|
+
if (cursorX >= -32 && cursorX < r.Width && cursorY >= -32 && cursorY < r.Height) {
|
|
122
|
+
IntPtr hdc = g.GetHdc();
|
|
123
|
+
try {
|
|
124
|
+
Native.DrawIcon(hdc, cursorX, cursorY, pci.hCursor);
|
|
125
|
+
} finally {
|
|
126
|
+
g.ReleaseHdc(hdc);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} catch {}
|
|
131
|
+
}
|
|
132
|
+
return bmp;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public static Bitmap Scale(Bitmap src, int maxWidth) {
|
|
136
|
+
if (src.Width <= maxWidth) return src;
|
|
137
|
+
int h = (int)Math.Round(src.Height * (double)maxWidth / src.Width);
|
|
138
|
+
var dst = new Bitmap(maxWidth, h, PixelFormat.Format24bppRgb);
|
|
139
|
+
using (var g = Graphics.FromImage(dst)) {
|
|
140
|
+
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
|
|
141
|
+
g.DrawImage(src, 0, 0, maxWidth, h);
|
|
142
|
+
}
|
|
143
|
+
src.Dispose();
|
|
144
|
+
return dst;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public static byte[] Jpeg(Bitmap bmp, int quality) {
|
|
148
|
+
var encoders = ImageCodecInfo.GetImageEncoders();
|
|
149
|
+
ImageCodecInfo codec = null;
|
|
150
|
+
for (int i = 0; i < encoders.Length; i++) {
|
|
151
|
+
if (encoders[i].MimeType == "image/jpeg") { codec = encoders[i]; break; }
|
|
152
|
+
}
|
|
153
|
+
if (codec == null) {
|
|
154
|
+
using (var ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Jpeg); return ms.ToArray(); }
|
|
155
|
+
}
|
|
156
|
+
var p = new EncoderParameters(1);
|
|
157
|
+
p.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
|
|
158
|
+
using (var ms = new MemoryStream()) {
|
|
159
|
+
bmp.Save(ms, codec, p);
|
|
160
|
+
return ms.ToArray();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
public static byte[] Thumb(Bitmap src) {
|
|
165
|
+
var t = new byte[64 * 36];
|
|
166
|
+
using (var small = new Bitmap(64, 36, PixelFormat.Format24bppRgb)) {
|
|
167
|
+
using (var g = Graphics.FromImage(small)) {
|
|
168
|
+
g.DrawImage(src, 0, 0, 64, 36);
|
|
169
|
+
}
|
|
170
|
+
for (int y = 0; y < 36; y++) {
|
|
171
|
+
for (int x = 0; x < 64; x++) {
|
|
172
|
+
var c = small.GetPixel(x, y);
|
|
173
|
+
t[y * 64 + x] = (byte)((c.R * 3 + c.G * 6 + c.B) / 10);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return t;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public static double Diff(byte[] a, byte[] b) {
|
|
181
|
+
if (a == null || b == null) return 255;
|
|
182
|
+
long s = 0;
|
|
183
|
+
for (int i = 0; i < a.Length; i++) s += Math.Abs(a[i] - b[i]);
|
|
184
|
+
return (double)s / a.Length;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public static class CastHost {
|
|
189
|
+
static volatile bool running = true;
|
|
190
|
+
static volatile int currentMonitor = -1;
|
|
191
|
+
static Rectangle currentBounds;
|
|
192
|
+
static int currentImageWidth = 0;
|
|
193
|
+
static int currentImageHeight = 0;
|
|
194
|
+
static volatile int targetMaxFps = 15;
|
|
195
|
+
static volatile int targetMaxWidth = 1366;
|
|
196
|
+
static volatile int targetQuality = 65;
|
|
197
|
+
|
|
198
|
+
public static void Run(string argsJson) {
|
|
199
|
+
Native.SetProcessDPIAware();
|
|
200
|
+
var a = Out.Parse(argsJson);
|
|
201
|
+
currentMonitor = a.ContainsKey("monitor") ? Convert.ToInt32(a["monitor"]) : -1;
|
|
202
|
+
targetMaxFps = a.ContainsKey("max_fps") ? Convert.ToInt32(a["max_fps"]) : 15;
|
|
203
|
+
if (targetMaxFps < 1) targetMaxFps = 1;
|
|
204
|
+
if (targetMaxFps > 30) targetMaxFps = 30;
|
|
205
|
+
targetMaxWidth = a.ContainsKey("max_width") ? Convert.ToInt32(a["max_width"]) : 1366;
|
|
206
|
+
targetQuality = a.ContainsKey("quality") ? Convert.ToInt32(a["quality"]) : 65;
|
|
207
|
+
|
|
208
|
+
currentBounds = CastScreen.GetBounds(currentMonitor);
|
|
209
|
+
|
|
210
|
+
// Luồng đọc lệnh điều khiển từ xa từ stdin
|
|
211
|
+
var inputThread = new Thread(ReadInputLoop);
|
|
212
|
+
inputThread.IsBackground = true;
|
|
213
|
+
inputThread.Start();
|
|
214
|
+
|
|
215
|
+
var readyMsg = Out.Msg("ready");
|
|
216
|
+
readyMsg["monitor"] = currentMonitor;
|
|
217
|
+
readyMsg["screens"] = CastScreen.GetScreens();
|
|
218
|
+
readyMsg["bounds"] = new Dictionary<string, object> {
|
|
219
|
+
{ "x", currentBounds.X }, { "y", currentBounds.Y },
|
|
220
|
+
{ "width", currentBounds.Width }, { "height", currentBounds.Height }
|
|
221
|
+
};
|
|
222
|
+
Out.Send(readyMsg);
|
|
223
|
+
|
|
224
|
+
var clock = System.Diagnostics.Stopwatch.StartNew();
|
|
225
|
+
byte[] lastThumb = null;
|
|
226
|
+
long lastSendMs = 0;
|
|
227
|
+
|
|
228
|
+
while (running) {
|
|
229
|
+
long frameStartMs = clock.ElapsedMilliseconds;
|
|
230
|
+
long targetFrameInterval = 1000 / Math.Max(1, targetMaxFps);
|
|
231
|
+
try {
|
|
232
|
+
currentBounds = CastScreen.GetBounds(currentMonitor);
|
|
233
|
+
using (var rawBmp = CastScreen.Grab(currentBounds)) {
|
|
234
|
+
var thumb = CastScreen.Thumb(rawBmp);
|
|
235
|
+
double diff = CastScreen.Diff(thumb, lastThumb);
|
|
236
|
+
long nowMs = clock.ElapsedMilliseconds;
|
|
237
|
+
bool forceKeepAlive = (nowMs - lastSendMs) >= 1000;
|
|
238
|
+
|
|
239
|
+
if (diff > 4.5 || forceKeepAlive || lastThumb == null) {
|
|
240
|
+
lastThumb = thumb;
|
|
241
|
+
lastSendMs = nowMs;
|
|
242
|
+
|
|
243
|
+
using (var scaled = CastScreen.Scale(new Bitmap(rawBmp), targetMaxWidth)) {
|
|
244
|
+
currentImageWidth = scaled.Width;
|
|
245
|
+
currentImageHeight = scaled.Height;
|
|
246
|
+
byte[] jpegBytes = CastScreen.Jpeg(scaled, targetQuality);
|
|
247
|
+
string base64 = Convert.ToBase64String(jpegBytes);
|
|
248
|
+
|
|
249
|
+
var frameMsg = Out.Msg("frame");
|
|
250
|
+
frameMsg["image"] = base64;
|
|
251
|
+
frameMsg["width"] = currentImageWidth;
|
|
252
|
+
frameMsg["height"] = currentImageHeight;
|
|
253
|
+
frameMsg["screenWidth"] = currentBounds.Width;
|
|
254
|
+
frameMsg["screenHeight"] = currentBounds.Height;
|
|
255
|
+
frameMsg["monitor"] = currentMonitor;
|
|
256
|
+
frameMsg["screens"] = CastScreen.GetScreens();
|
|
257
|
+
frameMsg["ts"] = nowMs;
|
|
258
|
+
Out.Send(frameMsg);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
} catch (Exception ex) {
|
|
263
|
+
var warnMsg = Out.Msg("warn");
|
|
264
|
+
warnMsg["error"] = ex.Message;
|
|
265
|
+
Out.Send(warnMsg);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
long elapsed = clock.ElapsedMilliseconds - frameStartMs;
|
|
269
|
+
long sleepMs = targetFrameInterval - elapsed;
|
|
270
|
+
if (sleepMs > 5) {
|
|
271
|
+
Thread.Sleep((int)sleepMs);
|
|
272
|
+
} else {
|
|
273
|
+
Thread.Sleep(5);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
var stoppedMsg = Out.Msg("stopped");
|
|
278
|
+
Out.Send(stoppedMsg);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static void ReadInputLoop() {
|
|
282
|
+
try {
|
|
283
|
+
string line;
|
|
284
|
+
while (running && (line = Console.In.ReadLine()) != null) {
|
|
285
|
+
line = line.Trim();
|
|
286
|
+
if (string.IsNullOrEmpty(line)) continue;
|
|
287
|
+
if (line == "stop") {
|
|
288
|
+
running = false;
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
var cmd = Out.Parse(line);
|
|
292
|
+
string op = cmd.ContainsKey("op") ? Convert.ToString(cmd["op"]) : "";
|
|
293
|
+
if (op == "stop") {
|
|
294
|
+
running = false;
|
|
295
|
+
break;
|
|
296
|
+
} else if (op == "input") {
|
|
297
|
+
HandleInput(cmd);
|
|
298
|
+
} else if (op == "config") {
|
|
299
|
+
HandleConfig(cmd);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
} catch {
|
|
303
|
+
running = false;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
static void HandleConfig(Dictionary<string, object> cmd) {
|
|
308
|
+
try {
|
|
309
|
+
if (cmd.ContainsKey("max_fps")) {
|
|
310
|
+
int fps = Convert.ToInt32(cmd["max_fps"]);
|
|
311
|
+
if (fps >= 1 && fps <= 30) targetMaxFps = fps;
|
|
312
|
+
}
|
|
313
|
+
if (cmd.ContainsKey("quality")) {
|
|
314
|
+
int q = Convert.ToInt32(cmd["quality"]);
|
|
315
|
+
if (q >= 10 && q <= 95) targetQuality = q;
|
|
316
|
+
}
|
|
317
|
+
if (cmd.ContainsKey("max_width")) {
|
|
318
|
+
int w = Convert.ToInt32(cmd["max_width"]);
|
|
319
|
+
if (w >= 640 && w <= 2560) targetMaxWidth = w;
|
|
320
|
+
}
|
|
321
|
+
if (cmd.ContainsKey("monitor")) {
|
|
322
|
+
int m = Convert.ToInt32(cmd["monitor"]);
|
|
323
|
+
if (m >= -1 && m < Screen.AllScreens.Length) {
|
|
324
|
+
currentMonitor = m;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
var ack = Out.Msg("config_ack");
|
|
328
|
+
ack["max_fps"] = targetMaxFps;
|
|
329
|
+
ack["quality"] = targetQuality;
|
|
330
|
+
ack["max_width"] = targetMaxWidth;
|
|
331
|
+
ack["monitor"] = currentMonitor;
|
|
332
|
+
Out.Send(ack);
|
|
333
|
+
} catch {
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
static byte KeyNameToVk(string name) {
|
|
338
|
+
if (string.IsNullOrEmpty(name)) return 0;
|
|
339
|
+
switch (name.ToLower().Trim()) {
|
|
340
|
+
case "win": case "windows": case "lwin": case "meta": case "cmd": return 0x5B;
|
|
341
|
+
case "rwin": return 0x5C;
|
|
342
|
+
case "ctrl": case "control": case "lctrl": return 0x11;
|
|
343
|
+
case "rctrl": return 0xA3;
|
|
344
|
+
case "alt": case "menu": case "lalt": return 0x12;
|
|
345
|
+
case "ralt": return 0xA5;
|
|
346
|
+
case "shift": case "lshift": return 0x10;
|
|
347
|
+
case "rshift": return 0xA1;
|
|
348
|
+
case "tab": return 0x09;
|
|
349
|
+
case "escape": case "esc": return 0x1B;
|
|
350
|
+
case "enter": case "return": return 0x0D;
|
|
351
|
+
case "backspace": return 0x08;
|
|
352
|
+
case "space": return 0x20;
|
|
353
|
+
case "delete": case "del": return 0x2E;
|
|
354
|
+
case "insert": case "ins": return 0x2D;
|
|
355
|
+
case "home": return 0x24;
|
|
356
|
+
case "end": return 0x23;
|
|
357
|
+
case "pageup": return 0x21;
|
|
358
|
+
case "pagedown": return 0x22;
|
|
359
|
+
case "up": case "arrowup": return 0x26;
|
|
360
|
+
case "down": case "arrowdown": return 0x28;
|
|
361
|
+
case "left": case "arrowleft": return 0x25;
|
|
362
|
+
case "right": case "arrowright": return 0x27;
|
|
363
|
+
case "f1": return 0x70;
|
|
364
|
+
case "f2": return 0x71;
|
|
365
|
+
case "f3": return 0x72;
|
|
366
|
+
case "f4": return 0x73;
|
|
367
|
+
case "f5": return 0x74;
|
|
368
|
+
case "f6": return 0x75;
|
|
369
|
+
case "f7": return 0x76;
|
|
370
|
+
case "f8": return 0x77;
|
|
371
|
+
case "f9": return 0x78;
|
|
372
|
+
case "f10": return 0x79;
|
|
373
|
+
case "f11": return 0x7A;
|
|
374
|
+
case "f12": return 0x7B;
|
|
375
|
+
case "printscreen": case "prtsc": return 0x2C;
|
|
376
|
+
default:
|
|
377
|
+
if (name.Length == 1) {
|
|
378
|
+
char ch = char.ToUpperInvariant(name[0]);
|
|
379
|
+
if (ch >= 'A' && ch <= 'Z') return (byte)ch;
|
|
380
|
+
if (ch >= '0' && ch <= '9') return (byte)ch;
|
|
381
|
+
}
|
|
382
|
+
return 0;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
static void HandleInput(Dictionary<string, object> cmd) {
|
|
387
|
+
try {
|
|
388
|
+
string action = cmd.ContainsKey("action") ? Convert.ToString(cmd["action"]) : "";
|
|
389
|
+
double rx = cmd.ContainsKey("x") ? Convert.ToDouble(cmd["x"]) : 0;
|
|
390
|
+
double ry = cmd.ContainsKey("y") ? Convert.ToDouble(cmd["y"]) : 0;
|
|
391
|
+
|
|
392
|
+
int targetX = currentBounds.X;
|
|
393
|
+
int targetY = currentBounds.Y;
|
|
394
|
+
if (currentImageWidth > 0 && currentImageHeight > 0) {
|
|
395
|
+
targetX += (int)Math.Round((rx / currentImageWidth) * currentBounds.Width);
|
|
396
|
+
targetY += (int)Math.Round((ry / currentImageHeight) * currentBounds.Height);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (action == "move") {
|
|
400
|
+
Native.SetCursorPos(targetX, targetY);
|
|
401
|
+
} else if (action == "click") {
|
|
402
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
403
|
+
Native.SetCursorPos(targetX, targetY);
|
|
404
|
+
Thread.Sleep(10);
|
|
405
|
+
if (button == 2) {
|
|
406
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
407
|
+
Thread.Sleep(15);
|
|
408
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
409
|
+
} else if (button == 1) {
|
|
410
|
+
Native.mouse_event(Native.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
|
|
411
|
+
Thread.Sleep(15);
|
|
412
|
+
Native.mouse_event(Native.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
|
|
413
|
+
} else {
|
|
414
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
415
|
+
Thread.Sleep(15);
|
|
416
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
417
|
+
}
|
|
418
|
+
} else if (action == "dblclick") {
|
|
419
|
+
Native.SetCursorPos(targetX, targetY);
|
|
420
|
+
Thread.Sleep(10);
|
|
421
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
422
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
423
|
+
Thread.Sleep(40);
|
|
424
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
425
|
+
Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
426
|
+
} else if (action == "rightclick") {
|
|
427
|
+
Native.SetCursorPos(targetX, targetY);
|
|
428
|
+
Thread.Sleep(10);
|
|
429
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
430
|
+
Thread.Sleep(15);
|
|
431
|
+
Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
432
|
+
} else if (action == "mousedown") {
|
|
433
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
434
|
+
Native.SetCursorPos(targetX, targetY);
|
|
435
|
+
if (button == 2) Native.mouse_event(Native.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
|
|
436
|
+
else if (button == 1) Native.mouse_event(Native.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
|
|
437
|
+
else Native.mouse_event(Native.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
|
|
438
|
+
} else if (action == "mouseup") {
|
|
439
|
+
int button = cmd.ContainsKey("button") ? Convert.ToInt32(cmd["button"]) : 0;
|
|
440
|
+
Native.SetCursorPos(targetX, targetY);
|
|
441
|
+
if (button == 2) Native.mouse_event(Native.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
|
|
442
|
+
else if (button == 1) Native.mouse_event(Native.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
|
|
443
|
+
else Native.mouse_event(Native.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
|
|
444
|
+
} else if (action == "scroll") {
|
|
445
|
+
int deltaY = cmd.ContainsKey("deltaY") ? Convert.ToInt32(cmd["deltaY"]) : 0;
|
|
446
|
+
Native.SetCursorPos(targetX, targetY);
|
|
447
|
+
Native.mouse_event(Native.MOUSEEVENTF_WHEEL, 0, 0, -deltaY, 0);
|
|
448
|
+
} else if (action == "type") {
|
|
449
|
+
string text = cmd.ContainsKey("text") ? Convert.ToString(cmd["text"]) : "";
|
|
450
|
+
if (!string.IsNullOrEmpty(text)) {
|
|
451
|
+
for (int i = 0; i < text.Length; i++) {
|
|
452
|
+
char c = text[i];
|
|
453
|
+
Native.keybd_event(0, (ushort)c, Native.KEYEVENTF_UNICODE, UIntPtr.Zero);
|
|
454
|
+
Native.keybd_event(0, (ushort)c, Native.KEYEVENTF_UNICODE | Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
} else if (action == "key") {
|
|
458
|
+
string key = cmd.ContainsKey("key") ? Convert.ToString(cmd["key"]) : "";
|
|
459
|
+
HandleSpecialKey(key);
|
|
460
|
+
} else if (action == "hotkey") {
|
|
461
|
+
System.Collections.ArrayList keys = cmd.ContainsKey("keys") ? cmd["keys"] as System.Collections.ArrayList : null;
|
|
462
|
+
if (keys != null && keys.Count > 0) {
|
|
463
|
+
var vks = new List<byte>();
|
|
464
|
+
for (int i = 0; i < keys.Count; i++) {
|
|
465
|
+
byte vk = KeyNameToVk(Convert.ToString(keys[i]));
|
|
466
|
+
if (vk != 0) vks.Add(vk);
|
|
467
|
+
}
|
|
468
|
+
for (int i = 0; i < vks.Count; i++) {
|
|
469
|
+
Native.keybd_event(vks[i], 0, 0, UIntPtr.Zero);
|
|
470
|
+
Thread.Sleep(10);
|
|
471
|
+
}
|
|
472
|
+
Thread.Sleep(40);
|
|
473
|
+
for (int i = vks.Count - 1; i >= 0; i--) {
|
|
474
|
+
Native.keybd_event(vks[i], 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
475
|
+
Thread.Sleep(10);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
} catch {
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
static void HandleSpecialKey(string key) {
|
|
484
|
+
byte vk = KeyNameToVk(key);
|
|
485
|
+
if (vk != 0) {
|
|
486
|
+
Native.keybd_event(vk, 0, 0, UIntPtr.Zero);
|
|
487
|
+
Thread.Sleep(15);
|
|
488
|
+
Native.keybd_event(vk, 0, Native.KEYEVENTF_KEYUP, UIntPtr.Zero);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
`;
|
|
494
|
+
|
|
495
|
+
export const DESKTOP_CAST_PS1 = String.raw`
|
|
496
|
+
$ErrorActionPreference = 'Stop'
|
|
497
|
+
$dll = $env:AIVIN_CAST_DLL
|
|
498
|
+
$src = $env:AIVIN_CAST_SRC
|
|
499
|
+
$refs = @('System.Drawing', 'System.Windows.Forms', 'System.Web.Extensions')
|
|
500
|
+
foreach ($r in $refs) { Add-Type -AssemblyName $r }
|
|
501
|
+
if ($dll -and (Test-Path -LiteralPath $dll)) {
|
|
502
|
+
Add-Type -Path $dll
|
|
503
|
+
} else {
|
|
504
|
+
Add-Type -TypeDefinition ([IO.File]::ReadAllText($src)) -ReferencedAssemblies $refs -OutputAssembly $dll -OutputType Library
|
|
505
|
+
Add-Type -Path $dll
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
[AivinDesktopCast.CastHost]::Run($env:AIVIN_CAST_ARGS)
|
|
509
|
+
} catch {
|
|
510
|
+
$m = @{ t = 'error'; error = $_.Exception.Message } | ConvertTo-Json -Compress
|
|
511
|
+
[Console]::Out.WriteLine($m)
|
|
512
|
+
exit 1
|
|
513
|
+
}
|
|
514
|
+
`;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Desktop Cast WebRTC transport. Frames use an unreliable/unordered data channel so an old desktop
|
|
3
|
+
* frame can never queue ahead of a fresh one. This is deliberately a transport layer: Windows
|
|
4
|
+
* capture stays in desktopCast.mjs and JPEG-over-Socket.IO remains the no-native-runtime fallback.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const MAX_BUFFERED_BYTES = 512 * 1024;
|
|
8
|
+
|
|
9
|
+
export function packDesktopFrame(frame) {
|
|
10
|
+
const image = Buffer.from(String(frame?.image || ''), 'base64');
|
|
11
|
+
const meta = Buffer.from(JSON.stringify({
|
|
12
|
+
t: 'frame', sessionId: frame?.sessionId, seq: frame?.seq, ts: frame?.ts,
|
|
13
|
+
width: frame?.width, height: frame?.height,
|
|
14
|
+
screenWidth: frame?.screenWidth, screenHeight: frame?.screenHeight,
|
|
15
|
+
mime: 'image/jpeg', bytes: image.length,
|
|
16
|
+
}) + '\n');
|
|
17
|
+
return Buffer.concat([meta, image]);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createDesktopWebRtc({ emitSignal = () => {}, emitStatus = () => {}, loadRuntime } = {}) {
|
|
21
|
+
const peers = new Map();
|
|
22
|
+
let runtimePromise;
|
|
23
|
+
let sequence = 0;
|
|
24
|
+
const load = () => (runtimePromise ||= (loadRuntime
|
|
25
|
+
? loadRuntime()
|
|
26
|
+
: import('@roamhq/wrtc').catch(() => null)));
|
|
27
|
+
|
|
28
|
+
async function start({ sessionId, viewerId, iceServers = [] }) {
|
|
29
|
+
close(viewerId);
|
|
30
|
+
const wrtc = await load();
|
|
31
|
+
const RTCPeerConnection = wrtc?.RTCPeerConnection || wrtc?.default?.RTCPeerConnection;
|
|
32
|
+
if (!RTCPeerConnection) return false;
|
|
33
|
+
const pc = new RTCPeerConnection({ iceServers });
|
|
34
|
+
const channel = pc.createDataChannel('aivin-desktop-frames', { ordered: false, maxRetransmits: 0 });
|
|
35
|
+
const peer = { pc, channel, sessionId, viewerId, open: false, dropped: 0 };
|
|
36
|
+
peers.set(viewerId, peer);
|
|
37
|
+
channel.binaryType = 'arraybuffer';
|
|
38
|
+
channel.onopen = () => {
|
|
39
|
+
peer.open = true;
|
|
40
|
+
emitStatus({ session_id: sessionId, viewer_id: viewerId, state: 'connected', transport: 'webrtc' });
|
|
41
|
+
};
|
|
42
|
+
channel.onclose = () => { peer.open = false; };
|
|
43
|
+
pc.onicecandidate = ({ candidate }) => {
|
|
44
|
+
emitSignal({ session_id: sessionId, viewer_id: viewerId, type: 'ice', candidate: candidate?.toJSON?.() || candidate || null });
|
|
45
|
+
};
|
|
46
|
+
pc.onconnectionstatechange = () => {
|
|
47
|
+
if (['failed', 'closed', 'disconnected'].includes(pc.connectionState)) {
|
|
48
|
+
peer.open = false;
|
|
49
|
+
emitStatus({ session_id: sessionId, viewer_id: viewerId, state: pc.connectionState, transport: 'jpeg' });
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
await pc.setLocalDescription(await pc.createOffer());
|
|
53
|
+
emitSignal({ session_id: sessionId, viewer_id: viewerId, type: 'offer', sdp: pc.localDescription?.sdp });
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function signal(msg) {
|
|
58
|
+
const peer = peers.get(String(msg?.viewer_id || ''));
|
|
59
|
+
if (!peer || msg?.session_id !== peer.sessionId) return false;
|
|
60
|
+
if (msg.type === 'answer' && msg.sdp) {
|
|
61
|
+
await peer.pc.setRemoteDescription({ type: 'answer', sdp: msg.sdp });
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
if (msg.type === 'ice') {
|
|
65
|
+
await peer.pc.addIceCandidate(msg.candidate || null).catch(() => {});
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function push(frame) {
|
|
72
|
+
const packet = packDesktopFrame({ ...frame, seq: ++sequence });
|
|
73
|
+
let sent = 0;
|
|
74
|
+
for (const peer of peers.values()) {
|
|
75
|
+
if (!peer.open || peer.channel.readyState !== 'open') continue;
|
|
76
|
+
if (peer.channel.bufferedAmount > MAX_BUFFERED_BYTES) { peer.dropped++; continue; }
|
|
77
|
+
try { peer.channel.send(packet); sent++; } catch { peer.dropped++; }
|
|
78
|
+
}
|
|
79
|
+
return sent;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function anyOpen() { return [...peers.values()].some(p => p.open && p.channel.readyState === 'open'); }
|
|
83
|
+
function needsFallback() { return peers.size === 0 || [...peers.values()].some(p => !p.open || p.channel.readyState !== 'open'); }
|
|
84
|
+
|
|
85
|
+
function close(viewerId) {
|
|
86
|
+
const peer = peers.get(String(viewerId || ''));
|
|
87
|
+
if (!peer) return;
|
|
88
|
+
peers.delete(String(viewerId));
|
|
89
|
+
try { peer.channel.close(); } catch { /* already closed */ }
|
|
90
|
+
try { peer.pc.close(); } catch { /* already closed */ }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function closeAll() { for (const id of [...peers.keys()]) close(id); }
|
|
94
|
+
return { start, signal, push, anyOpen, needsFallback, close, closeAll };
|
|
95
|
+
}
|