lightman-agent 1.0.18 → 1.0.21

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.
Files changed (52) hide show
  1. package/agent.config.json +22 -23
  2. package/agent.config.template.json +30 -31
  3. package/bin/cms-agent.js +269 -248
  4. package/package.json +1 -1
  5. package/public/assets/index-CcBNCz6h.css +1 -1
  6. package/public/assets/index-D9QHMG8k.js +1 -1
  7. package/public/assets/index-H-8HDl46.js +1 -1
  8. package/public/assets/index-YodeiCia.css +1 -1
  9. package/public/assets/index-legacy-DWtNM8y7.js +41 -41
  10. package/public/assets/polyfills-legacy-DyVYWHbW.js +4 -4
  11. package/scripts/guardian.ps1 +50 -124
  12. package/scripts/install-windows.ps1 +60 -116
  13. package/scripts/lightman-agent.logrotate +12 -12
  14. package/scripts/lightman-agent.service +38 -38
  15. package/scripts/reinstall-windows.ps1 +26 -26
  16. package/scripts/restore-desktop.ps1 +32 -32
  17. package/scripts/setup.ps1 +17 -22
  18. package/scripts/sync-display.mjs +20 -20
  19. package/scripts/uninstall-windows.ps1 +54 -54
  20. package/src/commands/display.ts +177 -177
  21. package/src/commands/kiosk.ts +113 -113
  22. package/src/commands/maintenance.ts +106 -106
  23. package/src/commands/network.ts +129 -129
  24. package/src/commands/power.ts +163 -163
  25. package/src/commands/rpi.ts +45 -45
  26. package/src/commands/screenshot.ts +166 -166
  27. package/src/commands/serial.ts +17 -17
  28. package/src/commands/update.ts +124 -124
  29. package/src/index.ts +173 -90
  30. package/src/lib/config.ts +2 -3
  31. package/src/lib/identity.ts +40 -40
  32. package/src/lib/logger.ts +137 -137
  33. package/src/lib/platform.ts +10 -10
  34. package/src/lib/rpi.ts +180 -180
  35. package/src/lib/screenMap.ts +135 -0
  36. package/src/lib/screens.ts +128 -128
  37. package/src/lib/types.ts +176 -177
  38. package/src/services/commands.ts +107 -107
  39. package/src/services/health.ts +161 -161
  40. package/src/services/localEvents.ts +60 -60
  41. package/src/services/logForwarder.ts +72 -72
  42. package/src/services/multiScreenKiosk.ts +116 -83
  43. package/src/services/oscBridge.ts +186 -186
  44. package/src/services/powerScheduler.ts +260 -260
  45. package/src/services/provisioning.ts +120 -122
  46. package/src/services/serialBridge.ts +230 -230
  47. package/src/services/serviceLauncher.ts +183 -183
  48. package/src/services/staticServer.ts +226 -226
  49. package/src/services/updater.ts +249 -249
  50. package/src/services/watchdog.ts +310 -310
  51. package/src/services/websocket.ts +152 -152
  52. package/tsconfig.json +28 -28
