diff options
-rw-r--r-- | pkg/config/version/checknewer.go | 86 | ||||
-rw-r--r-- | tests/vercheck/.gitignore | 1 | ||||
-rw-r--r-- | tests/vercheck/main.go | 18 |
3 files changed, 105 insertions, 0 deletions
diff --git a/pkg/config/version/checknewer.go b/pkg/config/version/checknewer.go new file mode 100644 index 0000000..4b89c34 --- /dev/null +++ b/pkg/config/version/checknewer.go @@ -0,0 +1,86 @@ +package version + +import ( + "io/ioutil" + "log" + "net/http" + "os" + "runtime" + "strings" +) + +const verURI = "https://downloads.leap.se/RiseupVPN/" + +// returns true if there's a newer version string published on the server +// this needs to manually bump latest version for every platform in the +// downloads server. +// at the moment, we hardcode RiseupVPN in the path, assuming that all clients +// stay in sync. +func CanUpgrade() bool { + log.Println("Checking for updates...") + uri := verURI + switch runtime.GOOS { + case "windows": + uri += "windows" + case "linux": + uri += "linux" + case "osx": + uri += "osx" + } + uri += "/lastver" + resp, err := http.Get(uri) + if err != nil { + log.Println(err) + return false + } + defer resp.Body.Close() + verStr, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println(err) + return false + } + r := strings.TrimSpace(string(verStr)) + if strings.Count(r, "\n") > 1 { + log.Println("No remote version found at " + uri) + return false + } + canUpgrade := versionOrdinal(r) > versionOrdinal(VERSION) + if os.Getenv("DEBUG") == "1" { + log.Println(">>> Remote version: " + r) + log.Println(">>> Current version: " + VERSION) + if canUpgrade { + log.Println("Newer version available") + } + } + return canUpgrade +} + +// https://stackoverflow.com/a/18411978 +func versionOrdinal(version string) string { + const maxByte = 1<<8 - 1 + vo := make([]byte, 0, len(version)+8) + j := -1 + for i := 0; i < len(version); i++ { + b := version[i] + if '0' > b || b > '9' { + vo = append(vo, b) + j = -1 + continue + } + if j == -1 { + vo = append(vo, 0x00) + j = len(vo) - 1 + } + if vo[j] == 1 && vo[j+1] == '0' { + vo[j+1] = b + continue + } + if vo[j]+1 > maxByte { + log.Println("VersionOrdinal: invalid version") + return string(vo) + } + vo = append(vo, b) + vo[j]++ + } + return string(vo) +} diff --git a/tests/vercheck/.gitignore b/tests/vercheck/.gitignore new file mode 100644 index 0000000..f734c59 --- /dev/null +++ b/tests/vercheck/.gitignore @@ -0,0 +1 @@ +vercheck diff --git a/tests/vercheck/main.go b/tests/vercheck/main.go new file mode 100644 index 0000000..bc8e04e --- /dev/null +++ b/tests/vercheck/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "0xacab.org/leap/bitmask-vpn/pkg/config/version" + "fmt" +) + +func main() { + fmt.Println("Testing version upgrade (checks network)") + fmt.Println("-> set DEBUG=1 for details") + u := version.CanUpgrade() + switch { + case u: + fmt.Println("can upgrade") + case !u: + fmt.Println("no new version available") + } +} |