From d49a29a4015026a012a03a2ebb3f8f845decb2d4 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Sat, 19 Aug 2023 19:10:51 -0700 Subject: WIP upgrade --- TODO.txt | 4 ++++ cmd/client/autoupdate.go | 28 ++++++++++++++++++++++++++++ cmd/client/client.go | 8 ++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 cmd/client/autoupdate.go diff --git a/TODO.txt b/TODO.txt index f0dba8d..e8b167f 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,3 +1,7 @@ [ ] Add metrics [ ] Log sessions to DB table [ ] Email on new session (maybe?) +[ ] Automatically upgrade client +[ ] Server driven session max TTL +[ ] Rate limit tokens +[ ] Rate limit CA diff --git a/cmd/client/autoupdate.go b/cmd/client/autoupdate.go new file mode 100644 index 0000000..feb272e --- /dev/null +++ b/cmd/client/autoupdate.go @@ -0,0 +1,28 @@ +package client + +import ( + "context" + "log" + "os" +) + +// TODO: +// Check the Server header from the host and if the second part does not match +// - Use HEAD, ignore 404 +// Download the latest client for this os/arch (remap GOARCH=amd64 to x86_64) +// Re-execute the client in the same process so the latest client is used +// Log some output to let the user know this happened +// Verify SHA256 based on server ETag +func upgradeAndRestartClient(ctx context.Context, version, host string) error { + // Download, chmod, verify, unlink original, rename old to new, exec new + exec, err := os.Executable() + if err != nil { + return err + } + + // Refuse if any path segment starts with go-build.* + + log.Printf("Executable is %s", exec) + + return nil +} diff --git a/cmd/client/client.go b/cmd/client/client.go index b72003d..afc7a1d 100644 --- a/cmd/client/client.go +++ b/cmd/client/client.go @@ -37,7 +37,7 @@ func NewClientCommand(appVersion string) *cobra.Command { Run: func(c *cobra.Command, args []string) { cfg := app.Config{} cli.MustGetConfig(c, &cfg) - clientMain(cfg, args[0], args[1], args[2]) + clientMain(cfg, appVersion, args[0], args[1], args[2]) }, } cli.AddFlags(clientCmd, &app.Config{}, app.DefaultConfig, "client") @@ -202,12 +202,16 @@ func fetchOauthToken(ctx context.Context, clientId, proxyHost string) (string, e return tokenResponse.AccessToken, nil } -func clientMain(cfg app.Config, host, port, username string) { +func clientMain(cfg app.Config, appVersion, host, port, username string) { log.SetOutput(os.Stderr) ctx, cancel := context.WithCancel(context.Background()) defer cancel() + if err := upgradeAndRestartClient(ctx, appVersion, cfg.ClientHost); err != nil { + log.Fatalf("Error attempting to upgrade client: %s", err) + } + agentConn, err := connectToAgent() if err != nil { log.Fatalf("Error connecting to agent, is it started?") -- cgit v1.2.3