Skip to content

Commit 9dbf7bb

Browse files
committed
Use SSH address also for host agent
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 518389e commit 9dbf7bb

File tree

6 files changed

+31
-24
lines changed

6 files changed

+31
-24
lines changed

pkg/hostagent/hostagent.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
176176
instName: instName,
177177
instSSHAddress: inst.SSHAddress,
178178
sshConfig: sshConfig,
179-
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, inst.VMType),
179+
portForwarder: newPortForwarder(sshConfig, inst.SSHAddress, sshLocalPort, rules, inst.VMType),
180180
driver: limaDriver,
181181
sigintCh: sigintCh,
182182
eventEnc: json.NewEncoder(stdout),
@@ -565,7 +565,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
565565
for _, rule := range a.y.PortForwards {
566566
if rule.GuestSocket != "" {
567567
local := hostAddress(rule, guestagentapi.IPPort{})
568-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
568+
_ = forwardSSH(ctx, a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
569569
}
570570
}
571571
}
@@ -580,13 +580,13 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
580580
if rule.GuestSocket != "" {
581581
local := hostAddress(rule, guestagentapi.IPPort{})
582582
// using ctx.Background() because ctx has already been cancelled
583-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
583+
if err := forwardSSH(context.Background(), a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
584584
errs = append(errs, err)
585585
}
586586
}
587587
}
588588
if a.driver.ForwardGuestAgent() {
589-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
589+
if err := forwardSSH(context.Background(), a.sshConfig, a.instSSHAddress, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
590590
errs = append(errs, err)
591591
}
592592
}
@@ -595,7 +595,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
595595
for {
596596
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
597597
if a.driver.ForwardGuestAgent() {
598-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
598+
_ = forwardSSH(ctx, a.sshConfig, a.instSSHAddress, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
599599
}
600600
}
601601
client, err := a.getOrCreateClient(ctx)
@@ -682,11 +682,11 @@ const (
682682
verbCancel = "cancel"
683683
)
684684

685-
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
685+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, command ...string) error {
686686
args := sshConfig.Args()
687687
args = append(args,
688688
"-p", strconv.Itoa(port),
689-
"127.0.0.1",
689+
addr,
690690
"--",
691691
)
692692
args = append(args, command...)
@@ -697,7 +697,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
697697
return nil
698698
}
699699

700-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string, reverse bool) error {
700+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote, verb string, reverse bool) error {
701701
args := sshConfig.Args()
702702
args = append(args,
703703
"-T",
@@ -716,15 +716,15 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
716716
"-N",
717717
"-f",
718718
"-p", strconv.Itoa(port),
719-
"127.0.0.1",
719+
addr,
720720
"--",
721721
)
722722
if strings.HasPrefix(local, "/") {
723723
switch verb {
724724
case verbForward:
725725
if reverse {
726726
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
727-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
727+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
728728
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
729729
}
730730
} else {
@@ -739,7 +739,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
739739
case verbCancel:
740740
if reverse {
741741
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
742-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
742+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
743743
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
744744
}
745745
} else {
@@ -759,7 +759,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
759759
if verb == verbForward && strings.HasPrefix(local, "/") {
760760
if reverse {
761761
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
762-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
762+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
763763
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
764764
}
765765
} else {

pkg/hostagent/mount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (a *HostAgent) setupMount(m limayaml.Mount) (*mount, error) {
5858
Driver: *m.SSHFS.SFTPDriver,
5959
SSHConfig: a.sshConfig,
6060
LocalPath: location,
61-
Host: "127.0.0.1",
61+
Host: a.instSSHAddress,
6262
Port: a.sshLocalPort,
6363
RemotePath: mountPoint,
6464
Readonly: !(*m.Writable),

pkg/hostagent/port.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ import (
1212

1313
type portForwarder struct {
1414
sshConfig *ssh.SSHConfig
15+
sshHostAddr string
1516
sshHostPort int
1617
rules []limayaml.PortForward
1718
vmType limayaml.VMType
1819
}
1920

2021
const sshGuestPort = 22
2122

22-
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limayaml.PortForward, vmType limayaml.VMType) *portForwarder {
23+
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostAddr string, sshHostPort int, rules []limayaml.PortForward, vmType limayaml.VMType) *portForwarder {
2324
return &portForwarder{
2425
sshConfig: sshConfig,
26+
sshHostAddr: sshHostAddr,
2527
sshHostPort: sshHostPort,
2628
rules: rules,
2729
vmType: vmType,
@@ -88,7 +90,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev api.Event, instSSHAddre
8890
continue
8991
}
9092
logrus.Infof("Stopping forwarding TCP from %s to %s", remote, local)
91-
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbCancel); err != nil {
93+
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostAddr, pf.sshHostPort, local, remote, verbCancel); err != nil {
9294
logrus.WithError(err).Warnf("failed to stop forwarding tcp port %d", f.Port)
9395
}
9496
}
@@ -99,7 +101,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev api.Event, instSSHAddre
99101
continue
100102
}
101103
logrus.Infof("Forwarding TCP from %s to %s", remote, local)
102-
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbForward); err != nil {
104+
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostAddr, pf.sshHostPort, local, remote, verbForward); err != nil {
103105
logrus.WithError(err).Warnf("failed to set up forwarding tcp port %d (negligible if already forwarded)", f.Port)
104106
}
105107
}

pkg/hostagent/port_darwin.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
)
1717

