1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-11 05:43:33 +00:00

Updated windows system tray support

This commit is contained in:
Bryan Roe
2020-07-30 18:38:27 -07:00
parent 8546a1333b
commit 2e1d102311
3 changed files with 135 additions and 46 deletions

File diff suppressed because one or more lines are too long

View File

@@ -73,7 +73,7 @@ function dispatch(options)
s.write(h); s.write(h);
s.write(d); s.write(d);
} }
d = Buffer.from(JSON.stringify({ command: 'launch', value: { module: this.parent.options.launch.module, method: this.parent.options.launch.method, args: this.parent.options.launch.args } })); d = Buffer.from(JSON.stringify({ command: 'launch', value: { split: this.parent.options.launch.split?true:false, module: this.parent.options.launch.module, method: this.parent.options.launch.method, args: this.parent.options.launch.args } }));
h.writeUInt32LE(d.length + 4); h.writeUInt32LE(d.length + 4);
s.write(h); s.write(h);
s.write(d); s.write(d);
@@ -156,10 +156,18 @@ function connect(ipc)
case 'launch': case 'launch':
var obj = require(cmd.value.module); var obj = require(cmd.value.module);
global._proxyStream = obj[cmd.value.method].apply(obj, cmd.value.args); global._proxyStream = obj[cmd.value.method].apply(obj, cmd.value.args);
global._proxyStream.pipe(this, { end: false }); if (cmd.value.split)
this.pipe(global._proxyStream, { end: false }); {
global._proxyStream.out.pipe(this, { end: false });
global._proxyStream.on('end', function () { process.exit(); }); this.pipe(global._proxyStream.in, { end: false });
global._proxyStream.out.on('end', function () { process.exit(); });
}
else
{
global._proxyStream.pipe(this, { end: false });
this.pipe(global._proxyStream, { end: false });
global._proxyStream.on('end', function () { process.exit(); });
}
this.on('end', function () { process.exit(); }); this.on('end', function () { process.exit(); });
break; break;
} }

View File

