Skip to content

Commit 0f5a91d

Browse files
committed
Improve OS detection and reduce data collected
This improves some cases (e.g. distinguishes Linux distro, for notable major distros) but also reduces the fingerprintable details (kernel patch & variant was included and is relatively unique - all versions are now major+minor only).
1 parent d53c945 commit 0f5a91d

File tree

2 files changed

+134
-50
lines changed

2 files changed

+134
-50
lines changed

src/device.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import * as os from 'os';
2+
import { promises as fs } from 'fs'
3+
import { promisify } from 'util';
4+
import { exec } from 'child_process';
5+
const execAsync = promisify(exec);
6+
7+
import { logError } from './errors';
8+
9+
export async function getDeviceDetails() {
10+
const [
11+
realArch,
12+
osDetails
13+
] = await Promise.all([
14+
getRealArch(),
15+
getOsDetails()
16+
]);
17+
18+
return {
19+
...osDetails,
20+
runtimeArch: os.arch(),
21+
realArch: realArch
22+
}
23+
}
24+
25+
const majorMinorOnly = <I extends string | undefined>(input: I): I =>
26+
input?.split('.').slice(0, 2).join('.') as I;
27+
28+
async function getOsDetails() {
29+
const rawPlatform = os.platform();
30+
if (rawPlatform == 'linux') {
31+
return await getLinuxOsDetails();
32+
} else if (rawPlatform === 'win32') {
33+
// For Windows, the version numbers are oddly precise and weird. We simplify:
34+
return {
35+
platform: rawPlatform,
36+
version: getWindowsVersion()
37+
};
38+
} else {
39+
return {
40+
platform: rawPlatform,
41+
release: majorMinorOnly(os.release())
42+
}
43+
}
44+
}
45+
46+
function getWindowsVersion() {
47+
const rawVersion = os.release();
48+
try {
49+
if (rawVersion.startsWith('10.0.')) {
50+
// Windows 10.0.x < 22000 is Windows 10, >= 22000 is Windows 11
51+
const buildNumber = parseInt(rawVersion.slice('10.0.'.length), 10);
52+
if (isNaN(buildNumber)) return 'Unknown';
53+
else if (buildNumber >= 22000) return '11';
54+
else return '10'
55+
} else {
56+
// Other versions - e.g. 6.3 is Windows 8.1
57+
return majorMinorOnly(rawVersion);
58+
}
59+
} catch (e) {
60+
logError(`Failed to detect windows version: ${e.message || e}`);
61+
return 'Unknown';
62+
}
63+
}
64+
65+
async function getLinuxOsDetails() {
66+
// For Linux, there's a relatively small number of users with a lot of variety.
67+
// We do a bit more digging, to try to get meaningful data (e.g. distro) and
68+
// drop unnecessary fingerprinting factors (kernel patch version & variants etc). End
69+
// result is e.g. "ubuntu + 20.04" (just major+minor, for big distros supporting
70+
// /etc/os-release) or "linux + 6.5" (just kernel major+minor).
71+
try {
72+
const osReleaseDetails = await fs.readFile('/etc/os-release', 'utf8')
73+
.catch(() => '');
74+
const osRelease = osReleaseDetails.split('\n').reduce((acc, line) => {
75+
const [key, value] = line.split('=');
76+
if (key && value) {
77+
acc[key] = value.replace(/(^")|("$)/g, '');
78+
}
79+
return acc;
80+
}, {} as { [key: string]: string | undefined });
81+
82+
return {
83+
platform: osRelease['ID'] || osRelease['NAME'] || 'linux',
84+
version: majorMinorOnly(osRelease['VERSION_ID'] || os.release())
85+
};
86+
} catch (e) {
87+
logError(`Failed to detect Linux version: ${e.message}`);
88+
return {
89+
platform: 'linux',
90+
version: majorMinorOnly(os.release())
91+
};
92+
}
93+
}
94+
95+
// Detect the 'real' architecture of the system. We're concerned here with detecting the real arch
96+
// despite emulation here, to help with launch subprocs. Not too worried about x86 vs x64.
97+
async function getRealArch() {
98+
try {
99+
switch (process.platform) {
100+
case 'darwin':
101+
const { stdout: armCheck } = await execAsync('sysctl -n hw.optional.arm64')
102+
.catch((e: any) => {
103+
const output = e.message + e.stdout + e.stderr;
104+
// This id may not be available:
105+
if (output?.includes?.("unknown oid")) return { stdout: "0" };
106+
else throw e;
107+
});
108+
if (armCheck.trim() === '1') {
109+
return 'arm64';
110+
}
111+
112+
case 'linux':
113+
const { stdout: cpuInfo } = await execAsync('cat /proc/cpuinfo');
114+
const lcCpuInfo = cpuInfo.toLowerCase();
115+
if (lcCpuInfo.includes('aarch64') || lcCpuInfo.includes('arm64')) {
116+
return 'arm64';
117+
}
118+
119+
case 'win32':
120+
const arch = process.env.PROCESSOR_ARCHITEW6432 || process.env.PROCESSOR_ARCHITECTURE;
121+
if (arch?.toLowerCase() === 'arm64') {
122+
return 'arm64';
123+
}
124+
}
125+
} catch (e) {
126+
console.warn(`Error querying system arch: ${e.message}`);
127+
logError(e);
128+
}
129+
130+
return os.arch();
131+
}

