1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-22 11:13:21 +00:00

Updated for Linux/MacOS

This commit is contained in:
Bryan Roe
2019-06-06 18:47:05 -07:00
parent f28ea50560
commit 67e10e0e33
4 changed files with 203 additions and 171 deletions

File diff suppressed because one or more lines are too long

View File

@@ -289,9 +289,16 @@ function macos_messageBox()
this.promise._rej('TIMEOUT'); this.promise._rej('TIMEOUT');
} }
else else
{
if (p.button == 'Yes')
{ {
this.promise._res(p.button); this.promise._res(p.button);
} }
else
{
this.promise._rej('denied');
}
}
break; break;
} }
}); });

View File

@@ -16,14 +16,21 @@ limitations under the License.
var promise = require('promise'); var promise = require('promise');
if (process.platform == 'linux') if (process.platform == 'linux' || process.platform == 'darwin')
{ {
function findPath(app) function findPath(app)
{ {
var child = require('child_process').execFile('/bin/sh', ['sh']); var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = ''; child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
if (process.platform == 'linux')
{
child.stdin.write("whereis " + app + " | awk '{ print $2 }'\nexit\n"); child.stdin.write("whereis " + app + " | awk '{ print $2 }'\nexit\n");
}
else
{
child.stdin.write("whereis " + app + "\nexit\n");
}
child.waitExit(); child.waitExit();
child.stdout.str = child.stdout.str.trim(); child.stdout.str = child.stdout.str.trim();
return (child.stdout.str == '' ? null : child.stdout.str); return (child.stdout.str == '' ? null : child.stdout.str);
@@ -39,25 +46,23 @@ function Toaster()
retVal.title = title; retVal.title = title;
retVal.caption = caption; retVal.caption = caption;
if (process.platform == 'win32') switch (process.platform) {
case 'win32':
{ {
var GM = require('_GenericMarshal'); var GM = require('_GenericMarshal');
var kernel32 = GM.CreateNativeProxy('kernel32.dll'); var kernel32 = GM.CreateNativeProxy('kernel32.dll');
kernel32.CreateMethod('ProcessIdToSessionId'); kernel32.CreateMethod('ProcessIdToSessionId');
var psid = GM.CreateVariable(4); var psid = GM.CreateVariable(4);
var consoleUid = require('user-sessions').consoleUid(); var consoleUid = require('user-sessions').consoleUid();
if (kernel32.ProcessIdToSessionId(process.pid, psid).Val == 0) if (kernel32.ProcessIdToSessionId(process.pid, psid).Val == 0) {
{
retVal._rej('internal error'); return (retVal); retVal._rej('internal error'); return (retVal);
} }
if (consoleUid == psid.toBuffer().readUInt32LE()) if (consoleUid == psid.toBuffer().readUInt32LE()) {
{
// We are running on the physical console // We are running on the physical console
retVal._child = require('ScriptContainer').Create({ processIsolation: true }); retVal._child = require('ScriptContainer').Create({ processIsolation: true });
} }
else else {
{
// We need so spawn the ScriptContainer into the correct session // We need so spawn the ScriptContainer into the correct session
retVal._child = require('ScriptContainer').Create({ processIsolation: true, sessionId: consoleUid }); retVal._child = require('ScriptContainer').Create({ processIsolation: true, sessionId: consoleUid });
} }
@@ -81,42 +86,35 @@ function Toaster()
retVal._child.ExecuteString(str); retVal._child.ExecuteString(str);
return (retVal); return (retVal);
} }
else break;
{ case 'linux':
try
{ {
try {
retVal.consoleUid = require('user-sessions').consoleUid(); retVal.consoleUid = require('user-sessions').consoleUid();
retVal.xinfo = require('monitor-info').getXInfo(retVal.consoleUid); retVal.xinfo = require('monitor-info').getXInfo(retVal.consoleUid);
} }
catch(xxe) catch (xxe) {
{
retVal._rej(xxe); retVal._rej(xxe);
return (retVal); return (retVal);
} }
var util = findPath('zenity'); var util = findPath('zenity');
if (util) if (util) {
{
// Use ZENITY // Use ZENITY
retVal.child = require('child_process').execFile(util, ['zenity', '--notification', '--title=' + title, '--text=' + caption, '--timeout=5'], { uid: retVal.consoleUid, env: { XAUTHORITY: retVal.xinfo.xauthority, DISPLAY: retVal.xinfo.display } }); retVal.child = require('child_process').execFile(util, ['zenity', '--notification', '--title=' + title, '--text=' + caption, '--timeout=5'], { uid: retVal.consoleUid, env: { XAUTHORITY: retVal.xinfo.xauthority, DISPLAY: retVal.xinfo.display } });
retVal.child.parent = retVal; retVal.child.parent = retVal;
retVal.child.stderr.str = ''; retVal.child.stderr.str = '';
retVal.child.stderr.on('data', function (chunk) { this.str += chunk.toString(); this.parent.kill(); }); retVal.child.stderr.on('data', function (chunk) { this.str += chunk.toString(); this.parent.kill(); });
retVal.child.stdout.on('data', function (chunk) { }); retVal.child.stdout.on('data', function (chunk) { });
retVal.child.on('exit', function (code) retVal.child.on('exit', function (code) {
{ if (this.stderr.str.trim() != '') {
if(this.stderr.str.trim() != '') if ((util = findPath('notify-send')) && this.stderr.str.split('GLib-CRITICAL').length > 1) {
{
if ((util = findPath('notify-send')) && this.stderr.str.split('GLib-CRITICAL').length > 1)
{
// This is a bug in zenity, so we should try notify-send // This is a bug in zenity, so we should try notify-send
if (process.env['DISPLAY']) if (process.env['DISPLAY']) {
{
// DISPLAY is set, so we good to go // DISPLAY is set, so we good to go
this.parent.child = require('child_process').execFile(util, ['notify-send', this.parent.title, this.parent.caption]); this.parent.child = require('child_process').execFile(util, ['notify-send', this.parent.title, this.parent.caption]);
this.parent.child.parent = this.parent; this.parent.child.parent = this.parent;
} }
else else {
{
// We need to find the DISPLAY to use // We need to find the DISPLAY to use
var username = require('user-sessions').getUsername(consoleUid); var username = require('user-sessions').getUsername(consoleUid);
this.parent.child = require('child_process').execFile('/bin/sh', ['sh']); this.parent.child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -128,36 +126,30 @@ function Toaster()
this.parent.child.waitExit(); this.parent.child.waitExit();
// NOTIFY-SEND has a bug where timeouts don't work, so the default is 5 seconds // NOTIFY-SEND has a bug where timeouts don't work, so the default is 5 seconds
this.parent._timeout = setTimeout(function onFakeDismissed(obj) this.parent._timeout = setTimeout(function onFakeDismissed(obj) {
{
obj._res('DISMISSED'); obj._res('DISMISSED');
}, 10000, this.parent); }, 10000, this.parent);
} }
else else {
{
// Fake a toast using zenity --info // Fake a toast using zenity --info
util = findPath('zenity'); util = findPath('zenity');
this.parent.child = require('child_process').execFile(util, ['zenity', '--info', '--title=' + this.parent.title, '--text=' + this.parent.caption, '--timeout=5'], { uid: this.parent.consoleUid, env: { XAUTHORITY: this.parent.xinfo.xauthority, DISPLAY: this.parent.xinfo.display } }); this.parent.child = require('child_process').execFile(util, ['zenity', '--info', '--title=' + this.parent.title, '--text=' + this.parent.caption, '--timeout=5'], { uid: this.parent.consoleUid, env: { XAUTHORITY: this.parent.xinfo.xauthority, DISPLAY: this.parent.xinfo.display } });
this.parent.child.parent = this.parent; this.parent.child.parent = this.parent;
this.parent.child.stderr.on('data', function (chunk) { }); this.parent.child.stderr.on('data', function (chunk) { });
this.parent.child.stdout.on('data', function (chunk) { }); this.parent.child.stdout.on('data', function (chunk) { });
this.parent.child.on('exit', function (code) this.parent.child.on('exit', function (code) {
{
this.parent._res('DISMISSED'); this.parent._res('DISMISSED');
}); });
} }
} }
else else {
{
this.parent._res('DISMISSED'); this.parent._res('DISMISSED');
} }
}); });
} }
else else {
{
util = findPath('kdialog'); util = findPath('kdialog');
if (util) if (util) {
{
// use KDIALOG // use KDIALOG
var xdg = require('user-sessions').findEnv(retVal.consoleUid, 'XDG_RUNTIME_DIR'); var xdg = require('user-sessions').findEnv(retVal.consoleUid, 'XDG_RUNTIME_DIR');
if (!retVal.xinfo || !retVal.xinfo.display || !retVal.xinfo.xauthority || !xdg) if (!retVal.xinfo || !retVal.xinfo.display || !retVal.xinfo.xauthority || !xdg)
@@ -173,12 +165,19 @@ function Toaster()
} }
else else
{ {
retVal._rej ('Zenity/KDialog not found'); retVal._rej('Zenity/KDialog not found');
} }
} }
}
break;
case 'darwin':
retVal._toast = require('message-box').notify(title, caption);
retVal._toast.parent = retVal;
retVal._toast.then(function (v) { this.parent._res(v); }, function (e) { this.parent._rej(e); });
break;
}
return (retVal); return (retVal);
}
}; };
} }

View File

@@ -393,6 +393,26 @@ function UserSessions()
} }
return (ret); return (ret);
} }
this.consoleUid = function consoleUid()
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write('who\nexit\n');
child.waitExit();
var lines = child.stdout.str.split('\n');
var tokens, i, j;
for (i in lines) {
tokens = lines[i].split(' ');
for (j = 1; j < tokens.length; ++j) {
if (tokens[j].length > 0) {
return (parseInt(this._users()[tokens[0]]));
}
}
}
throw ('nobody logged into console');
}
this.Current = function Current(cb) this.Current = function Current(cb)
{ {
var retVal = {}; var retVal = {};
@@ -652,6 +672,15 @@ function UserSessions()
} }
else if(process.platform == 'darwin') else if(process.platform == 'darwin')
{ {
this.getUid = function getUid(username)
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("id " + username + " | awk '{ split($1, token, \"=\"); split(token[2], uid, \"(\"); print uid[1]; }'\nexit\n");
child.waitExit();
return (parseInt(child.stdout.str.trim()));
};
this.getGroupID = function getGroupID(uid) this.getGroupID = function getGroupID(uid)
{ {
var child = require('child_process').execFile('/bin/sh', ['sh']); var child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -664,8 +693,10 @@ function UserSessions()
this.getUsername = function getUsername(uid) this.getUsername = function getUsername(uid)
{ {
var child = require('child_process').execFile('/bin/sh', ['sh']); var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stderr.str = '';
child.stdout.str = ''; child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("dscl . list /Users UniqueID | grep " + uid + " | awk '{ if($2==" + uid + "){ print $1 }}'\nexit\n"); child.stdin.write("dscl . list /Users UniqueID | grep " + uid + " | awk '{ if($2==" + uid + "){ print $1 }}'\nexit\n");
child.waitExit(); child.waitExit();
if(child.stdout.str.trim() != '') if(child.stdout.str.trim() != '')
@@ -677,6 +708,21 @@ function UserSessions()
throw ('uid: ' + uid + ' not found'); throw ('uid: ' + uid + ' not found');
} }
}; };
this.consoleUid = function consoleUid()
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("who | tr '\n' '\.' | awk '{ print $1 }'\nexit\n");
child.waitExit();
var ret = child.stdout.str.trim();
if (ret != '')
{
return (this.getUid(ret));
}
throw ('nobody logged into console');
}
this.getHomeFolder = function getHomeFolder(user) this.getHomeFolder = function getHomeFolder(user)
{ {
var child = require('child_process').execFile('/bin/sh', ['sh']); var child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -697,10 +743,13 @@ function UserSessions()
{ {
var child = require('child_process').execFile('/usr/bin/dscl', ['dscl', '.', 'list', '/Users', 'UniqueID']); var child = require('child_process').execFile('/usr/bin/dscl', ['dscl', '.', 'list', '/Users', 'UniqueID']);
child.stdout.str = ''; child.stdout.str = '';
child.stderr.str = '';
child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write('exit\n'); child.stdin.write('exit\n');
child.waitExit(); child.waitExit();
var lines = child.stdout.str.split('\n'); var lines = child.stdout.str.split('\n');
var tokens, i; var tokens, i;
var users = {}; var users = {};
@@ -803,31 +852,6 @@ function UserSessions()
{ {
return (this.Self() == 0); return (this.Self() == 0);
} }
this.consoleUid = function consoleUid()
{
var checkstr = process.platform == 'darwin' ? 'console' : ((process.env['DISPLAY'])?process.env['DISPLAY']:':0')
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write('who\nexit\n');
child.waitExit();
var lines = child.stdout.str.split('\n');
var tokens, i, j;
for (i in lines)
{
tokens = lines[i].split(' ');
for (j = 1; j < tokens.length; ++j)
{
if (tokens[j].length > 0)
{
return (parseInt(this._users()[tokens[0]]));
}
}
}
throw ('nobody logged into console');
}
} }