1818
// forwardTCP is not thread-safe
19-
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
19+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote, verb string) error {
2020
if strings.HasPrefix(local, "/") {
21-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
21+
return forwardSSH(ctx, sshConfig, addr, port, local, remote, verb, false)
2222
}
2323
localIPStr, localPortStr, err := net.SplitHostPort(local)
2424
if err != nil {
@@ -31,7 +31,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
3131
}
3232

3333
if !localIP.Equal(api.IPv4loopback1) || localPort >= 1024 {
34-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
34+
return forwardSSH(ctx, sshConfig, addr, port, local, remote, verb, false)
3535
}
3636

3737
// on macOS, listening on 127.0.0.1:80 requires root while 0.0.0.0:80 does not require root.
@@ -46,7 +46,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
4646
localUnix := plf.unixAddr.Name
4747
_ = plf.Close()
4848
delete(pseudoLoopbackForwarders, local)
49-
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
49+
if err := forwardSSH(ctx, sshConfig, addr, port, localUnix, remote, verb, false); err != nil {
5050
return err
5151
}
5252
} else {
@@ -61,12 +61,12 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
6161
}
6262
localUnix := filepath.Join(localUnixDir, "sock")
6363
logrus.Debugf("forwarding %q to %q", localUnix, remote)
64-
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
64+
if err := forwardSSH(ctx, sshConfig, addr, port, localUnix, remote, verb, false); err != nil {
6565
return err
6666
}
6767
plf, err := newPseudoLoopbackForwarder(localPort, localUnix)
6868
if err != nil {
69-
if cancelErr := forwardSSH(ctx, sshConfig, port, localUnix, remote, verbCancel, false); cancelErr != nil {
69+
if cancelErr := forwardSSH(ctx, sshConfig, addr, port, localUnix, remote, verbCancel, false); cancelErr != nil {
7070
logrus.WithError(cancelErr).Warnf("failed to cancel forwarding %q to %q", localUnix, remote)
7171
}
7272
return err

pkg/hostagent/port_others.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/lima-vm/sshocker/pkg/ssh"
99
)
1010

11-
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
12-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
11+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote, verb string) error {
12+
return forwardSSH(ctx, sshConfig, addr, port, local, remote, verb, false)
1313
}
1414

1515
func getFreeVSockPort() (int, error) {

pkg/hostagent/port_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import (
77
"github.com/lima-vm/sshocker/pkg/ssh"
88
)
99

10+
<<<<<<< HEAD
1011
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
1112
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
13+
=======
14+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote string, verb string) error {
15+
return forwardSSH(ctx, sshConfig, addr, port, local, remote, verb, false)
16+
>>>>>>> Use SSH address also for host agent
1217
}
1318

1419
func getFreeVSockPort() (int, error) {

0 commit comments

Comments
 (0)