@@ -567,6 +567,21 @@ const (
567
567
verbCancel = "cancel"
568
568
)
569
569
570
+ func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , command ... string ) error {
571
+ args := sshConfig .Args ()
572
+ args = append (args ,
573
+ "-p" , strconv .Itoa (port ),
574
+ "127.0.0.1" ,
575
+ "--" ,
576
+ )
577
+ args = append (args , command ... )
578
+ cmd := exec .CommandContext (ctx , sshConfig .Binary (), args ... )
579
+ if out , err := cmd .Output (); err != nil {
580
+ return fmt .Errorf ("failed to run %v: %q: %w" , cmd .Args , string (out ), err )
581
+ }
582
+ return nil
583
+ }
584
+
570
585
func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote string , verb string , reverse bool ) error {
571
586
args := sshConfig .Args ()
572
587
args = append (args ,
@@ -594,36 +609,49 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
594
609
case verbForward :
595
610
if reverse {
596
611
logrus .Infof ("Forwarding %q (host) to %q (guest)" , local , remote )
612
+ if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
613
+ logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) before setting up forwarding" , remote )
614
+ }
597
615
} else {
598
616
logrus .Infof ("Forwarding %q (guest) to %q (host)" , remote , local )
599
- }
600
- if err := os . RemoveAll ( local ); err != nil {
601
- logrus . WithError ( err ). Warnf ( "Failed to clean up %q (host) before setting up forwarding" , local )
617
+ if err := os . RemoveAll ( local ); err != nil {
618
+ logrus . WithError ( err ). Warnf ( "Failed to clean up %q (host) before setting up forwarding" , local )
619
+ }
602
620
}
603
621
if err := os .MkdirAll (filepath .Dir (local ), 0750 ); err != nil {
604
622
return fmt .Errorf ("can't create directory for local socket %q: %w" , local , err )
605
623
}
606
624
case verbCancel :
607
625
if reverse {
608
626
logrus .Infof ("Stopping forwarding %q (host) to %q (guest)" , local , remote )
627
+ if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
628
+ logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after stopping forwarding" , remote )
629
+ }
609
630
} else {
610
631
logrus .Infof ("Stopping forwarding %q (guest) to %q (host)" , remote , local )
632
+ defer func () {
633
+ if err := os .RemoveAll (local ); err != nil {
634
+ logrus .WithError (err ).Warnf ("Failed to clean up %q (host) after stopping forwarding" , local )
635
+ }
636
+ }()
611
637
}
612
- defer func () {
613
- if err := os .RemoveAll (local ); err != nil {
614
- logrus .WithError (err ).Warnf ("Failed to clean up %q (host) after stopping forwarding" , local )
615
- }
616
- }()
617
638
default :
618
639
panic (fmt .Errorf ("invalid verb %q" , verb ))
619
640
}
620
641
}
621
642
cmd := exec .CommandContext (ctx , sshConfig .Binary (), args ... )
622
643
if out , err := cmd .Output (); err != nil {
623
644
if verb == verbForward && strings .HasPrefix (local , "/" ) {
624
- logrus .WithError (err ).Warnf ("Failed to set up forward from %q (guest) to %q (host)" , remote , local )
625
- if removeErr := os .RemoveAll (local ); err != nil {
626
- logrus .WithError (removeErr ).Warnf ("Failed to clean up %q (host) after forwarding failed" , local )
645
+ if reverse {
646
+ logrus .WithError (err ).Warnf ("Failed to set up forward from %q (host) to %q (guest)" , local , remote )
647
+ if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
648
+ logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after forwarding failed" , remote )
649
+ }
650
+ } else {
651
+ logrus .WithError (err ).Warnf ("Failed to set up forward from %q (guest) to %q (host)" , remote , local )
652
+ if removeErr := os .RemoveAll (local ); err != nil {
653
+ logrus .WithError (removeErr ).Warnf ("Failed to clean up %q (host) after forwarding failed" , local )
654
+ }
627
655
}
628
656
}
629
657
return fmt .Errorf ("failed to run %v: %q: %w" , cmd .Args , string (out ), err )
0 commit comments