mirror of
https://github.com/OliveTin/OliveTin
synced 2025-12-12 00:55:34 +00:00
106 lines
2.2 KiB
Go
106 lines
2.2 KiB
Go
package updatecheck
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"github.com/denisbrodbeck/machineid"
|
|
"github.com/go-co-op/gocron"
|
|
config "github.com/jamesread/OliveTin/internal/config"
|
|
log "github.com/sirupsen/logrus"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"runtime"
|
|
"time"
|
|
)
|
|
|
|
type updateRequest struct {
|
|
CurrentVersion string
|
|
CurrentCommit string
|
|
OS string
|
|
Arch string
|
|
MachineID string
|
|
}
|
|
|
|
// 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 machineID() string {
|
|
v, err := machineid.ProtectedID("OliveTin")
|
|
|
|
if err != nil {
|
|
log.Warnf("Error getting machine ID: %v", err)
|
|
return "?"
|
|
}
|
|
|
|
return v
|
|
}
|
|
|
|
// StartUpdateChecker will start a job that runs periodically, checking
|
|
// for updates.
|
|
func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config) {
|
|
CurrentVersion = currentVersion
|
|
|
|
if !cfg.CheckForUpdates {
|
|
log.Warn("Update checking is disabled")
|
|
return
|
|
}
|
|
|
|
payload := updateRequest{
|
|
CurrentVersion: currentVersion,
|
|
CurrentCommit: currentCommit,
|
|
OS: runtime.GOOS,
|
|
Arch: runtime.GOARCH,
|
|
MachineID: machineID(),
|
|
}
|
|
|
|
s := gocron.NewScheduler(time.UTC)
|
|
|
|
s.Every(7).Days().Do(func() {
|
|
actualCheckForUpdate(payload)
|
|
})
|
|
|
|
s.StartAsync()
|
|
}
|
|
|
|
func doRequest(jsonUpdateRequest []byte) string {
|
|
req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewBuffer(jsonUpdateRequest))
|
|
|
|
if err != nil {
|
|
log.Errorf("Update check failed %v", err)
|
|
return ""
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
log.Errorf("Update check failed %v", err)
|
|
return ""
|
|
}
|
|
|
|
newVersion, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return string(newVersion)
|
|
}
|
|
|
|
func actualCheckForUpdate(payload updateRequest) {
|
|
jsonUpdateRequest, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
log.Errorf("Update check failed %v", err)
|
|
return
|
|
}
|
|
|
|
AvailableVersion = doRequest(jsonUpdateRequest)
|
|
|
|
log.WithFields(log.Fields{
|
|
"NewVersion": AvailableVersion,
|
|
}).Infof("Update check complete")
|
|
}
|