diff --git a/internal/event/event.go b/internal/event/event.go index 42272c7035638fee7167b5c3510c7975cb9c9394..ca02c6d89d67be1756b166aea152da165b2712c9 100644 --- a/internal/event/event.go +++ b/internal/event/event.go @@ -9,7 +9,6 @@ import ( "runtime" "github.com/charmbracelet/crush/internal/version" - "github.com/denisbrodbeck/machineid" "github.com/posthog/posthog-go" ) @@ -39,6 +38,7 @@ func Init() { slog.Error("Failed to initialize PostHog client", "error", err) } client = c + distinctId = getDistinctId() } // send logs an event to PostHog with the given event name and properties. @@ -47,7 +47,7 @@ func send(event string, props ...any) { return } err := client.Enqueue(posthog.Capture{ - DistinctId: distinctId(), + DistinctId: distinctId, Event: event, Properties: pairsToProps(props...).Merge(baseProps), }) @@ -105,11 +105,3 @@ func pairsToProps(props ...any) posthog.Properties { func isEven(n int) bool { return n%2 == 0 } - -func distinctId() string { - id, err := machineid.ProtectedID("charm") - if err != nil { - return "crush-cli" - } - return id -} diff --git a/internal/event/identifier.go b/internal/event/identifier.go new file mode 100644 index 0000000000000000000000000000000000000000..ee05f8f58f6dd9a8f662e94992983ce26a94d9b9 --- /dev/null +++ b/internal/event/identifier.go @@ -0,0 +1,49 @@ +package event + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "fmt" + "net" + + "github.com/denisbrodbeck/machineid" +) + +var distinctId string + +const ( + hashKey = "charm" + fallbackId = "unknown" +) + +func getDistinctId() string { + if id, err := machineid.ProtectedID(hashKey); err == nil { + return id + } + if macAddr, err := getMacAddr(); err == nil { + return hashString(macAddr) + } + return fallbackId +} + +func getMacAddr() (string, error) { + interfaces, err := net.Interfaces() + if err != nil { + return "", err + } + for _, iface := range interfaces { + if iface.Flags&net.FlagUp != 0 && iface.Flags&net.FlagLoopback == 0 && len(iface.HardwareAddr) > 0 { + if addrs, err := iface.Addrs(); err == nil && len(addrs) > 0 { + return iface.HardwareAddr.String(), nil + } + } + } + return "", fmt.Errorf("no active interface with mac address found") +} + +func hashString(str string) string { + hash := hmac.New(sha256.New, []byte(str)) + hash.Write([]byte(hashKey)) + return hex.EncodeToString(hash.Sum(nil)) +}