1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-15 15:53:55 +00:00

1. Added 'getProcessOwnerName()' for windows in user-sessions

2. Added notifybar-desktop for Windows
This commit is contained in:
Bryan Roe
2019-10-03 14:34:17 -07:00
parent 6210840201
commit 1774dc2e54
3 changed files with 220 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,147 @@
/*
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.
*/
var ptrsize = require('_GenericMarshal').PointerSize;
function windows_notifybar_check(title)
{
if(require('user-sessions').getProcessOwnerName(process.pid).tsid == 0)
{
return (windows_notifybar_system(title));
}
else
{
return (windows_notifybar_local(title));
}
}
function windows_notifybar_system(title)
{
var ret = {};
var script = Buffer.from("require('notifybar-desktop')('" + title + "').on('close', function(){process.exit();});").toString('base64');
require('events').EventEmitter.call(ret, true).createEvent('close');
console.log('switching');
ret.child = require('child_process').execFile(process.execPath, [process.execPath.split('\\').pop(), '-b64exec', script], { type: 1 });
ret.child.parent = ret;
ret.child.stdout.on('data', function (c) { });
ret.child.stderr.on('data', function (c) { });
ret.child.on('exit', function (code) { this.parent.emit('close', code); });
return (ret);
}
function windows_notifybar_local(title)
{
var MessagePump;
var ret;
MessagePump = require('win-message-pump');
ret = { _ObjectID: 'notifybar-desktop.Windows', title: title, _pumps: [], _promise: require('monitor-info').getInfo() };
ret._promise.notifybar = ret;
require('events').EventEmitter.call(ret, true)
.createEvent('close');
ret._promise.then(function (m)
{
var offset;
var barWidth, monWidth, offset, barHeight, monHeight;
for (var i in m)
{
//console.log('Monitor: ' + i + ' = Width[' + (m[i].right - m[i].left) + ']');
monWidth = (m[i].right - m[i].left);
monHeight = (m[i].bottom - m[i].top);
barWidth = Math.floor(monWidth * 0.30);
barHeight = Math.floor(monHeight * 0.035);
offset = Math.floor(monWidth * 0.50) - Math.floor(barWidth * 0.50);
start = m[i].left + offset;
//console.log(' ' + start + ', ' + barWidth + ', ' + barHeight);
var options =
{
window:
{
winstyles: MessagePump.WindowStyles.WS_VISIBLE | MessagePump.WindowStyles.WS_BORDER | MessagePump.WindowStyles.WS_CAPTION | MessagePump.WindowStyles.WS_SYSMENU,
x: start, width: barWidth, height: barHeight, title: this.notifybar.title
}
};
this.notifybar._pumps.push(new MessagePump(options));
this.notifybar._pumps.peek().notifybar = this.notifybar;
this.notifybar._pumps.peek().on('hwnd', function (h)
{
this._HANDLE = h;
});
this.notifybar._pumps.peek().on('exit', function (h)
{
for(var i in this.notifybar._pumps)
{
this.notifybar._pumps[i].removeAllListeners('exit');
this.notifybar._pumps[i].close();
}
this.notifybar.emit('close');
});
this.notifybar._pumps.peek().on('message', function onWindowsMessage(msg)
{
if (msg.message == 133)
{
//console.log("WM_NCPAINT");
}
if (msg.message == 70) // We are intercepting WM_WINDOWPOSCHANGING to DISABLE moving the window
{
if (this._HANDLE)
{
var flags = 0;
switch (ptrsize)
{
case 4:
flags = msg.lparam_raw.Deref(24, 4).toBuffer().readUInt32LE() | 0x0002; // Set SWP_NOMOVE
msg.lparam_raw.Deref(24, 4).toBuffer().writeUInt32LE(flags);
break;
case 8:
flags = msg.lparam_raw.Deref(32, 4).toBuffer().readUInt32LE() | 0x0002 // Set SWP_NOMOVE
msg.lparam_raw.Deref(32, 4).toBuffer().writeUInt32LE(flags);
break;
}
}
}
});
}
});
return (ret);
}
switch(process.platform)
{
case 'win32':
module.exports = windows_notifybar_check;
break;
case 'linux':
case 'freebsd':
break;
}

View File