@@ -1,226 +1,226 @@
1
- import { createServer, request as httpRequest, type IncomingMessage, type ServerResponse } from 'http';
2
- import { createReadStream, statSync, existsSync } from 'fs';
3
- import { resolve, extname, join } from 'path';
4
- import { WebSocket, WebSocketServer } from 'ws';
5
- import type { Logger } from '../lib/logger.js';
6
-
7
- const MIME: Record<string, string> = {
8
- '.html': 'text/html; charset=utf-8',
9
- '.js': 'application/javascript',
10
- '.css': 'text/css',
11
- '.json': 'application/json',
12
- '.png': 'image/png',
13
- '.jpg': 'image/jpeg',
14
- '.svg': 'image/svg+xml',
15
- '.ico': 'image/x-icon',
16
- '.woff': 'font/woff',
17
- '.woff2': 'font/woff2',
18
- '.ttf': 'font/ttf',
19
- '.mp4': 'video/mp4',
20
- '.webm': 'video/webm',
21
- };
22
-
23
- export class StaticServer {
24
- private port: number;
25
- private distPath: string;
26
- private serverUrl: string;
27
- private logger: Logger;
28
- private server: ReturnType<typeof createServer> | null = null;
29
-
30
- constructor(port: number, distPath: string, serverUrl: string, logger: Logger) {
31
- this.port = port;
32
- this.distPath = distPath;
33
- this.serverUrl = serverUrl;
34
- this.logger = logger;
35
- }
36
-
37
- start(): void {
38
- if (!existsSync(this.distPath)) {
39
- this.logger.warn(`StaticServer: dist path not found: ${this.distPath}`);
40
- return;
41
- }
42
-
43
- this.server = createServer((req: IncomingMessage, res: ServerResponse) => {
44
- this.handle(req, res);
45
- });
46
-
47
- // Proxy WebSocket upgrades (/ws/*) to the real server
48
- this.server.on('upgrade', (req, socket, head) => {
49
- const upstream = new URL(req.url || '/', this.serverUrl);
50
- const wsUrl = upstream.toString().replace(/^http/, 'ws');
51
- this.logger.debug(`WS proxy: ${req.url} → ${wsUrl}`);
52
-
53
- const upstreamWs = new WebSocket(wsUrl, {
54
- headers: { ...req.headers, host: new URL(this.serverUrl).host },
55
- });
56
-
57
- upstreamWs.on('open', () => {
58
- const wss = new WebSocketServer({ noServer: true });
59
- wss.handleUpgrade(req, socket, head, (clientWs) => {
60
- // Pipe client ↔ upstream
61
- clientWs.on('message', (data, isBinary) => {
62
- if (upstreamWs.readyState === WebSocket.OPEN) {
63
- upstreamWs.send(data, { binary: isBinary });
64
- }
65
- });
66
- upstreamWs.on('message', (data, isBinary) => {
67
- if (clientWs.readyState === WebSocket.OPEN) {
68
- clientWs.send(data, { binary: isBinary });
69
- }
70
- });
71
- clientWs.on('close', () => upstreamWs.close());
72
- upstreamWs.on('close', () => clientWs.close());
73
- clientWs.on('error', () => upstreamWs.close());
74
- upstreamWs.on('error', () => clientWs.close());
75
- });
76
- });
77
-
78
- upstreamWs.on('error', (err) => {
79
- this.logger.warn(`WS proxy error: ${err.message}`);
80
- socket.destroy();
81
- });
82
- });
83
-
84
- this.server.listen(this.port, '0.0.0.0', () => {
85
- this.logger.info(`Display static server listening on port ${this.port} (proxy → ${this.serverUrl})`);
86
- });
87
-
88
- this.server.on('error', (err) => {
89
- this.logger.error(`StaticServer error: ${err.message}`);
90
- });
91
- }
92
-
93
- stop(): void {
94
- if (this.server) {
95
- this.server.close();
96
- this.server = null;
97
- this.logger.info('Display static server stopped');
98
- }
99
- }
100
-
101
- private handle(req: IncomingMessage, res: ServerResponse): void {
102
- const rawUrl = req.url || '/';
103
- const path = rawUrl.split('?')[0];
104
-
105
- // Proxy /api/* and /storage/* to the real server
106
- if (path.startsWith('/api/') || path.startsWith('/storage/')) {
107
- this.proxyHttp(req, res);
108
- return;
109
- }
110
-
111
- // Proxy /display/* to the server first (always get latest build).
112
- // Only fall back to local files if server is unreachable.
113
- if (path.startsWith('/display')) {
114
- this.proxyWithFallback(req, res);
115
- return;
116
- }
117
-
118
- // Serve static files for non-display paths
119
- this.serveStatic(req, res);
120
- }
121
-
122
- private proxyHttp(req: IncomingMessage, res: ServerResponse): void {
123
- const target = new URL(this.serverUrl);
124
- const options = {
125
- hostname: target.hostname,
126
- port: target.port || 80,
127
- path: req.url,
128
- method: req.method,
129
- headers: { ...req.headers, host: target.host },
130
- };
131
-
132
- const proxy = httpRequest(options, (upstreamRes) => {
133
- res.writeHead(upstreamRes.statusCode || 502, upstreamRes.headers);
134
- upstreamRes.pipe(res);
135
- });
136
-
137
- proxy.on('error', (err) => {
138
- this.logger.warn(`HTTP proxy error: ${err.message}`);
139
- if (!res.headersSent) {
140
- res.writeHead(502);
141
- res.end('Bad Gateway');
142
- }
143
- });
144
-
145
- req.pipe(proxy);
146
- }
147
-
148
- /**
149
- * Try to proxy the request to the real server (latest display build).
150
- * If the server is unreachable, fall back to local static files.
151
- */
152
- private proxyWithFallback(req: IncomingMessage, res: ServerResponse): void {
153
- const target = new URL(this.serverUrl);
154
- let fell = false;
155
- const fallback = () => {
156
- if (fell || res.headersSent) return;
157
- fell = true;
158
- this.logger.debug(`Proxy failed for ${req.url}, serving from local files`);
159
- this.serveStatic(req, res);
160
- };
161
-
162
- const options = {
163
- hostname: target.hostname,
164
- port: target.port || 80,
165
- path: req.url,
166
- method: req.method,
167
- headers: { ...req.headers, host: target.host },
168
- timeout: 3000,
169
- };
170
-
171
- const proxy = httpRequest(options, (upstreamRes) => {
172
- const statusCode = upstreamRes.statusCode || 502;
173
-
174
- // If upstream display route is unavailable/misconfigured,
175
- // serve the local bundled display instead of surfacing server errors.
176
- if (statusCode >= 400) {
177
- upstreamRes.resume();
178
- fallback();
179
- return;
180
- }
181
-
182
- res.writeHead(statusCode, upstreamRes.headers);
183
- upstreamRes.pipe(res);
184
- });
185
-
186
- proxy.on('error', fallback);
187
- proxy.on('timeout', () => { proxy.destroy(); fallback(); });
188
-
189
- req.pipe(proxy);
190
- }
191
-
192
- private serveStatic(req: IncomingMessage, res: ServerResponse): void {
193
- const rawUrl = (req.url || '/').split('?')[0];
194
-
195
- // Strip /display prefix to map to dist files
196
- let filePath = rawUrl.startsWith('/display')
197
- ? rawUrl.slice('/display'.length) || '/'
198
- : rawUrl;
199
-
200
- let absPath = resolve(this.distPath, filePath.replace(/^\//, ''));
201
-
202
- // Security: stay inside distPath
203
- if (!absPath.startsWith(this.distPath)) {
204
- res.writeHead(403);
205
- res.end('Forbidden');
206
- return;
207
- }
208
-
209
- // SPA fallback
210
- if (!existsSync(absPath) || statSync(absPath).isDirectory()) {
211
- absPath = join(this.distPath, 'index.html');
212
- }
213
-
214
- if (!existsSync(absPath)) {
215
- res.writeHead(404);
216
- res.end('Not Found');
217
- return;
218
- }
219
-
220
- const ext = extname(absPath).toLowerCase();
221
- const mime = MIME[ext] || 'application/octet-stream';
222
-
223
- res.writeHead(200, { 'Content-Type': mime });
224
- createReadStream(absPath).pipe(res);
225
- }
226
- }
1
+ import { createServer, request as httpRequest, type IncomingMessage, type ServerResponse } from 'http';
2
+ import { createReadStream, statSync, existsSync } from 'fs';
3
+ import { resolve, extname, join } from 'path';
4
+ import { WebSocket, WebSocketServer } from 'ws';
5
+ import type { Logger } from '../lib/logger.js';
6
+
7
+ const MIME: Record<string, string> = {
8
+ '.html': 'text/html; charset=utf-8',
9
+ '.js': 'application/javascript',
10
+ '.css': 'text/css',
11
+ '.json': 'application/json',
12
+ '.png': 'image/png',
13
+ '.jpg': 'image/jpeg',
14
+ '.svg': 'image/svg+xml',
15
+ '.ico': 'image/x-icon',
16
+ '.woff': 'font/woff',
17
+ '.woff2': 'font/woff2',
18
+ '.ttf': 'font/ttf',
19
+ '.mp4': 'video/mp4',
20
+ '.webm': 'video/webm',
21
+ };
22
+
23
+ export class StaticServer {
24
+ private port: number;
25
+ private distPath: string;
26
+ private serverUrl: string;
27
+ private logger: Logger;
28
+ private server: ReturnType<typeof createServer> | null = null;
29
+
30
+ constructor(port: number, distPath: string, serverUrl: string, logger: Logger) {
31
+ this.port = port;
32
+ this.distPath = distPath;
33
+ this.serverUrl = serverUrl;
34
+ this.logger = logger;
35
+ }
36
+
37
+ start(): void {
38
+ if (!existsSync(this.distPath)) {
39
+ this.logger.warn(`StaticServer: dist path not found: ${this.distPath}`);
40
+ return;
41
+ }
42
+
43
+ this.server = createServer((req: IncomingMessage, res: ServerResponse) => {
44
+ this.handle(req, res);
45
+ });
46
+
47
+ // Proxy WebSocket upgrades (/ws/*) to the real server
48
+ this.server.on('upgrade', (req, socket, head) => {
49
+ const upstream = new URL(req.url || '/', this.serverUrl);
50
+ const wsUrl = upstream.toString().replace(/^http/, 'ws');
51
+ this.logger.debug(`WS proxy: ${req.url} → ${wsUrl}`);
52
+
53
+ const upstreamWs = new WebSocket(wsUrl, {
54
+ headers: { ...req.headers, host: new URL(this.serverUrl).host },
55
+ });
56
+
57
+ upstreamWs.on('open', () => {
58
+ const wss = new WebSocketServer({ noServer: true });
59
+ wss.handleUpgrade(req, socket, head, (clientWs) => {
60
+ // Pipe client ↔ upstream
61
+ clientWs.on('message', (data, isBinary) => {
62
+ if (upstreamWs.readyState === WebSocket.OPEN) {
63
+ upstreamWs.send(data, { binary: isBinary });
64
+ }
65
+ });
66
+ upstreamWs.on('message', (data, isBinary) => {
67
+ if (clientWs.readyState === WebSocket.OPEN) {
68
+ clientWs.send(data, { binary: isBinary });
69
+ }
70
+ });
71
+ clientWs.on('close', () => upstreamWs.close());
72
+ upstreamWs.on('close', () => clientWs.close());
73
+ clientWs.on('error', () => upstreamWs.close());
74
+ upstreamWs.on('error', () => clientWs.close());
75
+ });
76
+ });
77
+
78
+ upstreamWs.on('error', (err) => {
79
+ this.logger.warn(`WS proxy error: ${err.message}`);
80
+ socket.destroy();
81
+ });
82
+ });
83
+
84
+ this.server.listen(this.port, '0.0.0.0', () => {
85
+ this.logger.info(`Display static server listening on port ${this.port} (proxy → ${this.serverUrl})`);
86
+ });
87
+
88
+ this.server.on('error', (err) => {
89
+ this.logger.error(`StaticServer error: ${err.message}`);
90
+ });
91
+ }
92
+
93
+ stop(): void {
94
+ if (this.server) {
95
+ this.server.close();
96
+ this.server = null;
97
+ this.logger.info('Display static server stopped');
98
+ }
99
+ }
100
+
101
+ private handle(req: IncomingMessage, res: ServerResponse): void {
102
+ const rawUrl = req.url || '/';
103
+ const path = rawUrl.split('?')[0];
104
+
105
+ // Proxy /api/* and /storage/* to the real server
106
+ if (path.startsWith('/api/') || path.startsWith('/storage/')) {
107
+ this.proxyHttp(req, res);
108
+ return;
109
+ }
110
+
111
+ // Proxy /display/* to the server first (always get latest build).
112
+ // Only fall back to local files if server is unreachable.
113
+ if (path.startsWith('/display')) {
114
+ this.proxyWithFallback(req, res);
115
+ return;
116
+ }
117
+
118
+ // Serve static files for non-display paths
119
+ this.serveStatic(req, res);
120
+ }
121
+
122
+ private proxyHttp(req: IncomingMessage, res: ServerResponse): void {
123
+ const target = new URL(this.serverUrl);
124
+ const options = {
125
+ hostname: target.hostname,
126
+ port: target.port || 80,
127
+ path: req.url,
128
+ method: req.method,
129
+ headers: { ...req.headers, host: target.host },
130
+ };
131
+
132
+ const proxy = httpRequest(options, (upstreamRes) => {
133
+ res.writeHead(upstreamRes.statusCode || 502, upstreamRes.headers);
134
+ upstreamRes.pipe(res);
135
+ });
136
+
137
+ proxy.on('error', (err) => {
138
+ this.logger.warn(`HTTP proxy error: ${err.message}`);
139
+ if (!res.headersSent) {
140
+ res.writeHead(502);
141
+ res.end('Bad Gateway');
142
+ }
143
+ });
144
+
145
+ req.pipe(proxy);
146
+ }
147
+
148
+ /**
149
+ * Try to proxy the request to the real server (latest display build).
150
+ * If the server is unreachable, fall back to local static files.
151
+ */
152
+ private proxyWithFallback(req: IncomingMessage, res: ServerResponse): void {
153
+ const target = new URL(this.serverUrl);
154
+ let fell = false;
155
+ const fallback = () => {
156
+ if (fell || res.headersSent) return;
157
+ fell = true;
158
+ this.logger.debug(`Proxy failed for ${req.url}, serving from local files`);
159
+ this.serveStatic(req, res);
160
+ };
161
+
162
+ const options = {
163
+ hostname: target.hostname,
164
+ port: target.port || 80,
165
+ path: req.url,
166
+ method: req.method,
167
+ headers: { ...req.headers, host: target.host },
168
+ timeout: 3000,
169
+ };
170
+
171
+ const proxy = httpRequest(options, (upstreamRes) => {
172
+ const statusCode = upstreamRes.statusCode || 502;
173
+
174
+ // If upstream display route is unavailable/misconfigured,
175
+ // serve the local bundled display instead of surfacing server errors.
176
+ if (statusCode >= 400) {
177
+ upstreamRes.resume();
178
+ fallback();
179
+ return;
180
+ }
181
+
182
+ res.writeHead(statusCode, upstreamRes.headers);
183
+ upstreamRes.pipe(res);
184
+ });
185
+
186
+ proxy.on('error', fallback);
187
+ proxy.on('timeout', () => { proxy.destroy(); fallback(); });
188
+
189
+ req.pipe(proxy);
190
+ }
191
+
192
+ private serveStatic(req: IncomingMessage, res: ServerResponse): void {
193
+ const rawUrl = (req.url || '/').split('?')[0];
194
+
195
+ // Strip /display prefix to map to dist files
196
+ let filePath = rawUrl.startsWith('/display')
197
+ ? rawUrl.slice('/display'.length) || '/'
198
+ : rawUrl;
199
+
200
+ let absPath = resolve(this.distPath, filePath.replace(/^\//, ''));
201
+
202
+ // Security: stay inside distPath
203
+ if (!absPath.startsWith(this.distPath)) {
204
+ res.writeHead(403);
205
+ res.end('Forbidden');
206
+ return;
207
+ }
208
+
209
+ // SPA fallback
210
+ if (!existsSync(absPath) || statSync(absPath).isDirectory()) {
211
+ absPath = join(this.distPath, 'index.html');
212
+ }
213
+
214
+ if (!existsSync(absPath)) {
215
+ res.writeHead(404);
216
+ res.end('Not Found');
217
+ return;
218
+ }
219
+
220
+ const ext = extname(absPath).toLowerCase();
221
+ const mime = MIME[ext] || 'application/octet-stream';
222
+
223
+ res.writeHead(200, { 'Content-Type': mime });
224
+ createReadStream(absPath).pipe(res);
225
+ }
226
+ }