Skip to content

Commit

Permalink
Merge pull request #80 from patrickhener/updater
Browse files Browse the repository at this point in the history
Updater
  • Loading branch information
patrickhener authored Oct 17, 2024
2 parents 1264a32 + a1041ac commit 7532d88
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
!embedded/example.txt
/httpserver/embedded/*
!httpserver/embedded/example.txt
*.bak
9 changes: 9 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
version: 2

before:
hooks:
- go mod download
builds:
- env:
- CGO_ENABLED=0
binary: goshs
goos:
- linux
- windows
- darwin
goarch:
- "amd64"
- "386"
- "arm64"

archives:
- name_template: >-
{{ .ProjectName }}_
{{- .Os }}_
{{- if eq .Arch "amd64" }}x86_64{{ else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Version](https://img.shields.io/badge/Version-v0.4.1-green)
![Version](https://img.shields.io/badge/Version-v0.4.2-green)
[![GitHub](https://img.shields.io/github/license/patrickhener/goshs)](https://github.com/patrickhener/goshs/blob/master/LICENSE)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/patrickhener/goshs)
[![GitHub issues](https://img.shields.io/github/issues-raw/patrickhener/goshs)](https://github.com/patrickhener/goshs/issues)
Expand Down Expand Up @@ -51,6 +51,7 @@ For a detailed documentation go to [goshs.de](https://goshs.de)
* You can apply custom basic auth per folder
* You can restrict access to specific files completely
* Embed files on compile time
* Self updating binary

# Installation

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ toolchain go1.21.8

require (
github.com/go-acme/lego/v4 v4.16.1
github.com/google/go-github/v42 v42.0.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.4.2
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/sirupsen/logrus v1.9.3
golang.org/x/crypto v0.21.0
golang.org/x/net v0.23.0
Expand All @@ -18,6 +20,7 @@ require (
require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/miekg/dns v1.1.58 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ github.com/go-acme/lego/v4 v4.16.1 h1:JxZ93s4KG0jL27rZ30UsIgxap6VGzKuREsSkkyzeoC
github.com/go-acme/lego/v4 v4.16.1/go.mod h1:AVvwdPned/IWpD/ihHhMsKnveF7HHYAz/CmtXi7OZoE=
github.com/go-jose/go-jose/v4 v4.0.1 h1:QVEPDE3OluqXBQZDcnNvQrInro2h0e4eqNbnZSWqS6U=
github.com/go-jose/go-jose/v4 v4.0.1/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v42 v42.0.0 h1:YNT0FwjPrEysRkLIiKuEfSvBPCGKphW5aS5PxwaoLec=
github.com/google/go-github/v42 v42.0.0/go.mod h1:jgg/jvyI0YlDOM1/ps6XYh04HNQ3vKf0CVko62/EhRg=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef h1:A9HsByNhogrvm9cWb28sjiS3i7tcKCkflWFEkHfuAgM=
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -42,6 +49,7 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
28 changes: 24 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"github.com/patrickhener/goshs/ca"
"github.com/patrickhener/goshs/httpserver"
"github.com/patrickhener/goshs/logger"
"github.com/patrickhener/goshs/update"
"github.com/patrickhener/goshs/utils"
)

const goshsVersion = "v0.4.1"
const goshsVersion = "v0.4.2"

var (
port = 8000
Expand Down Expand Up @@ -82,6 +83,7 @@ Authentication options:
Misc options:
-u --user Drop privs to user (unix only) (default: current user)
--update Update goshs to most recent version
-V --verbose Activate verbose log output (default: false)
-v Print the current goshs version
Expand All @@ -100,7 +102,7 @@ Usage examples:
}
}

func flags() (*bool, *bool, *bool) {
func flags() (*bool, *bool, *bool, *bool) {
wd, _ := os.Getwd()

flag.StringVar(&ip, "i", ip, "ip")
Expand Down Expand Up @@ -151,6 +153,7 @@ func flags() (*bool, *bool, *bool) {
flag.StringVar(&leTLSPort, "le-tls", leTLSPort, "")
flag.BoolVar(&embedded, "e", embedded, "")
flag.BoolVar(&embedded, "embedded", embedded, "")
updateGoshs := flag.Bool("update", false, "update")
hash := flag.Bool("H", false, "hash")
hashLong := flag.Bool("hash", false, "hash")
version := flag.Bool("v", false, "goshs version")
Expand All @@ -159,7 +162,7 @@ func flags() (*bool, *bool, *bool) {

flag.Parse()

return hash, hashLong, version
return hash, hashLong, version, updateGoshs
}

func resolveInterface() {
Expand Down Expand Up @@ -205,7 +208,14 @@ func init() {
wd, _ := os.Getwd()

// flags
hash, hashLong, version := flags()
hash, hashLong, version, updateGoshs := flags()

if *updateGoshs {
err := update.UpdateTool(goshsVersion)
if err != nil {
logger.Fatalf("Failed to update tool: %+v", err)
}
}

if *version {
fmt.Printf("goshs version is: %+v\n", goshsVersion)
Expand Down Expand Up @@ -255,6 +265,16 @@ func parseBasicAuth() (string, string) {
}

func main() {
if yes, out := update.CheckForUpdates(goshsVersion); yes {
logger.Warnf("There is a newer Version (%s) of goshs available. Run --update to update goshs.", out)
} else {
if out != "" {
logger.Warnf("Failed to check for updates: %+v", out)
} else {
logger.Infof("You are running the newest version (%s) of goshs", goshsVersion)
}
}

user := ""
pass := ""

Expand Down
113 changes: 113 additions & 0 deletions update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package update

import (
"context"
"fmt"
"net/http"
"runtime"
"time"

"github.com/google/go-github/v42/github"
"github.com/inconshreveable/go-update"
"github.com/patrickhener/goshs/logger"
)

const (
owner = "patrickhener"
repo = "goshs"
GOSHS_WINDOWS_64 = "goshs_windows_x86_64.exe"
GOSHS_WINDOWS_32 = "goshs_windows_386.exe"
GOSHS_WINDOWS_ARM = "goshs_windows_arm64.exe"
GOSHS_LINUX_64 = "goshs_linux_x86_64"
GOSHS_LINUX_32 = "goshs_linux_386"
GOSHS_LINUX_ARM = "goshs_linux_arm64"
GOSHS_DARWIN_64 = "goshs_darwin_x86_64"
GOSHS_DARWIN_ARM = "goshs_darwin_arm64"
)

func CheckForUpdates(version string) (bool, string) {
latestRelease, err := getLatestRelease()
if err != nil {
return false, err.Error()
}

if latestRelease.GetTagName() != version {
return true, latestRelease.GetTagName()
}
return false, ""
}

func UpdateTool(version string) error {
latestRelease, err := getLatestRelease()
if err != nil {
return fmt.Errorf("failed to fetch latest release: %+v", err)
}

if latestRelease.GetTagName() == version {
logger.Infof("You are already running the newest version (%s) of goshs", version)
return nil
}

logger.Infof("Updating goshs to version %s...", latestRelease.GetTagName())

assetURL, err := getAssetURL(latestRelease)
if err != nil {
return fmt.Errorf("failed to get latest release: %+v", err)
}

err = applyUpdate(assetURL)
if err != nil {
return fmt.Errorf("failed to apply update: %+v", err)
}

logger.Info("Goshs was updated successfully")
return nil
}

func getLatestRelease() (*github.RepositoryRelease, error) {
client := github.NewClient(nil)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

release, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo)
if err != nil {
return nil, err
}
return release, nil
}

func getAssetURL(release *github.RepositoryRelease) (string, error) {
assets := release.Assets
for _, asset := range assets {
assetName := asset.GetName()
if assetMatchesOSAndArch(assetName) {
return asset.GetBrowserDownloadURL(), nil
}
}
return "", fmt.Errorf("no suitable release asset found for OS: %s, ARCH: %s", runtime.GOOS, runtime.GOARCH)
}

func applyUpdate(assetURL string) error {
resp, err := http.Get(assetURL)
if err != nil {
return fmt.Errorf("failed to download update: %+v", err)
}
defer resp.Body.Close()

err = update.Apply(resp.Body, update.Options{})
if err != nil {
return fmt.Errorf("failed to apply update: %+v", err)
}
return nil
}

func assetMatchesOSAndArch(assetName string) bool {
return (runtime.GOOS == "windows" && runtime.GOARCH == "amd64" && assetName == GOSHS_WINDOWS_64) ||
(runtime.GOOS == "windows" && runtime.GOARCH == "386" && assetName == GOSHS_WINDOWS_32) ||
(runtime.GOOS == "windows" && runtime.GOARCH == "arm64" && assetName == GOSHS_WINDOWS_ARM) ||
(runtime.GOOS == "linux" && runtime.GOARCH == "amd64" && assetName == GOSHS_LINUX_64) ||
(runtime.GOOS == "linux" && runtime.GOARCH == "386" && assetName == GOSHS_LINUX_32) ||
(runtime.GOOS == "linux" && runtime.GOARCH == "arm64" && assetName == GOSHS_LINUX_ARM) ||
(runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" && assetName == GOSHS_DARWIN_64) ||
(runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" && assetName == GOSHS_DARWIN_ARM)
}

0 comments on commit 7532d88

Please sign in to comment.