|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
4 | 6 | "fmt" |
| 7 | + "net" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "strings" |
5 | 11 | "time" |
6 | 12 |
|
7 | 13 | "github.com/fsnotify/fsnotify" |
8 | 14 | "github.com/segmentio/ksuid" |
9 | 15 | ) |
10 | 16 |
|
| 17 | +type HostInfo struct { |
| 18 | + Hostname string |
| 19 | + OS string |
| 20 | + Architecture string |
| 21 | + IPs []string |
| 22 | + MAC string |
| 23 | +} |
| 24 | + |
11 | 25 | func watchPaths(paths ...string) { |
12 | 26 | if len(paths) < 1 { |
13 | 27 | log.Fatal().Msg("must specify at least one path to watch") |
@@ -95,3 +109,72 @@ func processEvent(e fsnotify.Event) { |
95 | 109 | log.Error().Msgf("Policy not found in cache, watcher event [%s] didn't trigger policy process for: %s", e.Op.String(), e.Name) |
96 | 110 | } |
97 | 111 | } |
| 112 | + |
| 113 | +func GetHostInfo() (*HostInfo, error) { |
| 114 | + hostInfo := &HostInfo{} |
| 115 | + |
| 116 | + // Get hostname |
| 117 | + hostname, err := os.Hostname() |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("failed to get hostname: %v", err) |
| 120 | + } |
| 121 | + hostInfo.Hostname = hostname |
| 122 | + |
| 123 | + // Get OS and architecture |
| 124 | + hostInfo.OS = runtime.GOOS |
| 125 | + hostInfo.Architecture = runtime.GOARCH |
| 126 | + |
| 127 | + // Get IPs and MAC addresses |
| 128 | + interfaces, err := net.Interfaces() |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("failed to get network interfaces: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + for _, iface := range interfaces { |
| 134 | + if iface.Flags&net.FlagUp == 0 { |
| 135 | + continue // ignore interfaces that are down |
| 136 | + } |
| 137 | + |
| 138 | + addrs, err := iface.Addrs() |
| 139 | + if err != nil { |
| 140 | + return nil, fmt.Errorf("failed to get addresses for interface %v: %v", iface.Name, err) |
| 141 | + } |
| 142 | + |
| 143 | + for _, addr := range addrs { |
| 144 | + ip, _, err := net.ParseCIDR(addr.String()) |
| 145 | + if err != nil { |
| 146 | + return nil, fmt.Errorf("failed to parse IP address %v: %v", addr.String(), err) |
| 147 | + } |
| 148 | + |
| 149 | + if ip.IsLoopback() { |
| 150 | + continue // ignore loopback addresses |
| 151 | + } |
| 152 | + |
| 153 | + hostInfo.IPs = append(hostInfo.IPs, ip.String()) |
| 154 | + } |
| 155 | + // main MAC |
| 156 | + if iface.Flags&net.FlagUp != 0 && iface.HardwareAddr.String() != "" { |
| 157 | + hostInfo.MAC = iface.HardwareAddr.String() |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + return hostInfo, nil |
| 163 | +} |
| 164 | + |
| 165 | +// FingerprintHost generates a fingerprint for the host using its identifiable information |
| 166 | +func FingerprintHost(hostInfo *HostInfo) (string, string, error) { |
| 167 | + data := strings.Join([]string{ |
| 168 | + hostInfo.MAC, |
| 169 | + hostInfo.OS, |
| 170 | + hostInfo.Architecture, |
| 171 | + hostInfo.Hostname, |
| 172 | + }, "|") |
| 173 | + hash := sha256.New() |
| 174 | + _, err := hash.Write([]byte(data)) |
| 175 | + if err != nil { |
| 176 | + return "", "", fmt.Errorf("failed to generate hash: %v", err) |
| 177 | + } |
| 178 | + fingerprint := hex.EncodeToString(hash.Sum(nil)) |
| 179 | + return data, fingerprint, nil |
| 180 | +} |
0 commit comments