@@ -21,11 +21,57 @@ var WM_CLOSE = 0x0010;
function postMessage(tid) function postMessage(tid)
{ {
console.log('Post WM_QUIT to: ' + tid);
var gm = require('_GenericMarshal'); var gm = require('_GenericMarshal');
var user = gm.CreateNativeProxy('User32.dll'); var user = gm.CreateNativeProxy('User32.dll');
user.CreateMethod('PostThreadMessageA'); user.CreateMethod('PostThreadMessageA');
user.PostThreadMessageA(tid, WM_QUIT, 0, 0); user.PostThreadMessageA(tid, WM_QUIT);
}
function localsystem_createTrayIcon(trayOptions)
{
var retVal = new promise(function (res, rej) { this._res = res; this._rej = rej; });
if (trayOptions.user == null)
{
trayOptions.user = require('user-sessions').getUsername(require('user-sessions').consoleUid());
}
trayOptions.split = true;
retVal.trayOptions = trayOptions;
retVal._dispatcher = require('win-dispatcher').dispatch({ user: trayOptions.user, modules: [], launch: { module: 'win-systray', method: 'createTrayIcon', args: [trayOptions], split: true } });
retVal._dispatcher.ret = retVal;
retVal._dispatcher.on('connection', function (connection)
{
this.ret.connection = connection;
this.ret.connection.ret = this.ret;
this.ret.connection.on('data', function (c)
{
var val = c.toString();
if (val.includes('<<TID:'))
{
var t = val.split('<<TID:').pop().split('>>').shift();
this.ret.tid = parseInt(t);
}
else if (val.includes('<<menuitem:'))
{
var i = parseInt(val.split('<<menuitem:').pop().split('>>').shift());
if (this.ret.trayOptions.menuItems[i].func != null)
{
this.ret.trayOptions.menuItems[i].func.call(this.ret.trayOptions);
}
}
});
this.ret._cleanup = function _cleanup() { _cleanup.ret._dispatcher.invoke('remove', [_cleanup.ret.tid]); };
this.ret._cleanup.ret = this.ret;
process.on('exit', this.ret._cleanup);
});
retVal.remove = function ()
{
if (this._cleanup != null)
{
this._cleanup();
process.removeListener('exit', this._cleanup);
}
};
return (retVal);
} }
function createTrayIcon(trayOptions) function createTrayIcon(trayOptions)
@@ -48,52 +94,65 @@ function createTrayIcon(trayOptions)
{ {
options.env[c1e] = process.env[c1e]; options.env[c1e] = process.env[c1e];
} }
try
if (!trayOptions.split)
{ {
options.uid = trayOptions.tsid == null ? require('user-sessions').consoleUid() : trayOptions.tsid; try
if (options.uid == (cid = require('user-sessions').getProcessOwnerName(process.pid).tsid))
{ {
delete options.uid; options.uid = trayOptions.tsid == null ? require('user-sessions').consoleUid() : trayOptions.tsid;
} if (options.uid == (cid = require('user-sessions').getProcessOwnerName(process.pid).tsid))
else
{
if (trayOptions.tsid != null && cid != 0)
{ {
retVal._rej('Insufficient permission to set tray icon as uid: ' + trayOptions.tsid); delete options.uid;
return (retVal); }
else
{
if (trayOptions.tsid != null && cid != 0)
{
retVal._rej('Insufficient permission to set tray icon as uid: ' + trayOptions.tsid);
return (retVal);
}
retVal.options.type = require('child_process').SpawnTypes.USER;
} }
retVal.options.type = require('child_process').SpawnTypes.USER;
} }
} catch (ee)
catch (ee) {
{ retVal._rej('Cannot set tray icon when a user is not logged in');
retVal._rej('Cannot set tray icon when a user is not logged in'); return (retVal);
return (retVal); }
} }
retVal.child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], retVal.options); retVal.child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], retVal.options);
retVal.child.ret = retVal; retVal.child.ret = retVal;
retVal.child.on('exit', function () { this.ret._res(); }); retVal.child.on('exit', function () { this.ret._res(); });
retVal.child.descriptorMetadata = 'win-systray'; retVal.child.descriptorMetadata = 'win-systray';
retVal.child.stdout.on('data', function (c)
{
var val = c.toString();
if(val.includes('<<TID:'))
{
var t = val.split('<<TID:').pop().split('>>').shift();
this.parent.ret.tid = parseInt(t);
}
else if (val.includes('<<menuitem:'))
{
var i = parseInt(val.split('<<menuitem:').pop().split('>>').shift());
if (this.parent.ret.trayOptions.menuItems[i].func != null)
{
this.parent.ret.trayOptions.menuItems[i].func.call(this.parent.ret.trayOptions);
}
}
});
retVal.child.stderr.on('data', function (c) { });
if (!trayOptions.split)
{
retVal.child.stdout.on('data', function (c)
{
var val = c.toString();
if (val.includes('<<TID:'))
{
var t = val.split('<<TID:').pop().split('>>').shift();
this.parent.ret.tid = parseInt(t);
}
else if (val.includes('<<menuitem:'))
{
var i = parseInt(val.split('<<menuitem:').pop().split('>>').shift());
if (this.parent.ret.trayOptions.menuItems[i].func != null)
{
this.parent.ret.trayOptions.menuItems[i].func.call(this.parent.ret.trayOptions);
}
}
});
}
else
{
retVal.out = retVal.child.stdout;
retVal.in = retVal.child.stdin;
}
retVal.child.stderr.on('data', function (c) { });
retVal.child.stdin.write('$signature_gctid = $env:_tidsig\r\n'); retVal.child.stdin.write('$signature_gctid = $env:_tidsig\r\n');
retVal.child.stdin.write('Add-Type -MemberDefinition $signature_gctid -Name MyName -Namespace MyNameSpace -PassThru\r\n'); retVal.child.stdin.write('Add-Type -MemberDefinition $signature_gctid -Name MyName -Namespace MyNameSpace -PassThru\r\n');
retVal.child.stdin.write('$tid = [MyNameSpace.MyName]::GetCurrentThreadId();\r\n'); retVal.child.stdin.write('$tid = [MyNameSpace.MyName]::GetCurrentThreadId();\r\n');
@@ -129,15 +188,34 @@ function createTrayIcon(trayOptions)
retVal.child.stdin.write('[void][System.Windows.Forms.Application]::Run($appContext)\r\n'); retVal.child.stdin.write('[void][System.Windows.Forms.Application]::Run($appContext)\r\n');
retVal.child.stdin.write('$Main_Tool_Icon.dispose();\r\n'); retVal.child.stdin.write('$Main_Tool_Icon.dispose();\r\n');
retVal.child.stdin.write('exit\r\n'); retVal.child.stdin.write('exit\r\n');
retVal._cleanup = function _cleanup() { postMessage(_cleanup.self.tid); }; retVal._cleanup = function _cleanup(tid)
{
if (process.exitting)
{
postMessage(_cleanup.self.tid);
}
else
{
postMessage(tid ? tid : _cleanup.self.tid);
}
};
retVal._cleanup.self = retVal; retVal._cleanup.self = retVal;
process.on('exit', retVal._cleanup); process.on('exit', retVal._cleanup);
retVal.remove = function () retVal.remove = function (tid)
{ {
this._cleanup(); this._cleanup(tid);
process.removeListener('exit', this._cleanup); process.removeListener('exit', this._cleanup);
}; };
return (retVal); return (retVal);
} }
module.exports = { createTrayIcon: process.platform == 'win32' ? createTrayIcon : function () { throw (process.platform + ' not supported') } }; if (require('user-sessions').getProcessOwnerName(process.pid).tsid == 0)
{
module.exports = { createTrayIcon: process.platform == 'win32' ? localsystem_createTrayIcon : function () { throw (process.platform + ' not supported') } };
}
else
{
module.exports = { createTrayIcon: process.platform == 'win32' ? createTrayIcon : function () { throw (process.platform + ' not supported') } };
}