1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-06 00:13:33 +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;
}
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 )
{
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
AddUninstallIcon();
UpdateOwnerData();
/*
#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')
{
require('events').EventEmitter.call(retVal);
@@ -1532,6 +1547,7 @@ function serviceManager()
if (process.platform == 'win32')
{
var reg = require('win-registry');
if (!this.isAdmin()) { throw ('Installing as Service, requires admin'); }
// Before we start, we need to copy the binary to the right place
@@ -1616,13 +1632,24 @@ function serviceManager()
}
}
}
if (options.parameters)
{
var reg = require('win-registry');
var imagePath = reg.QueryKey(reg.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + options.name, 'ImagePath');
imagePath += (' ' + options.parameters.join(' '));
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')
{

View File

@@ -320,115 +320,117 @@ function UserSessions()
return (retVal);
};
// 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)
if (!global._noMessagePump)
{
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'
// 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
this.immediate = setImmediate(function (self)
// 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
// 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)
{
// 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
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)
{
switch(msg.message)
{
case WM_WTSSESSION_CHANGE:
switch(msg.wparam)
{
case WTS_SESSION_LOCK:
this.parent.enumerateUsers().then(function (users)
{
if (users[msg.lparam]) { this.parent.emit('locked', users[msg.lparam]); }
});
break;
case WTS_SESSION_UNLOCK:
this.parent.enumerateUsers().then(function (users)
{
if (users[msg.lparam]) { this.parent.emit('unlocked', users[msg.lparam]); }
});
break;
case WTS_SESSION_LOGON:
case WTS_SESSION_LOGOFF:
this.parent.emit('changed');
break;
}
break;
case WM_POWERBROADCAST:
switch(msg.wparam)
{
default:
console.log('WM_POWERBROADCAST [UNKNOWN wparam]: ' + msg.wparam);
break;
case PBT_APMSUSPEND:
require('power-monitor').emit('sx', 'SLEEP');
break;
case PBT_APMRESUMEAUTOMATIC:
require('power-monitor').emit('sx', 'RESUME_NON_INTERACTIVE');
break;
case PBT_APMRESUMESUSPEND:
require('power-monitor').emit('sx', 'RESUME_INTERACTIVE');
break;
case PBT_APMPOWERSTATUSCHANGE:
require('power-monitor').emit('changed');
break;
case PBT_POWERSETTINGCHANGE:
var lparam = this.parent._marshal.CreatePointer(Buffer.from(msg.lparam_hex, 'hex'));
var data = lparam.Deref(20, lparam.Deref(16, 4).toBuffer().readUInt32LE(0)).toBuffer();
switch(lparam.Deref(0, 16).toBuffer().toString('hex'))
{
case GUID_ACDC_POWER_SOURCE.Deref(0, 16).toBuffer().toString('hex'):
switch(data.readUInt32LE(0))
{
case 0:
require('power-monitor').emit('acdc', 'AC');
break;
case 1:
require('power-monitor').emit('acdc', 'BATTERY');
break;
case 2:
require('power-monitor').emit('acdc', 'HOT');
break;
}
break;
case GUID_BATTERY_PERCENTAGE_REMAINING.Deref(0, 16).toBuffer().toString('hex'):
require('power-monitor').emit('batteryLevel', data.readUInt32LE(0));
break;
case GUID_CONSOLE_DISPLAY_STATE.Deref(0, 16).toBuffer().toString('hex'):
switch(data.readUInt32LE(0))
{
case 0:
require('power-monitor').emit('display', 'OFF');
break;
case 1:
require('power-monitor').emit('display', 'ON');
break;
case 2:
require('power-monitor').emit('display', 'DIMMED');
break;
}
break;
}
break;
}
break;
default:
break;
}
});
switch (msg.message)
{
case WM_WTSSESSION_CHANGE:
switch (msg.wparam)
{
case WTS_SESSION_LOCK:
this.parent.enumerateUsers().then(function (users)
{
if (users[msg.lparam]) { this.parent.emit('locked', users[msg.lparam]); }
});
break;
case WTS_SESSION_UNLOCK:
this.parent.enumerateUsers().then(function (users)
{
if (users[msg.lparam]) { this.parent.emit('unlocked', users[msg.lparam]); }
});
break;
case WTS_SESSION_LOGON:
case WTS_SESSION_LOGOFF:
this.parent.emit('changed');
break;
}
break;
case WM_POWERBROADCAST:
switch (msg.wparam)
{
default:
console.log('WM_POWERBROADCAST [UNKNOWN wparam]: ' + msg.wparam);
break;
case PBT_APMSUSPEND:
require('power-monitor').emit('sx', 'SLEEP');
break;
case PBT_APMRESUMEAUTOMATIC:
require('power-monitor').emit('sx', 'RESUME_NON_INTERACTIVE');
break;
case PBT_APMRESUMESUSPEND:
require('power-monitor').emit('sx', 'RESUME_INTERACTIVE');
break;
case PBT_APMPOWERSTATUSCHANGE:
require('power-monitor').emit('changed');
break;
case PBT_POWERSETTINGCHANGE:
var lparam = this.parent._marshal.CreatePointer(Buffer.from(msg.lparam_hex, 'hex'));
var data = lparam.Deref(20, lparam.Deref(16, 4).toBuffer().readUInt32LE(0)).toBuffer();
switch (lparam.Deref(0, 16).toBuffer().toString('hex'))
{
case GUID_ACDC_POWER_SOURCE.Deref(0, 16).toBuffer().toString('hex'):
switch (data.readUInt32LE(0))
{
case 0:
require('power-monitor').emit('acdc', 'AC');
break;
case 1:
require('power-monitor').emit('acdc', 'BATTERY');
break;
case 2:
require('power-monitor').emit('acdc', 'HOT');
break;
}
break;
case GUID_BATTERY_PERCENTAGE_REMAINING.Deref(0, 16).toBuffer().toString('hex'):
require('power-monitor').emit('batteryLevel', data.readUInt32LE(0));
break;
case GUID_CONSOLE_DISPLAY_STATE.Deref(0, 16).toBuffer().toString('hex'):
switch (data.readUInt32LE(0))
{
case 0:
require('power-monitor').emit('display', 'OFF');
break;
case 1:
require('power-monitor').emit('display', 'ON');
break;
case 2:
require('power-monitor').emit('display', 'DIMMED');
break;
}
break;
}
break;
}
break;
default:
break;
}
});
}
}
else if(process.platform == 'linux' || process.platform == 'freebsd')
{