users tty improvement (windows)

This commit is contained in:
Sebastian Hildebrandt 2022-01-21 08:54:57 +01:00
parent 1aed87d72e
commit d760a2ccec
2 changed files with 43 additions and 44 deletions

View File

@ -176,44 +176,6 @@ function parseUsersDarwin(lines) {
return result;
}
// function parseUsersWin(lines, culture) {
// let result = [];
// const header = lines[0];
// const headerDelimiter = [];
// if (header) {
// const start = (header[0] === ' ') ? 1 : 0;
// headerDelimiter.push(start - 1);
// let nextSpace = 0;
// for (let i = start + 1; i < header.length; i++) {
// if (header[i] === ' ' && ((header[i - 1] === ' ') || (header[i - 1] === '.'))) {
// nextSpace = i;
// } else {
// if (nextSpace) {
// headerDelimiter.push(nextSpace);
// nextSpace = 0;
// }
// }
// }
// for (let i = 1; i < lines.length; i++) {
// if (lines[i].trim()) {
// const user = lines[i].substring(headerDelimiter[0] + 1, headerDelimiter[1]).trim() || '';
// const tty = lines[i].substring(headerDelimiter[1] + 1, headerDelimiter[2] - 2).trim() || '';
// const dateTime = util.parseDateTime(lines[i].substring(headerDelimiter[5] + 1, 2000).trim(), culture) || '';
// result.push({
// user: user,
// tty: tty,
// date: dateTime.date,
// time: dateTime.time,
// ip: '',
// command: ''
// });
// }
// }
// }
// return result;
// }
function users(callback) {
return new Promise((resolve) => {
@ -293,7 +255,8 @@ function users(callback) {
// ).then(data => {
let cmd = 'Get-WmiObject Win32_LogonSession | fl *' + '; echo \'#-#-#-#\';';
cmd += 'Get-WmiObject Win32_LoggedOnUser | fl *' + '; echo \'#-#-#-#\';';
cmd += 'Get-WmiObject Win32_Process -Filter "name=\'explorer.exe\'" | Select @{Name="sessionid";Expression={$_.SessionId}}, @{Name="domain";Expression={$_.GetOwner().Domain}}, @{Name="username";Expression={$_.GetOwner().User}} | fl';
cmd += 'Get-WmiObject Win32_Process -Filter "name=\'explorer.exe\'" | Select @{Name="sessionid";Expression={$_.SessionId}}, @{Name="domain";Expression={$_.GetOwner().Domain}}, @{Name="username";Expression={$_.GetOwner().User}} | fl' + '; echo \'#-#-#-#\';';
cmd += 'query user';
util.powerShell(cmd).then(data => {
// controller + vram
// let accounts = parseWinAccounts(data[0].split(/\n\s*\n/));
@ -301,7 +264,8 @@ function users(callback) {
data = data.split('#-#-#-#');
let sessions = parseWinSessions(data[0].split(/\n\s*\n/));
let loggedons = parseWinLoggedOn(data[1].split(/\n\s*\n/));
let users = parseWinUsers(data[2].split(/\n\s*\n/));
let queryUser = parseWinUsersQuery(data[3].split('\r\n'));
let users = parseWinUsers(data[2].split(/\n\s*\n/), queryUser);
for (let id in loggedons) {
if ({}.hasOwnProperty.call(loggedons, id)) {
loggedons[id].dateTime = {}.hasOwnProperty.call(sessions, id) ? sessions[id] : '';
@ -380,7 +344,7 @@ function parseWinSessions(sessionParts) {
return sessions;
}
function parseWinUsers(userParts) {
function parseWinUsers(userParts, userQuery) {
const users = [];
userParts.forEach(user => {
const lines = user.split('\r\n');
@ -390,10 +354,11 @@ function parseWinUsers(userParts) {
const sessionid = util.getValue(lines, 'sessionid', ':', true);
if (username) {
const quser = userQuery.filter(item => item.user === username);
users.push({
domain,
user: username,
tty: sessionid
tty: quser && quser[0] && quser[0].tty ? quser[0].tty : sessionid
});
}
});
@ -424,4 +389,38 @@ function parseWinLoggedOn(loggedonParts) {
return loggedons;
}
function parseWinUsersQuery(lines) {
let result = [];
const header = lines[0];
const headerDelimiter = [];
if (header) {
const start = (header[0] === ' ') ? 1 : 0;
headerDelimiter.push(start - 1);
let nextSpace = 0;
for (let i = start + 1; i < header.length; i++) {
if (header[i] === ' ' && ((header[i - 1] === ' ') || (header[i - 1] === '.'))) {
nextSpace = i;
} else {
if (nextSpace) {
headerDelimiter.push(nextSpace);
nextSpace = 0;
}
}
}
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim()) {
const user = lines[i].substring(headerDelimiter[0] + 1, headerDelimiter[1]).trim() || '';
const tty = lines[i].substring(headerDelimiter[1] + 1, headerDelimiter[2] - 2).trim() || '';
// const dateTime = util.parseDateTime(lines[i].substring(headerDelimiter[5] + 1, 2000).trim(), culture) || '';
result.push({
user: user,
tty: tty,
});
}
}
}
return result;
}
exports.users = users;

View File

@ -5,7 +5,7 @@ const testWithTimeout = async (fn) => {
(async () => {
const timeout = setTimeout(() => {
reject('Test Timeout');
}, 60000);
}, 80000);
const result = await fn();
clearTimeout(timeout);
return resolve(result);