From 6d867608837f879be2eb934d034f49359f973c84 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Sat, 29 Jul 2023 12:26:23 -0700 Subject: Allow building standalone client --- Makefile | 12 +++++++++++- app/config.go | 2 ++ clientmain.go | 21 +++++++++++++++++++++ cmd/client/client.go | 20 ++++++++++++-------- main.go | 2 ++ 5 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 clientmain.go diff --git a/Makefile b/Makefile index 6038948..81601bc 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ IMAGE=docker.crute.me/ssh-proxy BINARY=ssh-proxy +CLIENT_BINARY=ssh-proxy-client -$(BINARY): $(shell find . -name '*.go') +$(BINARY): $(CLIENT_BINARY) $(shell find . -name '*.go') @[ "$(CLIENT_ID)" ] || ( echo "CLIENT_ID must be set"; exit 1 ) CGO_ENABLED=0 go build \ @@ -9,6 +10,15 @@ $(BINARY): $(shell find . -name '*.go') -X code.crute.us/mcrute/ssh-proxy/cmd/client.clientId=$(CLIENT_ID)" \ -o $@ +$(CLIENT_BINARY): $(shell find . -name '*.go') + @[ "$(CLIENT_ID)" ] || ( echo "CLIENT_ID must be set"; exit 1 ) + + CGO_ENABLED=0 go build \ + -tags clientonly \ + -ldflags "-X main.appVersion=$(shell git describe --long --tags --dirty --always) \ + -X code.crute.us/mcrute/ssh-proxy/cmd/client.clientId=$(CLIENT_ID)" \ + -o $@ + .PHONY: vet vet: main.go go vet $< diff --git a/app/config.go b/app/config.go index 2ffd0cb..2cb2d92 100644 --- a/app/config.go +++ b/app/config.go @@ -21,6 +21,7 @@ type Config struct { OauthDevicePollSecs int `flag:"oauth-device-poll-secs" flag-scope:"web" flag-help:"Number of seconds between polls for oauth device flow"` OauthSessionTimeout time.Duration `flag:"oauth-session-timelut" flag-scope:"web" flag-help:"Timeout before oauth session expires"` InviteTimeout time.Duration `flag:"invite-timeout" flag-scope:"register" flag-help:"Timeout before inivitation code expires"` + ClientHost string `flag:"client-proxy-host" flag-scope:"client" flag-help:"Hostname and port for proxy"` } var DefaultConfig = &Config{ @@ -42,4 +43,5 @@ var DefaultConfig = &Config{ OauthDevicePollSecs: 5, OauthSessionTimeout: 5 * time.Minute, InviteTimeout: 1 * time.Hour, + ClientHost: "ssh-proxy.crute.me", } diff --git a/clientmain.go b/clientmain.go new file mode 100644 index 0000000..d75b8bc --- /dev/null +++ b/clientmain.go @@ -0,0 +1,21 @@ +//go:build clientonly + +package main + +import ( + "log" + + "code.crute.us/mcrute/ssh-proxy/cmd/client" + + // Import backup data. By default zoneinfo is installed in the docker image + // if something breaks this will still result in us having correct TZ info. + _ "time/tzdata" +) + +func main() { + cmd := client.NewClientCommand() + + if err := cmd.Execute(); err != nil { + log.Fatalf("Error running root command: %s", err) + } +} diff --git a/cmd/client/client.go b/cmd/client/client.go index 62f1f48..7799b87 100644 --- a/cmd/client/client.go +++ b/cmd/client/client.go @@ -26,19 +26,23 @@ import ( // This should be compiled into the binary var clientId string -func Register(root *cobra.Command) { +func NewClientCommand() *cobra.Command { clientCmd := &cobra.Command{ Use: "client proxy-host ssh-to-host ssh-port username", Short: "Run websocket client", - Args: cobra.ExactArgs(4), + Args: cobra.ExactArgs(3), Run: func(c *cobra.Command, args []string) { cfg := app.Config{} cli.MustGetConfig(c, &cfg) - clientMain(cfg, args[0], args[1], args[2], args[3]) + clientMain(cfg, args[0], args[1], args[2]) }, } cli.AddFlags(clientCmd, &app.Config{}, app.DefaultConfig, "client") - root.AddCommand(clientCmd) + return clientCmd +} + +func Register(root *cobra.Command) { + root.AddCommand(NewClientCommand()) } func generateCertificateRequest(username, host string) (ed25519.PrivateKey, []byte, error) { @@ -181,13 +185,13 @@ func fetchOauthToken(ctx context.Context, clientId, proxyHost string) (string, e return tokenResponse.AccessToken, nil } -func clientMain(cfg app.Config, proxyHost, host, port, username string) { +func clientMain(cfg app.Config, host, port, username string) { log.SetOutput(os.Stderr) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - oauthToken, err := fetchOauthToken(ctx, clientId, proxyHost) + oauthToken, err := fetchOauthToken(ctx, clientId, cfg.ClientHost) if err != nil { log.Fatalf("Error fetching oauth token: %s", err) } @@ -197,7 +201,7 @@ func clientMain(cfg app.Config, proxyHost, host, port, username string) { log.Fatalf("Error generating certificate request: %s", err) } - certificate, err := getCertificateFromCA(ctx, oauthToken, certRequest, proxyHost) + certificate, err := getCertificateFromCA(ctx, oauthToken, certRequest, cfg.ClientHost) if err != nil { log.Fatalf("Error fetching certificate: %s", err) } @@ -206,7 +210,7 @@ func clientMain(cfg app.Config, proxyHost, host, port, username string) { log.Fatalf("Error adding certificate to agent: %s", err) } - ws, err := dialProxyHost(ctx, oauthToken, proxyHost, host, port) + ws, err := dialProxyHost(ctx, oauthToken, cfg.ClientHost, host, port) if err != nil { log.Fatalf("Error dialing proxy host: %s", err) } diff --git a/main.go b/main.go index 3306a94..70d0f48 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,5 @@ +//go:build !clientonly + package main import ( -- cgit v1.2.3