From 975c4c60294e23cd0d4d6741320fd6afa22f46d4 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Tue, 26 Nov 2019 10:21:53 -0800 Subject: [PATCH] 1. Added proxy-helper to assist with exception checking 2. Added win-virtual-terminal, to support MSFT's new ConPTY API. --- modules/proxy-helper.js | 114 ++++++++++++++++++++++++++++ modules/win-virtual-terminal.js | 128 ++++++++++++++++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 modules/proxy-helper.js create mode 100644 modules/win-virtual-terminal.js diff --git a/modules/proxy-helper.js b/modules/proxy-helper.js new file mode 100644 index 0000000..1b6b9c5 --- /dev/null +++ b/modules/proxy-helper.js @@ -0,0 +1,114 @@ +/* +Copyright 2019 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +function posix_proxyCheck(uid, checkAddr) +{ + var g; + var x = process.env['no_proxy'] ? process.env['no_proxy'].split(',') : []; + var t; + + if (require('linux-gnome-helpers').available && (g = require('linux-gnome-helpers').getProxySettings(uid)).mode != 'none') + { + x = g.exceptions; + } + + for(var i in x) + { + if (x[i] == checkAddr) { return (true); } // Direct Match + if (checkAddr.endsWith('.' + x[i])) { return (true); } // Subdomain Match + if ((v = x[i].split('/')).length == 2) + { + try + { + if(require('ip-address').Address4.fromString(v[0]).mask(parseInt(v[1])) == require('ip-address').Address4.fromString(checkAddr).mask(parseInt(v[1]))) + { + return(true); + } + } + catch (ex) + { + } + } + } + return (false); +} + +function windows_proxyCheck(key, checkAddr) +{ + if(!key) + { + var i; + // Key wasn't specified, so lets try to figure it out + if((i=require('user-sessions').getProcessOwnerName(process.pid)).tsid == 0) + { + // We are a service, so we should check the user that installed the Mesh Agent + try + { + key = require('win-registry').QueryKey(require('win-registry').HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\Mesh Agent', '_InstalledBy'); + } + catch(xx) + { + // This info isn't available, so let's try to use the currently logged in user + try + { + key = require('win-registry').usernameToUserKey(require('user-sessions').getUsername(require('user-sessions').consoleUid())); + } + catch(xxx) + { + // No users are logged in, so as a last resort, let's try the last logged in user. + var entries = require('win-registry').QueryKey(require('win-registry').HKEY.Users); + for(i in entries.subkeys) + { + if(entries.subkeys[i].split('-').length>5 && !entries.subkeys[i].endsWith('_Classes')) + { + key = entries.subkeys[i]; + break; + } + } + } + } + } + else + { + // We are a logged in user + key = require('win-registry').usernameToUserKey(i.name); + } + if(!key) {throw('Could not determine which user proxy setting to query');} + } + var proxyOverride = require('win-registry').QueryKey(require('win-registry').HKEY.Users, key + '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings', 'ProxyOverride').split(';'); + for(var i in proxyOverride) + { + proxyOverride[i] = proxyOverride[i].trim(); + if ((checkAddr == '127.0.0.1' || checkAddr == '::1') && proxyOverride[i] == '') { return (true); } + if (checkAddr == proxyOverride[i]) { return (true); } // Exact Match + if (proxyOverride[i].startsWith('*.') && checkAddr.endsWith(proxyOverride[i].substring(1))) { return (true); } + if (proxyOverride[i].endsWith('.*') && checkAddr.startsWith(proxyOverride[i].substring(0, proxyOverride[i].length - 1))) { return (true); } + } + return (false); +} + +switch (process.platform) +{ + case 'linux': + case 'freebsd': + module.exports = { ignoreProxy: posix_proxyCheck }; + break; + case 'win32': + module.exports = { ignoreProxy: windows_proxyCheck }; + break; + case 'darwin': + break; +} diff --git a/modules/win-virtual-terminal.js b/modules/win-virtual-terminal.js new file mode 100644 index 0000000..c0d8549 --- /dev/null +++ b/modules/win-virtual-terminal.js @@ -0,0 +1,128 @@ +/* +Copyright 2019 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = 0x00020016; +const EXTENDED_STARTUPINFO_PRESENT = 0x00080000; +const HEAP_ZERO_MEMORY = 0x00000008; + +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 }); + 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 attrList; + var pi = this._GM.CreateVariable(this._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 (this._kernel32.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()); + + if(this._kernel32.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(this._kernel32.CreateProcessW(0, this._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(); + + 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); + flush(); + return (true); + }, + 'final': function (flush) + { + flush(); + } + }); + ds.terminal = ret; + ds._kernel32 = this._kernel32; + ds._rpbuf = this._GM.CreateVariable(4096); + ds._rpbufRead = this._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.then(function () + { + var len = this.parent._rpbufRead.toBuffer().readUInt32LE(); + this.parent.push(this.parent._rpbuf.toBuffer().slice(0, len)); + this.parent._read(); + }); + this._rp.parent = this; + }; + ds._read(); + return (ds); + } + else + { + console.log('FAILED!'); + } + } + + } + throw ('Internal Error'); + } +} + +if (process.platform == 'win32') +{ + module.exports = new vt(); +} \ No newline at end of file