From a35c994bc86c7e37622ba8e4fc1e6e799572e616 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sat, 22 Nov 2025 23:32:26 +0000 Subject: [PATCH] feat(port-forwarding): add `{{VPN_INTERFACE}}` template variable --- internal/portforward/service/command.go | 3 ++- internal/portforward/service/command_test.go | 7 ++++--- internal/portforward/service/start.go | 2 +- internal/portforward/service/stop.go | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/portforward/service/command.go b/internal/portforward/service/command.go index f2a20ed9..4d351c25 100644 --- a/internal/portforward/service/command.go +++ b/internal/portforward/service/command.go @@ -10,7 +10,7 @@ import ( ) func runCommand(ctx context.Context, cmder Cmder, logger Logger, - commandTemplate string, ports []uint16, + commandTemplate string, ports []uint16, vpnInterface string, ) (err error) { portStrings := make([]string, len(ports)) for i, port := range ports { @@ -19,6 +19,7 @@ func runCommand(ctx context.Context, cmder Cmder, logger Logger, portsString := strings.Join(portStrings, ",") commandString := strings.ReplaceAll(commandTemplate, "{{PORTS}}", portsString) commandString = strings.ReplaceAll(commandString, "{{PORT}}", portStrings[0]) + commandString = strings.ReplaceAll(commandString, "{{VPN_INTERFACE}}", vpnInterface) args, err := command.Split(commandString) if err != nil { return fmt.Errorf("parsing command: %w", err) diff --git a/internal/portforward/service/command_test.go b/internal/portforward/service/command_test.go index d2747250..2e1e719f 100644 --- a/internal/portforward/service/command_test.go +++ b/internal/portforward/service/command_test.go @@ -17,12 +17,13 @@ func Test_Service_runCommand(t *testing.T) { ctx := context.Background() cmder := command.New() - const commandTemplate = `/bin/sh -c "echo {{PORTS}}"` + const commandTemplate = `/bin/sh -c "echo {{PORTS}}-{{PORT}}-{{VPN_INTERFACE}}"` ports := []uint16{1234, 5678} + const vpnInterface = "tun0" logger := NewMockLogger(ctrl) - logger.EXPECT().Info("1234,5678") + logger.EXPECT().Info("1234,5678-1234-tun0") - err := runCommand(ctx, cmder, logger, commandTemplate, ports) + err := runCommand(ctx, cmder, logger, commandTemplate, ports, vpnInterface) require.NoError(t, err) } diff --git a/internal/portforward/service/start.go b/internal/portforward/service/start.go index a13ac0e4..ff3b5248 100644 --- a/internal/portforward/service/start.go +++ b/internal/portforward/service/start.go @@ -74,7 +74,7 @@ func (s *Service) Start(ctx context.Context) (runError <-chan error, err error) s.portMutex.Unlock() if s.settings.UpCommand != "" { - err = runCommand(ctx, s.cmder, s.logger, s.settings.UpCommand, ports) + err = runCommand(ctx, s.cmder, s.logger, s.settings.UpCommand, ports, s.settings.Interface) if err != nil { err = fmt.Errorf("running up command: %w", err) s.logger.Error(err.Error()) diff --git a/internal/portforward/service/stop.go b/internal/portforward/service/stop.go index 085dbcdc..8e86dfc9 100644 --- a/internal/portforward/service/stop.go +++ b/internal/portforward/service/stop.go @@ -34,7 +34,7 @@ func (s *Service) cleanup() (err error) { const downTimeout = 60 * time.Second ctx, cancel := context.WithTimeout(context.Background(), downTimeout) defer cancel() - err = runCommand(ctx, s.cmder, s.logger, s.settings.DownCommand, s.ports) + err = runCommand(ctx, s.cmder, s.logger, s.settings.DownCommand, s.ports, s.settings.Interface) if err != nil { err = fmt.Errorf("running down command: %w", err) s.logger.Error(err.Error())