4
0

updateCheck.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package updatecheck
  2. import (
  3. "encoding/json"
  4. config "github.com/OliveTin/OliveTin/internal/config"
  5. "github.com/OliveTin/OliveTin/internal/installationinfo"
  6. "github.com/robfig/cron/v3"
  7. "github.com/Masterminds/semver"
  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. s.AddFunc("@every 100h", func() {
  32. actualCheckForUpdate()
  33. })
  34. go actualCheckForUpdate() // On startup
  35. go s.Start()
  36. }
  37. func parseVersion(input []byte) string {
  38. versionMap := &versionMapType{}
  39. err := json.Unmarshal(input, &versionMap)
  40. if err != nil {
  41. log.Errorf("Update check unmarshal failure: %v", err)
  42. return "none"
  43. } else {
  44. log.Infof("Update check remote version: %+v, latest version: %+v", versionMap.Latest, installationinfo.Build.Version)
  45. if installationinfo.Build.Version == versionMap.Latest {
  46. return "none"
  47. } else {
  48. return parseIfVersionIsLater(installationinfo.Build.Version, versionMap.Latest)
  49. }
  50. }
  51. }
  52. func parseIfVersionIsLater(currentString string, latestString string) string {
  53. currentVersion, errCurrent := semver.NewVersion(currentString)
  54. latestVersion, errLatest := semver.NewVersion(latestString)
  55. if errCurrent != nil || errLatest != nil {
  56. log.Warnf("Version parse failure: %v %v", errCurrent, errLatest)
  57. return "version-parse-failure"
  58. }
  59. if latestVersion.GreaterThan(currentVersion) {
  60. return latestString
  61. }
  62. return "none"
  63. }
  64. func doRequest() string {
  65. req, err := http.NewRequest("GET", "http://update-check.olivetin.app/versions.json", nil)
  66. if err != nil {
  67. log.Errorf("Update check failed %v", err)
  68. return "none"
  69. }
  70. resp, err := http.DefaultClient.Do(req)
  71. if err != nil {
  72. log.Errorf("Update check failed %v", err)
  73. return "none"
  74. }
  75. versionMap, _ := io.ReadAll(resp.Body)
  76. defer resp.Body.Close()
  77. return parseVersion(versionMap)
  78. }
  79. func actualCheckForUpdate() {
  80. if installationinfo.Build.Version == "dev" && os.Getenv("OLIVETIN_FORCE_UPDATE_CHECK") == "" {
  81. installationinfo.Runtime.AvailableVersion = "you-are-using-a-dev-build"
  82. } else {
  83. installationinfo.Runtime.AvailableVersion = doRequest()
  84. }
  85. log.WithFields(log.Fields{
  86. "CurrentVersion": installationinfo.Build.Version,
  87. "NewVersion": installationinfo.Runtime.AvailableVersion,
  88. }).Infof("Update check complete")
  89. }