nikcli-remote 1.0.9 → 1.0.11
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/dist/chunk-5ANLFHJV.js +1145 -0
- package/dist/chunk-GI5RMYH6.js +37 -0
- package/dist/index.cjs +4683 -6082
- package/dist/index.d.cts +98 -60
- package/dist/index.d.ts +98 -60
- package/dist/index.js +228 -7
- package/dist/{localtunnel-XT32JGNN.js → localtunnel-6DCQIYU6.js} +1 -1
- package/dist/server-VOW4RWJA.js +7 -0
- package/package.json +1 -1
- package/src/index.ts +57 -12
- package/src/server.ts +113 -108
- package/src/tunnel.ts +24 -0
- package/dist/chunk-3IFHAOGG.js +0 -2747
- package/dist/chunk-MCKGQKYU.js +0 -15
- package/dist/server-MBJQBTJF.js +0 -7
package/dist/chunk-3IFHAOGG.js
DELETED
|
@@ -1,2747 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__commonJS,
|
|
3
|
-
__require
|
|
4
|
-
} from "./chunk-MCKGQKYU.js";
|
|
5
|
-
|
|
6
|
-
// node_modules/node-pty/lib/utils.js
|
|
7
|
-
var require_utils = __commonJS({
|
|
8
|
-
"node_modules/node-pty/lib/utils.js"(exports) {
|
|
9
|
-
"use strict";
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.loadNativeModule = exports.assign = void 0;
|
|
12
|
-
function assign(target) {
|
|
13
|
-
var sources = [];
|
|
14
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
15
|
-
sources[_i - 1] = arguments[_i];
|
|
16
|
-
}
|
|
17
|
-
sources.forEach(function(source) {
|
|
18
|
-
return Object.keys(source).forEach(function(key) {
|
|
19
|
-
return target[key] = source[key];
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
return target;
|
|
23
|
-
}
|
|
24
|
-
exports.assign = assign;
|
|
25
|
-
function loadNativeModule(name) {
|
|
26
|
-
var dirs = ["build/Release", "build/Debug", "prebuilds/" + process.platform + "-" + process.arch];
|
|
27
|
-
var relative = ["..", "."];
|
|
28
|
-
var lastError;
|
|
29
|
-
for (var _i = 0, dirs_1 = dirs; _i < dirs_1.length; _i++) {
|
|
30
|
-
var d = dirs_1[_i];
|
|
31
|
-
for (var _a = 0, relative_1 = relative; _a < relative_1.length; _a++) {
|
|
32
|
-
var r = relative_1[_a];
|
|
33
|
-
var dir = r + "/" + d + "/";
|
|
34
|
-
try {
|
|
35
|
-
return { dir, module: __require(dir + "/" + name + ".node") };
|
|
36
|
-
} catch (e) {
|
|
37
|
-
lastError = e;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
throw new Error("Failed to load native module: " + name + ".node, checked: " + dirs.join(", ") + ": " + lastError);
|
|
42
|
-
}
|
|
43
|
-
exports.loadNativeModule = loadNativeModule;
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// node_modules/node-pty/lib/eventEmitter2.js
|
|
48
|
-
var require_eventEmitter2 = __commonJS({
|
|
49
|
-
"node_modules/node-pty/lib/eventEmitter2.js"(exports) {
|
|
50
|
-
"use strict";
|
|
51
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
exports.EventEmitter2 = void 0;
|
|
53
|
-
var EventEmitter22 = (
|
|
54
|
-
/** @class */
|
|
55
|
-
(function() {
|
|
56
|
-
function EventEmitter23() {
|
|
57
|
-
this._listeners = [];
|
|
58
|
-
}
|
|
59
|
-
Object.defineProperty(EventEmitter23.prototype, "event", {
|
|
60
|
-
get: function() {
|
|
61
|
-
var _this = this;
|
|
62
|
-
if (!this._event) {
|
|
63
|
-
this._event = function(listener) {
|
|
64
|
-
_this._listeners.push(listener);
|
|
65
|
-
var disposable = {
|
|
66
|
-
dispose: function() {
|
|
67
|
-
for (var i = 0; i < _this._listeners.length; i++) {
|
|
68
|
-
if (_this._listeners[i] === listener) {
|
|
69
|
-
_this._listeners.splice(i, 1);
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
return disposable;
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
return this._event;
|
|
79
|
-
},
|
|
80
|
-
enumerable: false,
|
|
81
|
-
configurable: true
|
|
82
|
-
});
|
|
83
|
-
EventEmitter23.prototype.fire = function(data) {
|
|
84
|
-
var queue = [];
|
|
85
|
-
for (var i = 0; i < this._listeners.length; i++) {
|
|
86
|
-
queue.push(this._listeners[i]);
|
|
87
|
-
}
|
|
88
|
-
for (var i = 0; i < queue.length; i++) {
|
|
89
|
-
queue[i].call(void 0, data);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
return EventEmitter23;
|
|
93
|
-
})()
|
|
94
|
-
);
|
|
95
|
-
exports.EventEmitter2 = EventEmitter22;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// node_modules/node-pty/lib/terminal.js
|
|
100
|
-
var require_terminal = __commonJS({
|
|
101
|
-
"node_modules/node-pty/lib/terminal.js"(exports) {
|
|
102
|
-
"use strict";
|
|
103
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
104
|
-
exports.Terminal = exports.DEFAULT_ROWS = exports.DEFAULT_COLS = void 0;
|
|
105
|
-
var events_1 = __require("events");
|
|
106
|
-
var eventEmitter2_1 = require_eventEmitter2();
|
|
107
|
-
exports.DEFAULT_COLS = 80;
|
|
108
|
-
exports.DEFAULT_ROWS = 24;
|
|
109
|
-
var FLOW_CONTROL_PAUSE = "";
|
|
110
|
-
var FLOW_CONTROL_RESUME = "";
|
|
111
|
-
var Terminal = (
|
|
112
|
-
/** @class */
|
|
113
|
-
(function() {
|
|
114
|
-
function Terminal2(opt) {
|
|
115
|
-
this._pid = 0;
|
|
116
|
-
this._fd = 0;
|
|
117
|
-
this._cols = 0;
|
|
118
|
-
this._rows = 0;
|
|
119
|
-
this._readable = false;
|
|
120
|
-
this._writable = false;
|
|
121
|
-
this._onData = new eventEmitter2_1.EventEmitter2();
|
|
122
|
-
this._onExit = new eventEmitter2_1.EventEmitter2();
|
|
123
|
-
this._internalee = new events_1.EventEmitter();
|
|
124
|
-
this.handleFlowControl = !!(opt === null || opt === void 0 ? void 0 : opt.handleFlowControl);
|
|
125
|
-
this._flowControlPause = (opt === null || opt === void 0 ? void 0 : opt.flowControlPause) || FLOW_CONTROL_PAUSE;
|
|
126
|
-
this._flowControlResume = (opt === null || opt === void 0 ? void 0 : opt.flowControlResume) || FLOW_CONTROL_RESUME;
|
|
127
|
-
if (!opt) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
this._checkType("name", opt.name ? opt.name : void 0, "string");
|
|
131
|
-
this._checkType("cols", opt.cols ? opt.cols : void 0, "number");
|
|
132
|
-
this._checkType("rows", opt.rows ? opt.rows : void 0, "number");
|
|
133
|
-
this._checkType("cwd", opt.cwd ? opt.cwd : void 0, "string");
|
|
134
|
-
this._checkType("env", opt.env ? opt.env : void 0, "object");
|
|
135
|
-
this._checkType("uid", opt.uid ? opt.uid : void 0, "number");
|
|
136
|
-
this._checkType("gid", opt.gid ? opt.gid : void 0, "number");
|
|
137
|
-
this._checkType("encoding", opt.encoding ? opt.encoding : void 0, "string");
|
|
138
|
-
}
|
|
139
|
-
Object.defineProperty(Terminal2.prototype, "onData", {
|
|
140
|
-
get: function() {
|
|
141
|
-
return this._onData.event;
|
|
142
|
-
},
|
|
143
|
-
enumerable: false,
|
|
144
|
-
configurable: true
|
|
145
|
-
});
|
|
146
|
-
Object.defineProperty(Terminal2.prototype, "onExit", {
|
|
147
|
-
get: function() {
|
|
148
|
-
return this._onExit.event;
|
|
149
|
-
},
|
|
150
|
-
enumerable: false,
|
|
151
|
-
configurable: true
|
|
152
|
-
});
|
|
153
|
-
Object.defineProperty(Terminal2.prototype, "pid", {
|
|
154
|
-
get: function() {
|
|
155
|
-
return this._pid;
|
|
156
|
-
},
|
|
157
|
-
enumerable: false,
|
|
158
|
-
configurable: true
|
|
159
|
-
});
|
|
160
|
-
Object.defineProperty(Terminal2.prototype, "cols", {
|
|
161
|
-
get: function() {
|
|
162
|
-
return this._cols;
|
|
163
|
-
},
|
|
164
|
-
enumerable: false,
|
|
165
|
-
configurable: true
|
|
166
|
-
});
|
|
167
|
-
Object.defineProperty(Terminal2.prototype, "rows", {
|
|
168
|
-
get: function() {
|
|
169
|
-
return this._rows;
|
|
170
|
-
},
|
|
171
|
-
enumerable: false,
|
|
172
|
-
configurable: true
|
|
173
|
-
});
|
|
174
|
-
Terminal2.prototype.write = function(data) {
|
|
175
|
-
if (this.handleFlowControl) {
|
|
176
|
-
if (data === this._flowControlPause) {
|
|
177
|
-
this.pause();
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
if (data === this._flowControlResume) {
|
|
181
|
-
this.resume();
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
this._write(data);
|
|
186
|
-
};
|
|
187
|
-
Terminal2.prototype._forwardEvents = function() {
|
|
188
|
-
var _this = this;
|
|
189
|
-
this.on("data", function(e) {
|
|
190
|
-
return _this._onData.fire(e);
|
|
191
|
-
});
|
|
192
|
-
this.on("exit", function(exitCode, signal) {
|
|
193
|
-
return _this._onExit.fire({ exitCode, signal });
|
|
194
|
-
});
|
|
195
|
-
};
|
|
196
|
-
Terminal2.prototype._checkType = function(name, value, type, allowArray) {
|
|
197
|
-
if (allowArray === void 0) {
|
|
198
|
-
allowArray = false;
|
|
199
|
-
}
|
|
200
|
-
if (value === void 0) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
if (allowArray) {
|
|
204
|
-
if (Array.isArray(value)) {
|
|
205
|
-
value.forEach(function(v, i) {
|
|
206
|
-
if (typeof v !== type) {
|
|
207
|
-
throw new Error(name + "[" + i + "] must be a " + type + " (not a " + typeof v[i] + ")");
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
if (typeof value !== type) {
|
|
214
|
-
throw new Error(name + " must be a " + type + " (not a " + typeof value + ")");
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
Terminal2.prototype.end = function(data) {
|
|
218
|
-
this._socket.end(data);
|
|
219
|
-
};
|
|
220
|
-
Terminal2.prototype.pipe = function(dest, options) {
|
|
221
|
-
return this._socket.pipe(dest, options);
|
|
222
|
-
};
|
|
223
|
-
Terminal2.prototype.pause = function() {
|
|
224
|
-
return this._socket.pause();
|
|
225
|
-
};
|
|
226
|
-
Terminal2.prototype.resume = function() {
|
|
227
|
-
return this._socket.resume();
|
|
228
|
-
};
|
|
229
|
-
Terminal2.prototype.setEncoding = function(encoding) {
|
|
230
|
-
if (this._socket._decoder) {
|
|
231
|
-
delete this._socket._decoder;
|
|
232
|
-
}
|
|
233
|
-
if (encoding) {
|
|
234
|
-
this._socket.setEncoding(encoding);
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
Terminal2.prototype.addListener = function(eventName, listener) {
|
|
238
|
-
this.on(eventName, listener);
|
|
239
|
-
};
|
|
240
|
-
Terminal2.prototype.on = function(eventName, listener) {
|
|
241
|
-
if (eventName === "close") {
|
|
242
|
-
this._internalee.on("close", listener);
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
this._socket.on(eventName, listener);
|
|
246
|
-
};
|
|
247
|
-
Terminal2.prototype.emit = function(eventName) {
|
|
248
|
-
var args = [];
|
|
249
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
250
|
-
args[_i - 1] = arguments[_i];
|
|
251
|
-
}
|
|
252
|
-
if (eventName === "close") {
|
|
253
|
-
return this._internalee.emit.apply(this._internalee, arguments);
|
|
254
|
-
}
|
|
255
|
-
return this._socket.emit.apply(this._socket, arguments);
|
|
256
|
-
};
|
|
257
|
-
Terminal2.prototype.listeners = function(eventName) {
|
|
258
|
-
return this._socket.listeners(eventName);
|
|
259
|
-
};
|
|
260
|
-
Terminal2.prototype.removeListener = function(eventName, listener) {
|
|
261
|
-
this._socket.removeListener(eventName, listener);
|
|
262
|
-
};
|
|
263
|
-
Terminal2.prototype.removeAllListeners = function(eventName) {
|
|
264
|
-
this._socket.removeAllListeners(eventName);
|
|
265
|
-
};
|
|
266
|
-
Terminal2.prototype.once = function(eventName, listener) {
|
|
267
|
-
this._socket.once(eventName, listener);
|
|
268
|
-
};
|
|
269
|
-
Terminal2.prototype._close = function() {
|
|
270
|
-
this._socket.readable = false;
|
|
271
|
-
this.write = function() {
|
|
272
|
-
};
|
|
273
|
-
this.end = function() {
|
|
274
|
-
};
|
|
275
|
-
this._writable = false;
|
|
276
|
-
this._readable = false;
|
|
277
|
-
};
|
|
278
|
-
Terminal2.prototype._parseEnv = function(env) {
|
|
279
|
-
var keys = Object.keys(env || {});
|
|
280
|
-
var pairs = [];
|
|
281
|
-
for (var i = 0; i < keys.length; i++) {
|
|
282
|
-
if (keys[i] === void 0) {
|
|
283
|
-
continue;
|
|
284
|
-
}
|
|
285
|
-
pairs.push(keys[i] + "=" + env[keys[i]]);
|
|
286
|
-
}
|
|
287
|
-
return pairs;
|
|
288
|
-
};
|
|
289
|
-
return Terminal2;
|
|
290
|
-
})()
|
|
291
|
-
);
|
|
292
|
-
exports.Terminal = Terminal;
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
// node_modules/node-pty/lib/shared/conout.js
|
|
297
|
-
var require_conout = __commonJS({
|
|
298
|
-
"node_modules/node-pty/lib/shared/conout.js"(exports) {
|
|
299
|
-
"use strict";
|
|
300
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
301
|
-
exports.getWorkerPipeName = void 0;
|
|
302
|
-
function getWorkerPipeName(conoutPipeName) {
|
|
303
|
-
return conoutPipeName + "-worker";
|
|
304
|
-
}
|
|
305
|
-
exports.getWorkerPipeName = getWorkerPipeName;
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
// node_modules/node-pty/lib/windowsConoutConnection.js
|
|
310
|
-
var require_windowsConoutConnection = __commonJS({
|
|
311
|
-
"node_modules/node-pty/lib/windowsConoutConnection.js"(exports) {
|
|
312
|
-
"use strict";
|
|
313
|
-
var __awaiter = exports && exports.__awaiter || function(thisArg, _arguments, P, generator) {
|
|
314
|
-
function adopt(value) {
|
|
315
|
-
return value instanceof P ? value : new P(function(resolve) {
|
|
316
|
-
resolve(value);
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
return new (P || (P = Promise))(function(resolve, reject) {
|
|
320
|
-
function fulfilled(value) {
|
|
321
|
-
try {
|
|
322
|
-
step(generator.next(value));
|
|
323
|
-
} catch (e) {
|
|
324
|
-
reject(e);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
function rejected(value) {
|
|
328
|
-
try {
|
|
329
|
-
step(generator["throw"](value));
|
|
330
|
-
} catch (e) {
|
|
331
|
-
reject(e);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
function step(result) {
|
|
335
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
336
|
-
}
|
|
337
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
338
|
-
});
|
|
339
|
-
};
|
|
340
|
-
var __generator = exports && exports.__generator || function(thisArg, body) {
|
|
341
|
-
var _ = { label: 0, sent: function() {
|
|
342
|
-
if (t[0] & 1) throw t[1];
|
|
343
|
-
return t[1];
|
|
344
|
-
}, trys: [], ops: [] }, f, y, t, g;
|
|
345
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
346
|
-
return this;
|
|
347
|
-
}), g;
|
|
348
|
-
function verb(n) {
|
|
349
|
-
return function(v) {
|
|
350
|
-
return step([n, v]);
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
function step(op) {
|
|
354
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
355
|
-
while (_) try {
|
|
356
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
357
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
358
|
-
switch (op[0]) {
|
|
359
|
-
case 0:
|
|
360
|
-
case 1:
|
|
361
|
-
t = op;
|
|
362
|
-
break;
|
|
363
|
-
case 4:
|
|
364
|
-
_.label++;
|
|
365
|
-
return { value: op[1], done: false };
|
|
366
|
-
case 5:
|
|
367
|
-
_.label++;
|
|
368
|
-
y = op[1];
|
|
369
|
-
op = [0];
|
|
370
|
-
continue;
|
|
371
|
-
case 7:
|
|
372
|
-
op = _.ops.pop();
|
|
373
|
-
_.trys.pop();
|
|
374
|
-
continue;
|
|
375
|
-
default:
|
|
376
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
377
|
-
_ = 0;
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
381
|
-
_.label = op[1];
|
|
382
|
-
break;
|
|
383
|
-
}
|
|
384
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
385
|
-
_.label = t[1];
|
|
386
|
-
t = op;
|
|
387
|
-
break;
|
|
388
|
-
}
|
|
389
|
-
if (t && _.label < t[2]) {
|
|
390
|
-
_.label = t[2];
|
|
391
|
-
_.ops.push(op);
|
|
392
|
-
break;
|
|
393
|
-
}
|
|
394
|
-
if (t[2]) _.ops.pop();
|
|
395
|
-
_.trys.pop();
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
op = body.call(thisArg, _);
|
|
399
|
-
} catch (e) {
|
|
400
|
-
op = [6, e];
|
|
401
|
-
y = 0;
|
|
402
|
-
} finally {
|
|
403
|
-
f = t = 0;
|
|
404
|
-
}
|
|
405
|
-
if (op[0] & 5) throw op[1];
|
|
406
|
-
return { value: op[0] ? op[1] : void 0, done: true };
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
410
|
-
exports.ConoutConnection = void 0;
|
|
411
|
-
var worker_threads_1 = __require("worker_threads");
|
|
412
|
-
var conout_1 = require_conout();
|
|
413
|
-
var path_1 = __require("path");
|
|
414
|
-
var eventEmitter2_1 = require_eventEmitter2();
|
|
415
|
-
var FLUSH_DATA_INTERVAL = 1e3;
|
|
416
|
-
var ConoutConnection = (
|
|
417
|
-
/** @class */
|
|
418
|
-
(function() {
|
|
419
|
-
function ConoutConnection2(_conoutPipeName, _useConptyDll) {
|
|
420
|
-
var _this = this;
|
|
421
|
-
this._conoutPipeName = _conoutPipeName;
|
|
422
|
-
this._useConptyDll = _useConptyDll;
|
|
423
|
-
this._isDisposed = false;
|
|
424
|
-
this._onReady = new eventEmitter2_1.EventEmitter2();
|
|
425
|
-
var workerData = {
|
|
426
|
-
conoutPipeName: _conoutPipeName
|
|
427
|
-
};
|
|
428
|
-
var scriptPath = __dirname.replace("node_modules.asar", "node_modules.asar.unpacked");
|
|
429
|
-
this._worker = new worker_threads_1.Worker(path_1.join(scriptPath, "worker/conoutSocketWorker.js"), { workerData });
|
|
430
|
-
this._worker.on("message", function(message) {
|
|
431
|
-
switch (message) {
|
|
432
|
-
case 1:
|
|
433
|
-
_this._onReady.fire();
|
|
434
|
-
return;
|
|
435
|
-
default:
|
|
436
|
-
console.warn("Unexpected ConoutWorkerMessage", message);
|
|
437
|
-
}
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
Object.defineProperty(ConoutConnection2.prototype, "onReady", {
|
|
441
|
-
get: function() {
|
|
442
|
-
return this._onReady.event;
|
|
443
|
-
},
|
|
444
|
-
enumerable: false,
|
|
445
|
-
configurable: true
|
|
446
|
-
});
|
|
447
|
-
ConoutConnection2.prototype.dispose = function() {
|
|
448
|
-
if (!this._useConptyDll && this._isDisposed) {
|
|
449
|
-
return;
|
|
450
|
-
}
|
|
451
|
-
this._isDisposed = true;
|
|
452
|
-
this._drainDataAndClose();
|
|
453
|
-
};
|
|
454
|
-
ConoutConnection2.prototype.connectSocket = function(socket) {
|
|
455
|
-
socket.connect(conout_1.getWorkerPipeName(this._conoutPipeName));
|
|
456
|
-
};
|
|
457
|
-
ConoutConnection2.prototype._drainDataAndClose = function() {
|
|
458
|
-
var _this = this;
|
|
459
|
-
if (this._drainTimeout) {
|
|
460
|
-
clearTimeout(this._drainTimeout);
|
|
461
|
-
}
|
|
462
|
-
this._drainTimeout = setTimeout(function() {
|
|
463
|
-
return _this._destroySocket();
|
|
464
|
-
}, FLUSH_DATA_INTERVAL);
|
|
465
|
-
};
|
|
466
|
-
ConoutConnection2.prototype._destroySocket = function() {
|
|
467
|
-
return __awaiter(this, void 0, void 0, function() {
|
|
468
|
-
return __generator(this, function(_a) {
|
|
469
|
-
switch (_a.label) {
|
|
470
|
-
case 0:
|
|
471
|
-
return [4, this._worker.terminate()];
|
|
472
|
-
case 1:
|
|
473
|
-
_a.sent();
|
|
474
|
-
return [
|
|
475
|
-
2
|
|
476
|
-
/*return*/
|
|
477
|
-
];
|
|
478
|
-
}
|
|
479
|
-
});
|
|
480
|
-
});
|
|
481
|
-
};
|
|
482
|
-
return ConoutConnection2;
|
|
483
|
-
})()
|
|
484
|
-
);
|
|
485
|
-
exports.ConoutConnection = ConoutConnection;
|
|
486
|
-
}
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
// node_modules/node-pty/lib/windowsPtyAgent.js
|
|
490
|
-
var require_windowsPtyAgent = __commonJS({
|
|
491
|
-
"node_modules/node-pty/lib/windowsPtyAgent.js"(exports) {
|
|
492
|
-
"use strict";
|
|
493
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
494
|
-
exports.argsToCommandLine = exports.WindowsPtyAgent = void 0;
|
|
495
|
-
var fs = __require("fs");
|
|
496
|
-
var os2 = __require("os");
|
|
497
|
-
var path = __require("path");
|
|
498
|
-
var child_process_1 = __require("child_process");
|
|
499
|
-
var net_1 = __require("net");
|
|
500
|
-
var windowsConoutConnection_1 = require_windowsConoutConnection();
|
|
501
|
-
var utils_1 = require_utils();
|
|
502
|
-
var conptyNative;
|
|
503
|
-
var winptyNative;
|
|
504
|
-
var FLUSH_DATA_INTERVAL = 1e3;
|
|
505
|
-
var WindowsPtyAgent = (
|
|
506
|
-
/** @class */
|
|
507
|
-
(function() {
|
|
508
|
-
function WindowsPtyAgent2(file, args, env, cwd, cols, rows, debug, _useConpty, _useConptyDll, conptyInheritCursor) {
|
|
509
|
-
var _this = this;
|
|
510
|
-
if (_useConptyDll === void 0) {
|
|
511
|
-
_useConptyDll = false;
|
|
512
|
-
}
|
|
513
|
-
if (conptyInheritCursor === void 0) {
|
|
514
|
-
conptyInheritCursor = false;
|
|
515
|
-
}
|
|
516
|
-
this._useConpty = _useConpty;
|
|
517
|
-
this._useConptyDll = _useConptyDll;
|
|
518
|
-
this._pid = 0;
|
|
519
|
-
this._innerPid = 0;
|
|
520
|
-
if (this._useConpty === void 0 || this._useConpty === true) {
|
|
521
|
-
this._useConpty = this._getWindowsBuildNumber() >= 18309;
|
|
522
|
-
}
|
|
523
|
-
if (this._useConpty) {
|
|
524
|
-
if (!conptyNative) {
|
|
525
|
-
conptyNative = utils_1.loadNativeModule("conpty").module;
|
|
526
|
-
}
|
|
527
|
-
} else {
|
|
528
|
-
if (!winptyNative) {
|
|
529
|
-
winptyNative = utils_1.loadNativeModule("pty").module;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
this._ptyNative = this._useConpty ? conptyNative : winptyNative;
|
|
533
|
-
cwd = path.resolve(cwd);
|
|
534
|
-
var commandLine = argsToCommandLine(file, args);
|
|
535
|
-
var term;
|
|
536
|
-
if (this._useConpty) {
|
|
537
|
-
term = this._ptyNative.startProcess(file, cols, rows, debug, this._generatePipeName(), conptyInheritCursor, this._useConptyDll);
|
|
538
|
-
} else {
|
|
539
|
-
term = this._ptyNative.startProcess(file, commandLine, env, cwd, cols, rows, debug);
|
|
540
|
-
this._pid = term.pid;
|
|
541
|
-
this._innerPid = term.innerPid;
|
|
542
|
-
}
|
|
543
|
-
this._fd = term.fd;
|
|
544
|
-
this._pty = term.pty;
|
|
545
|
-
this._outSocket = new net_1.Socket();
|
|
546
|
-
this._outSocket.setEncoding("utf8");
|
|
547
|
-
this._conoutSocketWorker = new windowsConoutConnection_1.ConoutConnection(term.conout, this._useConptyDll);
|
|
548
|
-
this._conoutSocketWorker.onReady(function() {
|
|
549
|
-
_this._conoutSocketWorker.connectSocket(_this._outSocket);
|
|
550
|
-
});
|
|
551
|
-
this._outSocket.on("connect", function() {
|
|
552
|
-
_this._outSocket.emit("ready_datapipe");
|
|
553
|
-
});
|
|
554
|
-
var inSocketFD = fs.openSync(term.conin, "w");
|
|
555
|
-
this._inSocket = new net_1.Socket({
|
|
556
|
-
fd: inSocketFD,
|
|
557
|
-
readable: false,
|
|
558
|
-
writable: true
|
|
559
|
-
});
|
|
560
|
-
this._inSocket.setEncoding("utf8");
|
|
561
|
-
if (this._useConpty) {
|
|
562
|
-
var connect = this._ptyNative.connect(this._pty, commandLine, cwd, env, this._useConptyDll, function(c) {
|
|
563
|
-
return _this._$onProcessExit(c);
|
|
564
|
-
});
|
|
565
|
-
this._innerPid = connect.pid;
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
Object.defineProperty(WindowsPtyAgent2.prototype, "inSocket", {
|
|
569
|
-
get: function() {
|
|
570
|
-
return this._inSocket;
|
|
571
|
-
},
|
|
572
|
-
enumerable: false,
|
|
573
|
-
configurable: true
|
|
574
|
-
});
|
|
575
|
-
Object.defineProperty(WindowsPtyAgent2.prototype, "outSocket", {
|
|
576
|
-
get: function() {
|
|
577
|
-
return this._outSocket;
|
|
578
|
-
},
|
|
579
|
-
enumerable: false,
|
|
580
|
-
configurable: true
|
|
581
|
-
});
|
|
582
|
-
Object.defineProperty(WindowsPtyAgent2.prototype, "fd", {
|
|
583
|
-
get: function() {
|
|
584
|
-
return this._fd;
|
|
585
|
-
},
|
|
586
|
-
enumerable: false,
|
|
587
|
-
configurable: true
|
|
588
|
-
});
|
|
589
|
-
Object.defineProperty(WindowsPtyAgent2.prototype, "innerPid", {
|
|
590
|
-
get: function() {
|
|
591
|
-
return this._innerPid;
|
|
592
|
-
},
|
|
593
|
-
enumerable: false,
|
|
594
|
-
configurable: true
|
|
595
|
-
});
|
|
596
|
-
Object.defineProperty(WindowsPtyAgent2.prototype, "pty", {
|
|
597
|
-
get: function() {
|
|
598
|
-
return this._pty;
|
|
599
|
-
},
|
|
600
|
-
enumerable: false,
|
|
601
|
-
configurable: true
|
|
602
|
-
});
|
|
603
|
-
WindowsPtyAgent2.prototype.resize = function(cols, rows) {
|
|
604
|
-
if (this._useConpty) {
|
|
605
|
-
if (this._exitCode !== void 0) {
|
|
606
|
-
throw new Error("Cannot resize a pty that has already exited");
|
|
607
|
-
}
|
|
608
|
-
this._ptyNative.resize(this._pty, cols, rows, this._useConptyDll);
|
|
609
|
-
return;
|
|
610
|
-
}
|
|
611
|
-
this._ptyNative.resize(this._pid, cols, rows);
|
|
612
|
-
};
|
|
613
|
-
WindowsPtyAgent2.prototype.clear = function() {
|
|
614
|
-
if (this._useConpty) {
|
|
615
|
-
this._ptyNative.clear(this._pty, this._useConptyDll);
|
|
616
|
-
}
|
|
617
|
-
};
|
|
618
|
-
WindowsPtyAgent2.prototype.kill = function() {
|
|
619
|
-
var _this = this;
|
|
620
|
-
if (this._useConpty) {
|
|
621
|
-
if (!this._useConptyDll) {
|
|
622
|
-
this._inSocket.readable = false;
|
|
623
|
-
this._outSocket.readable = false;
|
|
624
|
-
this._getConsoleProcessList().then(function(consoleProcessList) {
|
|
625
|
-
consoleProcessList.forEach(function(pid) {
|
|
626
|
-
try {
|
|
627
|
-
process.kill(pid);
|
|
628
|
-
} catch (e) {
|
|
629
|
-
}
|
|
630
|
-
});
|
|
631
|
-
});
|
|
632
|
-
this._ptyNative.kill(this._pty, this._useConptyDll);
|
|
633
|
-
this._conoutSocketWorker.dispose();
|
|
634
|
-
} else {
|
|
635
|
-
this._inSocket.destroy();
|
|
636
|
-
this._ptyNative.kill(this._pty, this._useConptyDll);
|
|
637
|
-
this._outSocket.on("data", function() {
|
|
638
|
-
_this._conoutSocketWorker.dispose();
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
} else {
|
|
642
|
-
var processList = this._ptyNative.getProcessList(this._pid);
|
|
643
|
-
this._ptyNative.kill(this._pid, this._innerPid);
|
|
644
|
-
processList.forEach(function(pid) {
|
|
645
|
-
try {
|
|
646
|
-
process.kill(pid);
|
|
647
|
-
} catch (e) {
|
|
648
|
-
}
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
};
|
|
652
|
-
WindowsPtyAgent2.prototype._getConsoleProcessList = function() {
|
|
653
|
-
var _this = this;
|
|
654
|
-
return new Promise(function(resolve) {
|
|
655
|
-
var agent = child_process_1.fork(path.join(__dirname, "conpty_console_list_agent"), [_this._innerPid.toString()]);
|
|
656
|
-
agent.on("message", function(message) {
|
|
657
|
-
clearTimeout(timeout);
|
|
658
|
-
resolve(message.consoleProcessList);
|
|
659
|
-
});
|
|
660
|
-
var timeout = setTimeout(function() {
|
|
661
|
-
agent.kill();
|
|
662
|
-
resolve([_this._innerPid]);
|
|
663
|
-
}, 5e3);
|
|
664
|
-
});
|
|
665
|
-
};
|
|
666
|
-
Object.defineProperty(WindowsPtyAgent2.prototype, "exitCode", {
|
|
667
|
-
get: function() {
|
|
668
|
-
if (this._useConpty) {
|
|
669
|
-
return this._exitCode;
|
|
670
|
-
}
|
|
671
|
-
var winptyExitCode = this._ptyNative.getExitCode(this._innerPid);
|
|
672
|
-
return winptyExitCode === -1 ? void 0 : winptyExitCode;
|
|
673
|
-
},
|
|
674
|
-
enumerable: false,
|
|
675
|
-
configurable: true
|
|
676
|
-
});
|
|
677
|
-
WindowsPtyAgent2.prototype._getWindowsBuildNumber = function() {
|
|
678
|
-
var osVersion = /(\d+)\.(\d+)\.(\d+)/g.exec(os2.release());
|
|
679
|
-
var buildNumber = 0;
|
|
680
|
-
if (osVersion && osVersion.length === 4) {
|
|
681
|
-
buildNumber = parseInt(osVersion[3]);
|
|
682
|
-
}
|
|
683
|
-
return buildNumber;
|
|
684
|
-
};
|
|
685
|
-
WindowsPtyAgent2.prototype._generatePipeName = function() {
|
|
686
|
-
return "conpty-" + Math.random() * 1e7;
|
|
687
|
-
};
|
|
688
|
-
WindowsPtyAgent2.prototype._$onProcessExit = function(exitCode) {
|
|
689
|
-
var _this = this;
|
|
690
|
-
this._exitCode = exitCode;
|
|
691
|
-
if (!this._useConptyDll) {
|
|
692
|
-
this._flushDataAndCleanUp();
|
|
693
|
-
this._outSocket.on("data", function() {
|
|
694
|
-
return _this._flushDataAndCleanUp();
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
};
|
|
698
|
-
WindowsPtyAgent2.prototype._flushDataAndCleanUp = function() {
|
|
699
|
-
var _this = this;
|
|
700
|
-
if (this._useConptyDll) {
|
|
701
|
-
return;
|
|
702
|
-
}
|
|
703
|
-
if (this._closeTimeout) {
|
|
704
|
-
clearTimeout(this._closeTimeout);
|
|
705
|
-
}
|
|
706
|
-
this._closeTimeout = setTimeout(function() {
|
|
707
|
-
return _this._cleanUpProcess();
|
|
708
|
-
}, FLUSH_DATA_INTERVAL);
|
|
709
|
-
};
|
|
710
|
-
WindowsPtyAgent2.prototype._cleanUpProcess = function() {
|
|
711
|
-
if (this._useConptyDll) {
|
|
712
|
-
return;
|
|
713
|
-
}
|
|
714
|
-
this._inSocket.readable = false;
|
|
715
|
-
this._outSocket.readable = false;
|
|
716
|
-
this._outSocket.destroy();
|
|
717
|
-
};
|
|
718
|
-
return WindowsPtyAgent2;
|
|
719
|
-
})()
|
|
720
|
-
);
|
|
721
|
-
exports.WindowsPtyAgent = WindowsPtyAgent;
|
|
722
|
-
function argsToCommandLine(file, args) {
|
|
723
|
-
if (isCommandLine(args)) {
|
|
724
|
-
if (args.length === 0) {
|
|
725
|
-
return file;
|
|
726
|
-
}
|
|
727
|
-
return argsToCommandLine(file, []) + " " + args;
|
|
728
|
-
}
|
|
729
|
-
var argv = [file];
|
|
730
|
-
Array.prototype.push.apply(argv, args);
|
|
731
|
-
var result = "";
|
|
732
|
-
for (var argIndex = 0; argIndex < argv.length; argIndex++) {
|
|
733
|
-
if (argIndex > 0) {
|
|
734
|
-
result += " ";
|
|
735
|
-
}
|
|
736
|
-
var arg = argv[argIndex];
|
|
737
|
-
var hasLopsidedEnclosingQuote = xOr(arg[0] !== '"', arg[arg.length - 1] !== '"');
|
|
738
|
-
var hasNoEnclosingQuotes = arg[0] !== '"' && arg[arg.length - 1] !== '"';
|
|
739
|
-
var quote = arg === "" || (arg.indexOf(" ") !== -1 || arg.indexOf(" ") !== -1) && (arg.length > 1 && (hasLopsidedEnclosingQuote || hasNoEnclosingQuotes));
|
|
740
|
-
if (quote) {
|
|
741
|
-
result += '"';
|
|
742
|
-
}
|
|
743
|
-
var bsCount = 0;
|
|
744
|
-
for (var i = 0; i < arg.length; i++) {
|
|
745
|
-
var p = arg[i];
|
|
746
|
-
if (p === "\\") {
|
|
747
|
-
bsCount++;
|
|
748
|
-
} else if (p === '"') {
|
|
749
|
-
result += repeatText("\\", bsCount * 2 + 1);
|
|
750
|
-
result += '"';
|
|
751
|
-
bsCount = 0;
|
|
752
|
-
} else {
|
|
753
|
-
result += repeatText("\\", bsCount);
|
|
754
|
-
bsCount = 0;
|
|
755
|
-
result += p;
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
if (quote) {
|
|
759
|
-
result += repeatText("\\", bsCount * 2);
|
|
760
|
-
result += '"';
|
|
761
|
-
} else {
|
|
762
|
-
result += repeatText("\\", bsCount);
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
return result;
|
|
766
|
-
}
|
|
767
|
-
exports.argsToCommandLine = argsToCommandLine;
|
|
768
|
-
function isCommandLine(args) {
|
|
769
|
-
return typeof args === "string";
|
|
770
|
-
}
|
|
771
|
-
function repeatText(text, count) {
|
|
772
|
-
var result = "";
|
|
773
|
-
for (var i = 0; i < count; i++) {
|
|
774
|
-
result += text;
|
|
775
|
-
}
|
|
776
|
-
return result;
|
|
777
|
-
}
|
|
778
|
-
function xOr(arg1, arg2) {
|
|
779
|
-
return arg1 && !arg2 || !arg1 && arg2;
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
});
|
|
783
|
-
|
|
784
|
-
// node_modules/node-pty/lib/windowsTerminal.js
|
|
785
|
-
var require_windowsTerminal = __commonJS({
|
|
786
|
-
"node_modules/node-pty/lib/windowsTerminal.js"(exports) {
|
|
787
|
-
"use strict";
|
|
788
|
-
var __extends = exports && exports.__extends || /* @__PURE__ */ (function() {
|
|
789
|
-
var extendStatics = function(d, b) {
|
|
790
|
-
extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
|
|
791
|
-
d2.__proto__ = b2;
|
|
792
|
-
} || function(d2, b2) {
|
|
793
|
-
for (var p in b2) if (b2.hasOwnProperty(p)) d2[p] = b2[p];
|
|
794
|
-
};
|
|
795
|
-
return extendStatics(d, b);
|
|
796
|
-
};
|
|
797
|
-
return function(d, b) {
|
|
798
|
-
extendStatics(d, b);
|
|
799
|
-
function __() {
|
|
800
|
-
this.constructor = d;
|
|
801
|
-
}
|
|
802
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
803
|
-
};
|
|
804
|
-
})();
|
|
805
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
806
|
-
exports.WindowsTerminal = void 0;
|
|
807
|
-
var terminal_1 = require_terminal();
|
|
808
|
-
var windowsPtyAgent_1 = require_windowsPtyAgent();
|
|
809
|
-
var utils_1 = require_utils();
|
|
810
|
-
var DEFAULT_FILE = "cmd.exe";
|
|
811
|
-
var DEFAULT_NAME = "Windows Shell";
|
|
812
|
-
var WindowsTerminal = (
|
|
813
|
-
/** @class */
|
|
814
|
-
(function(_super) {
|
|
815
|
-
__extends(WindowsTerminal2, _super);
|
|
816
|
-
function WindowsTerminal2(file, args, opt) {
|
|
817
|
-
var _this = _super.call(this, opt) || this;
|
|
818
|
-
_this._checkType("args", args, "string", true);
|
|
819
|
-
args = args || [];
|
|
820
|
-
file = file || DEFAULT_FILE;
|
|
821
|
-
opt = opt || {};
|
|
822
|
-
opt.env = opt.env || process.env;
|
|
823
|
-
if (opt.encoding) {
|
|
824
|
-
console.warn("Setting encoding on Windows is not supported");
|
|
825
|
-
}
|
|
826
|
-
var env = utils_1.assign({}, opt.env);
|
|
827
|
-
_this._cols = opt.cols || terminal_1.DEFAULT_COLS;
|
|
828
|
-
_this._rows = opt.rows || terminal_1.DEFAULT_ROWS;
|
|
829
|
-
var cwd = opt.cwd || process.cwd();
|
|
830
|
-
var name = opt.name || env.TERM || DEFAULT_NAME;
|
|
831
|
-
var parsedEnv = _this._parseEnv(env);
|
|
832
|
-
_this._isReady = false;
|
|
833
|
-
_this._deferreds = [];
|
|
834
|
-
_this._agent = new windowsPtyAgent_1.WindowsPtyAgent(file, args, parsedEnv, cwd, _this._cols, _this._rows, false, opt.useConpty, opt.useConptyDll, opt.conptyInheritCursor);
|
|
835
|
-
_this._socket = _this._agent.outSocket;
|
|
836
|
-
_this._pid = _this._agent.innerPid;
|
|
837
|
-
_this._fd = _this._agent.fd;
|
|
838
|
-
_this._pty = _this._agent.pty;
|
|
839
|
-
_this._socket.on("ready_datapipe", function() {
|
|
840
|
-
_this._socket.once("data", function() {
|
|
841
|
-
if (!_this._isReady) {
|
|
842
|
-
_this._isReady = true;
|
|
843
|
-
_this._deferreds.forEach(function(fn) {
|
|
844
|
-
fn.run();
|
|
845
|
-
});
|
|
846
|
-
_this._deferreds = [];
|
|
847
|
-
}
|
|
848
|
-
});
|
|
849
|
-
_this._socket.on("error", function(err) {
|
|
850
|
-
_this._close();
|
|
851
|
-
if (err.code) {
|
|
852
|
-
if (~err.code.indexOf("errno 5") || ~err.code.indexOf("EIO"))
|
|
853
|
-
return;
|
|
854
|
-
}
|
|
855
|
-
if (_this.listeners("error").length < 2) {
|
|
856
|
-
throw err;
|
|
857
|
-
}
|
|
858
|
-
});
|
|
859
|
-
_this._socket.on("close", function() {
|
|
860
|
-
_this.emit("exit", _this._agent.exitCode);
|
|
861
|
-
_this._close();
|
|
862
|
-
});
|
|
863
|
-
});
|
|
864
|
-
_this._file = file;
|
|
865
|
-
_this._name = name;
|
|
866
|
-
_this._readable = true;
|
|
867
|
-
_this._writable = true;
|
|
868
|
-
_this._forwardEvents();
|
|
869
|
-
return _this;
|
|
870
|
-
}
|
|
871
|
-
WindowsTerminal2.prototype._write = function(data) {
|
|
872
|
-
this._defer(this._doWrite, data);
|
|
873
|
-
};
|
|
874
|
-
WindowsTerminal2.prototype._doWrite = function(data) {
|
|
875
|
-
this._agent.inSocket.write(data);
|
|
876
|
-
};
|
|
877
|
-
WindowsTerminal2.open = function(options) {
|
|
878
|
-
throw new Error("open() not supported on windows, use Fork() instead.");
|
|
879
|
-
};
|
|
880
|
-
WindowsTerminal2.prototype.resize = function(cols, rows) {
|
|
881
|
-
var _this = this;
|
|
882
|
-
if (cols <= 0 || rows <= 0 || isNaN(cols) || isNaN(rows) || cols === Infinity || rows === Infinity) {
|
|
883
|
-
throw new Error("resizing must be done using positive cols and rows");
|
|
884
|
-
}
|
|
885
|
-
this._deferNoArgs(function() {
|
|
886
|
-
_this._agent.resize(cols, rows);
|
|
887
|
-
_this._cols = cols;
|
|
888
|
-
_this._rows = rows;
|
|
889
|
-
});
|
|
890
|
-
};
|
|
891
|
-
WindowsTerminal2.prototype.clear = function() {
|
|
892
|
-
var _this = this;
|
|
893
|
-
this._deferNoArgs(function() {
|
|
894
|
-
_this._agent.clear();
|
|
895
|
-
});
|
|
896
|
-
};
|
|
897
|
-
WindowsTerminal2.prototype.destroy = function() {
|
|
898
|
-
var _this = this;
|
|
899
|
-
this._deferNoArgs(function() {
|
|
900
|
-
_this.kill();
|
|
901
|
-
});
|
|
902
|
-
};
|
|
903
|
-
WindowsTerminal2.prototype.kill = function(signal) {
|
|
904
|
-
var _this = this;
|
|
905
|
-
this._deferNoArgs(function() {
|
|
906
|
-
if (signal) {
|
|
907
|
-
throw new Error("Signals not supported on windows.");
|
|
908
|
-
}
|
|
909
|
-
_this._close();
|
|
910
|
-
_this._agent.kill();
|
|
911
|
-
});
|
|
912
|
-
};
|
|
913
|
-
WindowsTerminal2.prototype._deferNoArgs = function(deferredFn) {
|
|
914
|
-
var _this = this;
|
|
915
|
-
if (this._isReady) {
|
|
916
|
-
deferredFn.call(this);
|
|
917
|
-
return;
|
|
918
|
-
}
|
|
919
|
-
this._deferreds.push({
|
|
920
|
-
run: function() {
|
|
921
|
-
return deferredFn.call(_this);
|
|
922
|
-
}
|
|
923
|
-
});
|
|
924
|
-
};
|
|
925
|
-
WindowsTerminal2.prototype._defer = function(deferredFn, arg) {
|
|
926
|
-
var _this = this;
|
|
927
|
-
if (this._isReady) {
|
|
928
|
-
deferredFn.call(this, arg);
|
|
929
|
-
return;
|
|
930
|
-
}
|
|
931
|
-
this._deferreds.push({
|
|
932
|
-
run: function() {
|
|
933
|
-
return deferredFn.call(_this, arg);
|
|
934
|
-
}
|
|
935
|
-
});
|
|
936
|
-
};
|
|
937
|
-
Object.defineProperty(WindowsTerminal2.prototype, "process", {
|
|
938
|
-
get: function() {
|
|
939
|
-
return this._name;
|
|
940
|
-
},
|
|
941
|
-
enumerable: false,
|
|
942
|
-
configurable: true
|
|
943
|
-
});
|
|
944
|
-
Object.defineProperty(WindowsTerminal2.prototype, "master", {
|
|
945
|
-
get: function() {
|
|
946
|
-
throw new Error("master is not supported on Windows");
|
|
947
|
-
},
|
|
948
|
-
enumerable: false,
|
|
949
|
-
configurable: true
|
|
950
|
-
});
|
|
951
|
-
Object.defineProperty(WindowsTerminal2.prototype, "slave", {
|
|
952
|
-
get: function() {
|
|
953
|
-
throw new Error("slave is not supported on Windows");
|
|
954
|
-
},
|
|
955
|
-
enumerable: false,
|
|
956
|
-
configurable: true
|
|
957
|
-
});
|
|
958
|
-
return WindowsTerminal2;
|
|
959
|
-
})(terminal_1.Terminal)
|
|
960
|
-
);
|
|
961
|
-
exports.WindowsTerminal = WindowsTerminal;
|
|
962
|
-
}
|
|
963
|
-
});
|
|
964
|
-
|
|
965
|
-
// node_modules/node-pty/lib/unixTerminal.js
|
|
966
|
-
var require_unixTerminal = __commonJS({
|
|
967
|
-
"node_modules/node-pty/lib/unixTerminal.js"(exports) {
|
|
968
|
-
"use strict";
|
|
969
|
-
var __extends = exports && exports.__extends || /* @__PURE__ */ (function() {
|
|
970
|
-
var extendStatics = function(d, b) {
|
|
971
|
-
extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
|
|
972
|
-
d2.__proto__ = b2;
|
|
973
|
-
} || function(d2, b2) {
|
|
974
|
-
for (var p in b2) if (b2.hasOwnProperty(p)) d2[p] = b2[p];
|
|
975
|
-
};
|
|
976
|
-
return extendStatics(d, b);
|
|
977
|
-
};
|
|
978
|
-
return function(d, b) {
|
|
979
|
-
extendStatics(d, b);
|
|
980
|
-
function __() {
|
|
981
|
-
this.constructor = d;
|
|
982
|
-
}
|
|
983
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
984
|
-
};
|
|
985
|
-
})();
|
|
986
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
987
|
-
exports.UnixTerminal = void 0;
|
|
988
|
-
var fs = __require("fs");
|
|
989
|
-
var path = __require("path");
|
|
990
|
-
var tty = __require("tty");
|
|
991
|
-
var terminal_1 = require_terminal();
|
|
992
|
-
var utils_1 = require_utils();
|
|
993
|
-
var native = utils_1.loadNativeModule("pty");
|
|
994
|
-
var pty2 = native.module;
|
|
995
|
-
var helperPath = native.dir + "/spawn-helper";
|
|
996
|
-
helperPath = path.resolve(__dirname, helperPath);
|
|
997
|
-
helperPath = helperPath.replace("app.asar", "app.asar.unpacked");
|
|
998
|
-
helperPath = helperPath.replace("node_modules.asar", "node_modules.asar.unpacked");
|
|
999
|
-
var DEFAULT_FILE = "sh";
|
|
1000
|
-
var DEFAULT_NAME = "xterm";
|
|
1001
|
-
var DESTROY_SOCKET_TIMEOUT_MS = 200;
|
|
1002
|
-
var UnixTerminal = (
|
|
1003
|
-
/** @class */
|
|
1004
|
-
(function(_super) {
|
|
1005
|
-
__extends(UnixTerminal2, _super);
|
|
1006
|
-
function UnixTerminal2(file, args, opt) {
|
|
1007
|
-
var _a, _b;
|
|
1008
|
-
var _this = _super.call(this, opt) || this;
|
|
1009
|
-
_this._boundClose = false;
|
|
1010
|
-
_this._emittedClose = false;
|
|
1011
|
-
if (typeof args === "string") {
|
|
1012
|
-
throw new Error("args as a string is not supported on unix.");
|
|
1013
|
-
}
|
|
1014
|
-
args = args || [];
|
|
1015
|
-
file = file || DEFAULT_FILE;
|
|
1016
|
-
opt = opt || {};
|
|
1017
|
-
opt.env = opt.env || process.env;
|
|
1018
|
-
_this._cols = opt.cols || terminal_1.DEFAULT_COLS;
|
|
1019
|
-
_this._rows = opt.rows || terminal_1.DEFAULT_ROWS;
|
|
1020
|
-
var uid = (_a = opt.uid) !== null && _a !== void 0 ? _a : -1;
|
|
1021
|
-
var gid = (_b = opt.gid) !== null && _b !== void 0 ? _b : -1;
|
|
1022
|
-
var env = utils_1.assign({}, opt.env);
|
|
1023
|
-
if (opt.env === process.env) {
|
|
1024
|
-
_this._sanitizeEnv(env);
|
|
1025
|
-
}
|
|
1026
|
-
var cwd = opt.cwd || process.cwd();
|
|
1027
|
-
env.PWD = cwd;
|
|
1028
|
-
var name = opt.name || env.TERM || DEFAULT_NAME;
|
|
1029
|
-
env.TERM = name;
|
|
1030
|
-
var parsedEnv = _this._parseEnv(env);
|
|
1031
|
-
var encoding = opt.encoding === void 0 ? "utf8" : opt.encoding;
|
|
1032
|
-
var onexit = function(code, signal) {
|
|
1033
|
-
if (!_this._emittedClose) {
|
|
1034
|
-
if (_this._boundClose) {
|
|
1035
|
-
return;
|
|
1036
|
-
}
|
|
1037
|
-
_this._boundClose = true;
|
|
1038
|
-
var timeout_1 = setTimeout(function() {
|
|
1039
|
-
timeout_1 = null;
|
|
1040
|
-
_this._socket.destroy();
|
|
1041
|
-
}, DESTROY_SOCKET_TIMEOUT_MS);
|
|
1042
|
-
_this.once("close", function() {
|
|
1043
|
-
if (timeout_1 !== null) {
|
|
1044
|
-
clearTimeout(timeout_1);
|
|
1045
|
-
}
|
|
1046
|
-
_this.emit("exit", code, signal);
|
|
1047
|
-
});
|
|
1048
|
-
return;
|
|
1049
|
-
}
|
|
1050
|
-
_this.emit("exit", code, signal);
|
|
1051
|
-
};
|
|
1052
|
-
var term = pty2.fork(file, args, parsedEnv, cwd, _this._cols, _this._rows, uid, gid, encoding === "utf8", helperPath, onexit);
|
|
1053
|
-
_this._socket = new tty.ReadStream(term.fd);
|
|
1054
|
-
if (encoding !== null) {
|
|
1055
|
-
_this._socket.setEncoding(encoding);
|
|
1056
|
-
}
|
|
1057
|
-
_this._writeStream = new CustomWriteStream(term.fd, encoding || void 0);
|
|
1058
|
-
_this._socket.on("error", function(err) {
|
|
1059
|
-
if (err.code) {
|
|
1060
|
-
if (~err.code.indexOf("EAGAIN")) {
|
|
1061
|
-
return;
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
_this._close();
|
|
1065
|
-
if (!_this._emittedClose) {
|
|
1066
|
-
_this._emittedClose = true;
|
|
1067
|
-
_this.emit("close");
|
|
1068
|
-
}
|
|
1069
|
-
if (err.code) {
|
|
1070
|
-
if (~err.code.indexOf("errno 5") || ~err.code.indexOf("EIO")) {
|
|
1071
|
-
return;
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
if (_this.listeners("error").length < 2) {
|
|
1075
|
-
throw err;
|
|
1076
|
-
}
|
|
1077
|
-
});
|
|
1078
|
-
_this._pid = term.pid;
|
|
1079
|
-
_this._fd = term.fd;
|
|
1080
|
-
_this._pty = term.pty;
|
|
1081
|
-
_this._file = file;
|
|
1082
|
-
_this._name = name;
|
|
1083
|
-
_this._readable = true;
|
|
1084
|
-
_this._writable = true;
|
|
1085
|
-
_this._socket.on("close", function() {
|
|
1086
|
-
if (_this._emittedClose) {
|
|
1087
|
-
return;
|
|
1088
|
-
}
|
|
1089
|
-
_this._emittedClose = true;
|
|
1090
|
-
_this._close();
|
|
1091
|
-
_this.emit("close");
|
|
1092
|
-
});
|
|
1093
|
-
_this._forwardEvents();
|
|
1094
|
-
return _this;
|
|
1095
|
-
}
|
|
1096
|
-
Object.defineProperty(UnixTerminal2.prototype, "master", {
|
|
1097
|
-
get: function() {
|
|
1098
|
-
return this._master;
|
|
1099
|
-
},
|
|
1100
|
-
enumerable: false,
|
|
1101
|
-
configurable: true
|
|
1102
|
-
});
|
|
1103
|
-
Object.defineProperty(UnixTerminal2.prototype, "slave", {
|
|
1104
|
-
get: function() {
|
|
1105
|
-
return this._slave;
|
|
1106
|
-
},
|
|
1107
|
-
enumerable: false,
|
|
1108
|
-
configurable: true
|
|
1109
|
-
});
|
|
1110
|
-
UnixTerminal2.prototype._write = function(data) {
|
|
1111
|
-
this._writeStream.write(data);
|
|
1112
|
-
};
|
|
1113
|
-
Object.defineProperty(UnixTerminal2.prototype, "fd", {
|
|
1114
|
-
/* Accessors */
|
|
1115
|
-
get: function() {
|
|
1116
|
-
return this._fd;
|
|
1117
|
-
},
|
|
1118
|
-
enumerable: false,
|
|
1119
|
-
configurable: true
|
|
1120
|
-
});
|
|
1121
|
-
Object.defineProperty(UnixTerminal2.prototype, "ptsName", {
|
|
1122
|
-
get: function() {
|
|
1123
|
-
return this._pty;
|
|
1124
|
-
},
|
|
1125
|
-
enumerable: false,
|
|
1126
|
-
configurable: true
|
|
1127
|
-
});
|
|
1128
|
-
UnixTerminal2.open = function(opt) {
|
|
1129
|
-
var self = Object.create(UnixTerminal2.prototype);
|
|
1130
|
-
opt = opt || {};
|
|
1131
|
-
if (arguments.length > 1) {
|
|
1132
|
-
opt = {
|
|
1133
|
-
cols: arguments[1],
|
|
1134
|
-
rows: arguments[2]
|
|
1135
|
-
};
|
|
1136
|
-
}
|
|
1137
|
-
var cols = opt.cols || terminal_1.DEFAULT_COLS;
|
|
1138
|
-
var rows = opt.rows || terminal_1.DEFAULT_ROWS;
|
|
1139
|
-
var encoding = opt.encoding === void 0 ? "utf8" : opt.encoding;
|
|
1140
|
-
var term = pty2.open(cols, rows);
|
|
1141
|
-
self._master = new tty.ReadStream(term.master);
|
|
1142
|
-
if (encoding !== null) {
|
|
1143
|
-
self._master.setEncoding(encoding);
|
|
1144
|
-
}
|
|
1145
|
-
self._master.resume();
|
|
1146
|
-
self._slave = new tty.ReadStream(term.slave);
|
|
1147
|
-
if (encoding !== null) {
|
|
1148
|
-
self._slave.setEncoding(encoding);
|
|
1149
|
-
}
|
|
1150
|
-
self._slave.resume();
|
|
1151
|
-
self._socket = self._master;
|
|
1152
|
-
self._pid = -1;
|
|
1153
|
-
self._fd = term.master;
|
|
1154
|
-
self._pty = term.pty;
|
|
1155
|
-
self._file = process.argv[0] || "node";
|
|
1156
|
-
self._name = process.env.TERM || "";
|
|
1157
|
-
self._readable = true;
|
|
1158
|
-
self._writable = true;
|
|
1159
|
-
self._socket.on("error", function(err) {
|
|
1160
|
-
self._close();
|
|
1161
|
-
if (self.listeners("error").length < 2) {
|
|
1162
|
-
throw err;
|
|
1163
|
-
}
|
|
1164
|
-
});
|
|
1165
|
-
self._socket.on("close", function() {
|
|
1166
|
-
self._close();
|
|
1167
|
-
});
|
|
1168
|
-
return self;
|
|
1169
|
-
};
|
|
1170
|
-
UnixTerminal2.prototype.destroy = function() {
|
|
1171
|
-
var _this = this;
|
|
1172
|
-
this._close();
|
|
1173
|
-
this._socket.once("close", function() {
|
|
1174
|
-
_this.kill("SIGHUP");
|
|
1175
|
-
});
|
|
1176
|
-
this._socket.destroy();
|
|
1177
|
-
this._writeStream.dispose();
|
|
1178
|
-
};
|
|
1179
|
-
UnixTerminal2.prototype.kill = function(signal) {
|
|
1180
|
-
try {
|
|
1181
|
-
process.kill(this.pid, signal || "SIGHUP");
|
|
1182
|
-
} catch (e) {
|
|
1183
|
-
}
|
|
1184
|
-
};
|
|
1185
|
-
Object.defineProperty(UnixTerminal2.prototype, "process", {
|
|
1186
|
-
/**
|
|
1187
|
-
* Gets the name of the process.
|
|
1188
|
-
*/
|
|
1189
|
-
get: function() {
|
|
1190
|
-
if (process.platform === "darwin") {
|
|
1191
|
-
var title = pty2.process(this._fd);
|
|
1192
|
-
return title !== "kernel_task" ? title : this._file;
|
|
1193
|
-
}
|
|
1194
|
-
return pty2.process(this._fd, this._pty) || this._file;
|
|
1195
|
-
},
|
|
1196
|
-
enumerable: false,
|
|
1197
|
-
configurable: true
|
|
1198
|
-
});
|
|
1199
|
-
UnixTerminal2.prototype.resize = function(cols, rows) {
|
|
1200
|
-
if (cols <= 0 || rows <= 0 || isNaN(cols) || isNaN(rows) || cols === Infinity || rows === Infinity) {
|
|
1201
|
-
throw new Error("resizing must be done using positive cols and rows");
|
|
1202
|
-
}
|
|
1203
|
-
pty2.resize(this._fd, cols, rows);
|
|
1204
|
-
this._cols = cols;
|
|
1205
|
-
this._rows = rows;
|
|
1206
|
-
};
|
|
1207
|
-
UnixTerminal2.prototype.clear = function() {
|
|
1208
|
-
};
|
|
1209
|
-
UnixTerminal2.prototype._sanitizeEnv = function(env) {
|
|
1210
|
-
delete env["TMUX"];
|
|
1211
|
-
delete env["TMUX_PANE"];
|
|
1212
|
-
delete env["STY"];
|
|
1213
|
-
delete env["WINDOW"];
|
|
1214
|
-
delete env["WINDOWID"];
|
|
1215
|
-
delete env["TERMCAP"];
|
|
1216
|
-
delete env["COLUMNS"];
|
|
1217
|
-
delete env["LINES"];
|
|
1218
|
-
};
|
|
1219
|
-
return UnixTerminal2;
|
|
1220
|
-
})(terminal_1.Terminal)
|
|
1221
|
-
);
|
|
1222
|
-
exports.UnixTerminal = UnixTerminal;
|
|
1223
|
-
var CustomWriteStream = (
|
|
1224
|
-
/** @class */
|
|
1225
|
-
(function() {
|
|
1226
|
-
function CustomWriteStream2(_fd, _encoding) {
|
|
1227
|
-
this._fd = _fd;
|
|
1228
|
-
this._encoding = _encoding;
|
|
1229
|
-
this._writeQueue = [];
|
|
1230
|
-
}
|
|
1231
|
-
CustomWriteStream2.prototype.dispose = function() {
|
|
1232
|
-
clearImmediate(this._writeImmediate);
|
|
1233
|
-
this._writeImmediate = void 0;
|
|
1234
|
-
};
|
|
1235
|
-
CustomWriteStream2.prototype.write = function(data) {
|
|
1236
|
-
var buffer = typeof data === "string" ? Buffer.from(data, this._encoding) : Buffer.from(data);
|
|
1237
|
-
if (buffer.byteLength !== 0) {
|
|
1238
|
-
this._writeQueue.push({ buffer, offset: 0 });
|
|
1239
|
-
if (this._writeQueue.length === 1) {
|
|
1240
|
-
this._processWriteQueue();
|
|
1241
|
-
}
|
|
1242
|
-
}
|
|
1243
|
-
};
|
|
1244
|
-
CustomWriteStream2.prototype._processWriteQueue = function() {
|
|
1245
|
-
var _this = this;
|
|
1246
|
-
this._writeImmediate = void 0;
|
|
1247
|
-
if (this._writeQueue.length === 0) {
|
|
1248
|
-
return;
|
|
1249
|
-
}
|
|
1250
|
-
var task = this._writeQueue[0];
|
|
1251
|
-
fs.write(this._fd, task.buffer, task.offset, function(err, written) {
|
|
1252
|
-
if (err) {
|
|
1253
|
-
if ("code" in err && err.code === "EAGAIN") {
|
|
1254
|
-
_this._writeImmediate = setImmediate(function() {
|
|
1255
|
-
return _this._processWriteQueue();
|
|
1256
|
-
});
|
|
1257
|
-
} else {
|
|
1258
|
-
_this._writeQueue.length = 0;
|
|
1259
|
-
console.error("Unhandled pty write error", err);
|
|
1260
|
-
}
|
|
1261
|
-
return;
|
|
1262
|
-
}
|
|
1263
|
-
task.offset += written;
|
|
1264
|
-
if (task.offset >= task.buffer.byteLength) {
|
|
1265
|
-
_this._writeQueue.shift();
|
|
1266
|
-
}
|
|
1267
|
-
_this._processWriteQueue();
|
|
1268
|
-
});
|
|
1269
|
-
};
|
|
1270
|
-
return CustomWriteStream2;
|
|
1271
|
-
})()
|
|
1272
|
-
);
|
|
1273
|
-
}
|
|
1274
|
-
});
|
|
1275
|
-
|
|
1276
|
-
// node_modules/node-pty/lib/index.js
|
|
1277
|
-
var require_lib = __commonJS({
|
|
1278
|
-
"node_modules/node-pty/lib/index.js"(exports) {
|
|
1279
|
-
"use strict";
|
|
1280
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1281
|
-
exports.native = exports.open = exports.createTerminal = exports.fork = exports.spawn = void 0;
|
|
1282
|
-
var utils_1 = require_utils();
|
|
1283
|
-
var terminalCtor;
|
|
1284
|
-
if (process.platform === "win32") {
|
|
1285
|
-
terminalCtor = require_windowsTerminal().WindowsTerminal;
|
|
1286
|
-
} else {
|
|
1287
|
-
terminalCtor = require_unixTerminal().UnixTerminal;
|
|
1288
|
-
}
|
|
1289
|
-
function spawn3(file, args, opt) {
|
|
1290
|
-
return new terminalCtor(file, args, opt);
|
|
1291
|
-
}
|
|
1292
|
-
exports.spawn = spawn3;
|
|
1293
|
-
function fork(file, args, opt) {
|
|
1294
|
-
return new terminalCtor(file, args, opt);
|
|
1295
|
-
}
|
|
1296
|
-
exports.fork = fork;
|
|
1297
|
-
function createTerminal(file, args, opt) {
|
|
1298
|
-
return new terminalCtor(file, args, opt);
|
|
1299
|
-
}
|
|
1300
|
-
exports.createTerminal = createTerminal;
|
|
1301
|
-
function open(options) {
|
|
1302
|
-
return terminalCtor.open(options);
|
|
1303
|
-
}
|
|
1304
|
-
exports.open = open;
|
|
1305
|
-
exports.native = process.platform !== "win32" ? utils_1.loadNativeModule("pty").module : null;
|
|
1306
|
-
}
|
|
1307
|
-
});
|
|
1308
|
-
|
|
1309
|
-
// src/server.ts
|
|
1310
|
-
import { EventEmitter as EventEmitter2 } from "events";
|
|
1311
|
-
import { createServer } from "http";
|
|
1312
|
-
import { WebSocketServer, WebSocket } from "ws";
|
|
1313
|
-
import crypto from "crypto";
|
|
1314
|
-
import os from "os";
|
|
1315
|
-
|
|
1316
|
-
// src/types.ts
|
|
1317
|
-
var DEFAULT_CONFIG = {
|
|
1318
|
-
port: 0,
|
|
1319
|
-
host: "0.0.0.0",
|
|
1320
|
-
enableTunnel: true,
|
|
1321
|
-
tunnelProvider: "localtunnel",
|
|
1322
|
-
maxConnections: 5,
|
|
1323
|
-
heartbeatInterval: 3e4,
|
|
1324
|
-
shell: "/bin/bash",
|
|
1325
|
-
cols: 80,
|
|
1326
|
-
rows: 24,
|
|
1327
|
-
enableTerminal: true,
|
|
1328
|
-
sessionTimeout: 0
|
|
1329
|
-
};
|
|
1330
|
-
var MessageTypes = {
|
|
1331
|
-
// Auth
|
|
1332
|
-
AUTH_REQUIRED: "auth:required",
|
|
1333
|
-
AUTH: "auth",
|
|
1334
|
-
AUTH_SUCCESS: "auth:success",
|
|
1335
|
-
AUTH_FAILED: "auth:failed",
|
|
1336
|
-
// Terminal
|
|
1337
|
-
TERMINAL_OUTPUT: "terminal:output",
|
|
1338
|
-
TERMINAL_INPUT: "terminal:input",
|
|
1339
|
-
TERMINAL_RESIZE: "terminal:resize",
|
|
1340
|
-
TERMINAL_EXIT: "terminal:exit",
|
|
1341
|
-
TERMINAL_CLEAR: "terminal:clear",
|
|
1342
|
-
// Notifications
|
|
1343
|
-
NOTIFICATION: "notification",
|
|
1344
|
-
// Heartbeat
|
|
1345
|
-
PING: "ping",
|
|
1346
|
-
PONG: "pong",
|
|
1347
|
-
// Session
|
|
1348
|
-
SESSION_INFO: "session:info",
|
|
1349
|
-
SESSION_END: "session:end",
|
|
1350
|
-
// Commands (NikCLI specific)
|
|
1351
|
-
COMMAND: "command",
|
|
1352
|
-
COMMAND_RESULT: "command:result",
|
|
1353
|
-
// Agent events
|
|
1354
|
-
AGENT_START: "agent:start",
|
|
1355
|
-
AGENT_PROGRESS: "agent:progress",
|
|
1356
|
-
AGENT_COMPLETE: "agent:complete",
|
|
1357
|
-
AGENT_ERROR: "agent:error"
|
|
1358
|
-
};
|
|
1359
|
-
|
|
1360
|
-
// src/terminal.ts
|
|
1361
|
-
import { EventEmitter } from "events";
|
|
1362
|
-
import { spawn } from "child_process";
|
|
1363
|
-
var pty = null;
|
|
1364
|
-
try {
|
|
1365
|
-
pty = require_lib();
|
|
1366
|
-
} catch {
|
|
1367
|
-
}
|
|
1368
|
-
var TerminalManager = class extends EventEmitter {
|
|
1369
|
-
config;
|
|
1370
|
-
ptyProcess = null;
|
|
1371
|
-
process = null;
|
|
1372
|
-
running = false;
|
|
1373
|
-
constructor(config) {
|
|
1374
|
-
super();
|
|
1375
|
-
this.config = config;
|
|
1376
|
-
}
|
|
1377
|
-
/**
|
|
1378
|
-
* Start the terminal
|
|
1379
|
-
*/
|
|
1380
|
-
start() {
|
|
1381
|
-
if (this.running) return;
|
|
1382
|
-
const { shell, cols, rows, cwd, env } = this.config;
|
|
1383
|
-
const termEnv = {
|
|
1384
|
-
...process.env,
|
|
1385
|
-
...env,
|
|
1386
|
-
TERM: "xterm-256color",
|
|
1387
|
-
COLORTERM: "truecolor"
|
|
1388
|
-
};
|
|
1389
|
-
if (pty) {
|
|
1390
|
-
try {
|
|
1391
|
-
this.ptyProcess = pty.spawn(shell, [], {
|
|
1392
|
-
name: "xterm-256color",
|
|
1393
|
-
cols,
|
|
1394
|
-
rows,
|
|
1395
|
-
cwd: cwd || process.cwd(),
|
|
1396
|
-
env: termEnv
|
|
1397
|
-
});
|
|
1398
|
-
this.ptyProcess.onData((data) => {
|
|
1399
|
-
this.emit("data", data);
|
|
1400
|
-
});
|
|
1401
|
-
this.ptyProcess.onExit(({ exitCode }) => {
|
|
1402
|
-
this.running = false;
|
|
1403
|
-
this.emit("exit", exitCode);
|
|
1404
|
-
});
|
|
1405
|
-
this.running = true;
|
|
1406
|
-
return;
|
|
1407
|
-
} catch (error) {
|
|
1408
|
-
console.warn("node-pty failed, using fallback:", error.message);
|
|
1409
|
-
}
|
|
1410
|
-
}
|
|
1411
|
-
this.process = spawn(shell, [], {
|
|
1412
|
-
cwd: cwd || process.cwd(),
|
|
1413
|
-
env: termEnv,
|
|
1414
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
1415
|
-
shell: true
|
|
1416
|
-
});
|
|
1417
|
-
this.process.stdout?.on("data", (data) => {
|
|
1418
|
-
this.emit("data", data.toString());
|
|
1419
|
-
});
|
|
1420
|
-
this.process.stderr?.on("data", (data) => {
|
|
1421
|
-
this.emit("data", data.toString());
|
|
1422
|
-
});
|
|
1423
|
-
this.process.on("exit", (code) => {
|
|
1424
|
-
this.running = false;
|
|
1425
|
-
this.emit("exit", code || 0);
|
|
1426
|
-
});
|
|
1427
|
-
this.process.on("error", (error) => {
|
|
1428
|
-
this.emit("error", error);
|
|
1429
|
-
});
|
|
1430
|
-
this.running = true;
|
|
1431
|
-
}
|
|
1432
|
-
/**
|
|
1433
|
-
* Write data to terminal
|
|
1434
|
-
*/
|
|
1435
|
-
write(data) {
|
|
1436
|
-
if (this.ptyProcess) {
|
|
1437
|
-
this.ptyProcess.write(data);
|
|
1438
|
-
} else if (this.process?.stdin) {
|
|
1439
|
-
this.process.stdin.write(data);
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
|
-
* Resize terminal
|
|
1444
|
-
*/
|
|
1445
|
-
resize(cols, rows) {
|
|
1446
|
-
if (this.ptyProcess) {
|
|
1447
|
-
this.ptyProcess.resize(cols, rows);
|
|
1448
|
-
}
|
|
1449
|
-
}
|
|
1450
|
-
/**
|
|
1451
|
-
* Clear terminal
|
|
1452
|
-
*/
|
|
1453
|
-
clear() {
|
|
1454
|
-
this.emit("data", "\x1B[2J\x1B[H");
|
|
1455
|
-
}
|
|
1456
|
-
/**
|
|
1457
|
-
* Kill terminal process
|
|
1458
|
-
*/
|
|
1459
|
-
destroy() {
|
|
1460
|
-
this.running = false;
|
|
1461
|
-
if (this.ptyProcess) {
|
|
1462
|
-
this.ptyProcess.kill();
|
|
1463
|
-
this.ptyProcess = null;
|
|
1464
|
-
}
|
|
1465
|
-
if (this.process) {
|
|
1466
|
-
this.process.kill();
|
|
1467
|
-
this.process = null;
|
|
1468
|
-
}
|
|
1469
|
-
}
|
|
1470
|
-
/**
|
|
1471
|
-
* Check if terminal is running
|
|
1472
|
-
*/
|
|
1473
|
-
isRunning() {
|
|
1474
|
-
return this.running;
|
|
1475
|
-
}
|
|
1476
|
-
/**
|
|
1477
|
-
* Check if using PTY
|
|
1478
|
-
*/
|
|
1479
|
-
hasPty() {
|
|
1480
|
-
return this.ptyProcess !== null;
|
|
1481
|
-
}
|
|
1482
|
-
};
|
|
1483
|
-
|
|
1484
|
-
// src/tunnel.ts
|
|
1485
|
-
import { spawn as spawn2 } from "child_process";
|
|
1486
|
-
var TunnelManager = class {
|
|
1487
|
-
provider;
|
|
1488
|
-
process = null;
|
|
1489
|
-
url = null;
|
|
1490
|
-
tunnelInstance = null;
|
|
1491
|
-
constructor(provider) {
|
|
1492
|
-
this.provider = provider;
|
|
1493
|
-
}
|
|
1494
|
-
/**
|
|
1495
|
-
* Create tunnel and return public URL
|
|
1496
|
-
*/
|
|
1497
|
-
async create(port) {
|
|
1498
|
-
switch (this.provider) {
|
|
1499
|
-
case "localtunnel":
|
|
1500
|
-
return this.createLocaltunnel(port);
|
|
1501
|
-
case "cloudflared":
|
|
1502
|
-
return this.createCloudflared(port);
|
|
1503
|
-
case "ngrok":
|
|
1504
|
-
return this.createNgrok(port);
|
|
1505
|
-
default:
|
|
1506
|
-
throw new Error(`Unknown tunnel provider: ${this.provider}`);
|
|
1507
|
-
}
|
|
1508
|
-
}
|
|
1509
|
-
/**
|
|
1510
|
-
* Close tunnel
|
|
1511
|
-
*/
|
|
1512
|
-
async close() {
|
|
1513
|
-
if (this.tunnelInstance?.close) {
|
|
1514
|
-
this.tunnelInstance.close();
|
|
1515
|
-
this.tunnelInstance = null;
|
|
1516
|
-
}
|
|
1517
|
-
if (this.process) {
|
|
1518
|
-
this.process.kill();
|
|
1519
|
-
this.process = null;
|
|
1520
|
-
}
|
|
1521
|
-
this.url = null;
|
|
1522
|
-
}
|
|
1523
|
-
/**
|
|
1524
|
-
* Get tunnel URL
|
|
1525
|
-
*/
|
|
1526
|
-
getUrl() {
|
|
1527
|
-
return this.url;
|
|
1528
|
-
}
|
|
1529
|
-
/**
|
|
1530
|
-
* Create localtunnel
|
|
1531
|
-
*/
|
|
1532
|
-
async createLocaltunnel(port) {
|
|
1533
|
-
try {
|
|
1534
|
-
const localtunnel = await import("./localtunnel-XT32JGNN.js");
|
|
1535
|
-
const tunnel = await localtunnel.default({ port });
|
|
1536
|
-
this.tunnelInstance = tunnel;
|
|
1537
|
-
this.url = tunnel.url;
|
|
1538
|
-
tunnel.on("close", () => {
|
|
1539
|
-
this.url = null;
|
|
1540
|
-
});
|
|
1541
|
-
return tunnel.url;
|
|
1542
|
-
} catch {
|
|
1543
|
-
return this.createLocaltunnelCli(port);
|
|
1544
|
-
}
|
|
1545
|
-
}
|
|
1546
|
-
/**
|
|
1547
|
-
* Create localtunnel via CLI
|
|
1548
|
-
*/
|
|
1549
|
-
createLocaltunnelCli(port) {
|
|
1550
|
-
return new Promise((resolve, reject) => {
|
|
1551
|
-
this.process = spawn2("npx", ["localtunnel", "--port", port.toString(), "--print-requests", "false"], {
|
|
1552
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
1553
|
-
shell: true
|
|
1554
|
-
});
|
|
1555
|
-
let output = "";
|
|
1556
|
-
const timeout = setTimeout(() => {
|
|
1557
|
-
reject(new Error("Localtunnel timeout"));
|
|
1558
|
-
}, 3e4);
|
|
1559
|
-
this.process.stdout?.on("data", (data) => {
|
|
1560
|
-
output += data.toString();
|
|
1561
|
-
const match = output.match(/your url is:\s*(https?:\/\/[^\s]+)/i);
|
|
1562
|
-
if (match) {
|
|
1563
|
-
clearTimeout(timeout);
|
|
1564
|
-
this.url = match[1];
|
|
1565
|
-
resolve(match[1]);
|
|
1566
|
-
}
|
|
1567
|
-
});
|
|
1568
|
-
this.process.stderr?.on("data", () => {
|
|
1569
|
-
});
|
|
1570
|
-
this.process.on("error", (error) => {
|
|
1571
|
-
clearTimeout(timeout);
|
|
1572
|
-
reject(error);
|
|
1573
|
-
});
|
|
1574
|
-
this.process.on("exit", (code) => {
|
|
1575
|
-
if (code !== 0 && !this.url) {
|
|
1576
|
-
clearTimeout(timeout);
|
|
1577
|
-
reject(new Error(`Localtunnel exited with code ${code}`));
|
|
1578
|
-
}
|
|
1579
|
-
});
|
|
1580
|
-
});
|
|
1581
|
-
}
|
|
1582
|
-
/**
|
|
1583
|
-
* Create cloudflared tunnel
|
|
1584
|
-
*/
|
|
1585
|
-
createCloudflared(port) {
|
|
1586
|
-
return new Promise((resolve, reject) => {
|
|
1587
|
-
this.process = spawn2(
|
|
1588
|
-
"cloudflared",
|
|
1589
|
-
["tunnel", "--url", `http://localhost:${port}`, "--metrics", "localhost:0"],
|
|
1590
|
-
{
|
|
1591
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
1592
|
-
}
|
|
1593
|
-
);
|
|
1594
|
-
let output = "";
|
|
1595
|
-
const timeout = setTimeout(() => {
|
|
1596
|
-
reject(new Error("Cloudflared timeout"));
|
|
1597
|
-
}, 3e4);
|
|
1598
|
-
const handleData = (data) => {
|
|
1599
|
-
output += data.toString();
|
|
1600
|
-
const match = output.match(/(https:\/\/[^\s]+\.trycloudflare\.com)/i);
|
|
1601
|
-
if (match) {
|
|
1602
|
-
clearTimeout(timeout);
|
|
1603
|
-
this.url = match[1];
|
|
1604
|
-
resolve(match[1]);
|
|
1605
|
-
}
|
|
1606
|
-
};
|
|
1607
|
-
this.process.stdout?.on("data", handleData);
|
|
1608
|
-
this.process.stderr?.on("data", handleData);
|
|
1609
|
-
this.process.on("error", (error) => {
|
|
1610
|
-
clearTimeout(timeout);
|
|
1611
|
-
reject(error);
|
|
1612
|
-
});
|
|
1613
|
-
this.process.on("exit", (code) => {
|
|
1614
|
-
if (code !== 0 && !this.url) {
|
|
1615
|
-
clearTimeout(timeout);
|
|
1616
|
-
reject(new Error(`Cloudflared exited with code ${code}`));
|
|
1617
|
-
}
|
|
1618
|
-
});
|
|
1619
|
-
});
|
|
1620
|
-
}
|
|
1621
|
-
/**
|
|
1622
|
-
* Create ngrok tunnel
|
|
1623
|
-
*/
|
|
1624
|
-
createNgrok(port) {
|
|
1625
|
-
return new Promise((resolve, reject) => {
|
|
1626
|
-
this.process = spawn2("ngrok", ["http", port.toString(), "--log=stdout", "--log-level=info"], {
|
|
1627
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
1628
|
-
});
|
|
1629
|
-
let output = "";
|
|
1630
|
-
const timeout = setTimeout(() => {
|
|
1631
|
-
reject(new Error("Ngrok timeout"));
|
|
1632
|
-
}, 3e4);
|
|
1633
|
-
this.process.stdout?.on("data", (data) => {
|
|
1634
|
-
output += data.toString();
|
|
1635
|
-
const match = output.match(/url=(https?:\/\/[^\s]+)/i);
|
|
1636
|
-
if (match) {
|
|
1637
|
-
clearTimeout(timeout);
|
|
1638
|
-
this.url = match[1];
|
|
1639
|
-
resolve(match[1]);
|
|
1640
|
-
}
|
|
1641
|
-
});
|
|
1642
|
-
this.process.stderr?.on("data", () => {
|
|
1643
|
-
});
|
|
1644
|
-
this.process.on("error", (error) => {
|
|
1645
|
-
clearTimeout(timeout);
|
|
1646
|
-
reject(error);
|
|
1647
|
-
});
|
|
1648
|
-
this.process.on("exit", (code) => {
|
|
1649
|
-
if (code !== 0 && !this.url) {
|
|
1650
|
-
clearTimeout(timeout);
|
|
1651
|
-
reject(new Error(`Ngrok exited with code ${code}`));
|
|
1652
|
-
}
|
|
1653
|
-
});
|
|
1654
|
-
});
|
|
1655
|
-
}
|
|
1656
|
-
};
|
|
1657
|
-
|
|
1658
|
-
// src/web-client.ts
|
|
1659
|
-
function getWebClient() {
|
|
1660
|
-
return `<!DOCTYPE html>
|
|
1661
|
-
<html lang="en">
|
|
1662
|
-
<head>
|
|
1663
|
-
<meta charset="UTF-8">
|
|
1664
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
1665
|
-
<meta name="apple-mobile-web-app-capable" content="yes">
|
|
1666
|
-
<meta name="mobile-web-app-capable" content="yes">
|
|
1667
|
-
<meta name="theme-color" content="#0d1117">
|
|
1668
|
-
<title>NikCLI Remote</title>
|
|
1669
|
-
<style>
|
|
1670
|
-
:root {
|
|
1671
|
-
--bg: #0d1117;
|
|
1672
|
-
--bg-secondary: #161b22;
|
|
1673
|
-
--fg: #e6edf3;
|
|
1674
|
-
--fg-muted: #8b949e;
|
|
1675
|
-
--accent: #58a6ff;
|
|
1676
|
-
--success: #3fb950;
|
|
1677
|
-
--warning: #d29922;
|
|
1678
|
-
--error: #f85149;
|
|
1679
|
-
--border: #30363d;
|
|
1680
|
-
--font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
|
|
1681
|
-
}
|
|
1682
|
-
|
|
1683
|
-
* {
|
|
1684
|
-
box-sizing: border-box;
|
|
1685
|
-
margin: 0;
|
|
1686
|
-
padding: 0;
|
|
1687
|
-
-webkit-tap-highlight-color: transparent;
|
|
1688
|
-
}
|
|
1689
|
-
|
|
1690
|
-
html, body {
|
|
1691
|
-
height: 100%;
|
|
1692
|
-
background: var(--bg);
|
|
1693
|
-
color: var(--fg);
|
|
1694
|
-
font-family: var(--font-mono);
|
|
1695
|
-
font-size: 14px;
|
|
1696
|
-
overflow: hidden;
|
|
1697
|
-
touch-action: manipulation;
|
|
1698
|
-
}
|
|
1699
|
-
|
|
1700
|
-
#app {
|
|
1701
|
-
display: flex;
|
|
1702
|
-
flex-direction: column;
|
|
1703
|
-
height: 100%;
|
|
1704
|
-
height: 100dvh;
|
|
1705
|
-
}
|
|
1706
|
-
|
|
1707
|
-
/* Header */
|
|
1708
|
-
#header {
|
|
1709
|
-
display: flex;
|
|
1710
|
-
align-items: center;
|
|
1711
|
-
justify-content: space-between;
|
|
1712
|
-
padding: 12px 16px;
|
|
1713
|
-
background: var(--bg-secondary);
|
|
1714
|
-
border-bottom: 1px solid var(--border);
|
|
1715
|
-
flex-shrink: 0;
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
|
-
#header h1 {
|
|
1719
|
-
font-size: 16px;
|
|
1720
|
-
font-weight: 600;
|
|
1721
|
-
color: var(--accent);
|
|
1722
|
-
display: flex;
|
|
1723
|
-
align-items: center;
|
|
1724
|
-
gap: 8px;
|
|
1725
|
-
}
|
|
1726
|
-
|
|
1727
|
-
#header h1::before {
|
|
1728
|
-
content: '';
|
|
1729
|
-
width: 10px;
|
|
1730
|
-
height: 10px;
|
|
1731
|
-
background: var(--accent);
|
|
1732
|
-
border-radius: 2px;
|
|
1733
|
-
}
|
|
1734
|
-
|
|
1735
|
-
#status {
|
|
1736
|
-
display: flex;
|
|
1737
|
-
align-items: center;
|
|
1738
|
-
gap: 6px;
|
|
1739
|
-
font-size: 12px;
|
|
1740
|
-
color: var(--fg-muted);
|
|
1741
|
-
}
|
|
1742
|
-
|
|
1743
|
-
#status-dot {
|
|
1744
|
-
width: 8px;
|
|
1745
|
-
height: 8px;
|
|
1746
|
-
border-radius: 50%;
|
|
1747
|
-
background: var(--error);
|
|
1748
|
-
transition: background 0.3s;
|
|
1749
|
-
}
|
|
1750
|
-
|
|
1751
|
-
#status-dot.connected {
|
|
1752
|
-
background: var(--success);
|
|
1753
|
-
}
|
|
1754
|
-
|
|
1755
|
-
#status-dot.connecting {
|
|
1756
|
-
background: var(--warning);
|
|
1757
|
-
animation: pulse 1s infinite;
|
|
1758
|
-
}
|
|
1759
|
-
|
|
1760
|
-
@keyframes pulse {
|
|
1761
|
-
0%, 100% { opacity: 1; }
|
|
1762
|
-
50% { opacity: 0.5; }
|
|
1763
|
-
}
|
|
1764
|
-
|
|
1765
|
-
/* Terminal */
|
|
1766
|
-
#terminal-container {
|
|
1767
|
-
flex: 1;
|
|
1768
|
-
overflow: hidden;
|
|
1769
|
-
position: relative;
|
|
1770
|
-
}
|
|
1771
|
-
|
|
1772
|
-
#terminal {
|
|
1773
|
-
height: 100%;
|
|
1774
|
-
padding: 12px;
|
|
1775
|
-
overflow-y: auto;
|
|
1776
|
-
overflow-x: hidden;
|
|
1777
|
-
font-size: 13px;
|
|
1778
|
-
line-height: 1.5;
|
|
1779
|
-
white-space: pre-wrap;
|
|
1780
|
-
word-break: break-all;
|
|
1781
|
-
-webkit-overflow-scrolling: touch;
|
|
1782
|
-
}
|
|
1783
|
-
|
|
1784
|
-
#terminal::-webkit-scrollbar {
|
|
1785
|
-
width: 6px;
|
|
1786
|
-
}
|
|
1787
|
-
|
|
1788
|
-
#terminal::-webkit-scrollbar-track {
|
|
1789
|
-
background: var(--bg);
|
|
1790
|
-
}
|
|
1791
|
-
|
|
1792
|
-
#terminal::-webkit-scrollbar-thumb {
|
|
1793
|
-
background: var(--border);
|
|
1794
|
-
border-radius: 3px;
|
|
1795
|
-
}
|
|
1796
|
-
|
|
1797
|
-
.cursor {
|
|
1798
|
-
display: inline-block;
|
|
1799
|
-
width: 8px;
|
|
1800
|
-
height: 16px;
|
|
1801
|
-
background: var(--fg);
|
|
1802
|
-
animation: blink 1s step-end infinite;
|
|
1803
|
-
vertical-align: text-bottom;
|
|
1804
|
-
}
|
|
1805
|
-
|
|
1806
|
-
@keyframes blink {
|
|
1807
|
-
50% { opacity: 0; }
|
|
1808
|
-
}
|
|
1809
|
-
|
|
1810
|
-
/* Notifications */
|
|
1811
|
-
#notifications {
|
|
1812
|
-
position: fixed;
|
|
1813
|
-
top: 60px;
|
|
1814
|
-
left: 12px;
|
|
1815
|
-
right: 12px;
|
|
1816
|
-
z-index: 1000;
|
|
1817
|
-
pointer-events: none;
|
|
1818
|
-
}
|
|
1819
|
-
|
|
1820
|
-
.notification {
|
|
1821
|
-
background: var(--bg-secondary);
|
|
1822
|
-
border: 1px solid var(--border);
|
|
1823
|
-
border-radius: 8px;
|
|
1824
|
-
padding: 12px 16px;
|
|
1825
|
-
margin-bottom: 8px;
|
|
1826
|
-
animation: slideIn 0.3s ease;
|
|
1827
|
-
pointer-events: auto;
|
|
1828
|
-
box-shadow: 0 4px 12px rgba(0,0,0,0.4);
|
|
1829
|
-
}
|
|
1830
|
-
|
|
1831
|
-
.notification.success { border-left: 3px solid var(--success); }
|
|
1832
|
-
.notification.error { border-left: 3px solid var(--error); }
|
|
1833
|
-
.notification.warning { border-left: 3px solid var(--warning); }
|
|
1834
|
-
.notification.info { border-left: 3px solid var(--accent); }
|
|
1835
|
-
|
|
1836
|
-
.notification h4 {
|
|
1837
|
-
font-size: 14px;
|
|
1838
|
-
font-weight: 600;
|
|
1839
|
-
margin-bottom: 4px;
|
|
1840
|
-
}
|
|
1841
|
-
|
|
1842
|
-
.notification p {
|
|
1843
|
-
font-size: 12px;
|
|
1844
|
-
color: var(--fg-muted);
|
|
1845
|
-
}
|
|
1846
|
-
|
|
1847
|
-
@keyframes slideIn {
|
|
1848
|
-
from { transform: translateY(-20px); opacity: 0; }
|
|
1849
|
-
to { transform: translateY(0); opacity: 1; }
|
|
1850
|
-
}
|
|
1851
|
-
|
|
1852
|
-
/* Quick Keys */
|
|
1853
|
-
#quickkeys {
|
|
1854
|
-
display: grid;
|
|
1855
|
-
grid-template-columns: repeat(6, 1fr);
|
|
1856
|
-
gap: 6px;
|
|
1857
|
-
padding: 8px 12px;
|
|
1858
|
-
background: var(--bg-secondary);
|
|
1859
|
-
border-top: 1px solid var(--border);
|
|
1860
|
-
flex-shrink: 0;
|
|
1861
|
-
}
|
|
1862
|
-
|
|
1863
|
-
.qkey {
|
|
1864
|
-
background: var(--bg);
|
|
1865
|
-
border: 1px solid var(--border);
|
|
1866
|
-
border-radius: 6px;
|
|
1867
|
-
padding: 10px 4px;
|
|
1868
|
-
color: var(--fg);
|
|
1869
|
-
font-size: 11px;
|
|
1870
|
-
font-family: var(--font-mono);
|
|
1871
|
-
text-align: center;
|
|
1872
|
-
cursor: pointer;
|
|
1873
|
-
user-select: none;
|
|
1874
|
-
transition: background 0.1s, transform 0.1s;
|
|
1875
|
-
}
|
|
1876
|
-
|
|
1877
|
-
.qkey:active {
|
|
1878
|
-
background: var(--border);
|
|
1879
|
-
transform: scale(0.95);
|
|
1880
|
-
}
|
|
1881
|
-
|
|
1882
|
-
.qkey.wide {
|
|
1883
|
-
grid-column: span 2;
|
|
1884
|
-
}
|
|
1885
|
-
|
|
1886
|
-
.qkey.accent {
|
|
1887
|
-
background: var(--accent);
|
|
1888
|
-
border-color: var(--accent);
|
|
1889
|
-
color: #fff;
|
|
1890
|
-
}
|
|
1891
|
-
|
|
1892
|
-
/* Input */
|
|
1893
|
-
#input-container {
|
|
1894
|
-
padding: 12px;
|
|
1895
|
-
background: var(--bg-secondary);
|
|
1896
|
-
border-top: 1px solid var(--border);
|
|
1897
|
-
flex-shrink: 0;
|
|
1898
|
-
}
|
|
1899
|
-
|
|
1900
|
-
#input-row {
|
|
1901
|
-
display: flex;
|
|
1902
|
-
gap: 8px;
|
|
1903
|
-
}
|
|
1904
|
-
|
|
1905
|
-
#input {
|
|
1906
|
-
flex: 1;
|
|
1907
|
-
background: var(--bg);
|
|
1908
|
-
border: 1px solid var(--border);
|
|
1909
|
-
border-radius: 8px;
|
|
1910
|
-
padding: 12px 14px;
|
|
1911
|
-
color: var(--fg);
|
|
1912
|
-
font-family: var(--font-mono);
|
|
1913
|
-
font-size: 16px;
|
|
1914
|
-
outline: none;
|
|
1915
|
-
transition: border-color 0.2s;
|
|
1916
|
-
}
|
|
1917
|
-
|
|
1918
|
-
#input:focus {
|
|
1919
|
-
border-color: var(--accent);
|
|
1920
|
-
}
|
|
1921
|
-
|
|
1922
|
-
#input::placeholder {
|
|
1923
|
-
color: var(--fg-muted);
|
|
1924
|
-
}
|
|
1925
|
-
|
|
1926
|
-
#send {
|
|
1927
|
-
background: var(--accent);
|
|
1928
|
-
color: #fff;
|
|
1929
|
-
border: none;
|
|
1930
|
-
border-radius: 8px;
|
|
1931
|
-
padding: 12px 20px;
|
|
1932
|
-
font-size: 14px;
|
|
1933
|
-
font-weight: 600;
|
|
1934
|
-
font-family: var(--font-mono);
|
|
1935
|
-
cursor: pointer;
|
|
1936
|
-
transition: opacity 0.2s, transform 0.1s;
|
|
1937
|
-
}
|
|
1938
|
-
|
|
1939
|
-
#send:active {
|
|
1940
|
-
opacity: 0.8;
|
|
1941
|
-
transform: scale(0.98);
|
|
1942
|
-
}
|
|
1943
|
-
|
|
1944
|
-
/* Auth Screen */
|
|
1945
|
-
#auth-screen {
|
|
1946
|
-
position: fixed;
|
|
1947
|
-
inset: 0;
|
|
1948
|
-
background: var(--bg);
|
|
1949
|
-
display: flex;
|
|
1950
|
-
flex-direction: column;
|
|
1951
|
-
align-items: center;
|
|
1952
|
-
justify-content: center;
|
|
1953
|
-
gap: 20px;
|
|
1954
|
-
z-index: 2000;
|
|
1955
|
-
}
|
|
1956
|
-
|
|
1957
|
-
#auth-screen.hidden {
|
|
1958
|
-
display: none;
|
|
1959
|
-
}
|
|
1960
|
-
|
|
1961
|
-
.spinner {
|
|
1962
|
-
width: 40px;
|
|
1963
|
-
height: 40px;
|
|
1964
|
-
border: 3px solid var(--border);
|
|
1965
|
-
border-top-color: var(--accent);
|
|
1966
|
-
border-radius: 50%;
|
|
1967
|
-
animation: spin 1s linear infinite;
|
|
1968
|
-
}
|
|
1969
|
-
|
|
1970
|
-
@keyframes spin {
|
|
1971
|
-
to { transform: rotate(360deg); }
|
|
1972
|
-
}
|
|
1973
|
-
|
|
1974
|
-
#auth-screen p {
|
|
1975
|
-
color: var(--fg-muted);
|
|
1976
|
-
font-size: 14px;
|
|
1977
|
-
}
|
|
1978
|
-
|
|
1979
|
-
#auth-screen .error {
|
|
1980
|
-
color: var(--error);
|
|
1981
|
-
}
|
|
1982
|
-
|
|
1983
|
-
/* Safe area padding for notched devices */
|
|
1984
|
-
@supports (padding: env(safe-area-inset-bottom)) {
|
|
1985
|
-
#input-container {
|
|
1986
|
-
padding-bottom: calc(12px + env(safe-area-inset-bottom));
|
|
1987
|
-
}
|
|
1988
|
-
}
|
|
1989
|
-
|
|
1990
|
-
/* Landscape adjustments */
|
|
1991
|
-
@media (max-height: 500px) {
|
|
1992
|
-
#quickkeys {
|
|
1993
|
-
grid-template-columns: repeat(12, 1fr);
|
|
1994
|
-
padding: 6px 8px;
|
|
1995
|
-
}
|
|
1996
|
-
.qkey {
|
|
1997
|
-
padding: 8px 2px;
|
|
1998
|
-
font-size: 10px;
|
|
1999
|
-
}
|
|
2000
|
-
#terminal {
|
|
2001
|
-
font-size: 12px;
|
|
2002
|
-
}
|
|
2003
|
-
}
|
|
2004
|
-
</style>
|
|
2005
|
-
</head>
|
|
2006
|
-
<body>
|
|
2007
|
-
<div id="app">
|
|
2008
|
-
<div id="auth-screen">
|
|
2009
|
-
<div class="spinner"></div>
|
|
2010
|
-
<p id="auth-status">Connecting to NikCLI...</p>
|
|
2011
|
-
</div>
|
|
2012
|
-
|
|
2013
|
-
<header id="header">
|
|
2014
|
-
<h1>NikCLI Remote</h1>
|
|
2015
|
-
<div id="status">
|
|
2016
|
-
<span id="status-dot" class="connecting"></span>
|
|
2017
|
-
<span id="status-text">Connecting</span>
|
|
2018
|
-
</div>
|
|
2019
|
-
</header>
|
|
2020
|
-
|
|
2021
|
-
<div id="terminal-container">
|
|
2022
|
-
<div id="terminal"></div>
|
|
2023
|
-
</div>
|
|
2024
|
-
|
|
2025
|
-
<div id="notifications"></div>
|
|
2026
|
-
|
|
2027
|
-
<div id="quickkeys">
|
|
2028
|
-
<button class="qkey" data-key="\\t">Tab</button>
|
|
2029
|
-
<button class="qkey" data-key="\\x1b[A">\u2191</button>
|
|
2030
|
-
<button class="qkey" data-key="\\x1b[B">\u2193</button>
|
|
2031
|
-
<button class="qkey" data-key="\\x1b[D">\u2190</button>
|
|
2032
|
-
<button class="qkey" data-key="\\x1b[C">\u2192</button>
|
|
2033
|
-
<button class="qkey" data-key="\\x1b">Esc</button>
|
|
2034
|
-
<button class="qkey" data-key="\\x03">^C</button>
|
|
2035
|
-
<button class="qkey" data-key="\\x04">^D</button>
|
|
2036
|
-
<button class="qkey" data-key="\\x1a">^Z</button>
|
|
2037
|
-
<button class="qkey" data-key="\\x0c">^L</button>
|
|
2038
|
-
<button class="qkey wide accent" data-key="\\r">Enter \u23CE</button>
|
|
2039
|
-
</div>
|
|
2040
|
-
|
|
2041
|
-
<div id="input-container">
|
|
2042
|
-
<div id="input-row">
|
|
2043
|
-
<input type="text" id="input" placeholder="Type command..." autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">
|
|
2044
|
-
<button id="send">Send</button>
|
|
2045
|
-
</div>
|
|
2046
|
-
</div>
|
|
2047
|
-
</div>
|
|
2048
|
-
|
|
2049
|
-
<script>
|
|
2050
|
-
(function() {
|
|
2051
|
-
'use strict';
|
|
2052
|
-
|
|
2053
|
-
// Parse URL params
|
|
2054
|
-
const params = new URLSearchParams(location.search);
|
|
2055
|
-
const token = params.get('t');
|
|
2056
|
-
const sessionId = params.get('s');
|
|
2057
|
-
|
|
2058
|
-
// DOM elements
|
|
2059
|
-
const terminal = document.getElementById('terminal');
|
|
2060
|
-
const input = document.getElementById('input');
|
|
2061
|
-
const sendBtn = document.getElementById('send');
|
|
2062
|
-
const statusDot = document.getElementById('status-dot');
|
|
2063
|
-
const statusText = document.getElementById('status-text');
|
|
2064
|
-
const authScreen = document.getElementById('auth-screen');
|
|
2065
|
-
const authStatus = document.getElementById('auth-status');
|
|
2066
|
-
const notifications = document.getElementById('notifications');
|
|
2067
|
-
|
|
2068
|
-
// State
|
|
2069
|
-
let ws = null;
|
|
2070
|
-
let reconnectAttempts = 0;
|
|
2071
|
-
const maxReconnectAttempts = 5;
|
|
2072
|
-
let terminalEnabled = true;
|
|
2073
|
-
|
|
2074
|
-
// Connect to WebSocket
|
|
2075
|
-
function connect() {
|
|
2076
|
-
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
2077
|
-
ws = new WebSocket(protocol + '//' + location.host);
|
|
2078
|
-
|
|
2079
|
-
ws.onopen = function() {
|
|
2080
|
-
setStatus('connecting', 'Authenticating...');
|
|
2081
|
-
ws.send(JSON.stringify({ type: 'auth', token: token }));
|
|
2082
|
-
};
|
|
2083
|
-
|
|
2084
|
-
ws.onmessage = function(event) {
|
|
2085
|
-
try {
|
|
2086
|
-
const msg = JSON.parse(event.data);
|
|
2087
|
-
handleMessage(msg);
|
|
2088
|
-
} catch (e) {
|
|
2089
|
-
console.error('Parse error:', e);
|
|
2090
|
-
}
|
|
2091
|
-
};
|
|
2092
|
-
|
|
2093
|
-
ws.onclose = function() {
|
|
2094
|
-
setStatus('disconnected', 'Disconnected');
|
|
2095
|
-
if (reconnectAttempts < maxReconnectAttempts) {
|
|
2096
|
-
reconnectAttempts++;
|
|
2097
|
-
const delay = Math.min(2000 * reconnectAttempts, 10000);
|
|
2098
|
-
setTimeout(connect, delay);
|
|
2099
|
-
} else {
|
|
2100
|
-
authStatus.textContent = 'Connection failed. Refresh to retry.';
|
|
2101
|
-
authStatus.classList.add('error');
|
|
2102
|
-
authScreen.classList.remove('hidden');
|
|
2103
|
-
}
|
|
2104
|
-
};
|
|
2105
|
-
|
|
2106
|
-
ws.onerror = function() {
|
|
2107
|
-
console.error('WebSocket error');
|
|
2108
|
-
};
|
|
2109
|
-
}
|
|
2110
|
-
|
|
2111
|
-
// Handle incoming message
|
|
2112
|
-
function handleMessage(msg) {
|
|
2113
|
-
switch (msg.type) {
|
|
2114
|
-
case 'auth:required':
|
|
2115
|
-
// Already sent auth on open
|
|
2116
|
-
break;
|
|
2117
|
-
|
|
2118
|
-
case 'auth:success':
|
|
2119
|
-
authScreen.classList.add('hidden');
|
|
2120
|
-
setStatus('connected', 'Connected');
|
|
2121
|
-
reconnectAttempts = 0;
|
|
2122
|
-
terminalEnabled = msg.payload?.terminalEnabled !== false;
|
|
2123
|
-
if (terminalEnabled) {
|
|
2124
|
-
appendOutput('\\x1b[32mConnected to NikCLI\\x1b[0m\\n\\n');
|
|
2125
|
-
}
|
|
2126
|
-
break;
|
|
2127
|
-
|
|
2128
|
-
case 'auth:failed':
|
|
2129
|
-
authStatus.textContent = 'Authentication failed';
|
|
2130
|
-
authStatus.classList.add('error');
|
|
2131
|
-
break;
|
|
2132
|
-
|
|
2133
|
-
case 'terminal:output':
|
|
2134
|
-
if (msg.payload?.data) {
|
|
2135
|
-
appendOutput(msg.payload.data);
|
|
2136
|
-
}
|
|
2137
|
-
break;
|
|
2138
|
-
|
|
2139
|
-
case 'terminal:exit':
|
|
2140
|
-
appendOutput('\\n\\x1b[33m[Process exited with code ' + (msg.payload?.code || 0) + ']\\x1b[0m\\n');
|
|
2141
|
-
break;
|
|
2142
|
-
|
|
2143
|
-
case 'notification':
|
|
2144
|
-
showNotification(msg.payload);
|
|
2145
|
-
break;
|
|
2146
|
-
|
|
2147
|
-
case 'session:end':
|
|
2148
|
-
appendOutput('\\n\\x1b[31m[Session ended]\\x1b[0m\\n');
|
|
2149
|
-
setStatus('disconnected', 'Session ended');
|
|
2150
|
-
break;
|
|
2151
|
-
|
|
2152
|
-
case 'pong':
|
|
2153
|
-
// Heartbeat response
|
|
2154
|
-
break;
|
|
2155
|
-
|
|
2156
|
-
default:
|
|
2157
|
-
console.log('Unknown message:', msg.type);
|
|
2158
|
-
}
|
|
2159
|
-
}
|
|
2160
|
-
|
|
2161
|
-
// Append text to terminal with ANSI support
|
|
2162
|
-
function appendOutput(text) {
|
|
2163
|
-
// Simple ANSI to HTML conversion
|
|
2164
|
-
const html = ansiToHtml(text);
|
|
2165
|
-
terminal.innerHTML += html;
|
|
2166
|
-
terminal.scrollTop = terminal.scrollHeight;
|
|
2167
|
-
}
|
|
2168
|
-
|
|
2169
|
-
// Basic ANSI to HTML
|
|
2170
|
-
function ansiToHtml(text) {
|
|
2171
|
-
const ansiColors = {
|
|
2172
|
-
'30': '#6e7681', '31': '#f85149', '32': '#3fb950', '33': '#d29922',
|
|
2173
|
-
'34': '#58a6ff', '35': '#bc8cff', '36': '#76e3ea', '37': '#e6edf3',
|
|
2174
|
-
'90': '#6e7681', '91': '#f85149', '92': '#3fb950', '93': '#d29922',
|
|
2175
|
-
'94': '#58a6ff', '95': '#bc8cff', '96': '#76e3ea', '97': '#ffffff'
|
|
2176
|
-
};
|
|
2177
|
-
|
|
2178
|
-
let result = '';
|
|
2179
|
-
let currentStyle = '';
|
|
2180
|
-
|
|
2181
|
-
const parts = text.split(/\\x1b\\[([0-9;]+)m/);
|
|
2182
|
-
for (let i = 0; i < parts.length; i++) {
|
|
2183
|
-
if (i % 2 === 0) {
|
|
2184
|
-
// Text content
|
|
2185
|
-
result += escapeHtml(parts[i]);
|
|
2186
|
-
} else {
|
|
2187
|
-
// ANSI code
|
|
2188
|
-
const codes = parts[i].split(';');
|
|
2189
|
-
for (const code of codes) {
|
|
2190
|
-
if (code === '0') {
|
|
2191
|
-
if (currentStyle) {
|
|
2192
|
-
result += '</span>';
|
|
2193
|
-
currentStyle = '';
|
|
2194
|
-
}
|
|
2195
|
-
} else if (code === '1') {
|
|
2196
|
-
currentStyle = 'font-weight:bold;';
|
|
2197
|
-
result += '<span style="' + currentStyle + '">';
|
|
2198
|
-
} else if (ansiColors[code]) {
|
|
2199
|
-
if (currentStyle) result += '</span>';
|
|
2200
|
-
currentStyle = 'color:' + ansiColors[code] + ';';
|
|
2201
|
-
result += '<span style="' + currentStyle + '">';
|
|
2202
|
-
}
|
|
2203
|
-
}
|
|
2204
|
-
}
|
|
2205
|
-
}
|
|
2206
|
-
|
|
2207
|
-
if (currentStyle) result += '</span>';
|
|
2208
|
-
return result;
|
|
2209
|
-
}
|
|
2210
|
-
|
|
2211
|
-
// Escape HTML
|
|
2212
|
-
function escapeHtml(text) {
|
|
2213
|
-
return text
|
|
2214
|
-
.replace(/&/g, '&')
|
|
2215
|
-
.replace(/</g, '<')
|
|
2216
|
-
.replace(/>/g, '>')
|
|
2217
|
-
.replace(/"/g, '"')
|
|
2218
|
-
.replace(/\\n/g, '<br>')
|
|
2219
|
-
.replace(/ /g, ' ');
|
|
2220
|
-
}
|
|
2221
|
-
|
|
2222
|
-
// Set connection status
|
|
2223
|
-
function setStatus(state, text) {
|
|
2224
|
-
statusDot.className = state === 'connected' ? 'connected' :
|
|
2225
|
-
state === 'connecting' ? 'connecting' : '';
|
|
2226
|
-
statusText.textContent = text;
|
|
2227
|
-
}
|
|
2228
|
-
|
|
2229
|
-
// Send data to terminal
|
|
2230
|
-
function send(data) {
|
|
2231
|
-
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
2232
|
-
ws.send(JSON.stringify({ type: 'terminal:input', data: data }));
|
|
2233
|
-
}
|
|
2234
|
-
}
|
|
2235
|
-
|
|
2236
|
-
// Show notification
|
|
2237
|
-
function showNotification(n) {
|
|
2238
|
-
if (!n) return;
|
|
2239
|
-
const el = document.createElement('div');
|
|
2240
|
-
el.className = 'notification ' + (n.type || 'info');
|
|
2241
|
-
el.innerHTML = '<h4>' + escapeHtml(n.title || 'Notification') + '</h4>' +
|
|
2242
|
-
'<p>' + escapeHtml(n.body || '') + '</p>';
|
|
2243
|
-
notifications.appendChild(el);
|
|
2244
|
-
setTimeout(function() { el.remove(); }, 5000);
|
|
2245
|
-
}
|
|
2246
|
-
|
|
2247
|
-
// Event: Send button
|
|
2248
|
-
sendBtn.onclick = function() {
|
|
2249
|
-
if (input.value) {
|
|
2250
|
-
send(input.value + '\\r');
|
|
2251
|
-
input.value = '';
|
|
2252
|
-
}
|
|
2253
|
-
input.focus();
|
|
2254
|
-
};
|
|
2255
|
-
|
|
2256
|
-
// Event: Enter key in input
|
|
2257
|
-
input.onkeydown = function(e) {
|
|
2258
|
-
if (e.key === 'Enter') {
|
|
2259
|
-
e.preventDefault();
|
|
2260
|
-
sendBtn.click();
|
|
2261
|
-
}
|
|
2262
|
-
};
|
|
2263
|
-
|
|
2264
|
-
// Event: Quick keys
|
|
2265
|
-
document.querySelectorAll('.qkey').forEach(function(btn) {
|
|
2266
|
-
btn.onclick = function() {
|
|
2267
|
-
const key = btn.dataset.key;
|
|
2268
|
-
const decoded = key
|
|
2269
|
-
.replace(/\\\\x([0-9a-f]{2})/gi, function(_, hex) {
|
|
2270
|
-
return String.fromCharCode(parseInt(hex, 16));
|
|
2271
|
-
})
|
|
2272
|
-
.replace(/\\\\t/g, '\\t')
|
|
2273
|
-
.replace(/\\\\r/g, '\\r')
|
|
2274
|
-
.replace(/\\\\n/g, '\\n');
|
|
2275
|
-
send(decoded);
|
|
2276
|
-
input.focus();
|
|
2277
|
-
};
|
|
2278
|
-
});
|
|
2279
|
-
|
|
2280
|
-
// Heartbeat
|
|
2281
|
-
setInterval(function() {
|
|
2282
|
-
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
2283
|
-
ws.send(JSON.stringify({ type: 'ping' }));
|
|
2284
|
-
}
|
|
2285
|
-
}, 25000);
|
|
2286
|
-
|
|
2287
|
-
// Handle resize
|
|
2288
|
-
function sendResize() {
|
|
2289
|
-
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
2290
|
-
const cols = Math.floor(terminal.clientWidth / 8);
|
|
2291
|
-
const rows = Math.floor(terminal.clientHeight / 18);
|
|
2292
|
-
ws.send(JSON.stringify({ type: 'terminal:resize', cols: cols, rows: rows }));
|
|
2293
|
-
}
|
|
2294
|
-
}
|
|
2295
|
-
|
|
2296
|
-
window.addEventListener('resize', sendResize);
|
|
2297
|
-
setTimeout(sendResize, 1000);
|
|
2298
|
-
|
|
2299
|
-
// Start connection
|
|
2300
|
-
if (token) {
|
|
2301
|
-
connect();
|
|
2302
|
-
} else {
|
|
2303
|
-
authStatus.textContent = 'Invalid session URL';
|
|
2304
|
-
authStatus.classList.add('error');
|
|
2305
|
-
}
|
|
2306
|
-
})();
|
|
2307
|
-
</script>
|
|
2308
|
-
</body>
|
|
2309
|
-
</html>`;
|
|
2310
|
-
}
|
|
2311
|
-
|
|
2312
|
-
// src/server.ts
|
|
2313
|
-
var RemoteServer = class extends EventEmitter2 {
|
|
2314
|
-
config;
|
|
2315
|
-
httpServer = null;
|
|
2316
|
-
wss = null;
|
|
2317
|
-
clients = /* @__PURE__ */ new Map();
|
|
2318
|
-
session = null;
|
|
2319
|
-
terminal = null;
|
|
2320
|
-
tunnel = null;
|
|
2321
|
-
heartbeatTimer = null;
|
|
2322
|
-
sessionTimeoutTimer = null;
|
|
2323
|
-
isRunning = false;
|
|
2324
|
-
sessionSecret;
|
|
2325
|
-
constructor(config = {}) {
|
|
2326
|
-
super();
|
|
2327
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
2328
|
-
this.sessionSecret = config.sessionSecret || this.generateSecret();
|
|
2329
|
-
}
|
|
2330
|
-
/**
|
|
2331
|
-
* Start the remote server
|
|
2332
|
-
*/
|
|
2333
|
-
async start(options = {}) {
|
|
2334
|
-
if (this.isRunning) {
|
|
2335
|
-
throw new Error("Server already running");
|
|
2336
|
-
}
|
|
2337
|
-
const sessionId = this.generateSessionId();
|
|
2338
|
-
this.httpServer = createServer((req, res) => this.handleHttpRequest(req, res));
|
|
2339
|
-
this.wss = new WebSocketServer({ server: this.httpServer });
|
|
2340
|
-
this.setupWebSocketHandlers();
|
|
2341
|
-
const port = await new Promise((resolve, reject) => {
|
|
2342
|
-
this.httpServer.listen(this.config.port, this.config.host, () => {
|
|
2343
|
-
const addr = this.httpServer.address();
|
|
2344
|
-
resolve(typeof addr === "object" ? addr?.port || 0 : 0);
|
|
2345
|
-
});
|
|
2346
|
-
this.httpServer.on("error", reject);
|
|
2347
|
-
});
|
|
2348
|
-
const localIp = this.getLocalIP();
|
|
2349
|
-
const localUrl = `http://${localIp}:${port}`;
|
|
2350
|
-
this.session = {
|
|
2351
|
-
id: sessionId,
|
|
2352
|
-
name: options.name || `nikcli-${sessionId}`,
|
|
2353
|
-
qrCode: "",
|
|
2354
|
-
qrUrl: localUrl,
|
|
2355
|
-
localUrl,
|
|
2356
|
-
status: "starting",
|
|
2357
|
-
connectedDevices: [],
|
|
2358
|
-
startedAt: /* @__PURE__ */ new Date(),
|
|
2359
|
-
lastActivity: /* @__PURE__ */ new Date(),
|
|
2360
|
-
port
|
|
2361
|
-
};
|
|
2362
|
-
if (this.config.enableTunnel && this.config.tunnelProvider !== "none") {
|
|
2363
|
-
try {
|
|
2364
|
-
this.tunnel = new TunnelManager(this.config.tunnelProvider);
|
|
2365
|
-
const tunnelUrl = await this.tunnel.create(port);
|
|
2366
|
-
this.session.tunnelUrl = tunnelUrl;
|
|
2367
|
-
this.session.qrUrl = `${tunnelUrl}?s=${sessionId}&t=${this.sessionSecret}`;
|
|
2368
|
-
this.emit("tunnel:connected", tunnelUrl);
|
|
2369
|
-
} catch (error) {
|
|
2370
|
-
this.emit("tunnel:error", error);
|
|
2371
|
-
this.session.qrUrl = `${localUrl}?s=${sessionId}&t=${this.sessionSecret}`;
|
|
2372
|
-
}
|
|
2373
|
-
} else {
|
|
2374
|
-
this.session.qrUrl = `${localUrl}?s=${sessionId}&t=${this.sessionSecret}`;
|
|
2375
|
-
}
|
|
2376
|
-
if (this.config.enableTerminal) {
|
|
2377
|
-
this.terminal = new TerminalManager({
|
|
2378
|
-
shell: this.config.shell,
|
|
2379
|
-
cols: this.config.cols,
|
|
2380
|
-
rows: this.config.rows,
|
|
2381
|
-
cwd: this.config.cwd,
|
|
2382
|
-
env: this.config.env
|
|
2383
|
-
});
|
|
2384
|
-
this.terminal.on("data", (data) => {
|
|
2385
|
-
this.broadcast({ type: MessageTypes.TERMINAL_OUTPUT, payload: { data } });
|
|
2386
|
-
this.emit("terminal:output", data);
|
|
2387
|
-
});
|
|
2388
|
-
this.terminal.on("exit", (code) => {
|
|
2389
|
-
this.broadcast({ type: MessageTypes.TERMINAL_EXIT, payload: { code } });
|
|
2390
|
-
});
|
|
2391
|
-
}
|
|
2392
|
-
this.startHeartbeat();
|
|
2393
|
-
if (this.config.sessionTimeout > 0) {
|
|
2394
|
-
this.startSessionTimeout();
|
|
2395
|
-
}
|
|
2396
|
-
this.session.status = "waiting";
|
|
2397
|
-
this.isRunning = true;
|
|
2398
|
-
this.emit("started", this.session);
|
|
2399
|
-
return this.session;
|
|
2400
|
-
}
|
|
2401
|
-
/**
|
|
2402
|
-
* Stop the server
|
|
2403
|
-
*/
|
|
2404
|
-
async stop() {
|
|
2405
|
-
if (!this.isRunning) return;
|
|
2406
|
-
this.isRunning = false;
|
|
2407
|
-
if (this.heartbeatTimer) {
|
|
2408
|
-
clearInterval(this.heartbeatTimer);
|
|
2409
|
-
this.heartbeatTimer = null;
|
|
2410
|
-
}
|
|
2411
|
-
if (this.sessionTimeoutTimer) {
|
|
2412
|
-
clearTimeout(this.sessionTimeoutTimer);
|
|
2413
|
-
this.sessionTimeoutTimer = null;
|
|
2414
|
-
}
|
|
2415
|
-
this.broadcast({ type: MessageTypes.SESSION_END, payload: {} });
|
|
2416
|
-
for (const client of this.clients.values()) {
|
|
2417
|
-
client.ws.close(1e3, "Server shutting down");
|
|
2418
|
-
}
|
|
2419
|
-
this.clients.clear();
|
|
2420
|
-
if (this.terminal) {
|
|
2421
|
-
this.terminal.destroy();
|
|
2422
|
-
this.terminal = null;
|
|
2423
|
-
}
|
|
2424
|
-
if (this.tunnel) {
|
|
2425
|
-
await this.tunnel.close();
|
|
2426
|
-
this.tunnel = null;
|
|
2427
|
-
}
|
|
2428
|
-
if (this.wss) {
|
|
2429
|
-
this.wss.close();
|
|
2430
|
-
this.wss = null;
|
|
2431
|
-
}
|
|
2432
|
-
if (this.httpServer) {
|
|
2433
|
-
await new Promise((resolve) => {
|
|
2434
|
-
this.httpServer.close(() => resolve());
|
|
2435
|
-
});
|
|
2436
|
-
this.httpServer = null;
|
|
2437
|
-
}
|
|
2438
|
-
if (this.session) {
|
|
2439
|
-
this.session.status = "stopped";
|
|
2440
|
-
}
|
|
2441
|
-
this.emit("stopped");
|
|
2442
|
-
}
|
|
2443
|
-
/**
|
|
2444
|
-
* Broadcast message to all authenticated clients
|
|
2445
|
-
*/
|
|
2446
|
-
broadcast(message) {
|
|
2447
|
-
const data = JSON.stringify({
|
|
2448
|
-
type: message.type,
|
|
2449
|
-
payload: message.payload,
|
|
2450
|
-
timestamp: message.timestamp || Date.now()
|
|
2451
|
-
});
|
|
2452
|
-
for (const client of this.clients.values()) {
|
|
2453
|
-
if (client.authenticated && client.ws.readyState === WebSocket.OPEN) {
|
|
2454
|
-
client.ws.send(data);
|
|
2455
|
-
}
|
|
2456
|
-
}
|
|
2457
|
-
}
|
|
2458
|
-
/**
|
|
2459
|
-
* Send notification to clients
|
|
2460
|
-
*/
|
|
2461
|
-
notify(notification) {
|
|
2462
|
-
this.broadcast({
|
|
2463
|
-
type: MessageTypes.NOTIFICATION,
|
|
2464
|
-
payload: notification
|
|
2465
|
-
});
|
|
2466
|
-
}
|
|
2467
|
-
/**
|
|
2468
|
-
* Get current session
|
|
2469
|
-
*/
|
|
2470
|
-
getSession() {
|
|
2471
|
-
return this.session;
|
|
2472
|
-
}
|
|
2473
|
-
/**
|
|
2474
|
-
* Check if server is running
|
|
2475
|
-
*/
|
|
2476
|
-
isActive() {
|
|
2477
|
-
return this.isRunning && this.session?.status !== "stopped";
|
|
2478
|
-
}
|
|
2479
|
-
/**
|
|
2480
|
-
* Get connected client count
|
|
2481
|
-
*/
|
|
2482
|
-
getConnectedCount() {
|
|
2483
|
-
let count = 0;
|
|
2484
|
-
for (const client of this.clients.values()) {
|
|
2485
|
-
if (client.authenticated) count++;
|
|
2486
|
-
}
|
|
2487
|
-
return count;
|
|
2488
|
-
}
|
|
2489
|
-
/**
|
|
2490
|
-
* Write to terminal
|
|
2491
|
-
*/
|
|
2492
|
-
writeToTerminal(data) {
|
|
2493
|
-
this.terminal?.write(data);
|
|
2494
|
-
}
|
|
2495
|
-
/**
|
|
2496
|
-
* Resize terminal
|
|
2497
|
-
*/
|
|
2498
|
-
resizeTerminal(cols, rows) {
|
|
2499
|
-
this.terminal?.resize(cols, rows);
|
|
2500
|
-
}
|
|
2501
|
-
/**
|
|
2502
|
-
* Setup WebSocket handlers
|
|
2503
|
-
*/
|
|
2504
|
-
setupWebSocketHandlers() {
|
|
2505
|
-
this.wss.on("connection", (ws, req) => {
|
|
2506
|
-
const clientId = this.generateClientId();
|
|
2507
|
-
const client = {
|
|
2508
|
-
id: clientId,
|
|
2509
|
-
ws,
|
|
2510
|
-
authenticated: false,
|
|
2511
|
-
device: {
|
|
2512
|
-
id: clientId,
|
|
2513
|
-
userAgent: req.headers["user-agent"],
|
|
2514
|
-
ip: req.socket.remoteAddress,
|
|
2515
|
-
connectedAt: /* @__PURE__ */ new Date(),
|
|
2516
|
-
lastActivity: /* @__PURE__ */ new Date()
|
|
2517
|
-
},
|
|
2518
|
-
lastPing: Date.now()
|
|
2519
|
-
};
|
|
2520
|
-
if (this.clients.size >= this.config.maxConnections) {
|
|
2521
|
-
ws.close(1013, "Max connections reached");
|
|
2522
|
-
return;
|
|
2523
|
-
}
|
|
2524
|
-
this.clients.set(clientId, client);
|
|
2525
|
-
ws.send(JSON.stringify({ type: MessageTypes.AUTH_REQUIRED, timestamp: Date.now() }));
|
|
2526
|
-
ws.on("message", (data) => {
|
|
2527
|
-
try {
|
|
2528
|
-
const message = JSON.parse(data.toString());
|
|
2529
|
-
this.handleClientMessage(client, message);
|
|
2530
|
-
} catch {
|
|
2531
|
-
}
|
|
2532
|
-
});
|
|
2533
|
-
ws.on("close", () => {
|
|
2534
|
-
this.clients.delete(clientId);
|
|
2535
|
-
if (this.session && client.authenticated) {
|
|
2536
|
-
this.session.connectedDevices = this.session.connectedDevices.filter(
|
|
2537
|
-
(d) => d.id !== clientId
|
|
2538
|
-
);
|
|
2539
|
-
if (this.session.connectedDevices.length === 0) {
|
|
2540
|
-
this.session.status = "waiting";
|
|
2541
|
-
}
|
|
2542
|
-
this.emit("client:disconnected", client.device);
|
|
2543
|
-
}
|
|
2544
|
-
});
|
|
2545
|
-
ws.on("error", (error) => {
|
|
2546
|
-
this.emit("client:error", clientId, error);
|
|
2547
|
-
});
|
|
2548
|
-
ws.on("pong", () => {
|
|
2549
|
-
client.lastPing = Date.now();
|
|
2550
|
-
});
|
|
2551
|
-
});
|
|
2552
|
-
}
|
|
2553
|
-
/**
|
|
2554
|
-
* Handle client message
|
|
2555
|
-
*/
|
|
2556
|
-
handleClientMessage(client, message) {
|
|
2557
|
-
client.device.lastActivity = /* @__PURE__ */ new Date();
|
|
2558
|
-
if (this.session) {
|
|
2559
|
-
this.session.lastActivity = /* @__PURE__ */ new Date();
|
|
2560
|
-
}
|
|
2561
|
-
if (this.config.sessionTimeout > 0) {
|
|
2562
|
-
this.resetSessionTimeout();
|
|
2563
|
-
}
|
|
2564
|
-
switch (message.type) {
|
|
2565
|
-
case MessageTypes.AUTH:
|
|
2566
|
-
this.handleAuth(client, message.token);
|
|
2567
|
-
break;
|
|
2568
|
-
case MessageTypes.TERMINAL_INPUT:
|
|
2569
|
-
if (client.authenticated && message.data) {
|
|
2570
|
-
this.terminal?.write(message.data);
|
|
2571
|
-
}
|
|
2572
|
-
break;
|
|
2573
|
-
case MessageTypes.TERMINAL_RESIZE:
|
|
2574
|
-
if (client.authenticated && message.cols && message.rows) {
|
|
2575
|
-
this.terminal?.resize(message.cols, message.rows);
|
|
2576
|
-
}
|
|
2577
|
-
break;
|
|
2578
|
-
case MessageTypes.TERMINAL_CLEAR:
|
|
2579
|
-
if (client.authenticated) {
|
|
2580
|
-
this.terminal?.clear();
|
|
2581
|
-
}
|
|
2582
|
-
break;
|
|
2583
|
-
case MessageTypes.PING:
|
|
2584
|
-
client.ws.send(JSON.stringify({ type: MessageTypes.PONG, timestamp: Date.now() }));
|
|
2585
|
-
break;
|
|
2586
|
-
case MessageTypes.COMMAND:
|
|
2587
|
-
if (client.authenticated) {
|
|
2588
|
-
this.emit("command", {
|
|
2589
|
-
clientId: client.id,
|
|
2590
|
-
command: message.command,
|
|
2591
|
-
args: message.args
|
|
2592
|
-
});
|
|
2593
|
-
}
|
|
2594
|
-
break;
|
|
2595
|
-
default:
|
|
2596
|
-
this.emit("message", client, message);
|
|
2597
|
-
}
|
|
2598
|
-
}
|
|
2599
|
-
/**
|
|
2600
|
-
* Handle authentication
|
|
2601
|
-
*/
|
|
2602
|
-
handleAuth(client, token) {
|
|
2603
|
-
if (token === this.sessionSecret) {
|
|
2604
|
-
client.authenticated = true;
|
|
2605
|
-
if (this.session) {
|
|
2606
|
-
this.session.connectedDevices.push(client.device);
|
|
2607
|
-
this.session.status = "connected";
|
|
2608
|
-
}
|
|
2609
|
-
client.ws.send(
|
|
2610
|
-
JSON.stringify({
|
|
2611
|
-
type: MessageTypes.AUTH_SUCCESS,
|
|
2612
|
-
payload: {
|
|
2613
|
-
sessionId: this.session?.id,
|
|
2614
|
-
terminalEnabled: this.config.enableTerminal
|
|
2615
|
-
},
|
|
2616
|
-
timestamp: Date.now()
|
|
2617
|
-
})
|
|
2618
|
-
);
|
|
2619
|
-
if (this.config.enableTerminal && !this.terminal?.isRunning()) {
|
|
2620
|
-
this.terminal?.start();
|
|
2621
|
-
}
|
|
2622
|
-
this.emit("client:connected", client.device);
|
|
2623
|
-
} else {
|
|
2624
|
-
client.ws.send(JSON.stringify({ type: MessageTypes.AUTH_FAILED, timestamp: Date.now() }));
|
|
2625
|
-
setTimeout(() => client.ws.close(1008, "Authentication failed"), 100);
|
|
2626
|
-
}
|
|
2627
|
-
}
|
|
2628
|
-
/**
|
|
2629
|
-
* Handle HTTP request
|
|
2630
|
-
*/
|
|
2631
|
-
handleHttpRequest(req, res) {
|
|
2632
|
-
const url = new URL(req.url || "/", `http://${req.headers.host}`);
|
|
2633
|
-
const path = url.pathname;
|
|
2634
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
2635
|
-
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
2636
|
-
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
2637
|
-
if (req.method === "OPTIONS") {
|
|
2638
|
-
res.writeHead(204);
|
|
2639
|
-
res.end();
|
|
2640
|
-
return;
|
|
2641
|
-
}
|
|
2642
|
-
if (path === "/" || path === "/index.html") {
|
|
2643
|
-
res.writeHead(200, {
|
|
2644
|
-
"Content-Type": "text/html; charset=utf-8",
|
|
2645
|
-
"Content-Security-Policy": "default-src 'self' 'unsafe-inline' 'unsafe-eval' https: data: ws: wss:"
|
|
2646
|
-
});
|
|
2647
|
-
res.end(getWebClient());
|
|
2648
|
-
return;
|
|
2649
|
-
}
|
|
2650
|
-
if (path === "/health") {
|
|
2651
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
2652
|
-
res.end(JSON.stringify({ status: "ok", session: this.session?.id }));
|
|
2653
|
-
return;
|
|
2654
|
-
}
|
|
2655
|
-
if (path === "/api/session") {
|
|
2656
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
2657
|
-
res.end(
|
|
2658
|
-
JSON.stringify({
|
|
2659
|
-
id: this.session?.id,
|
|
2660
|
-
name: this.session?.name,
|
|
2661
|
-
status: this.session?.status,
|
|
2662
|
-
connectedDevices: this.session?.connectedDevices.length
|
|
2663
|
-
})
|
|
2664
|
-
);
|
|
2665
|
-
return;
|
|
2666
|
-
}
|
|
2667
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
2668
|
-
res.end("Not Found");
|
|
2669
|
-
}
|
|
2670
|
-
/**
|
|
2671
|
-
* Start heartbeat
|
|
2672
|
-
*/
|
|
2673
|
-
startHeartbeat() {
|
|
2674
|
-
this.heartbeatTimer = setInterval(() => {
|
|
2675
|
-
const now = Date.now();
|
|
2676
|
-
for (const [id, client] of this.clients) {
|
|
2677
|
-
if (now - client.lastPing > this.config.heartbeatInterval * 2) {
|
|
2678
|
-
client.ws.terminate();
|
|
2679
|
-
this.clients.delete(id);
|
|
2680
|
-
} else if (client.ws.readyState === WebSocket.OPEN) {
|
|
2681
|
-
client.ws.ping();
|
|
2682
|
-
}
|
|
2683
|
-
}
|
|
2684
|
-
}, this.config.heartbeatInterval);
|
|
2685
|
-
}
|
|
2686
|
-
/**
|
|
2687
|
-
* Start session timeout
|
|
2688
|
-
*/
|
|
2689
|
-
startSessionTimeout() {
|
|
2690
|
-
this.sessionTimeoutTimer = setTimeout(() => {
|
|
2691
|
-
if (this.session?.connectedDevices.length === 0) {
|
|
2692
|
-
this.stop();
|
|
2693
|
-
}
|
|
2694
|
-
}, this.config.sessionTimeout);
|
|
2695
|
-
}
|
|
2696
|
-
/**
|
|
2697
|
-
* Reset session timeout
|
|
2698
|
-
*/
|
|
2699
|
-
resetSessionTimeout() {
|
|
2700
|
-
if (this.sessionTimeoutTimer) {
|
|
2701
|
-
clearTimeout(this.sessionTimeoutTimer);
|
|
2702
|
-
}
|
|
2703
|
-
if (this.config.sessionTimeout > 0) {
|
|
2704
|
-
this.startSessionTimeout();
|
|
2705
|
-
}
|
|
2706
|
-
}
|
|
2707
|
-
/**
|
|
2708
|
-
* Get local IP
|
|
2709
|
-
*/
|
|
2710
|
-
getLocalIP() {
|
|
2711
|
-
const interfaces = os.networkInterfaces();
|
|
2712
|
-
for (const name of Object.keys(interfaces)) {
|
|
2713
|
-
for (const iface of interfaces[name] || []) {
|
|
2714
|
-
if (iface.family === "IPv4" && !iface.internal) {
|
|
2715
|
-
return iface.address;
|
|
2716
|
-
}
|
|
2717
|
-
}
|
|
2718
|
-
}
|
|
2719
|
-
return "127.0.0.1";
|
|
2720
|
-
}
|
|
2721
|
-
/**
|
|
2722
|
-
* Generate session ID
|
|
2723
|
-
*/
|
|
2724
|
-
generateSessionId() {
|
|
2725
|
-
return crypto.randomBytes(4).toString("hex");
|
|
2726
|
-
}
|
|
2727
|
-
/**
|
|
2728
|
-
* Generate client ID
|
|
2729
|
-
*/
|
|
2730
|
-
generateClientId() {
|
|
2731
|
-
return "c_" + crypto.randomBytes(4).toString("hex");
|
|
2732
|
-
}
|
|
2733
|
-
/**
|
|
2734
|
-
* Generate secret
|
|
2735
|
-
*/
|
|
2736
|
-
generateSecret() {
|
|
2737
|
-
return crypto.randomBytes(16).toString("hex");
|
|
2738
|
-
}
|
|
2739
|
-
};
|
|
2740
|
-
|
|
2741
|
-
export {
|
|
2742
|
-
DEFAULT_CONFIG,
|
|
2743
|
-
MessageTypes,
|
|
2744
|
-
TerminalManager,
|
|
2745
|
-
getWebClient,
|
|
2746
|
-
RemoteServer
|
|
2747
|
-
};
|