Skip to content

Commit

Permalink
Add feature to open the browser on ready (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Nov 7, 2019
1 parent 87fdf45 commit bffa08c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Forwarding from 127.0.0.1:57866 -> 8443
Forwarding from [::1]:57866 -> 8443
```

Open the URL and you can access the Kubernetes Dashboard with the token.
Kauthproxy will open the browser and you can access the Kubernetes Dashboard.

### Access Kibana

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/golang/mock v1.3.1
github.com/google/wire v0.3.0
github.com/int128/listener v1.0.0
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
golang.org/x/sync v0.0.0-20190423024810-112230192c58
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
10 changes: 10 additions & 0 deletions pkg/adaptors/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"

"github.com/google/wire"
"github.com/pkg/browser"
"golang.org/x/xerrors"
)

Expand All @@ -16,6 +17,7 @@ var Set = wire.NewSet(

type Interface interface {
AllocateLocalPort() (int, error)
OpenBrowser(url string) error
}

type Env struct{}
Expand All @@ -33,3 +35,11 @@ func (*Env) AllocateLocalPort() (int, error) {
}
return addr.Port, nil
}

// OpenBrowser opens the default browser.
func (*Env) OpenBrowser(url string) error {
if err := browser.OpenURL(url); err != nil {
return xerrors.Errorf("could not open the browser: %w", err)
}
return nil
}
14 changes: 14 additions & 0 deletions pkg/adaptors/env/mock_env/mock_env.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions pkg/usecases/authproxy/auth_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/url"
"strings"
"sync"

"github.com/cenkalti/backoff"
"github.com/google/wire"
Expand Down Expand Up @@ -85,9 +86,10 @@ func (u *AuthProxy) Do(ctx context.Context, o Option) error {
TargetHost: "localhost",
TargetPort: transitPort,
}
var once sync.Once
b := backoff.NewExponentialBackOff()
if err := backoff.Retry(func() error {
if err := u.run(ctx, pfo, rpo); err != nil {
if err := u.run(ctx, pfo, rpo, &once); err != nil {
if xerrors.Is(err, portForwarderConnectionLostError) {
u.Logger.Printf("retrying: %s", err)
return err
Expand All @@ -105,7 +107,7 @@ func (u *AuthProxy) Do(ctx context.Context, o Option) error {
//
// 1. Run a port forwarder.
// 2. When the port forwarder is ready, run a reverse proxy.
// 3. When the reverse proxy is ready, open the browser.
// 3. When the reverse proxy is ready, open the browser (only first time).
//
// When the context is canceled,
//
Expand All @@ -115,7 +117,7 @@ func (u *AuthProxy) Do(ctx context.Context, o Option) error {
// This never returns nil.
// It returns an error which wraps context.Canceled if the context is canceled.
// It returns portForwarderConnectionLostError if a connection has lost.
func (u *AuthProxy) run(ctx context.Context, pfo portforwarder.Option, rpo reverseproxy.Option) error {
func (u *AuthProxy) run(ctx context.Context, pfo portforwarder.Option, rpo reverseproxy.Option, once *sync.Once) error {
portForwarderIsReady := make(chan struct{})
reverseProxyIsReady := make(chan reverseproxy.Instance, 1)
stopPortForwarder := make(chan struct{})
Expand Down Expand Up @@ -163,7 +165,13 @@ func (u *AuthProxy) run(ctx context.Context, pfo portforwarder.Option, rpo rever
select {
case rp := <-reverseProxyIsReady:
u.Logger.V(1).Infof("the reverse proxy is ready")
u.Logger.Printf("Open %s", rp.URL().String())
rpURL := rp.URL().String()
u.Logger.Printf("Open %s", rpURL)
once.Do(func() {
if err := u.Env.OpenBrowser(rpURL); err != nil {
u.Logger.Printf("error while opening the browser: %s", err)
}
})
// shutdown the reverse proxy when the context is done
eg.Go(func() error {
<-ctx.Done()
Expand Down
14 changes: 10 additions & 4 deletions pkg/usecases/authproxy/auth_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func TestAuthProxy_Do(t *testing.T) {
return nil
})
m := newMocks(ctrl)
m.env.EXPECT().
OpenBrowser("http://localhost:8000")
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
Expand Down Expand Up @@ -270,6 +272,8 @@ func TestAuthProxy_Do(t *testing.T) {
}).
Times(2)
m := newMocks(ctrl)
m.env.EXPECT().
OpenBrowser("http://localhost:8000")
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
Expand All @@ -295,18 +299,18 @@ func TestAuthProxy_Do(t *testing.T) {
type mocks struct {
resolverFactory *mock_resolver.MockFactoryInterface
transportFactory *mock_transport.MockFactoryInterface
network *mock_env.MockInterface
env *mock_env.MockInterface
}
newMocks := func(ctrl *gomock.Controller) mocks {
m := mocks{
resolverFactory: mock_resolver.NewMockFactoryInterface(ctrl),
transportFactory: mock_transport.NewMockFactoryInterface(ctrl),
network: mock_env.NewMockInterface(ctrl),
env: mock_env.NewMockInterface(ctrl),
}
m.transportFactory.EXPECT().
New(&restConfig).
Return(&authProxyTransport, nil)
m.network.EXPECT().
m.env.EXPECT().
AllocateLocalPort().
Return(transitPort, nil)
mockResolver := mock_resolver.NewMockInterface(ctrl)
Expand Down Expand Up @@ -360,12 +364,14 @@ func TestAuthProxy_Do(t *testing.T) {
return nil
})
m := newMocks(ctrl)
m.env.EXPECT().
OpenBrowser("http://localhost:8000")
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
TransportFactory: m.transportFactory,
Env: m.network,
Env: m.env,
Logger: mock_logger.New(t),
}
o := Option{
Expand Down

0 comments on commit bffa08c

Please sign in to comment.