dsh-convenient-window 0.2.0-beta.3 → 0.2.0
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 +117 -26
- package/host/assets.js +33 -33
- package/host/helper.js +214 -214
- package/host/native-service.js +156 -156
- package/host/panel-controller.js +493 -493
- package/host/settings.js +98 -98
- package/lib/client.js +1 -1
- package/lib/cw-dsh-native.exe +0 -0
- package/lib/model.mjs +651 -540
- package/lib/native-identity.json +1 -1
- package/lib/panel.html +14 -14
- package/lib/panel.js +51 -51
- package/package.json +79 -79
- package/payload/magic-corners-helper.exe +0 -0
- package/runtime-assets.json +29 -29
package/host/helper.js
CHANGED
|
@@ -1,214 +1,214 @@
|
|
|
1
|
-
import { execFile } from 'node:child_process';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import { readFile } from 'node:fs/promises';
|
|
4
|
-
import { join, dirname } from 'node:path';
|
|
5
|
-
import { NativeService } from './native-service.js';
|
|
6
|
-
|
|
7
|
-
import { promisify } from 'node:util';
|
|
8
|
-
import { assets, verifyPayload } from './assets.js';
|
|
9
|
-
import { atomicJSON, initial, runtimeConfig } from './settings.js';
|
|
10
|
-
|
|
11
|
-
const exec = promisify(execFile);
|
|
12
|
-
const system = join(process.env.SystemRoot || 'C:\\Windows', 'System32');
|
|
13
|
-
const PORT = 56873; // Fixed by the shared engine; never probe its protocol to discover its owner.
|
|
14
|
-
const commandOptions = {
|
|
15
|
-
windowsHide: true,
|
|
16
|
-
timeout: 9500,
|
|
17
|
-
maxBuffer: 8 * 1024 * 1024,
|
|
18
|
-
encoding: 'utf8',
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// Use numeric endpoints and PID, not localized status text or a WMI provider.
|
|
22
|
-
export function listenerPids(table, port = PORT) {
|
|
23
|
-
const result = new Set();
|
|
24
|
-
for (const line of table.split(/\r?\n/)) {
|
|
25
|
-
const [protocol, local, remote, , pid] = line.trim().split(/\s+/);
|
|
26
|
-
if (
|
|
27
|
-
protocol !== 'TCP' ||
|
|
28
|
-
!local.endsWith(`:${port}`) ||
|
|
29
|
-
!['0.0.0.0:0', '[::]:0'].includes(remote)
|
|
30
|
-
)
|
|
31
|
-
continue;
|
|
32
|
-
if (!/^\d+$/.test(pid)) throw Error('owner-unavailable');
|
|
33
|
-
result.add(Number(pid));
|
|
34
|
-
}
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
async function checkListener(expectedPid) {
|
|
38
|
-
let table;
|
|
39
|
-
try {
|
|
40
|
-
({ stdout: table } = await exec(
|
|
41
|
-
join(system, 'netstat.exe'),
|
|
42
|
-
['-ano'],
|
|
43
|
-
commandOptions,
|
|
44
|
-
));
|
|
45
|
-
} catch {
|
|
46
|
-
throw Error('owner-unavailable');
|
|
47
|
-
}
|
|
48
|
-
const pids = listenerPids(table);
|
|
49
|
-
if (!pids.size) return false;
|
|
50
|
-
if (pids.size !== 1 || !pids.has(expectedPid)) throw Error('helper-in-use');
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
export function matchesReady(d, pid, elevated = false) {
|
|
54
|
-
return (
|
|
55
|
-
!!d &&
|
|
56
|
-
d.processId === pid &&
|
|
57
|
-
d.version === assets.version &&
|
|
58
|
-
d.protocolVersion === 7 &&
|
|
59
|
-
d.schemaVersion === 9 &&
|
|
60
|
-
d.desktopManaged === true &&
|
|
61
|
-
d.elevated === elevated
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
export class Helper {
|
|
65
|
-
constructor(dir) {
|
|
66
|
-
this.dir = dir;
|
|
67
|
-
this.native = new NativeService(dirname(dir));
|
|
68
|
-
this.native.onExit = () => {
|
|
69
|
-
if (this.child) this.fault = 'native-disconnected';
|
|
70
|
-
this.invalidate();
|
|
71
|
-
};
|
|
72
|
-
this.child = this.ws = this.pending = this.fault = null;
|
|
73
|
-
this.ready = false;
|
|
74
|
-
}
|
|
75
|
-
get running() {
|
|
76
|
-
return !!(this.ready && this.ws?.readyState === 1 && this.child);
|
|
77
|
-
}
|
|
78
|
-
invalidate() {
|
|
79
|
-
this.ready = false;
|
|
80
|
-
this.child = null;
|
|
81
|
-
const ws = this.ws;
|
|
82
|
-
this.ws = null;
|
|
83
|
-
ws?.close();
|
|
84
|
-
this.pending?.fail('helper-disconnected');
|
|
85
|
-
this.onEnd?.();
|
|
86
|
-
}
|
|
87
|
-
async start() {
|
|
88
|
-
if (this.running) return { alreadyRunning: true, elevated: this.elevated };
|
|
89
|
-
if (process.platform !== 'win32' || process.arch !== 'x64')
|
|
90
|
-
throw Error('unsupported-platform');
|
|
91
|
-
await verifyPayload().catch(() => {
|
|
92
|
-
throw Error('payload-invalid');
|
|
93
|
-
});
|
|
94
|
-
await checkListener(null);
|
|
95
|
-
const inert = await runtimeConfig(initial());
|
|
96
|
-
inert.enabled = false;
|
|
97
|
-
await atomicJSON(this.dir, 'config.json', inert);
|
|
98
|
-
return this.launch('start', null);
|
|
99
|
-
}
|
|
100
|
-
async elevation(enabled) {
|
|
101
|
-
if (!this.running) throw Error('helper-disconnected');
|
|
102
|
-
this.invalidate();
|
|
103
|
-
return this.launch('elevation', { enabled });
|
|
104
|
-
}
|
|
105
|
-
async launch(op, data) {
|
|
106
|
-
const session = await this.native.open();
|
|
107
|
-
let info;
|
|
108
|
-
try {
|
|
109
|
-
info = await this.native.send(session, op, data);
|
|
110
|
-
if (!Number.isInteger(info.pid) || !(await checkListener(info.pid)))
|
|
111
|
-
throw Error('owner-unavailable');
|
|
112
|
-
const token = (
|
|
113
|
-
await readFile(join(this.dir, 'auth-token'), 'utf8')
|
|
114
|
-
).trim();
|
|
115
|
-
if (!/^[a-f0-9]{64}$/i.test(token)) throw Error('helper-start-failed');
|
|
116
|
-
this.child = { pid: info.pid };
|
|
117
|
-
this.elevated = !!info.elevated;
|
|
118
|
-
this.fault = null;
|
|
119
|
-
const ws = new WebSocket(`ws://127.0.0.1:${PORT}`, token);
|
|
120
|
-
this.ws = ws;
|
|
121
|
-
await new Promise((ok, no) => {
|
|
122
|
-
const fail = () => {
|
|
123
|
-
clearTimeout(timer);
|
|
124
|
-
no(Error('helper-disconnected'));
|
|
125
|
-
};
|
|
126
|
-
const timer = setTimeout(() => no(Error('helper-timeout')), 7000);
|
|
127
|
-
ws.addEventListener('error', fail);
|
|
128
|
-
ws.addEventListener('close', () => {
|
|
129
|
-
if (this.ws === ws) {
|
|
130
|
-
this.fault = 'helper-disconnected';
|
|
131
|
-
this.invalidate();
|
|
132
|
-
void this.native.send(session, 'stop').catch(() => {});
|
|
133
|
-
}
|
|
134
|
-
fail();
|
|
135
|
-
});
|
|
136
|
-
ws.addEventListener('message', (event) => {
|
|
137
|
-
if (this.ws !== ws) return;
|
|
138
|
-
let m;
|
|
139
|
-
try {
|
|
140
|
-
m = JSON.parse(event.data);
|
|
141
|
-
} catch {
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
if (!this.ready && m.type === 'helper.ready') {
|
|
145
|
-
if (!matchesReady(m.data, info.pid, info.elevated)) {
|
|
146
|
-
clearTimeout(timer);
|
|
147
|
-
no(Error('incompatible-helper'));
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
clearTimeout(timer);
|
|
151
|
-
this.ready = true;
|
|
152
|
-
this.onFrame?.(m);
|
|
153
|
-
ok();
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
if (!this.ready) return;
|
|
157
|
-
this.onFrame?.(m);
|
|
158
|
-
const p = this.pending;
|
|
159
|
-
if (!p || m.data?.requestId !== p.id) return;
|
|
160
|
-
if (m.type === 'config.applied' && m.data.revision === p.rev) p.ok();
|
|
161
|
-
else if (m.type === 'runtime.error') p.fail('apply-failed');
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
return info;
|
|
165
|
-
} catch (e) {
|
|
166
|
-
this.invalidate();
|
|
167
|
-
await this.native.send(session, 'stop').catch(() => {});
|
|
168
|
-
throw e;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
async apply(config, rev, labels) {
|
|
172
|
-
if (!this.running) throw Error('helper-disconnected');
|
|
173
|
-
if (this.pending) throw Error('apply-busy');
|
|
174
|
-
const id = randomUUID();
|
|
175
|
-
return new Promise((resolve, reject) => {
|
|
176
|
-
const finish = (code) => {
|
|
177
|
-
if (this.pending?.id !== id) return;
|
|
178
|
-
clearTimeout(timer);
|
|
179
|
-
this.pending = null;
|
|
180
|
-
code ? reject(Error(code)) : resolve();
|
|
181
|
-
};
|
|
182
|
-
const timer = setTimeout(() => finish('helper-timeout'), 5500);
|
|
183
|
-
this.pending = { id, rev, ok: () => finish(), fail: finish };
|
|
184
|
-
try {
|
|
185
|
-
this.ws.send(
|
|
186
|
-
JSON.stringify({
|
|
187
|
-
id,
|
|
188
|
-
type: 'config.update',
|
|
189
|
-
time: Date.now(),
|
|
190
|
-
data: {
|
|
191
|
-
protocolVersion: 7,
|
|
192
|
-
revision: rev,
|
|
193
|
-
config,
|
|
194
|
-
gestureLabels: labels,
|
|
195
|
-
},
|
|
196
|
-
}),
|
|
197
|
-
);
|
|
198
|
-
} catch {
|
|
199
|
-
finish('helper-disconnected');
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
async stop() {
|
|
204
|
-
this.invalidate();
|
|
205
|
-
if (this.native.session)
|
|
206
|
-
await this.native.send(this.native.session, 'stop');
|
|
207
|
-
this.elevated = false;
|
|
208
|
-
this.fault = null;
|
|
209
|
-
}
|
|
210
|
-
async dispose() {
|
|
211
|
-
this.invalidate();
|
|
212
|
-
await this.native.close();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import { join, dirname } from 'node:path';
|
|
5
|
+
import { NativeService } from './native-service.js';
|
|
6
|
+
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { assets, verifyPayload } from './assets.js';
|
|
9
|
+
import { atomicJSON, initial, runtimeConfig } from './settings.js';
|
|
10
|
+
|
|
11
|
+
const exec = promisify(execFile);
|
|
12
|
+
const system = join(process.env.SystemRoot || 'C:\\Windows', 'System32');
|
|
13
|
+
const PORT = 56873; // Fixed by the shared engine; never probe its protocol to discover its owner.
|
|
14
|
+
const commandOptions = {
|
|
15
|
+
windowsHide: true,
|
|
16
|
+
timeout: 9500,
|
|
17
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
18
|
+
encoding: 'utf8',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Use numeric endpoints and PID, not localized status text or a WMI provider.
|
|
22
|
+
export function listenerPids(table, port = PORT) {
|
|
23
|
+
const result = new Set();
|
|
24
|
+
for (const line of table.split(/\r?\n/)) {
|
|
25
|
+
const [protocol, local, remote, , pid] = line.trim().split(/\s+/);
|
|
26
|
+
if (
|
|
27
|
+
protocol !== 'TCP' ||
|
|
28
|
+
!local.endsWith(`:${port}`) ||
|
|
29
|
+
!['0.0.0.0:0', '[::]:0'].includes(remote)
|
|
30
|
+
)
|
|
31
|
+
continue;
|
|
32
|
+
if (!/^\d+$/.test(pid)) throw Error('owner-unavailable');
|
|
33
|
+
result.add(Number(pid));
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
async function checkListener(expectedPid) {
|
|
38
|
+
let table;
|
|
39
|
+
try {
|
|
40
|
+
({ stdout: table } = await exec(
|
|
41
|
+
join(system, 'netstat.exe'),
|
|
42
|
+
['-ano'],
|
|
43
|
+
commandOptions,
|
|
44
|
+
));
|
|
45
|
+
} catch {
|
|
46
|
+
throw Error('owner-unavailable');
|
|
47
|
+
}
|
|
48
|
+
const pids = listenerPids(table);
|
|
49
|
+
if (!pids.size) return false;
|
|
50
|
+
if (pids.size !== 1 || !pids.has(expectedPid)) throw Error('helper-in-use');
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
export function matchesReady(d, pid, elevated = false) {
|
|
54
|
+
return (
|
|
55
|
+
!!d &&
|
|
56
|
+
d.processId === pid &&
|
|
57
|
+
d.version === assets.version &&
|
|
58
|
+
d.protocolVersion === 7 &&
|
|
59
|
+
d.schemaVersion === 9 &&
|
|
60
|
+
d.desktopManaged === true &&
|
|
61
|
+
d.elevated === elevated
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
export class Helper {
|
|
65
|
+
constructor(dir) {
|
|
66
|
+
this.dir = dir;
|
|
67
|
+
this.native = new NativeService(dirname(dir));
|
|
68
|
+
this.native.onExit = () => {
|
|
69
|
+
if (this.child) this.fault = 'native-disconnected';
|
|
70
|
+
this.invalidate();
|
|
71
|
+
};
|
|
72
|
+
this.child = this.ws = this.pending = this.fault = null;
|
|
73
|
+
this.ready = false;
|
|
74
|
+
}
|
|
75
|
+
get running() {
|
|
76
|
+
return !!(this.ready && this.ws?.readyState === 1 && this.child);
|
|
77
|
+
}
|
|
78
|
+
invalidate() {
|
|
79
|
+
this.ready = false;
|
|
80
|
+
this.child = null;
|
|
81
|
+
const ws = this.ws;
|
|
82
|
+
this.ws = null;
|
|
83
|
+
ws?.close();
|
|
84
|
+
this.pending?.fail('helper-disconnected');
|
|
85
|
+
this.onEnd?.();
|
|
86
|
+
}
|
|
87
|
+
async start() {
|
|
88
|
+
if (this.running) return { alreadyRunning: true, elevated: this.elevated };
|
|
89
|
+
if (process.platform !== 'win32' || process.arch !== 'x64')
|
|
90
|
+
throw Error('unsupported-platform');
|
|
91
|
+
await verifyPayload().catch(() => {
|
|
92
|
+
throw Error('payload-invalid');
|
|
93
|
+
});
|
|
94
|
+
await checkListener(null);
|
|
95
|
+
const inert = await runtimeConfig(initial());
|
|
96
|
+
inert.enabled = false;
|
|
97
|
+
await atomicJSON(this.dir, 'config.json', inert);
|
|
98
|
+
return this.launch('start', null);
|
|
99
|
+
}
|
|
100
|
+
async elevation(enabled) {
|
|
101
|
+
if (!this.running) throw Error('helper-disconnected');
|
|
102
|
+
this.invalidate();
|
|
103
|
+
return this.launch('elevation', { enabled });
|
|
104
|
+
}
|
|
105
|
+
async launch(op, data) {
|
|
106
|
+
const session = await this.native.open();
|
|
107
|
+
let info;
|
|
108
|
+
try {
|
|
109
|
+
info = await this.native.send(session, op, data);
|
|
110
|
+
if (!Number.isInteger(info.pid) || !(await checkListener(info.pid)))
|
|
111
|
+
throw Error('owner-unavailable');
|
|
112
|
+
const token = (
|
|
113
|
+
await readFile(join(this.dir, 'auth-token'), 'utf8')
|
|
114
|
+
).trim();
|
|
115
|
+
if (!/^[a-f0-9]{64}$/i.test(token)) throw Error('helper-start-failed');
|
|
116
|
+
this.child = { pid: info.pid };
|
|
117
|
+
this.elevated = !!info.elevated;
|
|
118
|
+
this.fault = null;
|
|
119
|
+
const ws = new WebSocket(`ws://127.0.0.1:${PORT}`, token);
|
|
120
|
+
this.ws = ws;
|
|
121
|
+
await new Promise((ok, no) => {
|
|
122
|
+
const fail = () => {
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
no(Error('helper-disconnected'));
|
|
125
|
+
};
|
|
126
|
+
const timer = setTimeout(() => no(Error('helper-timeout')), 7000);
|
|
127
|
+
ws.addEventListener('error', fail);
|
|
128
|
+
ws.addEventListener('close', () => {
|
|
129
|
+
if (this.ws === ws) {
|
|
130
|
+
this.fault = 'helper-disconnected';
|
|
131
|
+
this.invalidate();
|
|
132
|
+
void this.native.send(session, 'stop').catch(() => {});
|
|
133
|
+
}
|
|
134
|
+
fail();
|
|
135
|
+
});
|
|
136
|
+
ws.addEventListener('message', (event) => {
|
|
137
|
+
if (this.ws !== ws) return;
|
|
138
|
+
let m;
|
|
139
|
+
try {
|
|
140
|
+
m = JSON.parse(event.data);
|
|
141
|
+
} catch {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (!this.ready && m.type === 'helper.ready') {
|
|
145
|
+
if (!matchesReady(m.data, info.pid, info.elevated)) {
|
|
146
|
+
clearTimeout(timer);
|
|
147
|
+
no(Error('incompatible-helper'));
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
clearTimeout(timer);
|
|
151
|
+
this.ready = true;
|
|
152
|
+
this.onFrame?.(m);
|
|
153
|
+
ok();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (!this.ready) return;
|
|
157
|
+
this.onFrame?.(m);
|
|
158
|
+
const p = this.pending;
|
|
159
|
+
if (!p || m.data?.requestId !== p.id) return;
|
|
160
|
+
if (m.type === 'config.applied' && m.data.revision === p.rev) p.ok();
|
|
161
|
+
else if (m.type === 'runtime.error') p.fail('apply-failed');
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
return info;
|
|
165
|
+
} catch (e) {
|
|
166
|
+
this.invalidate();
|
|
167
|
+
await this.native.send(session, 'stop').catch(() => {});
|
|
168
|
+
throw e;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async apply(config, rev, labels) {
|
|
172
|
+
if (!this.running) throw Error('helper-disconnected');
|
|
173
|
+
if (this.pending) throw Error('apply-busy');
|
|
174
|
+
const id = randomUUID();
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
const finish = (code) => {
|
|
177
|
+
if (this.pending?.id !== id) return;
|
|
178
|
+
clearTimeout(timer);
|
|
179
|
+
this.pending = null;
|
|
180
|
+
code ? reject(Error(code)) : resolve();
|
|
181
|
+
};
|
|
182
|
+
const timer = setTimeout(() => finish('helper-timeout'), 5500);
|
|
183
|
+
this.pending = { id, rev, ok: () => finish(), fail: finish };
|
|
184
|
+
try {
|
|
185
|
+
this.ws.send(
|
|
186
|
+
JSON.stringify({
|
|
187
|
+
id,
|
|
188
|
+
type: 'config.update',
|
|
189
|
+
time: Date.now(),
|
|
190
|
+
data: {
|
|
191
|
+
protocolVersion: 7,
|
|
192
|
+
revision: rev,
|
|
193
|
+
config,
|
|
194
|
+
gestureLabels: labels,
|
|
195
|
+
},
|
|
196
|
+
}),
|
|
197
|
+
);
|
|
198
|
+
} catch {
|
|
199
|
+
finish('helper-disconnected');
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
async stop() {
|
|
204
|
+
this.invalidate();
|
|
205
|
+
if (this.native.session)
|
|
206
|
+
await this.native.send(this.native.session, 'stop');
|
|
207
|
+
this.elevated = false;
|
|
208
|
+
this.fault = null;
|
|
209
|
+
}
|
|
210
|
+
async dispose() {
|
|
211
|
+
this.invalidate();
|
|
212
|
+
await this.native.close();
|
|
213
|
+
}
|
|
214
|
+
}
|