src/index.ts

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const DEV_MODE = process.env.HTK_DEV === 'true';
33
// Set up error handling before everything else:
44
import { logError, addBreadcrumb } from './errors';
55

6-
import { spawn, exec, ChildProcess } from 'child_process';
6+
import { spawn, ChildProcess } from 'child_process';
77
import * as os from 'os';
88
import { promises as fs, createWriteStream, WriteStream } from 'fs'
99
import * as net from 'net';
@@ -18,7 +18,6 @@ import * as semver from 'semver';
1818
import * as rimraf from 'rimraf';
1919

2020
const rmRF = promisify(rimraf);
21-
const execAsync = promisify(exec);
2221

2322
import * as windowStateKeeper from 'electron-window-state';
2423
import { getSystemProxy } from 'os-proxy-config';
@@ -28,6 +27,7 @@ import { getDeferred, delay } from './util';
2827
import { getMenu, shouldAutoHideMenu } from './menu';
2928
import { ContextMenuDefinition, openContextMenu } from './context-menu';
3029
import { stopServer } from './stop-server';
30+
import { getDeviceDetails } from './device';
3131

3232
const packageJson = require('../package.json');
3333

@@ -650,54 +650,7 @@ ipcMain.handle('open-context-menu', ipcHandler((options: ContextMenuDefinition)
650650

651651
ipcMain.handle('get-desktop-version', ipcHandler(() => DESKTOP_VERSION));
652652
ipcMain.handle('get-server-auth-token', ipcHandler(() => AUTH_TOKEN));
653-
ipcMain.handle('get-device-info', ipcHandler(async () => {
654-
const realArch = await getRealArch();
655-
656-
return {
657-
platform: os.platform(),
658-
release: os.release(),
659-
runtimeArch: os.arch(),
660-
realArch: realArch
661-
}
662-
}));
663-
664-
// Detect the 'real' architecture of the system. We're concerned here with detecting the real arch
665-
// despite emulation here, to help with launch subprocs. Not too worried about x86 vs x64.
666-
async function getRealArch() {
667-
try {
668-
switch (process.platform) {
669-
case 'darwin':
670-
const { stdout: armCheck } = await execAsync('sysctl -n hw.optional.arm64')
671-
.catch((e: any) => {
672-
const output = e.message + e.stdout + e.stderr;
673-
// This id may not be available:
674-
if (output?.includes?.("unknown oid")) return { stdout: "0" };
675-
else throw e;
676-
});
677-
if (armCheck.trim() === '1') {
678-
return 'arm64';
679-
}
680-
681-
case 'linux':
682-
const { stdout: cpuInfo } = await execAsync('cat /proc/cpuinfo');
683-
const lcCpuInfo = cpuInfo.toLowerCase();
684-
if (lcCpuInfo.includes('aarch64') || lcCpuInfo.includes('arm64')) {
685-
return 'arm64';
686-
}
687-
688-
case 'win32':
689-
const arch = process.env.PROCESSOR_ARCHITEW6432 || process.env.PROCESSOR_ARCHITECTURE;
690-
if (arch?.toLowerCase() === 'arm64') {
691-
return 'arm64';
692-
}
693-
}
694-
} catch (e) {
695-
console.warn(`Error querying system arch: ${e.message}`);
696-
logError(e);
697-
}
698-
699-
return os.arch();
700-
}
653+
ipcMain.handle('get-device-info', ipcHandler(() => getDeviceDetails()));
701654

702655
let restarting = false;
703656
ipcMain.handle('restart-app', ipcHandler(() => {

0 commit comments

Comments
 (0)