|
| 1 | +import { homedir, platform, userInfo } from 'os'; |
| 2 | +import { AnyValueObject } from '../../_types/global'; |
| 3 | +import { runCommand } from './runCommand.js'; |
| 4 | + |
| 5 | +export async function getSid(): Promise<string> { |
| 6 | + const platformName = platform(); |
| 7 | + |
| 8 | + if (platformName === 'win32') { |
| 9 | + try { |
| 10 | + const profileLists = await runCommand( |
| 11 | + `REG QUERY "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList" /s` |
| 12 | + ); |
| 13 | + |
| 14 | + const profiles: AnyValueObject = {}; |
| 15 | + const sections = profileLists.split('HKEY_LOCAL_MACHINE\\'); |
| 16 | + |
| 17 | + sections.forEach((section) => { |
| 18 | + const lines = section |
| 19 | + .split('\n') |
| 20 | + .map((line) => line.trim()) |
| 21 | + .filter((line) => line); |
| 22 | + |
| 23 | + if (lines.length > 0) { |
| 24 | + const keyName: string = lines[0].split('\\').pop() || 'Unknown'; |
| 25 | + const profileImagePath = lines.find((line) => line.startsWith('ProfileImagePath')); |
| 26 | + |
| 27 | + if (profileImagePath) { |
| 28 | + profiles[keyName] = profileImagePath.split(' ').pop() || 'Unknown'; |
| 29 | + } |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + for (let i = 0; i < Object.keys(profiles).length; i += 1) { |
| 34 | + const key = Object.keys(profiles)[i]; |
| 35 | + const value = profiles[key]; |
| 36 | + |
| 37 | + if (value === homedir()) { |
| 38 | + return key; |
| 39 | + } |
| 40 | + } |
| 41 | + } catch (error) { |
| 42 | + if (error instanceof Error) { |
| 43 | + throw new Error(error?.message); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + throw new Error('Failed to get machine id'); |
| 48 | + } |
| 49 | + |
| 50 | + if (platformName === 'darwin') { |
| 51 | + try { |
| 52 | + const execResult = await runCommand(`dsmemberutil getsid -U ${userInfo().username}`); |
| 53 | + |
| 54 | + if (execResult) { |
| 55 | + return execResult.replace(/\r?\n/g, ''); |
| 56 | + } |
| 57 | + } catch (error) { |
| 58 | + if (error instanceof Error) { |
| 59 | + throw new Error(error?.message); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + throw new Error('Failed to get machine id'); |
| 64 | + } |
| 65 | + |
| 66 | + throw new Error('Not supported on this operating system.'); |
| 67 | +} |
0 commit comments