mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-12 22:33:55 +00:00
Fixed connection handling
This commit is contained in:
@@ -23,83 +23,101 @@ var duplex = require('stream').Duplex;
|
||||
function vt()
|
||||
{
|
||||
this._ObjectID = 'win-virtual-terminal';
|
||||
this._GM = require('_GenericMarshal');
|
||||
this._kernel32 = this._GM.CreateNativeProxy('kernel32.dll');
|
||||
try
|
||||
{
|
||||
this._kernel32.CreateMethod('CreatePipe');
|
||||
this._kernel32.CreateMethod('CreateProcessW');
|
||||
this._kernel32.CreateMethod('CreatePseudoConsole');
|
||||
this._kernel32.CreateMethod('GetProcessHeap');
|
||||
this._kernel32.CreateMethod('HeapAlloc');
|
||||
this._kernel32.CreateMethod('InitializeProcThreadAttributeList');
|
||||
this._kernel32.CreateMethod('UpdateProcThreadAttribute');
|
||||
this._kernel32.CreateMethod('WriteFile');
|
||||
this._kernel32.CreateMethod('ReadFile');
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
|
||||
Object.defineProperty(this, 'supported', { value: this._kernel32.CreatePseudoConsole != null });
|
||||
Object.defineProperty(this, 'supported', {
|
||||
value: (function ()
|
||||
{
|
||||
var gm = require('_GenericMarshal');
|
||||
var k32 = gm.CreateNativeProxy('kernel32.dll');
|
||||
try
|
||||
{
|
||||
k32.CreateMethod('CreatePseudoConsole');
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
return (false);
|
||||
}
|
||||
return (true);
|
||||
})()
|
||||
});
|
||||
this.Create = function Create(path, width, height)
|
||||
{
|
||||
if (!this.supported) { throw ('This build of Windows does not have support for PseudoConsoles'); }
|
||||
if (!width) { width = 80; }
|
||||
if (!height) { height = 25; }
|
||||
var ret = { _h: this._GM.CreatePointer(), _consoleInput: this._GM.CreatePointer(), _consoleOutput: this._GM.CreatePointer(), _input: this._GM.CreatePointer(), _output: this._GM.CreatePointer(), vt: this };
|
||||
var attrSize = this._GM.CreateVariable(8);
|
||||
|
||||
var GM = require('_GenericMarshal');
|
||||
var k32 = GM.CreateNativeProxy('kernel32.dll');
|
||||
k32.CreateMethod('CreatePipe');
|
||||
k32.CreateMethod('CreateProcessW');
|
||||
k32.CreateMethod('CreatePseudoConsole');
|
||||
k32.CreateMethod('GetProcessHeap');
|
||||
k32.CreateMethod('HeapAlloc');
|
||||
k32.CreateMethod('InitializeProcThreadAttributeList');
|
||||
k32.CreateMethod('UpdateProcThreadAttribute');
|
||||
k32.CreateMethod('WriteFile');
|
||||
k32.CreateMethod('ReadFile');
|
||||
k32.CreateMethod('TerminateProcess');
|
||||
|
||||
var ret = { _h: GM.CreatePointer(), _consoleInput: GM.CreatePointer(), _consoleOutput: GM.CreatePointer(), _input: GM.CreatePointer(), _output: GM.CreatePointer(), k32: k32 };
|
||||
var attrSize = GM.CreateVariable(8);
|
||||
var attrList;
|
||||
var pi = this._GM.CreateVariable(this._GM.PointerSize == 4 ? 16 : 24);
|
||||
var pi = GM.CreateVariable(GM.PointerSize == 4 ? 16 : 24);
|
||||
|
||||
// Create the necessary pipes
|
||||
if (this._kernel32.CreatePipe(ret._consoleInput, ret._input, 0, 0).Val == 0) { console.log('PIPE/FAIL'); }
|
||||
if (this._kernel32.CreatePipe(ret._output, ret._consoleOutput, 0, 0).Val == 0) { console.log('PIPE/FAIL'); }
|
||||
if (k32.CreatePipe(ret._consoleInput, ret._input, 0, 0).Val == 0) { console.log('PIPE/FAIL'); }
|
||||
if (k32.CreatePipe(ret._output, ret._consoleOutput, 0, 0).Val == 0) { console.log('PIPE/FAIL'); }
|
||||
|
||||
|
||||
if (this._kernel32.CreatePseudoConsole((height << 16) | width, ret._consoleInput.Deref(), ret._consoleOutput.Deref(), 0, ret._h).Val != 0)
|
||||
if (k32.CreatePseudoConsole((height << 16) | width, ret._consoleInput.Deref(), ret._consoleOutput.Deref(), 0, ret._h).Val != 0)
|
||||
{
|
||||
console.log('CreatePseudoConsole Error');
|
||||
throw ('Error calling CreatePseudoConsole()');
|
||||
}
|
||||
|
||||
this._kernel32.InitializeProcThreadAttributeList(0, 1, 0, attrSize);
|
||||
attrList = this._GM.CreateVariable(attrSize.toBuffer().readUInt32LE());
|
||||
var startupinfoex = this._GM.CreateVariable(this._GM.PointerSize == 8 ? 112 : 72);
|
||||
startupinfoex.toBuffer().writeUInt32LE(this._GM.PointerSize == 8 ? 112 : 72, 0);
|
||||
attrList.pointerBuffer().copy(startupinfoex.Deref(this._GM.PointerSize == 8 ? 104 : 68, this._GM.PointerSize).toBuffer());
|
||||
k32.InitializeProcThreadAttributeList(0, 1, 0, attrSize);
|
||||
attrList = GM.CreateVariable(attrSize.toBuffer().readUInt32LE());
|
||||
var startupinfoex = GM.CreateVariable(GM.PointerSize == 8 ? 112 : 72);
|
||||
startupinfoex.toBuffer().writeUInt32LE(GM.PointerSize == 8 ? 112 : 72, 0);
|
||||
attrList.pointerBuffer().copy(startupinfoex.Deref(GM.PointerSize == 8 ? 104 : 68, GM.PointerSize).toBuffer());
|
||||
|
||||
if(this._kernel32.InitializeProcThreadAttributeList(attrList, 1, 0, attrSize).Val != 0)
|
||||
if (k32.InitializeProcThreadAttributeList(attrList, 1, 0, attrSize).Val != 0)
|
||||
{
|
||||
if (this._kernel32.UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, ret._h.Deref(), this._GM.PointerSize, 0, 0).Val != 0)
|
||||
if (k32.UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, ret._h.Deref(), GM.PointerSize, 0, 0).Val != 0)
|
||||
{
|
||||
if(this._kernel32.CreateProcessW(0, this._GM.CreateVariable(path, { wide: true }), 0, 0, 1, EXTENDED_STARTUPINFO_PRESENT, 0, 0, startupinfoex, pi).Val != 0)
|
||||
if (k32.CreateProcessW(0, GM.CreateVariable(path, { wide: true }), 0, 0, 1, EXTENDED_STARTUPINFO_PRESENT, 0, 0, startupinfoex, pi).Val != 0)
|
||||
{
|
||||
ret._startupinfoex = startupinfoex;
|
||||
ret._pid = pi.Deref(this._GM.PointerSize == 4 ? 8 : 16, 4).toBuffer().readUInt32LE();
|
||||
|
||||
ret._process = pi.Deref(0);
|
||||
ret._pid = pi.Deref(GM.PointerSize == 4 ? 8 : 16, 4).toBuffer().readUInt32LE();
|
||||
var ds = new duplex(
|
||||
{
|
||||
'write': function (chunk, flush)
|
||||
{
|
||||
var written = this.terminal.vt._GM.CreateVariable(4);
|
||||
this.terminal.vt._kernel32.WriteFile(this.terminal._input.Deref(), this.terminal.vt._GM.CreateVariable(chunk), chunk.length, written, 0);
|
||||
var written = require('_GenericMarshal').CreateVariable(4);
|
||||
this.terminal.k32.WriteFile(this.terminal._input.Deref(), require('_GenericMarshal').CreateVariable(chunk), chunk.length, written, 0);
|
||||
flush();
|
||||
return (true);
|
||||
},
|
||||
'final': function (flush)
|
||||
{
|
||||
if (this.terminal._process)
|
||||
{
|
||||
this.terminal.k32.TerminateProcess(this.terminal._process, 0);
|
||||
this.terminal._process = null;
|
||||
this.terminal.k32.ReadFile.async.abort();
|
||||
}
|
||||
flush();
|
||||
}
|
||||
});
|
||||
ds.terminal = ret;
|
||||
ds._kernel32 = this._kernel32;
|
||||
ds._rpbuf = this._GM.CreateVariable(4096);
|
||||
ds._rpbufRead = this._GM.CreateVariable(4);
|
||||
ret._waiter = require('DescriptorEvents').addDescriptor(pi.Deref(0));
|
||||
ret._waiter.ds = ds;
|
||||
ret._waiter.on('signaled', function () { this.ds.push(null); });
|
||||
|
||||
ds.terminal = ret;
|
||||
ds._rpbuf = GM.CreateVariable(4096);
|
||||
ds._rpbufRead = GM.CreateVariable(4);
|
||||
ds._read = function _read()
|
||||
{
|
||||
this._rp = this._kernel32.ReadFile.async(this.terminal._output.Deref(), this._rpbuf, this._rpbuf._size, this._rpbufRead, 0);
|
||||
this._rp = this.terminal.k32.ReadFile.async(this.terminal._output.Deref(), this._rpbuf, this._rpbuf._size, this._rpbufRead, 0);
|
||||
this._rp.then(function ()
|
||||
{
|
||||
var len = this.parent._rpbufRead.toBuffer().readUInt32LE();
|
||||
|
||||
Reference in New Issue
Block a user