-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows.go
More file actions
361 lines (305 loc) · 10.7 KB
/
windows.go
File metadata and controls
361 lines (305 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//go:build windows
package machineid
import (
"context"
"log/slog"
"strings"
"sync"
)
// componentResult holds the result from a single concurrent component collection.
type componentResult struct {
component string
prefix string
value string // for single-value components
values []string // for multi-value components (MAC, disk)
err error
multi bool // true if this is a multi-value result
}
// collectIdentifiers gathers Windows-specific hardware identifiers concurrently.
// Windows commands (wmic, PowerShell) are slow due to process startup overhead,
// so all components are collected in parallel to minimize total latency.
func collectIdentifiers(ctx context.Context, p *Provider, diag *DiagnosticInfo) ([]string, error) {
logger := p.logger
var wg sync.WaitGroup
resultsCh := make(chan componentResult, 5)
if p.includeCPU {
wg.Add(1)
go func() {
defer wg.Done()
value, err := windowsCPUID(ctx, p.commandExecutor, logger)
resultsCh <- componentResult{component: ComponentCPU, prefix: "cpu:", value: value, err: err}
}()
}
if p.includeMotherboard {
wg.Add(1)
go func() {
defer wg.Done()
value, err := windowsMotherboardSerial(ctx, p.commandExecutor, logger)
resultsCh <- componentResult{component: ComponentMotherboard, prefix: "mb:", value: value, err: err}
}()
}
if p.includeSystemUUID {
wg.Add(1)
go func() {
defer wg.Done()
value, err := windowsSystemUUID(ctx, p.commandExecutor, logger)
resultsCh <- componentResult{component: ComponentSystemUUID, prefix: "uuid:", value: value, err: err}
}()
}
if p.includeMAC {
wg.Add(1)
go func() {
defer wg.Done()
values, err := collectMACAddresses(p.macFilter, logger)
resultsCh <- componentResult{component: ComponentMAC, prefix: "mac:", values: values, err: err, multi: true}
}()
}
if p.includeDisk {
wg.Add(1)
go func() {
defer wg.Done()
values, err := windowsDiskSerials(ctx, p.commandExecutor, logger)
resultsCh <- componentResult{component: ComponentDisk, prefix: "disk:", values: values, err: err, multi: true}
}()
}
// Close channel once all goroutines complete.
go func() {
wg.Wait()
close(resultsCh)
}()
// Collect results and build identifiers.
var identifiers []string
for r := range resultsCh {
if r.multi {
identifiers = appendMultiResult(identifiers, r, diag, logger)
} else {
identifiers = appendSingleResult(identifiers, r, diag, logger)
}
}
return identifiers, nil
}
// appendSingleResult processes a single-value component result into identifiers.
func appendSingleResult(identifiers []string, r componentResult, diag *DiagnosticInfo, logger *slog.Logger) []string {
if r.err != nil {
compErr := &ComponentError{Component: r.component, Err: r.err}
if diag != nil {
diag.Errors[r.component] = compErr
}
if logger != nil {
logger.Warn("component failed", "component", r.component, "error", r.err)
}
return identifiers
}
if r.value == "" {
compErr := &ComponentError{Component: r.component, Err: ErrEmptyValue}
if diag != nil {
diag.Errors[r.component] = compErr
}
if logger != nil {
logger.Warn("component returned empty value", "component", r.component)
}
return identifiers
}
if diag != nil {
diag.Collected = append(diag.Collected, r.component)
}
if logger != nil {
logger.Info("component collected", "component", r.component)
logger.Debug("component value", "component", r.component, "value", r.value)
}
return append(identifiers, r.prefix+r.value)
}
// appendMultiResult processes a multi-value component result into identifiers.
func appendMultiResult(identifiers []string, r componentResult, diag *DiagnosticInfo, logger *slog.Logger) []string {
if r.err != nil {
compErr := &ComponentError{Component: r.component, Err: r.err}
if diag != nil {
diag.Errors[r.component] = compErr
}
if logger != nil {
logger.Warn("component failed", "component", r.component, "error", r.err)
}
return identifiers
}
if len(r.values) == 0 {
compErr := &ComponentError{Component: r.component, Err: ErrNoValues}
if diag != nil {
diag.Errors[r.component] = compErr
}
if logger != nil {
logger.Warn("component returned no values", "component", r.component)
}
return identifiers
}
if diag != nil {
diag.Collected = append(diag.Collected, r.component)
}
if logger != nil {
logger.Info("component collected", "component", r.component, "count", len(r.values))
logger.Debug("component values", "component", r.component, "values", r.values)
}
for _, value := range r.values {
identifiers = append(identifiers, r.prefix+value)
}
return identifiers
}
// parseWmicValue extracts value from wmic output with given prefix.
func parseWmicValue(output, prefix string) (string, error) {
lines := strings.SplitSeq(output, "\n")
for line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, prefix) {
value := strings.TrimSpace(strings.TrimPrefix(line, prefix))
if value == "" || value == biosFirmwareMessage {
continue
}
return value, nil
}
}
return "", &ParseError{Source: "wmic output", Err: ErrNotFound}
}
// parseWmicMultipleValues extracts all values from wmic output with given prefix.
func parseWmicMultipleValues(output, prefix string) []string {
var values []string
lines := strings.SplitSeq(output, "\n")
for line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, prefix) {
value := strings.TrimSpace(strings.TrimPrefix(line, prefix))
if value == "" || value == biosFirmwareMessage {
continue
}
values = append(values, value)
}
}
return values
}
// parsePowerShellValue extracts a trimmed, non-empty value from PowerShell output.
// OEM placeholder strings (see biosFirmwareMessage) are rejected with ErrOEMPlaceholder.
func parsePowerShellValue(output string) (string, error) {
value := strings.TrimSpace(output)
if value == "" {
return "", &ParseError{Source: "PowerShell output", Err: ErrEmptyValue}
}
if value == biosFirmwareMessage {
return "", &ParseError{Source: "PowerShell output", Err: ErrOEMPlaceholder}
}
return value, nil
}
// parsePowerShellMultipleValues extracts multiple trimmed, non-empty values from PowerShell output.
// OEM placeholder lines (see biosFirmwareMessage) are filtered out.
func parsePowerShellMultipleValues(output string) []string {
var values []string
lines := strings.SplitSeq(output, "\n")
for line := range lines {
value := strings.TrimSpace(line)
if value == "" || value == biosFirmwareMessage {
continue
}
values = append(values, value)
}
return values
}
// windowsCPUID retrieves CPU processor ID using wmic, with PowerShell fallback.
func windowsCPUID(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
output, err := executeCommand(ctx, executor, logger, "wmic", "cpu", "get", "ProcessorId", "/value")
if err == nil {
if value, parseErr := parseWmicValue(output, "ProcessorId="); parseErr == nil {
return value, nil
} else if logger != nil {
logger.Debug("wmic CPU ID parsing failed", "error", parseErr)
}
}
// Fallback to PowerShell Get-CimInstance
if logger != nil {
logger.Info("falling back to PowerShell for CPU ID")
}
psOutput, psErr := executeCommand(ctx, executor, logger, "powershell", "-Command",
"Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty ProcessorId")
if psErr != nil {
if logger != nil {
logger.Warn("all CPU ID methods failed")
}
return "", ErrAllMethodsFailed
}
return parsePowerShellValue(psOutput)
}
// windowsMotherboardSerial retrieves motherboard serial number using wmic, with PowerShell fallback.
func windowsMotherboardSerial(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
output, err := executeCommand(ctx, executor, logger, "wmic", "baseboard", "get", "SerialNumber", "/value")
if err == nil {
if value, parseErr := parseWmicValue(output, "SerialNumber="); parseErr == nil {
return value, nil
} else if logger != nil {
logger.Debug("wmic motherboard serial parsing failed", "error", parseErr)
}
}
// Fallback to PowerShell Get-CimInstance
if logger != nil {
logger.Info("falling back to PowerShell for motherboard serial")
}
psOutput, psErr := executeCommand(ctx, executor, logger, "powershell", "-Command",
"Get-CimInstance -ClassName Win32_BaseBoard | Select-Object -ExpandProperty SerialNumber")
if psErr != nil {
if logger != nil {
logger.Warn("all motherboard serial methods failed")
}
return "", ErrAllMethodsFailed
}
return parsePowerShellValue(psOutput)
}
// windowsSystemUUID retrieves system UUID using wmic or PowerShell.
func windowsSystemUUID(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
// Try wmic first
output, err := executeCommand(ctx, executor, logger, "wmic", "csproduct", "get", "UUID", "/value")
if err == nil {
if value, parseErr := parseWmicValue(output, "UUID="); parseErr == nil {
return value, nil
} else if logger != nil {
logger.Debug("wmic UUID parsing failed", "error", parseErr)
}
}
// Fallback to PowerShell
if logger != nil {
logger.Info("falling back to PowerShell for system UUID")
}
return windowsSystemUUIDViaPowerShell(ctx, executor, logger)
}
// windowsSystemUUIDViaPowerShell retrieves system UUID using PowerShell.
func windowsSystemUUIDViaPowerShell(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
output, err := executeCommand(ctx, executor, logger, "powershell", "-Command",
"Get-CimInstance -ClassName Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID")
if err != nil {
return "", err
}
return parsePowerShellValue(output)
}
// windowsDiskSerials retrieves disk serial numbers using wmic, with PowerShell fallback.
func windowsDiskSerials(ctx context.Context, executor CommandExecutor, logger *slog.Logger) ([]string, error) {
output, err := executeCommand(ctx, executor, logger, "wmic", "diskdrive", "get", "SerialNumber", "/value")
if err == nil {
if values := parseWmicMultipleValues(output, "SerialNumber="); len(values) > 0 {
return values, nil
}
if logger != nil {
logger.Debug("wmic returned no disk serials")
}
}
// Fallback to PowerShell Get-CimInstance
if logger != nil {
logger.Info("falling back to PowerShell for disk serials")
}
psOutput, psErr := executeCommand(ctx, executor, logger, "powershell", "-Command",
"Get-CimInstance -ClassName Win32_DiskDrive | Select-Object -ExpandProperty SerialNumber")
if psErr != nil {
if logger != nil {
logger.Warn("all disk serial methods failed")
}
return nil, ErrAllMethodsFailed
}
values := parsePowerShellMultipleValues(psOutput)
if len(values) == 0 {
return nil, &ParseError{Source: "PowerShell output", Err: ErrNotFound}
}
return values, nil
}