|
| 1 | +package updatemanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | + |
| 15 | + v "github.com/hashicorp/go-version" |
| 16 | + log "github.com/sirupsen/logrus" |
| 17 | + |
| 18 | + "github.com/netbirdio/netbird/client/internal/peer" |
| 19 | + cProto "github.com/netbirdio/netbird/client/proto" |
| 20 | + "github.com/netbirdio/netbird/version" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + latestVersion = "latest" |
| 25 | + disableAutoUpdate = "disabled" |
| 26 | + unknownVersion = "Unknown" |
| 27 | +) |
| 28 | + |
| 29 | +type UpdateManager struct { |
| 30 | + ctx context.Context |
| 31 | + cancel context.CancelFunc |
| 32 | + version string |
| 33 | + latestVersion string |
| 34 | + update *version.Update |
| 35 | + lastTrigger time.Time |
| 36 | + statusRecorder *peer.Status |
| 37 | + mutex sync.Mutex |
| 38 | + waitGroup sync.WaitGroup |
| 39 | +} |
| 40 | + |
| 41 | +func NewUpdateManager(ctx context.Context, statusRecorder *peer.Status) *UpdateManager { |
| 42 | + update := version.NewUpdate("nb/client") |
| 43 | + ctx, cancel := context.WithCancel(ctx) |
| 44 | + manager := &UpdateManager{ |
| 45 | + update: update, |
| 46 | + lastTrigger: time.Now().Add(-10 * time.Minute), |
| 47 | + statusRecorder: statusRecorder, |
| 48 | + ctx: ctx, |
| 49 | + cancel: cancel, |
| 50 | + version: disableAutoUpdate, |
| 51 | + latestVersion: unknownVersion, |
| 52 | + } |
| 53 | + update.SetDaemonVersion(version.NetbirdVersion()) |
| 54 | + update.SetOnUpdateListener(manager.Updated) |
| 55 | + return manager |
| 56 | +} |
| 57 | + |
| 58 | +func (u *UpdateManager) SetVersion(v string) { |
| 59 | + u.mutex.Lock() |
| 60 | + if u.version != v { |
| 61 | + log.Tracef("Auto-update version set to %s", v) |
| 62 | + u.version = v |
| 63 | + u.mutex.Unlock() |
| 64 | + go u.Updated("N/A") |
| 65 | + } else { |
| 66 | + u.mutex.Unlock() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (u *UpdateManager) Stop() { |
| 71 | + u.update.StopWatch() |
| 72 | + u.cancel() |
| 73 | + u.waitGroup.Wait() |
| 74 | +} |
| 75 | + |
| 76 | +func (u *UpdateManager) Updated(latestVersion string) { |
| 77 | + u.waitGroup.Add(1) |
| 78 | + defer u.waitGroup.Done() |
| 79 | + u.mutex.Lock() |
| 80 | + defer u.mutex.Unlock() |
| 81 | + select { |
| 82 | + case <-u.ctx.Done(): |
| 83 | + return |
| 84 | + default: |
| 85 | + } |
| 86 | + if latestVersion != "N/A" { |
| 87 | + u.latestVersion = latestVersion |
| 88 | + } |
| 89 | + ctx, cancel := context.WithDeadline(u.ctx, time.Now().Add(time.Minute)) |
| 90 | + defer cancel() |
| 91 | + u.CheckForUpdates(ctx) |
| 92 | +} |
| 93 | + |
| 94 | +func (u *UpdateManager) CheckForUpdates(ctx context.Context) { |
| 95 | + if u.version == disableAutoUpdate { |
| 96 | + log.Trace("Skipped checking for updates, auto-update is disabled") |
| 97 | + return |
| 98 | + } |
| 99 | + currentVersionString := version.NetbirdVersion() |
| 100 | + updateVersionString := u.version |
| 101 | + if updateVersionString == latestVersion || updateVersionString == "" { |
| 102 | + if u.latestVersion == unknownVersion { |
| 103 | + log.Tracef("Latest version not fetched yet") |
| 104 | + return |
| 105 | + } |
| 106 | + updateVersionString = u.latestVersion |
| 107 | + } |
| 108 | + currentVersion, err := v.NewVersion(currentVersionString) |
| 109 | + if err != nil { |
| 110 | + log.Errorf("Error checking for update, error parsing version `%s`: %v", currentVersionString, err) |
| 111 | + return |
| 112 | + } |
| 113 | + updateVersion, err := v.NewVersion(updateVersionString) |
| 114 | + if err != nil { |
| 115 | + log.Errorf("Error checking for update, error parsing version `%s`: %v", updateVersionString, err) |
| 116 | + return |
| 117 | + } |
| 118 | + if currentVersion.LessThan(updateVersion) { |
| 119 | + if u.lastTrigger.Add(5 * time.Minute).Before(time.Now()) { |
| 120 | + u.lastTrigger = time.Now() |
| 121 | + log.Debugf("Auto-update triggered, current version: %s, target version: %s", currentVersionString, updateVersionString) |
| 122 | + u.statusRecorder.PublishEvent( |
| 123 | + cProto.SystemEvent_INFO, |
| 124 | + cProto.SystemEvent_SYSTEM, |
| 125 | + "Automatically updating client", |
| 126 | + "Your client version is older than auto-update version set in Management, updating client now.", |
| 127 | + nil, |
| 128 | + ) |
| 129 | + err = u.triggerUpdate(ctx, updateVersionString) |
| 130 | + if err != nil { |
| 131 | + log.Errorf("Error triggering auto-update: %v", err) |
| 132 | + } |
| 133 | + } |
| 134 | + } else { |
| 135 | + log.Debugf("Current version (%s) is equal to or higher than auto-update version (%s)", currentVersionString, updateVersionString) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func downloadFileToTemporaryDir(ctx context.Context, fileURL string) (string, error) { //nolint:unused |
| 140 | + tempDir, err := os.MkdirTemp("", "netbird-installer-*") |
| 141 | + if err != nil { |
| 142 | + return "", fmt.Errorf("error creating temporary directory: %w", err) |
| 143 | + } |
| 144 | + fileNameParts := strings.Split(fileURL, "/") |
| 145 | + out, err := os.Create(filepath.Join(tempDir, fileNameParts[len(fileNameParts)-1])) |
| 146 | + if err != nil { |
| 147 | + return "", fmt.Errorf("error creating temporary file: %w", err) |
| 148 | + } |
| 149 | + defer func() { |
| 150 | + if err := out.Close(); err != nil { |
| 151 | + log.Errorf("Error closing temporary file: %v", err) |
| 152 | + } |
| 153 | + }() |
| 154 | + |
| 155 | + req, err := http.NewRequestWithContext(ctx, "GET", fileURL, nil) |
| 156 | + if err != nil { |
| 157 | + return "", fmt.Errorf("error creating file download request: %w", err) |
| 158 | + } |
| 159 | + resp, err := http.DefaultClient.Do(req) |
| 160 | + if err != nil { |
| 161 | + return "", fmt.Errorf("error downloading file: %w", err) |
| 162 | + } |
| 163 | + defer func() { |
| 164 | + if err := resp.Body.Close(); err != nil { |
| 165 | + log.Errorf("Error closing response body: %v", err) |
| 166 | + } |
| 167 | + }() |
| 168 | + |
| 169 | + _, err = io.Copy(out, resp.Body) |
| 170 | + if err != nil { |
| 171 | + return "", fmt.Errorf("error downloading file: %w", err) |
| 172 | + } |
| 173 | + |
| 174 | + log.Tracef("Downloaded update file to %s", out.Name()) |
| 175 | + |
| 176 | + return out.Name(), nil |
| 177 | +} |
| 178 | + |
| 179 | +func urlWithVersionArch(url, version string) string { //nolint:unused |
| 180 | + url = strings.ReplaceAll(url, "%version", version) |
| 181 | + url = strings.ReplaceAll(url, "%arch", runtime.GOARCH) |
| 182 | + return url |
| 183 | +} |
0 commit comments