|
@@ -1,11 +1,9 @@
|
|
|
package updatecheck
|
|
package updatecheck
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
- "bytes"
|
|
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
config "github.com/OliveTin/OliveTin/internal/config"
|
|
config "github.com/OliveTin/OliveTin/internal/config"
|
|
|
- installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo"
|
|
|
|
|
- "github.com/google/uuid"
|
|
|
|
|
|
|
+ "github.com/OliveTin/OliveTin/internal/installationinfo"
|
|
|
"github.com/robfig/cron/v3"
|
|
"github.com/robfig/cron/v3"
|
|
|
log "github.com/sirupsen/logrus"
|
|
log "github.com/sirupsen/logrus"
|
|
|
"io"
|
|
"io"
|
|
@@ -13,99 +11,63 @@ import (
|
|
|
"os"
|
|
"os"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-type updateRequest struct {
|
|
|
|
|
- CurrentVersion string
|
|
|
|
|
- CurrentCommit string
|
|
|
|
|
- OS string
|
|
|
|
|
- Arch string
|
|
|
|
|
- InstallationID string
|
|
|
|
|
- InContainer bool
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// AvailableVersion is updated when checking with the update service.
|
|
|
|
|
-var AvailableVersion = "none"
|
|
|
|
|
-
|
|
|
|
|
-// CurrentVersion is set by the main cmd (which is in tern set as a compile constant)
|
|
|
|
|
-var CurrentVersion = "?"
|
|
|
|
|
-
|
|
|
|
|
-func installationID(filename string) string {
|
|
|
|
|
- var content string
|
|
|
|
|
- contentBytes, err := os.ReadFile(filename)
|
|
|
|
|
-
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- fileHandle, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
|
|
|
|
|
-
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Warnf("Could not read + create installation ID file: %v", err)
|
|
|
|
|
- return "cant-create"
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- content = uuid.NewString()
|
|
|
|
|
- fileHandle.WriteString(content)
|
|
|
|
|
- fileHandle.Close()
|
|
|
|
|
- } else {
|
|
|
|
|
- content = string(contentBytes)
|
|
|
|
|
-
|
|
|
|
|
- _, err := uuid.Parse(content)
|
|
|
|
|
-
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Errorf("Invalid installation ID, %v", err)
|
|
|
|
|
- content = "invalid-installation-id"
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- log.WithFields(log.Fields{
|
|
|
|
|
- "content": content,
|
|
|
|
|
- "from": filename,
|
|
|
|
|
- }).Infof("Installation ID")
|
|
|
|
|
-
|
|
|
|
|
- return content
|
|
|
|
|
|
|
+type versionMapType struct {
|
|
|
|
|
+ ApiVersion int
|
|
|
|
|
+ Latest string
|
|
|
|
|
+ History map[string]string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// StartUpdateChecker will start a job that runs periodically, checking
|
|
// StartUpdateChecker will start a job that runs periodically, checking
|
|
|
// for updates.
|
|
// for updates.
|
|
|
-func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config, configDir string) {
|
|
|
|
|
- CurrentVersion = currentVersion
|
|
|
|
|
-
|
|
|
|
|
|
|
+func StartUpdateChecker(cfg *config.Config) {
|
|
|
if !cfg.CheckForUpdates {
|
|
if !cfg.CheckForUpdates {
|
|
|
|
|
+ installationinfo.Runtime.AvailableVersion = "none"
|
|
|
log.Warn("Update checking is disabled")
|
|
log.Warn("Update checking is disabled")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- payload := updateRequest{
|
|
|
|
|
- CurrentVersion: currentVersion,
|
|
|
|
|
- CurrentCommit: currentCommit,
|
|
|
|
|
- OS: installationinfo.Runtime.OS,
|
|
|
|
|
- Arch: installationinfo.Runtime.Arch,
|
|
|
|
|
- InstallationID: installationID(configDir + "/installation-id.txt"),
|
|
|
|
|
- InContainer: installationinfo.Runtime.InContainer,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- s := cron.New(cron.WithSeconds())
|
|
|
|
|
|
|
+ s := cron.New()
|
|
|
|
|
|
|
|
// Several values have been tried here.
|
|
// Several values have been tried here.
|
|
|
// 1st: Every 24h - very spammy.
|
|
// 1st: Every 24h - very spammy.
|
|
|
// 2nd: Every 7d - (168 hours - much more reasonable, but it checks in at the same time/day each week.
|
|
// 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.
|
|
// Current: Every 100h is not so spammy, and has the advantage that the checkin time "shifts" hours.
|
|
|
s.AddFunc("@every 100h", func() {
|
|
s.AddFunc("@every 100h", func() {
|
|
|
- actualCheckForUpdate(payload)
|
|
|
|
|
|
|
+ actualCheckForUpdate()
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- go actualCheckForUpdate(payload) // On startup
|
|
|
|
|
|
|
+ go actualCheckForUpdate() // On startup
|
|
|
|
|
|
|
|
go s.Start()
|
|
go s.Start()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func doRequest(jsonUpdateRequest []byte) string {
|
|
|
|
|
- req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewBuffer(jsonUpdateRequest))
|
|
|
|
|
|
|
+func parseVersion(input []byte) string {
|
|
|
|
|
+ versionMap := &versionMapType{}
|
|
|
|
|
+
|
|
|
|
|
+ err := json.Unmarshal(input, &versionMap)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Warnf("Update check unmarshal failure: %v", err)
|
|
|
|
|
+ return "error-during-check"
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.Infof("Update check remote version: %+v", versionMap)
|
|
|
|
|
+
|
|
|
|
|
+ if installationinfo.Build.Version != versionMap.Latest {
|
|
|
|
|
+ return versionMap.Latest
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return "none"
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func doRequest() string {
|
|
|
|
|
+ req, err := http.NewRequest("GET", "http://update-check.olivetin.app/versions.json", nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
log.Errorf("Update check failed %v", err)
|
|
log.Errorf("Update check failed %v", err)
|
|
|
return ""
|
|
return ""
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
-
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -113,26 +75,22 @@ func doRequest(jsonUpdateRequest []byte) string {
|
|
|
return ""
|
|
return ""
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- newVersion, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
+ versionMap, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
- return string(newVersion)
|
|
|
|
|
|
|
+ return parseVersion(versionMap)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func actualCheckForUpdate(payload updateRequest) {
|
|
|
|
|
- jsonUpdateRequest, err := json.Marshal(payload)
|
|
|
|
|
-
|
|
|
|
|
- log.Debugf("Update request payload: %+v", payload)
|
|
|
|
|
-
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Errorf("Update check failed %v", err)
|
|
|
|
|
- return
|
|
|
|
|
|
|
+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()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- AvailableVersion = doRequest(jsonUpdateRequest)
|
|
|
|
|
-
|
|
|
|
|
log.WithFields(log.Fields{
|
|
log.WithFields(log.Fields{
|
|
|
- "NewVersion": AvailableVersion,
|
|
|
|
|
|
|
+ "CurrentVersion": installationinfo.Build.Version,
|
|
|
|
|
+ "NewVersion": installationinfo.Runtime.AvailableVersion,
|
|
|
}).Infof("Update check complete")
|
|
}).Infof("Update check complete")
|
|
|
}
|
|
}
|