Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian-McM committed Feb 27, 2025
1 parent 697530d commit e7ed676
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
7 changes: 6 additions & 1 deletion guardian/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ func (cfg *Config) TLSConfig() (*tls.Config, *tls.Certificate, error) {
logrus.Debug("expecting TLS server name: ", serverName)
tlsConfig.ServerName = serverName
} else {
tlsConfig.ServerName = strings.Split(cfg.VoltronURL, ":")[0]
u, err := url.Parse(cfg.VoltronURL)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse voltron url %s: %w", cfg.VoltronURL, err)
}

tlsConfig.ServerName = u.Hostname()
}

tlsConfig.RootCAs = rootCA
Expand Down
2 changes: 2 additions & 0 deletions guardian/pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func Run(cfg config.Config, proxyTargets []server.Target) {
logrus.WithError(err).Fatal("Failed to create tls config")
}

logrus.Infof("Using server name %s", tlsConfig.ServerName)

ctx := GetShutdownContext()

dialer, err := tunnel.NewTLSSessionDialer(cfg.VoltronURL, tlsConfig, tunnelDialOpts...)
Expand Down
51 changes: 29 additions & 22 deletions guardian/pkg/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type tunnel struct {
acceptConnExecutor asyncutil.AsyncCommandExecutor[any, net.Conn]
getAddrExecutor asyncutil.AsyncCommandExecutor[any, net.Addr]

stopExecutors context.CancelFunc
stopCoordinator context.CancelFunc

connectOnce sync.Once

Expand Down Expand Up @@ -103,26 +103,33 @@ func (t *tunnel) Connect(ctx context.Context) error {
return
}

coordinatorCtx, stopDispatcher := context.WithCancel(context.Background())
t.stopExecutors = stopDispatcher

t.openConnExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff, func(ctx context.Context, a any) (net.Conn, error) {
logrus.Debug("Opening connection to other side of tunnel.")
return t.session.Open()
})
t.getListenerExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff, func(ctx context.Context, a any) (net.Listener, error) {
logrus.Debug("Getting listener for requests from the other side of the tunnel.")
return newListener(t), nil
})
t.acceptConnExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff, func(ctx context.Context, a any) (net.Conn, error) {
logrus.Debug("Accepting connection from the other side of the tunnel.")

return t.session.Accept()
})
t.getAddrExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff, func(ctx context.Context, a any) (net.Addr, error) {
logrus.Debug("Getting tunnel address.")
return newTunnelAddress(t.session.Addr().String()), nil
})
coordinatorCtx, stopCoordinator := context.WithCancel(context.Background())
t.stopCoordinator = stopCoordinator

// We use AsyncCommandExecutors to handle interacting with the session. The executors run the commands in the
// background and respond over a channel. The executors help to manage the life cycle of these commands and
// facilitate fail / retry handling, as well as shutdown logic.
t.openConnExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff,
func(ctx context.Context, a any) (net.Conn, error) {
logrus.Debug("Opening connection to other side of tunnel.")
return t.session.Open()
})
t.getListenerExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff,
func(ctx context.Context, a any) (net.Listener, error) {
logrus.Debug("Getting listener for requests from the other side of the tunnel.")
return newListener(t), nil
})
t.acceptConnExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff,
func(ctx context.Context, a any) (net.Conn, error) {
logrus.Debug("Accepting connection from the other side of the tunnel.")
return t.session.Accept()
})
t.getAddrExecutor = asyncutil.NewAsyncCommandExecutor(coordinatorCtx, t.cmdErrBuff,
func(ctx context.Context, a any) (net.Addr, error) {
logrus.Debug("Getting tunnel address.")
return newTunnelAddress(t.session.Addr().String()), nil
})

go t.startServiceLoop(ctx)
})
return err
Expand All @@ -136,7 +143,7 @@ func (t *tunnel) startServiceLoop(ctx context.Context) {

defer func() {
defer close(t.closed)
defer t.stopExecutors()
defer t.stopCoordinator()

if t.session != nil {
logrus.Info("Closing session.")
Expand Down

0 comments on commit e7ed676

Please sign in to comment.