@@ -23,6 +23,13 @@ var PBT_APMSUSPEND = 0x4;
var PBT_APMRESUMESUSPEND = 0x7;
var PBT_APMRESUMEAUTOMATIC = 0x12;
var PBT_APMPOWERSTATUSCHANGE = 0xA;
var PROCESS_QUERY_INFORMATION = 0x0400;
var TOKEN_QUERY = 0x0008;
var TokenUser = 1;
var TokenType = 8;
var TokenSessionId = 12;
var ERROR_INSUFFICIENT_BUFFER = 122;
var HEAP_ZERO_MEMORY = 0x00000008;
var WTS_CONSOLE_CONNECT = (0x1);
var WTS_CONSOLE_DISCONNECT = (0x2);
@@ -91,7 +98,8 @@ function UserSessions()
this._kernel32 = this._marshal.CreateNativeProxy('Kernel32.dll');
this._kernel32.CreateMethod('GetLastError');
this._kernel32.CreateMethod('WTSGetActiveConsoleSessionId')
this._kernel32.CreateMethod('CloseHandle');
try
{
this._wts = this._marshal.CreateNativeProxy('Wtsapi32.dll');
@@ -129,6 +137,19 @@ function UserSessions()
}
this._rpcrt.StringToUUID.us = this;
try
{
this._kernel32.CreateMethod('OpenProcess')
this._advapi.CreateMethod('OpenProcessToken');
this._advapi.CreateMethod('GetTokenInformation');
this._advapi.CreateMethod('LookupAccountSidW');
this._advapi.CreateMethod('OpenThreadToken');
}
catch(e)
{
}
GUID_ACDC_POWER_SOURCE = this._rpcrt.StringToUUID('5d3e9a59-e9D5-4b00-a6bd-ff34ff516548');
GUID_BATTERY_PERCENTAGE_REMAINING = this._rpcrt.StringToUUID('a7ad8041-b45a-4cae-87a3-eecbb468a9e1');
GUID_CONSOLE_DISPLAY_STATE = this._rpcrt.StringToUUID('6fe69556-704a-47a0-8f24-c28d936fda47');
@@ -187,6 +208,46 @@ function UserSessions()
}
return admin;
}
this.getProcessOwnerName = function getProcessOwnerName(pid)
{
var ret = null;
var name = this._marshal.CreateVariable(1024);
var domain = this._marshal.CreateVariable(1024);
var nameDomainLength = this._marshal.CreateVariable(4); nameDomainLength.toBuffer().writeUInt32LE(1024);
var bufferLength = this._marshal.CreateVariable(4);
var sidtype = this._marshal.CreateVariable(4);
var tokenuser = 0;
var token = this._marshal.CreatePointer();
var h = this._kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, 1, pid);
if (h.Val == 0) { throw ('Failed to query process id: ' + pid); }
if(this._advapi.OpenProcessToken(h, TOKEN_QUERY, token).Val==0)
{
this._kernel32.CloseHandle(h);
throw ('Failed to Query Process Token for pid: ' + pid);
}
var tsid = this._marshal.CreateVariable(4);
this._advapi.GetTokenInformation(token.Deref(), TokenSessionId, tsid, 4, bufferLength);
this._advapi.GetTokenInformation(token.Deref(), TokenUser, tokenuser, 0, bufferLength);
tokenuser = this._marshal.CreateVariable(bufferLength.toBuffer().readUInt32LE());
if (this._advapi.GetTokenInformation(token.Deref(), TokenUser, tokenuser, bufferLength.toBuffer().readUInt32LE(), bufferLength).Val == 0) { throw ('Internal Error'); }
if(this._advapi.LookupAccountSidW(0, tokenuser.Deref(), name, nameDomainLength, domain, nameDomainLength, sidtype).Val == 0)
{
throw ('Lookup Error');
}
else
{
name._size = 0; domain._size = 0;
ret = { name: name.Wide2UTF8, domain: domain.Wide2UTF8, tsid: tsid.toBuffer().readUInt32LE() };
}
this._kernel32.CloseHandle(token.Deref());
this._kernel32.CloseHandle(h);
return (ret);
};
this.getSessionAttribute = function getSessionAttribute(sessionId, attr)
{