Skip to content

Commit

Permalink
feat: add flag to disable a browser popping up
Browse files Browse the repository at this point in the history
Signed-off-by: Johan Siebens <johan.siebens@gmail.com>
  • Loading branch information
jsiebens committed May 23, 2022
1 parent d1b8d04 commit f269288
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
14 changes: 8 additions & 6 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
"time"
)

func Authenticate(ctx context.Context, proxy string, caFile string, insecureSkipVerify bool, showQR bool) error {
clt, err := createClient(proxy, caFile, insecureSkipVerify, showQR)
func Authenticate(ctx context.Context, proxy string, caFile string, insecureSkipVerify bool, noBrowser, showQR bool) error {
clt, err := createClient(proxy, caFile, insecureSkipVerify, noBrowser, showQR)
if err != nil {
return err
}
return clt.authenticate(ctx)
}

func StartClient(ctx context.Context, proxy string, listenPort uint64, target string, caFile string, insecureSkipVerify bool, showQR bool, onConnect OnConnect) error {
clt, err := createClient(proxy, caFile, insecureSkipVerify, showQR)
func StartClient(ctx context.Context, proxy string, listenPort uint64, target string, caFile string, insecureSkipVerify bool, noBrowser, showQR bool, onConnect OnConnect) error {
clt, err := createClient(proxy, caFile, insecureSkipVerify, noBrowser, showQR)
if err != nil {
return err
}
Expand All @@ -42,7 +42,7 @@ func StartClient(ctx context.Context, proxy string, listenPort uint64, target st
return clt.start(ctx)
}

func createClient(proxy, caFile string, insecureSkipVerify bool, showQR bool) (*Client, error) {
func createClient(proxy, caFile string, insecureSkipVerify bool, noBrowser, showQR bool) (*Client, error) {
var caCertPool *x509.CertPool

targetBaseUrl, err := util.NormalizeHttpUrl(proxy)
Expand Down Expand Up @@ -95,6 +95,7 @@ func createClient(proxy, caFile string, insecureSkipVerify bool, showQR bool) (*
c := &Client{
httpClient: resty.NewWithClient(client),
dialer: websocketDialer,
noBrowser: noBrowser,
showQR: showQR,
}

Expand All @@ -106,6 +107,7 @@ type Client struct {
dialer *websocket.Dialer
forwarder *Forwarder
target string
noBrowser bool
showQR bool
}

Expand Down Expand Up @@ -301,7 +303,7 @@ func (c *Client) connectToProxy(rootCtx context.Context, proxyURL string, header
}

func (c *Client) openOrShowAuthUrl(sn *api.SessionTokenResponse) {
if c.showQR || util.OpenURL(sn.AuthUrl) != nil {
if c.noBrowser || c.showQR || util.OpenURL(sn.AuthUrl) != nil {
fmt.Println()
fmt.Println("To authenticate, visit:")
fmt.Println()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func loginCommand() *coral.Command {
return fmt.Errorf("required flag --proxy-addr is missing")
}

return client.Authenticate(cmd.Context(), proxyAddr, caFile, tlsSkipVerify, showQR)
return client.Authenticate(cmd.Context(), proxyAddr, caFile, tlsSkipVerify, noBrowser, showQR)
}

return command
Expand Down
10 changes: 6 additions & 4 deletions internal/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ var (
proxyAddrFlag string
tlsSkipVerify bool
caFile string
noBrowser bool
showQR bool
)

func registerProxyFlags(command *coral.Command) {
command.Flags().StringVarP(&proxyAddrFlag, "proxy-addr", "r", "", fmt.Sprintf("Addr of the Brink proxy. This can also be specified via the environment variable %s.", BrinkProxyAddr))
command.Flags().BoolVar(&tlsSkipVerify, "tls-skip-verify", false, "Disable verification of TLS certificates, highly discouraged as it decreases the security of data transmissions.")
command.Flags().StringVar(&caFile, "ca-file", "", "Path on the local disk to a single PEM-encoded CA certificate to verify the proxy or server SSL certificate.")
command.Flags().BoolVar(&noBrowser, "no-browser", false, "Disable the usage of a browser, just print the login URL")
command.Flags().BoolVar(&showQR, "qr", false, "Show QR code for login URLs")
}

Expand Down Expand Up @@ -72,20 +74,20 @@ func connectCommand() *coral.Command {
}

if listenOnStdin {
return client.StartClient(cancelCtx, proxyAddr, 0, targetAddr, caFile, tlsSkipVerify, showQR, client.StartNC)
return client.StartClient(cancelCtx, proxyAddr, 0, targetAddr, caFile, tlsSkipVerify, noBrowser, showQR, client.StartNC)
}

if execCommand != "" {
onConnect, result := execOnConnect(execCommand, noArgs, args, cancelFunc)

if err := client.StartClient(cancelCtx, proxyAddr, 0, targetAddr, caFile, tlsSkipVerify, showQR, onConnect); err != nil {
if err := client.StartClient(cancelCtx, proxyAddr, 0, targetAddr, caFile, tlsSkipVerify, noBrowser, showQR, onConnect); err != nil {
return err
}

return <-result
}

return client.StartClient(cancelCtx, proxyAddr, listenPort, targetAddr, caFile, tlsSkipVerify, showQR, client.PrintListenerInfo)
return client.StartClient(cancelCtx, proxyAddr, listenPort, targetAddr, caFile, tlsSkipVerify, noBrowser, showQR, client.PrintListenerInfo)
}

return command
Expand Down Expand Up @@ -137,7 +139,7 @@ func sshCommand() *coral.Command {

onConnect, result := execOnConnect("ssh", buildArgs, args, cancelFunc)

if err := client.StartClient(cancelCtx, proxyAddr, 0, targetAddr, caFile, tlsSkipVerify, showQR, onConnect); err != nil {
if err := client.StartClient(cancelCtx, proxyAddr, 0, targetAddr, caFile, tlsSkipVerify, noBrowser, showQR, onConnect); err != nil {
return err
}

Expand Down

0 comments on commit f269288

Please sign in to comment.