Including system metadata when posting to utility API

This commit is contained in:
Cuong Manh Le
2025-12-10 17:43:57 +07:00
committed by Cuong Manh Le
parent 8b92dc97a3
commit ba3dd3a4b0
20 changed files with 374 additions and 119 deletions
+25
View File
@@ -0,0 +1,25 @@
package system
import (
"errors"
"fmt"
"github.com/brunogui0812/sysprofiler"
)
// GetChassisInfo retrieves hardware information including machine model type and vendor from the system profiler.
func GetChassisInfo() (*ChassisInfo, error) {
hardwares, err := sysprofiler.Hardware()
if err != nil {
return nil, fmt.Errorf("failed to get hardware info: %w", err)
}
if len(hardwares) == 0 {
return nil, errors.New("no hardware info found")
}
hardware := hardwares[0]
info := &ChassisInfo{
Type: hardware.MachineModel,
Vendor: "Apple Inc.",
}
return info, nil
}
+18
View File
@@ -0,0 +1,18 @@
//go:build !darwin
package system
import "github.com/jaypipes/ghw"
// GetChassisInfo retrieves hardware information including machine model type and vendor from the system profiler.
func GetChassisInfo() (*ChassisInfo, error) {
chassis, err := ghw.Chassis()
if err != nil {
return nil, err
}
info := &ChassisInfo{
Type: chassis.TypeDescription,
Vendor: chassis.Vendor,
}
return info, nil
}
+7
View File
@@ -0,0 +1,7 @@
package system
// ChassisInfo represents the structural framework of a device, specifying its type and manufacturer information.
type ChassisInfo struct {
Type string
Vendor string
}
+8
View File
@@ -0,0 +1,8 @@
//go:build !windows
package system
// GetActiveDirectoryDomain returns AD domain name of this computer.
func GetActiveDirectoryDomain() (string, error) {
return "", nil
}
+74
View File
@@ -0,0 +1,74 @@
package system
import (
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"unsafe"
"github.com/microsoft/wmi/pkg/base/host"
hh "github.com/microsoft/wmi/pkg/hardware/host"
"golang.org/x/sys/windows"
)
// GetActiveDirectoryDomain returns AD domain name of this computer.
func GetActiveDirectoryDomain() (string, error) {
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stderr)
// 1) Check environment variable
envDomain := os.Getenv("USERDNSDOMAIN")
if envDomain != "" {
return strings.TrimSpace(envDomain), nil
}
// 2) Query WMI via the microsoft/wmi library
whost := host.NewWmiLocalHost()
cs, err := hh.GetComputerSystem(whost)
if cs != nil {
defer cs.Close()
}
if err != nil {
return "", err
}
pod, err := cs.GetPropertyPartOfDomain()
if err != nil {
return "", err
}
if pod {
domainVal, err := cs.GetPropertyDomain()
if err != nil {
return "", fmt.Errorf("failed to get domain property: %w", err)
}
domainName := strings.TrimSpace(fmt.Sprintf("%v", domainVal))
if domainName == "" {
return "", errors.New("machine does not appear to have a domain set")
}
return domainName, nil
}
return "", nil
}
// DomainJoinedStatus returns the domain joined status of the current computer.
//
// NETSETUP_JOIN_STATUS constants from Microsoft Windows API
// See: https://learn.microsoft.com/en-us/windows/win32/api/lmjoin/ne-lmjoin-netsetup_join_status
//
// NetSetupUnknownStatus uint32 = 0 // The status is unknown
// NetSetupUnjoined uint32 = 1 // The computer is not joined to a domain or workgroup
// NetSetupWorkgroupName uint32 = 2 // The computer is joined to a workgroup
// NetSetupDomainName uint32 = 3 // The computer is joined to a domain
func DomainJoinedStatus() (uint32, error) {
var domain *uint16
var status uint32
if err := windows.NetGetJoinInformation(nil, &domain, &status); err != nil {
return 0, fmt.Errorf("failed to get domain join status: %w", err)
}
defer windows.NetApiBufferFree((*byte)(unsafe.Pointer(domain)))
return status, nil
}