4
0

updateCheck.go 3.0 KB

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