|
| 1 | +package version |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net" |
| 11 | + "net/http" |
| 12 | + "os" |
| 13 | + "runtime" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +// UpdateInfo contains information about available updates |
| 19 | +type UpdateInfo struct { |
| 20 | + UpdateAvailable bool |
| 21 | + LatestVersion string |
| 22 | + CurrentVersion string |
| 23 | + ReleaseURL string |
| 24 | + Severity string |
| 25 | +} |
| 26 | + |
| 27 | +// VersionResponse represents the API response from the version check endpoint |
| 28 | +type VersionResponse struct { |
| 29 | + CurrentVersion string `json:"current_version"` |
| 30 | + Latest Latest `json:"latest"` |
| 31 | + UpdateAvailable bool `json:"update_available"` |
| 32 | + Severity string `json:"severity"` |
| 33 | + CacheLastUpdate string `json:"cache_last_updated"` |
| 34 | +} |
| 35 | + |
| 36 | +// Latest contains information about the latest release |
| 37 | +type Latest struct { |
| 38 | + Tag string `json:"tag"` |
| 39 | + PublishedAt string `json:"published_at"` |
| 40 | + URL string `json:"url"` |
| 41 | + Notes string `json:"notes"` |
| 42 | + Assets map[string]string `json:"assets"` |
| 43 | +} |
| 44 | + |
| 45 | +// Checker handles version checking in the background |
| 46 | +type Checker struct { |
| 47 | + currentVersion string |
| 48 | + commit string |
| 49 | + updateInfo *UpdateInfo |
| 50 | + checkComplete chan bool |
| 51 | +} |
| 52 | + |
| 53 | +// NewChecker creates a new version checker |
| 54 | +func NewChecker(currentVersion, commit string) *Checker { |
| 55 | + return &Checker{ |
| 56 | + currentVersion: currentVersion, |
| 57 | + commit: commit, |
| 58 | + checkComplete: make(chan bool, 1), |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// CheckInBackground starts a background goroutine to check for updates |
| 63 | +func (c *Checker) CheckInBackground() { |
| 64 | + go func() { |
| 65 | + defer func() { |
| 66 | + // Ensure we always signal completion, even if panic occurs |
| 67 | + select { |
| 68 | + case c.checkComplete <- true: |
| 69 | + default: |
| 70 | + } |
| 71 | + }() |
| 72 | + |
| 73 | + // Don't check for updates in development builds |
| 74 | + if c.currentVersion == "dev" || c.currentVersion == "" { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + // Create context with timeout for the HTTP request |
| 79 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 80 | + defer cancel() |
| 81 | + |
| 82 | + // Get anonymous ID for caching uniqueness |
| 83 | + anonID := getOrCreateAnonID() |
| 84 | + |
| 85 | + // Determine channel (stable for tagged releases, edge for local builds) |
| 86 | + channel := "stable" |
| 87 | + if strings.Contains(c.currentVersion, "-dirty") || strings.Contains(c.currentVersion, "-g") { |
| 88 | + channel = "edge" |
| 89 | + } |
| 90 | + |
| 91 | + // Build the API URL with parameters |
| 92 | + url := fmt.Sprintf( |
| 93 | + "https://gonzo-version.controltheory.com/v1/check?app=gonzo&version=%s&platform=%s&arch=%s&commit=%s&channel=%s&anon_id=%s", |
| 94 | + c.currentVersion, |
| 95 | + runtime.GOOS, |
| 96 | + runtime.GOARCH, |
| 97 | + c.commit, |
| 98 | + channel, |
| 99 | + anonID, |
| 100 | + ) |
| 101 | + |
| 102 | + // Create HTTP request with context |
| 103 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 104 | + if err != nil { |
| 105 | + return // Silently ignore errors |
| 106 | + } |
| 107 | + |
| 108 | + // Make the HTTP request |
| 109 | + client := &http.Client{Timeout: 10 * time.Second} |
| 110 | + resp, err := client.Do(req) |
| 111 | + if err != nil { |
| 112 | + return // Silently ignore errors |
| 113 | + } |
| 114 | + defer resp.Body.Close() |
| 115 | + |
| 116 | + // Read response body |
| 117 | + body, err := io.ReadAll(resp.Body) |
| 118 | + if err != nil { |
| 119 | + return // Silently ignore errors |
| 120 | + } |
| 121 | + |
| 122 | + // Parse JSON response |
| 123 | + var versionResp VersionResponse |
| 124 | + if err := json.Unmarshal(body, &versionResp); err != nil { |
| 125 | + return // Silently ignore errors |
| 126 | + } |
| 127 | + |
| 128 | + // Store update information |
| 129 | + c.updateInfo = &UpdateInfo{ |
| 130 | + UpdateAvailable: versionResp.UpdateAvailable, |
| 131 | + LatestVersion: strings.TrimPrefix(versionResp.Latest.Tag, "v"), |
| 132 | + CurrentVersion: c.currentVersion, |
| 133 | + ReleaseURL: versionResp.Latest.URL, |
| 134 | + Severity: versionResp.Severity, |
| 135 | + } |
| 136 | + }() |
| 137 | +} |
| 138 | + |
| 139 | +// GetUpdateInfo returns the update information if available |
| 140 | +// It waits up to 100ms for the check to complete, then returns whatever is available |
| 141 | +func (c *Checker) GetUpdateInfo() *UpdateInfo { |
| 142 | + select { |
| 143 | + case <-c.checkComplete: |
| 144 | + // Check completed, return result |
| 145 | + return c.updateInfo |
| 146 | + case <-time.After(100 * time.Millisecond): |
| 147 | + // Don't wait too long, return whatever we have |
| 148 | + return c.updateInfo |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// GetUpdateInfoNonBlocking returns the update information without waiting |
| 153 | +func (c *Checker) GetUpdateInfoNonBlocking() *UpdateInfo { |
| 154 | + return c.updateInfo |
| 155 | +} |
| 156 | + |
| 157 | +// getOrCreateAnonID creates a consistent anonymous ID based on machine characteristics |
| 158 | +func getOrCreateAnonID() string { |
| 159 | + // Create a machine-specific but anonymous identifier |
| 160 | + // This will be the same for each machine but doesn't identify the user |
| 161 | + |
| 162 | + hostname, _ := os.Hostname() |
| 163 | + macAddr := getMACAddress() |
| 164 | + osArch := runtime.GOOS + "-" + runtime.GOARCH |
| 165 | + |
| 166 | + // Combine machine characteristics |
| 167 | + machineInfo := hostname + "|" + macAddr + "|" + osArch |
| 168 | + |
| 169 | + // Hash to create anonymous but consistent ID |
| 170 | + h := sha256.Sum256([]byte(machineInfo)) |
| 171 | + return hex.EncodeToString(h[:8]) // Use first 8 bytes for shorter ID |
| 172 | +} |
| 173 | + |
| 174 | +// getMACAddress attempts to get a MAC address from network interfaces |
| 175 | +func getMACAddress() string { |
| 176 | + interfaces, err := net.Interfaces() |
| 177 | + if err != nil { |
| 178 | + return "unknown" |
| 179 | + } |
| 180 | + |
| 181 | + for _, iface := range interfaces { |
| 182 | + // Skip loopback and down interfaces |
| 183 | + if iface.Flags&net.FlagLoopback == 0 && iface.Flags&net.FlagUp != 0 { |
| 184 | + if iface.HardwareAddr != nil { |
| 185 | + return iface.HardwareAddr.String() |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return "unknown" |
| 191 | +} |
0 commit comments