1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-15 07:43:50 +00:00

1. Added 'global' polyfill

2. Updated service-manager in Native and JS, to add '_installedBy' on Windows
This commit is contained in:
Bryan Roe
2019-11-20 14:41:22 -08:00
parent e68d0cc7c5
commit bca47688ff
4 changed files with 186 additions and 128 deletions

View File

@@ -108,6 +108,27 @@ BOOL RunAsAdmin(char* args) {
return FALSE; return FALSE;
} }
void UpdateOwnerData()
{
WCHAR str[_MAX_PATH];
DWORD strLen;
strLen = GetModuleFileNameW(NULL, str, _MAX_PATH);
int exePathLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)str, -1, NULL, 0, NULL, NULL);
char *exePath = (char*)ILibMemory_SmartAllocate(exePathLen);
WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)str, -1, exePath, exePathLen, NULL, NULL);
void *chain = ILibCreateChain();
duk_context *ctx = ILibDuktape_ScriptContainer_InitializeJavaScriptEngineEx(0, 0, chain, NULL, NULL, exePath, NULL, NULL, NULL);
duk_peval_string_noresult(ctx, "global._noMessagePump=true;var key=require('win-registry').usernameToUserKey(require('user-sessions').getProcessOwnerName(process.pid).name);var reg=require('win-registry');reg.WriteKey(reg.HKEY.LocalMachine, 'SYSTEM\\\\CurrentControlSet\\\\Services\\\\Mesh Agent', '_InstalledBy', key);");
duk_destroy_heap(ctx);
ILibChain_DestroyEx(chain);
ILibMemory_Free(exePath);
}
DWORD WINAPI ServiceControlHandler( DWORD controlCode, DWORD eventType, void *eventData, void* eventContext ) DWORD WINAPI ServiceControlHandler( DWORD controlCode, DWORD eventType, void *eventData, void* eventContext )
{ {
switch (controlCode) switch (controlCode)
@@ -874,6 +895,7 @@ void fullinstall(int uninstallonly, char* proxy, int proxylen, char* tag, int ta
// Add the uninstall icon in the control panel // Add the uninstall icon in the control panel
AddUninstallIcon(); AddUninstallIcon();
UpdateOwnerData();
/* /*
#if defined(_LINKVM) #if defined(_LINKVM)

File diff suppressed because one or more lines are too long

View File

@@ -618,6 +618,21 @@ function serviceManager()
} }
} }
}); });
Object.defineProperty(retVal, 'installedBy',
{
get: function()
{
var reg = require('win-registry');
try
{
return(reg.QueryKey(reg.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + this.name, '_InstalledBy'));
}
catch(xx)
{
return (null);
}
}
});
if (retVal.status.state != 'UNKNOWN') if (retVal.status.state != 'UNKNOWN')
{ {
require('events').EventEmitter.call(retVal); require('events').EventEmitter.call(retVal);
@@ -1532,6 +1547,7 @@ function serviceManager()
if (process.platform == 'win32') if (process.platform == 'win32')
{ {
var reg = require('win-registry');
if (!this.isAdmin()) { throw ('Installing as Service, requires admin'); } if (!this.isAdmin()) { throw ('Installing as Service, requires admin'); }
// Before we start, we need to copy the binary to the right place // Before we start, we need to copy the binary to the right place
@@ -1616,13 +1632,24 @@ function serviceManager()
} }
} }
} }
if (options.parameters) if (options.parameters)
{ {
var reg = require('win-registry');
var imagePath = reg.QueryKey(reg.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + options.name, 'ImagePath'); var imagePath = reg.QueryKey(reg.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + options.name, 'ImagePath');
imagePath += (' ' + options.parameters.join(' ')); imagePath += (' ' + options.parameters.join(' '));
reg.WriteKey(reg.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + options.name, 'ImagePath', imagePath); reg.WriteKey(reg.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + options.name, 'ImagePath', imagePath);
} }
try
{
reg.WriteKey(reg.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + options.name, '_InstalledBy', reg.usernameToUserKey(require('user-sessions').getProcessOwnerName(process.pid).name));
}
catch (xx)
{
}
} }
if (process.platform == 'freebsd') if (process.platform == 'freebsd')
{ {

View File

@@ -320,115 +320,117 @@ function UserSessions()
return (retVal); return (retVal);
}; };
if (!global._noMessagePump)
// We need to spin up a message pump, and fetch a window handle
var message_pump = require('win-message-pump');
this._messagepump = new message_pump({ filter: WM_WTSSESSION_CHANGE }); this._messagepump.parent = this;
this._messagepump.on('exit', function (code) { this.parent._wts.WTSUnRegisterSessionNotification(this.parent.hwnd); });
this._messagepump.on('hwnd', function (h)
{ {
this.parent.hwnd = h; // We need to spin up a message pump, and fetch a window handle
var message_pump = require('win-message-pump');
this._messagepump = new message_pump({ filter: WM_WTSSESSION_CHANGE }); this._messagepump.parent = this;
this._messagepump.on('exit', function (code) { this.parent._wts.WTSUnRegisterSessionNotification(this.parent.hwnd); });
this._messagepump.on('hwnd', function (h)
{
this.parent.hwnd = h;
// We need to yield, and do this in the next event loop pass, becuase we don't want to call 'RegisterPowerSettingNotification' // We need to yield, and do this in the next event loop pass, becuase we don't want to call 'RegisterPowerSettingNotification'
// from the messagepump 'thread', because we are actually on the microstack thread, such that the message pump thread, is holding // from the messagepump 'thread', because we are actually on the microstack thread, such that the message pump thread, is holding
// on a semaphore for us to return. If we call now, we may deadlock on Windows 7, becuase it will try to notify immediately // on a semaphore for us to return. If we call now, we may deadlock on Windows 7, becuase it will try to notify immediately
this.immediate = setImmediate(function (self) this.immediate = setImmediate(function (self)
{
// Now that we have a window handle, we can register it to receive Windows Messages
if (self.parent._wts) { self.parent._wts.WTSRegisterSessionNotification(self.parent.hwnd, NOTIFY_FOR_ALL_SESSIONS); }
self.parent._user32.ACDC_H = self.parent._user32.RegisterPowerSettingNotification(self.parent.hwnd, GUID_ACDC_POWER_SOURCE, 0);
self.parent._user32.BATT_H = self.parent._user32.RegisterPowerSettingNotification(self.parent.hwnd, GUID_BATTERY_PERCENTAGE_REMAINING, 0);
self.parent._user32.DISP_H = self.parent._user32.RegisterPowerSettingNotification(self.parent.hwnd, GUID_CONSOLE_DISPLAY_STATE, 0);
//console.log(self.parent._user32.ACDC_H.Val, self.parent._user32.BATT_H.Val, self.parent._user32.DISP_H.Val);
}, this);
});
this._messagepump.on('message', function (msg)
{ {
// Now that we have a window handle, we can register it to receive Windows Messages switch (msg.message)
if (self.parent._wts) { self.parent._wts.WTSRegisterSessionNotification(self.parent.hwnd, NOTIFY_FOR_ALL_SESSIONS); } {
self.parent._user32.ACDC_H = self.parent._user32.RegisterPowerSettingNotification(self.parent.hwnd, GUID_ACDC_POWER_SOURCE, 0); case WM_WTSSESSION_CHANGE:
self.parent._user32.BATT_H = self.parent._user32.RegisterPowerSettingNotification(self.parent.hwnd, GUID_BATTERY_PERCENTAGE_REMAINING, 0); switch (msg.wparam)
self.parent._user32.DISP_H = self.parent._user32.RegisterPowerSettingNotification(self.parent.hwnd, GUID_CONSOLE_DISPLAY_STATE, 0); {
//console.log(self.parent._user32.ACDC_H.Val, self.parent._user32.BATT_H.Val, self.parent._user32.DISP_H.Val); case WTS_SESSION_LOCK:
}, this); this.parent.enumerateUsers().then(function (users)
}); {
this._messagepump.on('message', function (msg) if (users[msg.lparam]) { this.parent.emit('locked', users[msg.lparam]); }
{ });
switch(msg.message) break;
{ case WTS_SESSION_UNLOCK:
case WM_WTSSESSION_CHANGE: this.parent.enumerateUsers().then(function (users)
switch(msg.wparam) {
{ if (users[msg.lparam]) { this.parent.emit('unlocked', users[msg.lparam]); }
case WTS_SESSION_LOCK: });
this.parent.enumerateUsers().then(function (users) break;
{ case WTS_SESSION_LOGON:
if (users[msg.lparam]) { this.parent.emit('locked', users[msg.lparam]); } case WTS_SESSION_LOGOFF:
}); this.parent.emit('changed');
break; break;
case WTS_SESSION_UNLOCK: }
this.parent.enumerateUsers().then(function (users) break;
{ case WM_POWERBROADCAST:
if (users[msg.lparam]) { this.parent.emit('unlocked', users[msg.lparam]); } switch (msg.wparam)
}); {
break; default:
case WTS_SESSION_LOGON: console.log('WM_POWERBROADCAST [UNKNOWN wparam]: ' + msg.wparam);
case WTS_SESSION_LOGOFF: break;
this.parent.emit('changed'); case PBT_APMSUSPEND:
break; require('power-monitor').emit('sx', 'SLEEP');
} break;
break; case PBT_APMRESUMEAUTOMATIC:
case WM_POWERBROADCAST: require('power-monitor').emit('sx', 'RESUME_NON_INTERACTIVE');
switch(msg.wparam) break;
{ case PBT_APMRESUMESUSPEND:
default: require('power-monitor').emit('sx', 'RESUME_INTERACTIVE');
console.log('WM_POWERBROADCAST [UNKNOWN wparam]: ' + msg.wparam); break;
break; case PBT_APMPOWERSTATUSCHANGE:
case PBT_APMSUSPEND: require('power-monitor').emit('changed');
require('power-monitor').emit('sx', 'SLEEP'); break;
break; case PBT_POWERSETTINGCHANGE:
case PBT_APMRESUMEAUTOMATIC: var lparam = this.parent._marshal.CreatePointer(Buffer.from(msg.lparam_hex, 'hex'));
require('power-monitor').emit('sx', 'RESUME_NON_INTERACTIVE'); var data = lparam.Deref(20, lparam.Deref(16, 4).toBuffer().readUInt32LE(0)).toBuffer();
break; switch (lparam.Deref(0, 16).toBuffer().toString('hex'))
case PBT_APMRESUMESUSPEND: {
require('power-monitor').emit('sx', 'RESUME_INTERACTIVE'); case GUID_ACDC_POWER_SOURCE.Deref(0, 16).toBuffer().toString('hex'):
break; switch (data.readUInt32LE(0))
case PBT_APMPOWERSTATUSCHANGE: {
require('power-monitor').emit('changed'); case 0:
break; require('power-monitor').emit('acdc', 'AC');
case PBT_POWERSETTINGCHANGE: break;
var lparam = this.parent._marshal.CreatePointer(Buffer.from(msg.lparam_hex, 'hex')); case 1:
var data = lparam.Deref(20, lparam.Deref(16, 4).toBuffer().readUInt32LE(0)).toBuffer(); require('power-monitor').emit('acdc', 'BATTERY');
switch(lparam.Deref(0, 16).toBuffer().toString('hex')) break;
{ case 2:
case GUID_ACDC_POWER_SOURCE.Deref(0, 16).toBuffer().toString('hex'): require('power-monitor').emit('acdc', 'HOT');
switch(data.readUInt32LE(0)) break;
{ }
case 0: break;
require('power-monitor').emit('acdc', 'AC'); case GUID_BATTERY_PERCENTAGE_REMAINING.Deref(0, 16).toBuffer().toString('hex'):
break; require('power-monitor').emit('batteryLevel', data.readUInt32LE(0));
case 1: break;
require('power-monitor').emit('acdc', 'BATTERY'); case GUID_CONSOLE_DISPLAY_STATE.Deref(0, 16).toBuffer().toString('hex'):
break; switch (data.readUInt32LE(0))
case 2: {
require('power-monitor').emit('acdc', 'HOT'); case 0:
break; require('power-monitor').emit('display', 'OFF');
} break;
break; case 1:
case GUID_BATTERY_PERCENTAGE_REMAINING.Deref(0, 16).toBuffer().toString('hex'): require('power-monitor').emit('display', 'ON');
require('power-monitor').emit('batteryLevel', data.readUInt32LE(0)); break;
break; case 2:
case GUID_CONSOLE_DISPLAY_STATE.Deref(0, 16).toBuffer().toString('hex'): require('power-monitor').emit('display', 'DIMMED');
switch(data.readUInt32LE(0)) break;
{ }
case 0: break;
require('power-monitor').emit('display', 'OFF'); }
break; break;
case 1: }
require('power-monitor').emit('display', 'ON'); break;
break; default:
case 2: break;
require('power-monitor').emit('display', 'DIMMED'); }
break; });
} }
break;
}
break;
}
break;
default:
break;
}
});
} }
else if(process.platform == 'linux' || process.platform == 'freebsd') else if(process.platform == 'linux' || process.platform == 'freebsd')
{ {