| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package updatecheck
- import (
- "encoding/json"
- "github.com/Masterminds/semver"
- config "github.com/OliveTin/OliveTin/internal/config"
- "github.com/OliveTin/OliveTin/internal/installationinfo"
- "github.com/robfig/cron/v3"
- log "github.com/sirupsen/logrus"
- "io"
- "net/http"
- "os"
- )
- type versionMapType struct {
- ApiVersion int
- Latest string
- History map[string]string
- }
- // StartUpdateChecker will start a job that runs periodically, checking
- // for updates.
- func StartUpdateChecker(cfg *config.Config) {
- if !cfg.CheckForUpdates {
- installationinfo.Runtime.AvailableVersion = "none"
- log.Infof("Update checking is disabled")
- return
- }
- s := cron.New()
- // Several values have been tried here.
- // 1st: Every 24h - very spammy.
- // 2nd: Every 7d - (168 hours - much more reasonable, but it checks in at the same time/day each week.
- // Current: Every 100h is not so spammy, and has the advantage that the checkin time "shifts" hours.
- _, err := s.AddFunc("@every 100h", func() {
- actualCheckForUpdate()
- })
- if err != nil {
- log.Errorf("update check cron job failed to start: %v", err)
- return
- }
- go actualCheckForUpdate() // On startup
- go s.Start()
- }
- func parseVersion(input []byte) string {
- versionMap := &versionMapType{}
- err := json.Unmarshal(input, &versionMap)
- if err != nil {
- log.Errorf("update check unmarshal failure: %v", err)
- return "none"
- } else {
- log.Infof("Update check remote version: %+v, latest version: %+v", versionMap.Latest, installationinfo.Build.Version)
- if installationinfo.Build.Version == versionMap.Latest {
- return "none"
- } else {
- return parseIfVersionIsLater(installationinfo.Build.Version, versionMap.Latest)
- }
- }
- }
- func parseIfVersionIsLater(currentString string, latestString string) string {
- currentVersion, errCurrent := semver.NewVersion(currentString)
- latestVersion, errLatest := semver.NewVersion(latestString)
- if errCurrent != nil || errLatest != nil {
- log.Warnf("Version parse failure: %v %v", errCurrent, errLatest)
- return "version-parse-failure"
- }
- if latestVersion.GreaterThan(currentVersion) {
- return latestString
- }
- return "none"
- }
- func doRequest() string {
- req, err := http.NewRequest("GET", "http://update-check.olivetin.app/versions.json", nil)
- if err != nil {
- log.Errorf("Update check failed %v", err)
- return "none"
- }
- resp, err := http.DefaultClient.Do(req)
- if err != nil {
- log.Errorf("Update check failed %v", err)
- return "none"
- }
- versionMap, _ := io.ReadAll(resp.Body)
- err = resp.Body.Close()
- if err != nil {
- log.Errorf("Update check failed to close body %v", err)
- return "none"
- }
- return parseVersion(versionMap)
- }
- func actualCheckForUpdate() {
- if installationinfo.Build.Version == "dev" && os.Getenv("OLIVETIN_FORCE_UPDATE_CHECK") == "" {
- installationinfo.Runtime.AvailableVersion = "you-are-using-a-dev-build"
- } else {
- installationinfo.Runtime.AvailableVersion = doRequest()
- }
- log.WithFields(log.Fields{
- "CurrentVersion": installationinfo.Build.Version,
- "NewVersion": installationinfo.Runtime.AvailableVersion,
- }).Infof("Update check complete")
- }
|