diff options
author | Mike Crute <mike@crute.us> | 2023-07-29 12:26:23 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-07-29 12:26:23 -0700 |
commit | 6d867608837f879be2eb934d034f49359f973c84 (patch) | |
tree | 5c35ec4d145d95e5a8b52b4fd778a9c906827e31 | |
parent | 4e995f9e6c3adc43a361b6fa9b976d25378f1594 (diff) | |
download | websocket_proxy-6d867608837f879be2eb934d034f49359f973c84.tar.bz2 websocket_proxy-6d867608837f879be2eb934d034f49359f973c84.tar.xz websocket_proxy-6d867608837f879be2eb934d034f49359f973c84.zip |
Allow building standalone client
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | app/config.go | 2 | ||||
-rw-r--r-- | clientmain.go | 21 | ||||
-rw-r--r-- | cmd/client/client.go | 20 | ||||
-rw-r--r-- | main.go | 2 |
5 files changed, 48 insertions, 9 deletions
@@ -1,7 +1,8 @@ | |||
1 | IMAGE=docker.crute.me/ssh-proxy | 1 | IMAGE=docker.crute.me/ssh-proxy |
2 | BINARY=ssh-proxy | 2 | BINARY=ssh-proxy |
3 | CLIENT_BINARY=ssh-proxy-client | ||
3 | 4 | ||
4 | $(BINARY): $(shell find . -name '*.go') | 5 | $(BINARY): $(CLIENT_BINARY) $(shell find . -name '*.go') |
5 | @[ "$(CLIENT_ID)" ] || ( echo "CLIENT_ID must be set"; exit 1 ) | 6 | @[ "$(CLIENT_ID)" ] || ( echo "CLIENT_ID must be set"; exit 1 ) |
6 | 7 | ||
7 | CGO_ENABLED=0 go build \ | 8 | CGO_ENABLED=0 go build \ |
@@ -9,6 +10,15 @@ $(BINARY): $(shell find . -name '*.go') | |||
9 | -X code.crute.us/mcrute/ssh-proxy/cmd/client.clientId=$(CLIENT_ID)" \ | 10 | -X code.crute.us/mcrute/ssh-proxy/cmd/client.clientId=$(CLIENT_ID)" \ |
10 | -o $@ | 11 | -o $@ |
11 | 12 | ||
13 | $(CLIENT_BINARY): $(shell find . -name '*.go') | ||
14 | @[ "$(CLIENT_ID)" ] || ( echo "CLIENT_ID must be set"; exit 1 ) | ||
15 | |||
16 | CGO_ENABLED=0 go build \ | ||
17 | -tags clientonly \ | ||
18 | -ldflags "-X main.appVersion=$(shell git describe --long --tags --dirty --always) \ | ||
19 | -X code.crute.us/mcrute/ssh-proxy/cmd/client.clientId=$(CLIENT_ID)" \ | ||
20 | -o $@ | ||
21 | |||
12 | .PHONY: vet | 22 | .PHONY: vet |
13 | vet: main.go | 23 | vet: main.go |
14 | go vet $< | 24 | 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 { | |||
21 | OauthDevicePollSecs int `flag:"oauth-device-poll-secs" flag-scope:"web" flag-help:"Number of seconds between polls for oauth device flow"` | 21 | OauthDevicePollSecs int `flag:"oauth-device-poll-secs" flag-scope:"web" flag-help:"Number of seconds between polls for oauth device flow"` |
22 | OauthSessionTimeout time.Duration `flag:"oauth-session-timelut" flag-scope:"web" flag-help:"Timeout before oauth session expires"` | 22 | OauthSessionTimeout time.Duration `flag:"oauth-session-timelut" flag-scope:"web" flag-help:"Timeout before oauth session expires"` |
23 | InviteTimeout time.Duration `flag:"invite-timeout" flag-scope:"register" flag-help:"Timeout before inivitation code expires"` | 23 | InviteTimeout time.Duration `flag:"invite-timeout" flag-scope:"register" flag-help:"Timeout before inivitation code expires"` |
24 | ClientHost string `flag:"client-proxy-host" flag-scope:"client" flag-help:"Hostname and port for proxy"` | ||
24 | } | 25 | } |
25 | 26 | ||
26 | var DefaultConfig = &Config{ | 27 | var DefaultConfig = &Config{ |
@@ -42,4 +43,5 @@ var DefaultConfig = &Config{ | |||
42 | OauthDevicePollSecs: 5, | 43 | OauthDevicePollSecs: 5, |
43 | OauthSessionTimeout: 5 * time.Minute, | 44 | OauthSessionTimeout: 5 * time.Minute, |
44 | InviteTimeout: 1 * time.Hour, | 45 | InviteTimeout: 1 * time.Hour, |
46 | ClientHost: "ssh-proxy.crute.me", | ||
45 | } | 47 | } |
diff --git a/clientmain.go b/clientmain.go new file mode 100644 index 0000000..d75b8bc --- /dev/null +++ b/clientmain.go | |||
@@ -0,0 +1,21 @@ | |||
1 | //go:build clientonly | ||
2 | |||
3 | package main | ||
4 | |||
5 | import ( | ||
6 | "log" | ||
7 | |||
8 | "code.crute.us/mcrute/ssh-proxy/cmd/client" | ||
9 | |||
10 | // Import backup data. By default zoneinfo is installed in the docker image | ||
11 | // if something breaks this will still result in us having correct TZ info. | ||
12 | _ "time/tzdata" | ||
13 | ) | ||
14 | |||
15 | func main() { | ||
16 | cmd := client.NewClientCommand() | ||
17 | |||
18 | if err := cmd.Execute(); err != nil { | ||
19 | log.Fatalf("Error running root command: %s", err) | ||
20 | } | ||
21 | } | ||
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 ( | |||
26 | // This should be compiled into the binary | 26 | // This should be compiled into the binary |
27 | var clientId string | 27 | var clientId string |
28 | 28 | ||
29 | func Register(root *cobra.Command) { | 29 | func NewClientCommand() *cobra.Command { |
30 | clientCmd := &cobra.Command{ | 30 | clientCmd := &cobra.Command{ |
31 | Use: "client proxy-host ssh-to-host ssh-port username", | 31 | Use: "client proxy-host ssh-to-host ssh-port username", |
32 | Short: "Run websocket client", | 32 | Short: "Run websocket client", |
33 | Args: cobra.ExactArgs(4), | 33 | Args: cobra.ExactArgs(3), |
34 | Run: func(c *cobra.Command, args []string) { | 34 | Run: func(c *cobra.Command, args []string) { |
35 | cfg := app.Config{} | 35 | cfg := app.Config{} |
36 | cli.MustGetConfig(c, &cfg) | 36 | cli.MustGetConfig(c, &cfg) |
37 | clientMain(cfg, args[0], args[1], args[2], args[3]) | 37 | clientMain(cfg, args[0], args[1], args[2]) |
38 | }, | 38 | }, |
39 | } | 39 | } |
40 | cli.AddFlags(clientCmd, &app.Config{}, app.DefaultConfig, "client") | 40 | cli.AddFlags(clientCmd, &app.Config{}, app.DefaultConfig, "client") |
41 | root.AddCommand(clientCmd) | 41 | return clientCmd |
42 | } | ||
43 | |||
44 | func Register(root *cobra.Command) { | ||
45 | root.AddCommand(NewClientCommand()) | ||
42 | } | 46 | } |
43 | 47 | ||
44 | func generateCertificateRequest(username, host string) (ed25519.PrivateKey, []byte, error) { | 48 | func generateCertificateRequest(username, host string) (ed25519.PrivateKey, []byte, error) { |
@@ -181,13 +185,13 @@ func fetchOauthToken(ctx context.Context, clientId, proxyHost string) (string, e | |||
181 | return tokenResponse.AccessToken, nil | 185 | return tokenResponse.AccessToken, nil |
182 | } | 186 | } |
183 | 187 | ||
184 | func clientMain(cfg app.Config, proxyHost, host, port, username string) { | 188 | func clientMain(cfg app.Config, host, port, username string) { |
185 | log.SetOutput(os.Stderr) | 189 | log.SetOutput(os.Stderr) |
186 | 190 | ||
187 | ctx, cancel := context.WithCancel(context.Background()) | 191 | ctx, cancel := context.WithCancel(context.Background()) |
188 | defer cancel() | 192 | defer cancel() |
189 | 193 | ||
190 | oauthToken, err := fetchOauthToken(ctx, clientId, proxyHost) | 194 | oauthToken, err := fetchOauthToken(ctx, clientId, cfg.ClientHost) |
191 | if err != nil { | 195 | if err != nil { |
192 | log.Fatalf("Error fetching oauth token: %s", err) | 196 | log.Fatalf("Error fetching oauth token: %s", err) |
193 | } | 197 | } |
@@ -197,7 +201,7 @@ func clientMain(cfg app.Config, proxyHost, host, port, username string) { | |||
197 | log.Fatalf("Error generating certificate request: %s", err) | 201 | log.Fatalf("Error generating certificate request: %s", err) |
198 | } | 202 | } |
199 | 203 | ||
200 | certificate, err := getCertificateFromCA(ctx, oauthToken, certRequest, proxyHost) | 204 | certificate, err := getCertificateFromCA(ctx, oauthToken, certRequest, cfg.ClientHost) |
201 | if err != nil { | 205 | if err != nil { |
202 | log.Fatalf("Error fetching certificate: %s", err) | 206 | log.Fatalf("Error fetching certificate: %s", err) |
203 | } | 207 | } |
@@ -206,7 +210,7 @@ func clientMain(cfg app.Config, proxyHost, host, port, username string) { | |||
206 | log.Fatalf("Error adding certificate to agent: %s", err) | 210 | log.Fatalf("Error adding certificate to agent: %s", err) |
207 | } | 211 | } |
208 | 212 | ||
209 | ws, err := dialProxyHost(ctx, oauthToken, proxyHost, host, port) | 213 | ws, err := dialProxyHost(ctx, oauthToken, cfg.ClientHost, host, port) |
210 | if err != nil { | 214 | if err != nil { |
211 | log.Fatalf("Error dialing proxy host: %s", err) | 215 | log.Fatalf("Error dialing proxy host: %s", err) |
212 | } | 216 | } |
@@ -1,3 +1,5 @@ | |||
1 | //go:build !clientonly | ||
2 | |||
1 | package main | 3 | package main |
2 | 4 | ||
3 | import ( | 5 | import ( |