@codeo-engineering/licenserail 0.0.0-stage → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -2
- package/dist/client.d.ts +192 -0
- package/dist/client.js +451 -0
- package/dist/core.d.ts +147 -0
- package/dist/core.js +225 -0
- package/dist/fingerprint.d.ts +3 -0
- package/dist/fingerprint.js +73 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/management.d.ts +73 -0
- package/dist/management.js +66 -0
- package/dist/vm.d.ts +53 -0
- package/dist/vm.js +184 -0
- package/package.json +22 -4
package/dist/vm.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { platform } from 'node:os';
|
|
4
|
+
// Best-effort detection of virtual machines and containers, without admin rights or dependencies. Products can refuse new
|
|
5
|
+
// activations and trials on virtual machines; the client sends the result as `virtualMachine`. The rules are the same in every
|
|
6
|
+
// LicenseRail SDK (license-rules.md, "Virtual machine detection") and deliberately narrow: a false positive refuses a paying
|
|
7
|
+
// customer. The parsers are exported so they can be tested with sample text.
|
|
8
|
+
/** How long the probe (PowerShell on Windows, sysctl on macOS) may run before it is killed. */
|
|
9
|
+
export const VM_PROBE_TIMEOUT_MS = 5000;
|
|
10
|
+
const HYPERVISOR_NAME = /\b(?:vmware|virtualbox|innotek|qemu|kvm|xen|hvm domu|parallels|bochs|bhyve)\b/i;
|
|
11
|
+
const HYPER_V_GUEST = /\bvirtual machine\b/i;
|
|
12
|
+
const CONTAINER_CGROUP = ['docker', 'kubepods', 'containerd', 'lxc'];
|
|
13
|
+
const WINDOWS_PROBE = ['-NoProfile', '-NonInteractive', '-Command', '$c = Get-CimInstance Win32_ComputerSystem; $c.Manufacturer; $c.Model'];
|
|
14
|
+
/**
|
|
15
|
+
* Whether a system manufacturer and model (Windows Win32_ComputerSystem; Linux /sys/class/dmi/id sys_vendor and product_name)
|
|
16
|
+
* name a hypervisor: VMware, VirtualBox, innotek, QEMU, KVM, Xen, HVM domU, Parallels, Bochs or bhyve as whole words
|
|
17
|
+
* (case-insensitive), or "Virtual Machine" made by "Microsoft Corporation" (a Hyper-V guest). Cloud vendor names are not on the
|
|
18
|
+
* list: they also sell bare metal.
|
|
19
|
+
*/
|
|
20
|
+
export function isVirtualHardware(manufacturer, model) {
|
|
21
|
+
const m = (manufacturer ?? '').trim();
|
|
22
|
+
const p = (model ?? '').trim();
|
|
23
|
+
if (!m && !p)
|
|
24
|
+
return false;
|
|
25
|
+
if (m.toLowerCase() === 'microsoft corporation' && HYPER_V_GUEST.test(p))
|
|
26
|
+
return true;
|
|
27
|
+
return HYPERVISOR_NAME.test(m) || HYPERVISOR_NAME.test(p);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The Windows probe's output (Win32_ComputerSystem Manufacturer, then Model, one per line); null when there is no output (the
|
|
31
|
+
* probe failed). Windows hosts with Hyper-V or VBS enabled report their real maker and model, so they don't match.
|
|
32
|
+
*/
|
|
33
|
+
export function parseWindowsComputerSystem(output) {
|
|
34
|
+
const lines = (output ?? '').split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
|
|
35
|
+
return lines.length === 0 ? null : isVirtualHardware(lines[0], lines[1]);
|
|
36
|
+
}
|
|
37
|
+
/** Whether /proc/sys/kernel/osrelease is a WSL kernel (contains "microsoft" or "WSL"). WSL is not a virtual machine. */
|
|
38
|
+
export function isWsl(osrelease) {
|
|
39
|
+
return !!osrelease && /microsoft|wsl/i.test(osrelease);
|
|
40
|
+
}
|
|
41
|
+
/** Whether /proc/cpuinfo lists the "hypervisor" CPU flag. */
|
|
42
|
+
export function cpuInfoHasHypervisor(cpuinfo) {
|
|
43
|
+
return !!cpuinfo && cpuinfo.split('\n').some((l) => l.startsWith('flags') && /\bhypervisor\b/.test(l));
|
|
44
|
+
}
|
|
45
|
+
/** Whether /proc/1/cgroup shows a container runtime (docker, kubepods, containerd or lxc). */
|
|
46
|
+
export function cgroupIsContainer(cgroup) {
|
|
47
|
+
const c = (cgroup ?? '').toLowerCase();
|
|
48
|
+
return CONTAINER_CGROUP.some((w) => c.includes(w));
|
|
49
|
+
}
|
|
50
|
+
/** macOS `sysctl -n kern.hv_vmm_present`: "1" in a virtual machine. */
|
|
51
|
+
export function sysctlSaysVirtual(output) {
|
|
52
|
+
return output?.trim() === '1';
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The Linux rule: false under WSL; otherwise true when the DMI vendor or product names a hypervisor, the CPU has the
|
|
56
|
+
* `hypervisor` flag, or the process runs in a container.
|
|
57
|
+
*/
|
|
58
|
+
export function linuxIsVirtual(p) {
|
|
59
|
+
if (isWsl(p.osrelease))
|
|
60
|
+
return false;
|
|
61
|
+
return isVirtualHardware(p.sysVendor, p.productName) || cpuInfoHasHypervisor(p.cpuinfo) || !!p.containerFile || cgroupIsContainer(p.cgroup);
|
|
62
|
+
}
|
|
63
|
+
function read(path) {
|
|
64
|
+
try {
|
|
65
|
+
return existsSync(path) ? readFileSync(path, 'utf8') : null;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function linuxProbe() {
|
|
72
|
+
return linuxIsVirtual({
|
|
73
|
+
osrelease: read('/proc/sys/kernel/osrelease'),
|
|
74
|
+
sysVendor: read('/sys/class/dmi/id/sys_vendor'),
|
|
75
|
+
productName: read('/sys/class/dmi/id/product_name'),
|
|
76
|
+
cpuinfo: read('/proc/cpuinfo'),
|
|
77
|
+
containerFile: existsSync('/.dockerenv') || existsSync('/run/.containerenv'),
|
|
78
|
+
cgroup: read('/proc/1/cgroup'),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/** Runs a probe without a window; resolves null on error, a non-zero exit or the timeout (the process is killed). */
|
|
82
|
+
function run(cmd, args) {
|
|
83
|
+
return new Promise((resolve) => {
|
|
84
|
+
let out = '';
|
|
85
|
+
let done = false;
|
|
86
|
+
const finish = (value) => {
|
|
87
|
+
if (done)
|
|
88
|
+
return;
|
|
89
|
+
done = true;
|
|
90
|
+
clearTimeout(timer);
|
|
91
|
+
resolve(value);
|
|
92
|
+
};
|
|
93
|
+
let child;
|
|
94
|
+
try {
|
|
95
|
+
child = spawn(cmd, args, { windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
resolve(null);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const timer = setTimeout(() => {
|
|
102
|
+
try {
|
|
103
|
+
child.kill('SIGKILL');
|
|
104
|
+
}
|
|
105
|
+
catch { /* already gone */ }
|
|
106
|
+
finish(null);
|
|
107
|
+
}, VM_PROBE_TIMEOUT_MS);
|
|
108
|
+
timer.unref?.();
|
|
109
|
+
child.stdout?.setEncoding('utf8');
|
|
110
|
+
child.stdout?.on('data', (d) => { out += d; });
|
|
111
|
+
child.on('error', () => finish(null));
|
|
112
|
+
child.on('close', (code) => finish(code === 0 ? out : null));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function runSync(cmd, args) {
|
|
116
|
+
try {
|
|
117
|
+
return execFileSync(cmd, args, { encoding: 'utf8', timeout: VM_PROBE_TIMEOUT_MS, killSignal: 'SIGKILL', windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
let cached = null;
|
|
124
|
+
let settled;
|
|
125
|
+
async function detectNow() {
|
|
126
|
+
try {
|
|
127
|
+
const os = platform();
|
|
128
|
+
if (os === 'win32')
|
|
129
|
+
return parseWindowsComputerSystem(await run('powershell.exe', WINDOWS_PROBE));
|
|
130
|
+
if (os === 'darwin') {
|
|
131
|
+
const out = await run('sysctl', ['-n', 'kern.hv_vmm_present']);
|
|
132
|
+
return out === null ? null : sysctlSaysVirtual(out);
|
|
133
|
+
}
|
|
134
|
+
return linuxProbe();
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Whether this process runs in a virtual machine or a container: detected once per process without blocking (the probe runs
|
|
142
|
+
* as a child process with a 5-second limit and no window). null when it couldn't be detected (error or timeout). Nothing runs at
|
|
143
|
+
* import time: the probe starts on the first call (the client calls it at the first activate, checkout or trial, or from
|
|
144
|
+
* warmUpVirtualMachineDetection()).
|
|
145
|
+
*/
|
|
146
|
+
export function detectVirtualMachine() {
|
|
147
|
+
if (!cached) {
|
|
148
|
+
cached = detectNow();
|
|
149
|
+
void cached.then((v) => { settled = v; });
|
|
150
|
+
}
|
|
151
|
+
return cached;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Synchronous form of detectVirtualMachine() (false when not detected). It never starts a second probe: once detection has
|
|
155
|
+
* finished it returns the cached result; while the asynchronous probe is still running it returns false at once (the answer
|
|
156
|
+
* isn't known yet; await detectVirtualMachine() for it). Only before the first detection has started does it run the probe
|
|
157
|
+
* itself and block the event loop until it ends (up to 5 seconds, once per process); that result is then cached for both forms.
|
|
158
|
+
* @deprecated Use detectVirtualMachine().
|
|
159
|
+
*/
|
|
160
|
+
export function isVirtualMachine() {
|
|
161
|
+
if (settled !== undefined)
|
|
162
|
+
return settled === true;
|
|
163
|
+
if (cached)
|
|
164
|
+
return false; // the async probe is running: don't start a second one or block on it
|
|
165
|
+
let value;
|
|
166
|
+
try {
|
|
167
|
+
const os = platform();
|
|
168
|
+
value = os === 'win32'
|
|
169
|
+
? parseWindowsComputerSystem(runSync('powershell.exe', WINDOWS_PROBE))
|
|
170
|
+
: os === 'darwin'
|
|
171
|
+
? (() => { const out = runSync('sysctl', ['-n', 'kern.hv_vmm_present']); return out === null ? null : sysctlSaysVirtual(out); })()
|
|
172
|
+
: linuxProbe();
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
value = null;
|
|
176
|
+
}
|
|
177
|
+
settled = value;
|
|
178
|
+
cached = Promise.resolve(value);
|
|
179
|
+
return value === true;
|
|
180
|
+
}
|
|
181
|
+
/** Whether detection has started in this process (for tests: the probe never starts at import or client construction). */
|
|
182
|
+
export function virtualMachineDetectionStarted() {
|
|
183
|
+
return cached !== null;
|
|
184
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeo-engineering/licenserail",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "LicenseRail client SDK: offline license verification, activation, refresh, floating leases and the management API.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": { "access": "public" },
|
|
7
|
+
"author": "Codeo Solutions Limited",
|
|
8
|
+
"homepage": "https://licenserail.com",
|
|
9
|
+
"repository": { "type": "git", "url": "git+https://github.com/codeo-engineering/licenserail-sdks.git", "directory": "typescript" },
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"exports": { ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" } },
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"engines": { "node": ">=18" },
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc -p tsconfig.json",
|
|
18
|
+
"test": "npm run build && node --test"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^20.11.0",
|
|
22
|
+
"typescript": "^5.6.0"
|
|
23
|
+
}
|
|
24
|
+
}
|