|
| 1 | +package machine |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/md5" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +type MachineInfo struct { |
| 15 | + Hostname string `json:"hostname"` |
| 16 | + MACAddresses []string `json:"mac_addresses"` |
| 17 | + OSType string `json:"os_type"` |
| 18 | + OSRelease string `json:"os_release"` |
| 19 | + CPUInfo string `json:"cpu_info"` |
| 20 | +} |
| 21 | + |
| 22 | +func GetMachineInfo() (*MachineInfo, error) { |
| 23 | + hostname, err := os.Hostname() |
| 24 | + if err != nil { |
| 25 | + return nil, fmt.Errorf("failed to get hostname: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + macAddresses, err := getMACAddresses() |
| 29 | + if err != nil { |
| 30 | + return nil, fmt.Errorf("failed to get MAC addresses: %w", err) |
| 31 | + } |
| 32 | + |
| 33 | + return &MachineInfo{ |
| 34 | + Hostname: hostname, |
| 35 | + MACAddresses: macAddresses, |
| 36 | + OSType: runtime.GOOS, |
| 37 | + OSRelease: getOSRelease(), |
| 38 | + CPUInfo: getCPUInfo(), |
| 39 | + }, nil |
| 40 | +} |
| 41 | + |
| 42 | +func GenerateMachineID() (string, error) { |
| 43 | + machineInfo, err := GetMachineInfo() |
| 44 | + if err != nil { |
| 45 | + return "", err |
| 46 | + } |
| 47 | + |
| 48 | + var parts []string |
| 49 | + parts = append(parts, machineInfo.Hostname) |
| 50 | + parts = append(parts, strings.Join(machineInfo.MACAddresses, ",")) |
| 51 | + parts = append(parts, machineInfo.OSType) |
| 52 | + parts = append(parts, machineInfo.OSRelease) |
| 53 | + parts = append(parts, machineInfo.CPUInfo) |
| 54 | + |
| 55 | + combined := strings.Join(parts, "|") |
| 56 | + hash := sha256.Sum256([]byte(combined)) |
| 57 | + return fmt.Sprintf("%x", hash), nil |
| 58 | +} |
| 59 | + |
| 60 | +func GenerateShortMachineID() (string, error) { |
| 61 | + machineInfo, err := GetMachineInfo() |
| 62 | + if err != nil { |
| 63 | + return "", err |
| 64 | + } |
| 65 | + |
| 66 | + var parts []string |
| 67 | + parts = append(parts, machineInfo.Hostname) |
| 68 | + if len(machineInfo.MACAddresses) > 0 { |
| 69 | + parts = append(parts, machineInfo.MACAddresses[0]) |
| 70 | + } |
| 71 | + parts = append(parts, machineInfo.OSType) |
| 72 | + |
| 73 | + combined := strings.Join(parts, "|") |
| 74 | + hash := md5.Sum([]byte(combined)) |
| 75 | + return fmt.Sprintf("%x", hash), nil |
| 76 | +} |
| 77 | + |
| 78 | +func getMACAddresses() ([]string, error) { |
| 79 | + interfaces, err := net.Interfaces() |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + var macAddresses []string |
| 85 | + for _, iface := range interfaces { |
| 86 | + if iface.Flags&net.FlagLoopback != 0 || len(iface.HardwareAddr) == 0 { |
| 87 | + continue |
| 88 | + } |
| 89 | + macAddresses = append(macAddresses, iface.HardwareAddr.String()) |
| 90 | + } |
| 91 | + |
| 92 | + sort.Strings(macAddresses) |
| 93 | + return macAddresses, nil |
| 94 | +} |
| 95 | + |
| 96 | +func getOSRelease() string { |
| 97 | + return runtime.GOARCH |
| 98 | +} |
| 99 | + |
| 100 | +func getCPUInfo() string { |
| 101 | + return fmt.Sprintf("%s_%d", runtime.GOARCH, runtime.NumCPU()) |
| 102 | +} |
0 commit comments