Skip to content

Commit f13c6b6

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

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

pkg/hostagent/hostagent.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
178178
instName: instName,
179179
instSSHAddress: inst.SSHAddress,
180180
sshConfig: sshConfig,
181-
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, inst.VMType),
181+
portForwarder: newPortForwarder(sshConfig, inst.SSHAddress, sshLocalPort, rules, inst.VMType),
182182
driver: limaDriver,
183183
signalCh: signalCh,
184184
eventEnc: json.NewEncoder(stdout),
@@ -567,7 +567,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
567567
for _, rule := range a.y.PortForwards {
568568
if rule.GuestSocket != "" {
569569
local := hostAddress(rule, guestagentapi.IPPort{})
570-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
570+
_ = forwardSSH(ctx, a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
571571
}
572572
}
573573
}
@@ -582,13 +582,13 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
582582
if rule.GuestSocket != "" {
583583
local := hostAddress(rule, guestagentapi.IPPort{})
584584
// using ctx.Background() because ctx has already been cancelled
585-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
585+
if err := forwardSSH(context.Background(), a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
586586
errs = append(errs, err)
587587
}
588588
}
589589
}
590590
if a.driver.ForwardGuestAgent() {
591-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
591+
if err := forwardSSH(context.Background(), a.sshConfig, a.instSSHAddress, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
592592
errs = append(errs, err)
593593
}
594594
}
@@ -597,7 +597,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
597597
for {
598598
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
599599
if a.driver.ForwardGuestAgent() {
600-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
600+
_ = forwardSSH(ctx, a.sshConfig, a.instSSHAddress, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
601601
}
602602
}
603603
client, err := a.getOrCreateClient(ctx)
@@ -684,11 +684,11 @@ const (
684684
verbCancel = "cancel"
685685
)
686686

687-
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
687+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, command ...string) error {
688688
args := sshConfig.Args()
689689
args = append(args,
690690
"-p", strconv.Itoa(port),
691-
"127.0.0.1",
691+
addr,
692692
"--",
693693
)
694694
args = append(args, command...)
@@ -699,7 +699,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
699699
return nil
700700
}
701701

702-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string, reverse bool) error {
702+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote, verb string, reverse bool) error {
703703
args := sshConfig.Args()
704704
args = append(args,
705705
"-T",
@@ -718,15 +718,15 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
718718
"-N",
719719
"-f",
720720
"-p", strconv.Itoa(port),
721-
"127.0.0.1",
721+
addr,
722722
"--",
723723
)
724724
if strings.HasPrefix(local, "/") {
725725
switch verb {
726726
case verbForward:
727727
if reverse {
728728
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
729-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
729+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
730730
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
731731
}
732732
} else {
@@ -741,7 +741,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
741741
case verbCancel:
742742
if reverse {
743743
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
744-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
744+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
745745
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
746746
}
747747
} else {
@@ -761,7 +761,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
761761
if verb == verbForward && strings.HasPrefix(local, "/") {
762762
if reverse {
763763
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
764-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
764+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
765765
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
766766
}
767767
} 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/lima-vm/sshocker/pkg/ssh"
88
)
99

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

1414
func getFreeVSockPort() (int, error) {

0 commit comments

Comments
 (0)