updateCheck.go 3.4 KB

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