updateCheck.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package updatecheck
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/Masterminds/semver"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. "github.com/OliveTin/OliveTin/internal/installationinfo"
  8. "github.com/robfig/cron/v3"
  9. log "github.com/sirupsen/logrus"
  10. "io"
  11. "net/http"
  12. "os"
  13. "time"
  14. )
  15. type versionMapType struct {
  16. History map[string]string
  17. Latest string
  18. ApiVersion int
  19. }
  20. // StartUpdateChecker will start a job that runs periodically, checking
  21. // for updates.
  22. func StartUpdateChecker(cfg *config.Config) {
  23. if !cfg.CheckForUpdates {
  24. installationinfo.Runtime.AvailableVersion = "none"
  25. log.Infof("Update checking is disabled")
  26. return
  27. }
  28. s := cron.New()
  29. // Several values have been tried here.
  30. // 1st: Every 24h - very spammy.
  31. // 2nd: Every 7d - (168 hours - much more reasonable, but it checks in at the same time/day each week.
  32. // Current: Every 100h is not so spammy, and has the advantage that the checkin time "shifts" hours.
  33. _, err := s.AddFunc("@every 100h", func() {
  34. actualCheckForUpdate()
  35. })
  36. if err != nil {
  37. log.Errorf("update check cron job failed to start: %v", err)
  38. return
  39. }
  40. go actualCheckForUpdate() // On startup
  41. go s.Start()
  42. }
  43. func parseVersion(input []byte) string {
  44. versionMap := &versionMapType{}
  45. err := json.Unmarshal(input, &versionMap)
  46. if err != nil {
  47. log.Errorf("update check unmarshal failure: %v", err)
  48. return "none"
  49. } else {
  50. log.Infof("Update check remote version: %+v, latest version: %+v", versionMap.Latest, installationinfo.Build.Version)
  51. if installationinfo.Build.Version == versionMap.Latest {
  52. return "none"
  53. } else {
  54. return parseIfVersionIsLater(installationinfo.Build.Version, versionMap.Latest)
  55. }
  56. }
  57. }
  58. func parseIfVersionIsLater(currentString string, latestString string) string {
  59. currentVersion, errCurrent := semver.NewVersion(currentString)
  60. latestVersion, errLatest := semver.NewVersion(latestString)
  61. if errCurrent != nil || errLatest != nil {
  62. log.Warnf("Version parse failure: %v %v", errCurrent, errLatest)
  63. return "version-parse-failure"
  64. }
  65. if latestVersion.GreaterThan(currentVersion) {
  66. return latestString
  67. }
  68. return "none"
  69. }
  70. func doRequest() string {
  71. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  72. defer cancel()
  73. req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://update-check.olivetin.app/versions.json", nil)
  74. if err != nil {
  75. log.Errorf("Update check failed %v", err)
  76. return "none"
  77. }
  78. resp, err := http.DefaultClient.Do(req)
  79. if err != nil {
  80. log.Errorf("Update check failed %v", err)
  81. return "none"
  82. }
  83. versionMap, _ := io.ReadAll(resp.Body)
  84. err = resp.Body.Close()
  85. if err != nil {
  86. log.Errorf("Update check failed to close body %v", err)
  87. return "none"
  88. }
  89. return parseVersion(versionMap)
  90. }
  91. func actualCheckForUpdate() {
  92. if installationinfo.Build.Version == "dev" && os.Getenv("OLIVETIN_FORCE_UPDATE_CHECK") == "" {
  93. installationinfo.Runtime.AvailableVersion = "you-are-using-a-dev-build"
  94. } else {
  95. installationinfo.Runtime.AvailableVersion = doRequest()
  96. }
  97. log.WithFields(log.Fields{
  98. "CurrentVersion": installationinfo.Build.Version,
  99. "NewVersion": installationinfo.Runtime.AvailableVersion,
  100. }).Infof("Update check complete")
  101. }