From eae4097677e5592ebd60b8733b7ca65c2d6681ef Mon Sep 17 00:00:00 2001 From: Rob Loranger Date: Thu, 29 Aug 2019 15:05:59 -0700 Subject: [PATCH 1/3] add update checks includes cache of latest version and page to view if updates are available with a link to the latest update's release notes and a link to check for the latest update now, refreshing the cache manually. --- admin.go | 37 ++- app.go | 5 +- config.ini.example | 1 + config/config.go | 6 +- go.mod | 2 +- routes.go | 8 +- semver.go | 315 ++++++++++++++++++++++++++ templates/user/admin/app-updates.tmpl | 23 ++ templates/user/include/header.tmpl | 1 + updates.go | 103 +++++++++ 10 files changed, 490 insertions(+), 11 deletions(-) create mode 100644 semver.go create mode 100644 templates/user/admin/app-updates.tmpl create mode 100644 updates.go diff --git a/admin.go b/admin.go index fe19ad5..9436ff5 100644 --- a/admin.go +++ b/admin.go @@ -13,16 +13,17 @@ package writefreely import ( "database/sql" "fmt" + "net/http" + "runtime" + "strconv" + "time" + "github.com/gogits/gogs/pkg/tool" "github.com/gorilla/mux" "github.com/writeas/impart" "github.com/writeas/web-core/auth" "github.com/writeas/web-core/log" "github.com/writeas/writefreely/config" - "net/http" - "runtime" - "strconv" - "time" ) var ( @@ -112,7 +113,6 @@ func handleViewAdminDash(app *App, u *User, w http.ResponseWriter, r *http.Reque Message: r.FormValue("m"), ConfigMessage: r.FormValue("cm"), } - showUserPage(w, "admin", p) return nil } @@ -451,3 +451,30 @@ func adminResetPassword(app *App, u *User, newPass string) error { } return nil } + +func handleViewAdminUpdates(app *App, u *User, w http.ResponseWriter, r *http.Request) error { + check := r.URL.Query().Get("check") + + if check == "now" && app.cfg.App.UpdateChecks { + app.updates.CheckNow() + } + + p := struct { + *UserPage + LastChecked string + LatestVersion string + LatestReleaseURL string + UpdateAvailable bool + }{ + UserPage: NewUserPage(app, r, u, "Updates", nil), + } + if app.cfg.App.UpdateChecks { + p.LastChecked = app.updates.lastCheck.Format("January 2, 2006, 3:04 PM") + p.LatestVersion = app.updates.LatestVersion() + p.LatestReleaseURL = app.updates.ReleaseURL() + p.UpdateAvailable = app.updates.AreAvailable() + } + + showUserPage(w, "app-updates", p) + return nil +} diff --git a/app.go b/app.go index c52f59d..dea31bf 100644 --- a/app.go +++ b/app.go @@ -30,7 +30,7 @@ import ( "github.com/gorilla/schema" "github.com/gorilla/sessions" "github.com/manifoldco/promptui" - "github.com/writeas/go-strip-markdown" + stripmd "github.com/writeas/go-strip-markdown" "github.com/writeas/impart" "github.com/writeas/web-core/auth" "github.com/writeas/web-core/converter" @@ -72,6 +72,7 @@ type App struct { keys *key.Keychain sessionStore *sessions.CookieStore formDecoder *schema.Decoder + updates *updatesCache timeline *localTimeline } @@ -346,6 +347,8 @@ func Initialize(apper Apper, debug bool) (*App, error) { if err != nil { return nil, fmt.Errorf("init keys: %s", err) } + apper.App().InitUpdates() + apper.App().InitSession() apper.App().InitDecoder() diff --git a/config.ini.example b/config.ini.example index dcbd6ee..7ac944e 100644 --- a/config.ini.example +++ b/config.ini.example @@ -23,4 +23,5 @@ max_blogs = 1 federation = true public_stats = true private = false +update_checks = true diff --git a/config/config.go b/config/config.go index e27ffb9..f915431 100644 --- a/config/config.go +++ b/config/config.go @@ -12,8 +12,9 @@ package config import ( - "gopkg.in/ini.v1" "strings" + + "gopkg.in/ini.v1" ) const ( @@ -89,6 +90,9 @@ type ( // Defaults DefaultVisibility string `ini:"default_visibility"` + + // Check for Updates + UpdateChecks bool `ini:"update_checks"` } // Config holds the complete configuration for running a writefreely instance diff --git a/go.mod b/go.mod index 5e040ba..bde8334 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,7 @@ require ( golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1 // indirect golang.org/x/net v0.0.0-20190206173232-65e2d4e15006 // indirect golang.org/x/sys v0.0.0-20190209173611-3b5209105503 // indirect - golang.org/x/tools v0.0.0-20190208222737-3744606dbb67 // indirect + golang.org/x/tools v0.0.0-20190208222737-3744606dbb67 google.golang.org/appengine v1.4.0 // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c // indirect gopkg.in/bufio.v1 v1.0.0-20140618132640-567b2bfa514e // indirect diff --git a/routes.go b/routes.go index 724c532..937f8bc 100644 --- a/routes.go +++ b/routes.go @@ -11,13 +11,14 @@ package writefreely import ( + "net/http" + "path/filepath" + "strings" + "github.com/gorilla/mux" "github.com/writeas/go-webfinger" "github.com/writeas/web-core/log" "github.com/writefreely/go-nodeinfo" - "net/http" - "path/filepath" - "strings" ) // InitStaticRoutes adds routes for serving static files. @@ -147,6 +148,7 @@ func InitRoutes(apper Apper, r *mux.Router) *mux.Router { write.HandleFunc("/admin/page/{slug}", handler.Admin(handleViewAdminPage)).Methods("GET") write.HandleFunc("/admin/update/config", handler.AdminApper(handleAdminUpdateConfig)).Methods("POST") write.HandleFunc("/admin/update/{page}", handler.Admin(handleAdminUpdateSite)).Methods("POST") + write.HandleFunc("/admin/updates", handler.Admin(handleViewAdminUpdates)).Methods("GET") // Handle special pages first write.HandleFunc("/login", handler.Web(viewLogin, UserLevelNoneRequired)) diff --git a/semver.go b/semver.go new file mode 100644 index 0000000..18fb276 --- /dev/null +++ b/semver.go @@ -0,0 +1,315 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package semver implements comparison of semantic version strings. +// In this package, semantic version strings must begin with a leading "v", +// as in "v1.0.0". +// +// The general form of a semantic version string accepted by this package is +// +// vMAJOR[.MINOR[.PATCH[-PRERELEASE][+BUILD]]] +// +// where square brackets indicate optional parts of the syntax; +// MAJOR, MINOR, and PATCH are decimal integers without extra leading zeros; +// PRERELEASE and BUILD are each a series of non-empty dot-separated identifiers +// using only alphanumeric characters and hyphens; and +// all-numeric PRERELEASE identifiers must not have leading zeros. +// +// This package follows Semantic Versioning 2.0.0 (see semver.org) +// with two exceptions. First, it requires the "v" prefix. Second, it recognizes +// vMAJOR and vMAJOR.MINOR (with no prerelease or build suffixes) +// as shorthands for vMAJOR.0.0 and vMAJOR.MINOR.0. + +// Package writefreely +// copied from +// https://github.com/golang/tools/blob/master/internal/semver/semver.go +// slight modifications made +package writefreely + +// parsed returns the parsed form of a semantic version string. +type parsed struct { + major string + minor string + patch string + short string + prerelease string + build string + err string +} + +// IsValid reports whether v is a valid semantic version string. +func IsValid(v string) bool { + _, ok := semParse(v) + return ok +} + +// CompareSemver returns an integer comparing two versions according to +// according to semantic version precedence. +// The result will be 0 if v == w, -1 if v < w, or +1 if v > w. +// +// An invalid semantic version string is considered less than a valid one. +// All invalid semantic version strings compare equal to each other. +func CompareSemver(v, w string) int { + pv, ok1 := semParse(v) + pw, ok2 := semParse(w) + if !ok1 && !ok2 { + return 0 + } + if !ok1 { + return -1 + } + if !ok2 { + return +1 + } + if c := compareInt(pv.major, pw.major); c != 0 { + return c + } + if c := compareInt(pv.minor, pw.minor); c != 0 { + return c + } + if c := compareInt(pv.patch, pw.patch); c != 0 { + return c + } + return comparePrerelease(pv.prerelease, pw.prerelease) +} + +func semParse(v string) (p parsed, ok bool) { + if v == "" || v[0] != 'v' { + p.err = "missing v prefix" + return + } + p.major, v, ok = parseInt(v[1:]) + if !ok { + p.err = "bad major version" + return + } + if v == "" { + p.minor = "0" + p.patch = "0" + p.short = ".0.0" + return + } + if v[0] != '.' { + p.err = "bad minor prefix" + ok = false + return + } + p.minor, v, ok = parseInt(v[1:]) + if !ok { + p.err = "bad minor version" + return + } + if v == "" { + p.patch = "0" + p.short = ".0" + return + } + if v[0] != '.' { + p.err = "bad patch prefix" + ok = false + return + } + p.patch, v, ok = parseInt(v[1:]) + if !ok { + p.err = "bad patch version" + return + } + if len(v) > 0 && v[0] == '-' { + p.prerelease, v, ok = parsePrerelease(v) + if !ok { + p.err = "bad prerelease" + return + } + } + if len(v) > 0 && v[0] == '+' { + p.build, v, ok = parseBuild(v) + if !ok { + p.err = "bad build" + return + } + } + if v != "" { + p.err = "junk on end" + ok = false + return + } + ok = true + return +} + +func parseInt(v string) (t, rest string, ok bool) { + if v == "" { + return + } + if v[0] < '0' || '9' < v[0] { + return + } + i := 1 + for i < len(v) && '0' <= v[i] && v[i] <= '9' { + i++ + } + if v[0] == '0' && i != 1 { + return + } + return v[:i], v[i:], true +} + +func parsePrerelease(v string) (t, rest string, ok bool) { + // "A pre-release version MAY be denoted by appending a hyphen and + // a series of dot separated identifiers immediately following the patch version. + // Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-]. + // Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes." + if v == "" || v[0] != '-' { + return + } + i := 1 + start := 1 + for i < len(v) && v[i] != '+' { + if !isIdentChar(v[i]) && v[i] != '.' { + return + } + if v[i] == '.' { + if start == i || isBadNum(v[start:i]) { + return + } + start = i + 1 + } + i++ + } + if start == i || isBadNum(v[start:i]) { + return + } + return v[:i], v[i:], true +} + +func parseBuild(v string) (t, rest string, ok bool) { + if v == "" || v[0] != '+' { + return + } + i := 1 + start := 1 + for i < len(v) { + if !isIdentChar(v[i]) { + return + } + if v[i] == '.' { + if start == i { + return + } + start = i + 1 + } + i++ + } + if start == i { + return + } + return v[:i], v[i:], true +} + +func isIdentChar(c byte) bool { + return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-' +} + +func isBadNum(v string) bool { + i := 0 + for i < len(v) && '0' <= v[i] && v[i] <= '9' { + i++ + } + return i == len(v) && i > 1 && v[0] == '0' +} + +func isNum(v string) bool { + i := 0 + for i < len(v) && '0' <= v[i] && v[i] <= '9' { + i++ + } + return i == len(v) +} + +func compareInt(x, y string) int { + if x == y { + return 0 + } + if len(x) < len(y) { + return -1 + } + if len(x) > len(y) { + return +1 + } + if x < y { + return -1 + } else { + return +1 + } +} + +func comparePrerelease(x, y string) int { + // "When major, minor, and patch are equal, a pre-release version has + // lower precedence than a normal version. + // Example: 1.0.0-alpha < 1.0.0. + // Precedence for two pre-release versions with the same major, minor, + // and patch version MUST be determined by comparing each dot separated + // identifier from left to right until a difference is found as follows: + // identifiers consisting of only digits are compared numerically and + // identifiers with letters or hyphens are compared lexically in ASCII + // sort order. Numeric identifiers always have lower precedence than + // non-numeric identifiers. A larger set of pre-release fields has a + // higher precedence than a smaller set, if all of the preceding + // identifiers are equal. + // Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < + // 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0." + if x == y { + return 0 + } + if x == "" { + return +1 + } + if y == "" { + return -1 + } + for x != "" && y != "" { + x = x[1:] // skip - or . + y = y[1:] // skip - or . + var dx, dy string + dx, x = nextIdent(x) + dy, y = nextIdent(y) + if dx != dy { + ix := isNum(dx) + iy := isNum(dy) + if ix != iy { + if ix { + return -1 + } else { + return +1 + } + } + if ix { + if len(dx) < len(dy) { + return -1 + } + if len(dx) > len(dy) { + return +1 + } + } + if dx < dy { + return -1 + } else { + return +1 + } + } + } + if x == "" { + return -1 + } else { + return +1 + } +} + +func nextIdent(x string) (dx, rest string) { + i := 0 + for i < len(x) && x[i] != '.' { + i++ + } + return x[:i], x[i:] +} diff --git a/templates/user/admin/app-updates.tmpl b/templates/user/admin/app-updates.tmpl new file mode 100644 index 0000000..540fb7c --- /dev/null +++ b/templates/user/admin/app-updates.tmpl @@ -0,0 +1,23 @@ +{{define "app-updates"}} +{{template "header" .}} + + + +
+ {{template "admin-header" .}} + + {{if not .UpdateAvailable}} +

WriteFreely is up to date.

+ {{else}} +

WriteFreely {{.LatestVersion}} is available.

+
+ For details on features, bug fixes or notes on upgrading, read the release notes. +
+ {{end}} +

Last checked at: {{.LastChecked}}. Check now.

+ +{{template "footer" .}} + +{{template "body-end" .}} +{{end}} diff --git a/templates/user/include/header.tmpl b/templates/user/include/header.tmpl index 312d0b8..48946d6 100644 --- a/templates/user/include/header.tmpl +++ b/templates/user/include/header.tmpl @@ -69,6 +69,7 @@ {{if not .SingleUser}} Users Pages + {{if .UpdateChecks}}Updates{{end}} {{end}} diff --git a/updates.go b/updates.go new file mode 100644 index 0000000..248f691 --- /dev/null +++ b/updates.go @@ -0,0 +1,103 @@ +/* + * Copyright © 2018-2019 A Bunch Tell LLC. + * + * This file is part of WriteFreely. + * + * WriteFreely is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, included + * in the LICENSE file in this source code package. + */ + +package writefreely + +import ( + "io/ioutil" + "net/http" + "strings" + "sync" + "time" +) + +// updatesCacheTime is the default interval between cache updates for new +// software versions +const updatesCacheTime = 12 * time.Hour + +// updatesCache holds data about current and new releases of the writefreely +// software +type updatesCache struct { + mu sync.Mutex + frequency time.Duration + lastCheck time.Time + latestVersion string + currentVersion string +} + +// CheckNow asks for the latest released version of writefreely and updates +// the cache last checked time. If the version postdates the current 'latest' +// the version value is replaced. +func (uc *updatesCache) CheckNow() error { + uc.mu.Lock() + defer uc.mu.Unlock() + latestRemote, err := newVersionCheck(uc.currentVersion) + if err != nil { + return err + } + uc.lastCheck = time.Now() + if CompareSemver(latestRemote, uc.latestVersion) == 1 { + uc.latestVersion = latestRemote + } + return nil +} + +// AreAvailable updates the cache if the frequency duration has passed +// then returns if the latest release is newer than the current running version. +func (uc updatesCache) AreAvailable() bool { + if time.Since(uc.lastCheck) > uc.frequency { + uc.CheckNow() + } + return CompareSemver(uc.latestVersion, uc.currentVersion) == 1 +} + +// LatestVersion returns the latest stored version available. +func (uc updatesCache) LatestVersion() string { + return uc.latestVersion +} + +// ReleaseURL returns the full URL to the blog.writefreely.org release notes +// for the latest version as stored in the cache. +func (uc updatesCache) ReleaseURL() string { + ver := strings.TrimPrefix(uc.latestVersion, "v") + ver = strings.TrimSuffix(ver, ".0") + return "https://blog.writefreely.org/version-" + strings.ReplaceAll(ver, ".", "-") +} + +// newUpdatesCache returns an initialized updates cache +func newUpdatesCache() *updatesCache { + cache := updatesCache{ + frequency: updatesCacheTime, + currentVersion: "v" + softwareVer, + } + cache.CheckNow() + return &cache +} + +// InitUpdates initializes the updates cache, if the config value is set +func (app *App) InitUpdates() { + if app.cfg.App.UpdateChecks { + app.updates = newUpdatesCache() + } +} + +func newVersionCheck(serverVersion string) (string, error) { + res, err := http.Get("https://version.writefreely.org") + if err == nil && res.StatusCode == http.StatusOK { + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return "", err + } + return string(body), nil + } + return "", err +} From 2a7a8298e19c8a809df69e03c1e6f02701b85e51 Mon Sep 17 00:00:00 2001 From: Rob Loranger Date: Thu, 29 Aug 2019 16:20:41 -0700 Subject: [PATCH 2/3] strings.ReplaceAll is not in go 1.11 --- updates.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/updates.go b/updates.go index 248f691..63b2378 100644 --- a/updates.go +++ b/updates.go @@ -68,7 +68,9 @@ func (uc updatesCache) LatestVersion() string { func (uc updatesCache) ReleaseURL() string { ver := strings.TrimPrefix(uc.latestVersion, "v") ver = strings.TrimSuffix(ver, ".0") - return "https://blog.writefreely.org/version-" + strings.ReplaceAll(ver, ".", "-") + // hack until go 1.12 in build/travis + seg := strings.Split(ver, ".") + return "https://blog.writefreely.org/version-" + strings.Join(seg, "-") } // newUpdatesCache returns an initialized updates cache From 908f009248ec540576f3dd02ecf6d35edb0c5dce Mon Sep 17 00:00:00 2001 From: Rob Loranger Date: Mon, 9 Sep 2019 10:24:29 -0700 Subject: [PATCH 3/3] clean up and add tests for updates cache - removes the parameter for newVersionCheck as was not being used - changes newUpdatesCache to take expiry parameter for possible future configuration option - adds basic test quite to verify all cache fucntions work as expected --- updates.go | 13 ++++---- updates_test.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 updates_test.go diff --git a/updates.go b/updates.go index 63b2378..c33247b 100644 --- a/updates.go +++ b/updates.go @@ -20,7 +20,7 @@ import ( // updatesCacheTime is the default interval between cache updates for new // software versions -const updatesCacheTime = 12 * time.Hour +const defaultUpdatesCacheTime = 12 * time.Hour // updatesCache holds data about current and new releases of the writefreely // software @@ -38,7 +38,7 @@ type updatesCache struct { func (uc *updatesCache) CheckNow() error { uc.mu.Lock() defer uc.mu.Unlock() - latestRemote, err := newVersionCheck(uc.currentVersion) + latestRemote, err := newVersionCheck() if err != nil { return err } @@ -74,9 +74,9 @@ func (uc updatesCache) ReleaseURL() string { } // newUpdatesCache returns an initialized updates cache -func newUpdatesCache() *updatesCache { +func newUpdatesCache(expiry time.Duration) *updatesCache { cache := updatesCache{ - frequency: updatesCacheTime, + frequency: expiry, currentVersion: "v" + softwareVer, } cache.CheckNow() @@ -84,13 +84,14 @@ func newUpdatesCache() *updatesCache { } // InitUpdates initializes the updates cache, if the config value is set +// It uses the defaultUpdatesCacheTime for the cache expiry func (app *App) InitUpdates() { if app.cfg.App.UpdateChecks { - app.updates = newUpdatesCache() + app.updates = newUpdatesCache(defaultUpdatesCacheTime) } } -func newVersionCheck(serverVersion string) (string, error) { +func newVersionCheck() (string, error) { res, err := http.Get("https://version.writefreely.org") if err == nil && res.StatusCode == http.StatusOK { defer res.Body.Close() diff --git a/updates_test.go b/updates_test.go new file mode 100644 index 0000000..2cb9f92 --- /dev/null +++ b/updates_test.go @@ -0,0 +1,82 @@ +package writefreely + +import ( + "regexp" + "testing" + "time" +) + +func TestUpdatesRoundTrip(t *testing.T) { + cache := newUpdatesCache(defaultUpdatesCacheTime) + t.Run("New Updates Cache", func(t *testing.T) { + + if cache == nil { + t.Fatal("Returned nil cache") + } + + if cache.frequency != defaultUpdatesCacheTime { + t.Fatalf("Got cache expiry frequency: %s but expected: %s", cache.frequency, defaultUpdatesCacheTime) + } + + if cache.currentVersion != "v"+softwareVer { + t.Fatalf("Got current version: %s but expected: %s", cache.currentVersion, "v"+softwareVer) + } + }) + + t.Run("Release URL", func(t *testing.T) { + url := cache.ReleaseURL() + + reg, err := regexp.Compile(`^https:\/\/blog.writefreely.org\/version(-\d+){1,}$`) + if err != nil { + t.Fatalf("Test Case Error: Failed to compile regex: %v", err) + } + match := reg.MatchString(url) + + if !match { + t.Fatalf("Malformed Release URL: %s", url) + } + }) + + t.Run("Check Now", func(t *testing.T) { + // ensure time between init and next check + time.Sleep(1 * time.Second) + + prevLastCheck := cache.lastCheck + + // force to known older version for latest and current + prevLatestVer := "v0.8.1" + cache.latestVersion = prevLatestVer + cache.currentVersion = "v0.8.0" + + err := cache.CheckNow() + if err != nil { + t.Fatalf("Error should be nil, got: %v", err) + } + + if prevLastCheck == cache.lastCheck { + t.Fatal("Expected lastCheck to update") + } + + if cache.lastCheck.Before(prevLastCheck) { + t.Fatal("Last check should be newer than previous") + } + + if prevLatestVer == cache.latestVersion { + t.Fatal("expected latestVersion to update") + } + + }) + + t.Run("Are Available", func(t *testing.T) { + if !cache.AreAvailable() { + t.Fatalf("Cache reports not updates but Current is %s and Latest is %s", cache.currentVersion, cache.latestVersion) + } + }) + + t.Run("Latest Version", func(t *testing.T) { + gotLatest := cache.LatestVersion() + if gotLatest != cache.latestVersion { + t.Fatalf("Malformed latest version. Expected: %s but got: %s", cache.latestVersion, gotLatest) + } + }) +}