2021-11-09 11:37:51 +00:00
|
|
|
//go:build !windows
|
2021-05-03 23:58:48 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2020-08-12 16:16:14 +00:00
|
|
|
package updater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
2021-03-08 11:16:32 +00:00
|
|
|
"errors"
|
2020-08-12 16:16:14 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-10-02 18:01:05 +00:00
|
|
|
"net/http/httptest"
|
2020-08-12 16:16:14 +00:00
|
|
|
"os"
|
2021-05-03 23:58:48 +00:00
|
|
|
"path/filepath"
|
2020-08-12 16:16:14 +00:00
|
|
|
"runtime"
|
2021-03-08 11:16:32 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-08-12 16:16:14 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-12 16:16:14 +00:00
|
|
|
)
|
|
|
|
|
2021-05-03 23:58:48 +00:00
|
|
|
var testFilePath = filepath.Join(os.TempDir(), "test")
|
|
|
|
|
2020-08-12 16:16:14 +00:00
|
|
|
func respondWithJSON(w http.ResponseWriter, v interface{}, status int) {
|
|
|
|
data, _ := json.Marshal(v)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func respondWithData(w http.ResponseWriter, b []byte, status int) {
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
2021-02-28 23:24:38 +00:00
|
|
|
const mostRecentVersion = "2021.2.5"
|
|
|
|
const mostRecentBetaVersion = "2021.3.0"
|
|
|
|
const knownBuggyVersion = "2020.12.0"
|
|
|
|
const expectedUserMsg = "This message is expected when running a known buggy version"
|
|
|
|
|
2020-08-12 16:16:14 +00:00
|
|
|
func updateHandler(w http.ResponseWriter, r *http.Request) {
|
2021-02-28 23:24:38 +00:00
|
|
|
version := mostRecentVersion
|
2020-10-02 18:01:05 +00:00
|
|
|
host := fmt.Sprintf("http://%s", r.Host)
|
2020-08-12 16:16:14 +00:00
|
|
|
url := host + "/download"
|
|
|
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
if query.Get(BetaKeyName) == "true" {
|
2021-02-28 23:24:38 +00:00
|
|
|
version = mostRecentBetaVersion
|
2020-08-12 16:16:14 +00:00
|
|
|
url = host + "/beta"
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedVersion := query.Get(VersionKeyName)
|
|
|
|
if requestedVersion != "" {
|
|
|
|
version = requestedVersion
|
|
|
|
url = fmt.Sprintf("%s?version=%s", url, requestedVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
if query.Get(ArchitectureKeyName) != runtime.GOARCH || query.Get(OSKeyName) != runtime.GOOS {
|
|
|
|
respondWithJSON(w, VersionResponse{Error: "unsupported os and architecture"}, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
fmt.Fprint(h, version)
|
|
|
|
checksum := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
|
2021-02-28 23:24:38 +00:00
|
|
|
var userMessage = ""
|
|
|
|
if query.Get(ClientVersionName) == knownBuggyVersion {
|
|
|
|
userMessage = expectedUserMsg
|
|
|
|
}
|
2021-03-08 11:16:32 +00:00
|
|
|
shouldUpdate := requestedVersion != "" || IsNewerVersion(query.Get(ClientVersionName), version)
|
2021-02-28 23:24:38 +00:00
|
|
|
|
2021-03-08 11:16:32 +00:00
|
|
|
v := VersionResponse{
|
|
|
|
URL: url, Version: version, Checksum: checksum, UserMessage: userMessage, ShouldUpdate: shouldUpdate,
|
|
|
|
}
|
2020-08-12 16:16:14 +00:00
|
|
|
respondWithJSON(w, v, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func gzipUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println("got a request!")
|
|
|
|
version := "2020.09.02"
|
|
|
|
h := sha256.New()
|
|
|
|
fmt.Fprint(h, version)
|
|
|
|
checksum := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
url := fmt.Sprintf("http://%s/gzip-download.tgz", r.Host)
|
2021-03-08 11:16:32 +00:00
|
|
|
v := VersionResponse{URL: url, Version: version, Checksum: checksum, ShouldUpdate: true}
|
2020-08-12 16:16:14 +00:00
|
|
|
respondWithJSON(w, v, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compressedDownloadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
version := "2020.09.02"
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
gw := gzip.NewWriter(buf)
|
|
|
|
tw := tar.NewWriter(gw)
|
|
|
|
|
|
|
|
header := &tar.Header{
|
|
|
|
Size: int64(len(version)),
|
|
|
|
Name: "download",
|
|
|
|
}
|
|
|
|
tw.WriteHeader(header)
|
|
|
|
tw.Write([]byte(version))
|
|
|
|
|
|
|
|
tw.Close()
|
|
|
|
gw.Close()
|
|
|
|
|
|
|
|
respondWithData(w, buf.Bytes(), http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadHandler(w http.ResponseWriter, r *http.Request) {
|
2021-02-28 23:24:38 +00:00
|
|
|
version := mostRecentVersion
|
2020-08-12 16:16:14 +00:00
|
|
|
requestedVersion := r.URL.Query().Get(VersionKeyName)
|
|
|
|
if requestedVersion != "" {
|
|
|
|
version = requestedVersion
|
|
|
|
}
|
|
|
|
respondWithData(w, []byte(version), http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func betaHandler(w http.ResponseWriter, r *http.Request) {
|
2021-02-28 23:24:38 +00:00
|
|
|
respondWithData(w, []byte(mostRecentBetaVersion), http.StatusOK)
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func failureHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
respondWithJSON(w, VersionResponse{Error: "unsupported os and architecture"}, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
2021-03-08 11:16:32 +00:00
|
|
|
func IsNewerVersion(current string, check string) bool {
|
|
|
|
if current == "" || check == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if strings.Contains(strings.ToLower(current), "dev") {
|
|
|
|
return false // dev builds shouldn't update
|
|
|
|
}
|
|
|
|
|
|
|
|
cMajor, cMinor, cPatch, err := SemanticParts(current)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
nMajor, nMinor, nPatch, err := SemanticParts(check)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if nMajor > cMajor {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if nMajor == cMajor && nMinor > cMinor {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if nMajor == cMajor && nMinor == cMinor && nPatch > cPatch {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func SemanticParts(version string) (major int, minor int, patch int, err error) {
|
|
|
|
major = 0
|
|
|
|
minor = 0
|
|
|
|
patch = 0
|
|
|
|
parts := strings.Split(version, ".")
|
|
|
|
if len(parts) != 3 {
|
|
|
|
err = errors.New("invalid version")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
major, err = strconv.Atoi(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
minor, err = strconv.Atoi(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
patch, err = strconv.Atoi(parts[2])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
func createServer() *httptest.Server {
|
2020-08-12 16:16:14 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/updater", updateHandler)
|
|
|
|
mux.HandleFunc("/download", downloadHandler)
|
|
|
|
mux.HandleFunc("/beta", betaHandler)
|
|
|
|
mux.HandleFunc("/fail", failureHandler)
|
|
|
|
mux.HandleFunc("/compressed", gzipUpdateHandler)
|
|
|
|
mux.HandleFunc("/gzip-download.tgz", compressedDownloadHandler)
|
2020-10-02 18:01:05 +00:00
|
|
|
return httptest.NewServer(mux)
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createTestFile(t *testing.T, path string) {
|
2021-02-28 23:24:38 +00:00
|
|
|
f, err := os.Create(path)
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-12 16:16:14 +00:00
|
|
|
fmt.Fprint(f, "2020.08.04")
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateService(t *testing.T) {
|
2020-10-02 18:01:05 +00:00
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
2020-08-12 16:16:14 +00:00
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
2020-10-02 18:01:05 +00:00
|
|
|
log.Println("server url: ", ts.URL)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
s := NewWorkersService("2020.8.2", fmt.Sprintf("%s/updater", ts.URL), testFilePath, Options{})
|
2020-08-12 16:16:14 +00:00
|
|
|
v, err := s.Check()
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, v.Version(), mostRecentVersion)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, v.Apply())
|
2023-07-15 01:42:48 +00:00
|
|
|
dat, err := os.ReadFile(testFilePath)
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, string(dat), mostRecentVersion)
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBetaUpdateService(t *testing.T) {
|
2020-10-02 18:01:05 +00:00
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
2020-08-12 16:16:14 +00:00
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
s := NewWorkersService("2020.8.2", fmt.Sprintf("%s/updater", ts.URL), testFilePath, Options{IsBeta: true})
|
2020-08-12 16:16:14 +00:00
|
|
|
v, err := s.Check()
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, v.Version(), mostRecentBetaVersion)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, v.Apply())
|
2023-07-15 01:42:48 +00:00
|
|
|
dat, err := os.ReadFile(testFilePath)
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, string(dat), mostRecentBetaVersion)
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailUpdateService(t *testing.T) {
|
2020-10-02 18:01:05 +00:00
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
2020-08-12 16:16:14 +00:00
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
s := NewWorkersService("2020.8.2", fmt.Sprintf("%s/fail", ts.URL), testFilePath, Options{})
|
2020-08-12 16:16:14 +00:00
|
|
|
v, err := s.Check()
|
2020-10-02 18:01:05 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, v)
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoUpdateService(t *testing.T) {
|
2020-10-02 18:01:05 +00:00
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
2020-08-12 16:16:14 +00:00
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
|
|
|
|
2021-02-28 23:24:38 +00:00
|
|
|
s := NewWorkersService(mostRecentVersion, fmt.Sprintf("%s/updater", ts.URL), testFilePath, Options{})
|
2020-08-12 16:16:14 +00:00
|
|
|
v, err := s.Check()
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-28 23:24:38 +00:00
|
|
|
require.NotNil(t, v)
|
|
|
|
require.Empty(t, v.Version())
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestForcedUpdateService(t *testing.T) {
|
2020-10-02 18:01:05 +00:00
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
2020-08-12 16:16:14 +00:00
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
s := NewWorkersService("2020.8.5", fmt.Sprintf("%s/updater", ts.URL), testFilePath, Options{IsForced: true})
|
2020-08-12 16:16:14 +00:00
|
|
|
v, err := s.Check()
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, v.Version(), mostRecentVersion)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, v.Apply())
|
2023-07-15 01:42:48 +00:00
|
|
|
dat, err := os.ReadFile(testFilePath)
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, string(dat), mostRecentVersion)
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateSpecificVersionService(t *testing.T) {
|
2020-10-02 18:01:05 +00:00
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
2020-08-12 16:16:14 +00:00
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
|
|
|
reqVersion := "2020.9.1"
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
s := NewWorkersService("2020.8.2", fmt.Sprintf("%s/updater", ts.URL), testFilePath, Options{RequestedVersion: reqVersion})
|
2020-08-12 16:16:14 +00:00
|
|
|
v, err := s.Check()
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, reqVersion, v.Version())
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, v.Apply())
|
2023-07-15 01:42:48 +00:00
|
|
|
dat, err := os.ReadFile(testFilePath)
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
require.Equal(t, reqVersion, string(dat))
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompressedUpdateService(t *testing.T) {
|
2020-10-02 18:01:05 +00:00
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
2020-08-12 16:16:14 +00:00
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
s := NewWorkersService("2020.8.2", fmt.Sprintf("%s/compressed", ts.URL), testFilePath, Options{})
|
2020-08-12 16:16:14 +00:00
|
|
|
v, err := s.Check()
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-28 23:24:38 +00:00
|
|
|
require.Equal(t, "2020.09.02", v.Version())
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, v.Apply())
|
2023-07-15 01:42:48 +00:00
|
|
|
dat, err := os.ReadFile(testFilePath)
|
2020-10-02 18:01:05 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-12 16:16:14 +00:00
|
|
|
|
2020-10-02 18:01:05 +00:00
|
|
|
require.Equal(t, "2020.09.02", string(dat))
|
2020-08-12 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 23:24:38 +00:00
|
|
|
func TestUpdateWhenRunningKnownBuggyVersion(t *testing.T) {
|
|
|
|
ts := createServer()
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
createTestFile(t, testFilePath)
|
|
|
|
defer os.Remove(testFilePath)
|
|
|
|
|
|
|
|
s := NewWorkersService(knownBuggyVersion, fmt.Sprintf("%s/updater", ts.URL), testFilePath, Options{})
|
|
|
|
v, err := s.Check()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, v.Version(), mostRecentVersion)
|
|
|
|
require.Equal(t, v.UserMessage(), expectedUserMsg)
|
|
|
|
}
|