Skip to content

Commit 9cea961

Browse files
committed
Use SSH address also for host agent
1 parent 2737a11 commit 9cea961

File tree

6 files changed

+26
-24
lines changed

6 files changed

+26
-24
lines changed

Diff for: pkg/hostagent/hostagent.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
171171
instName: instName,
172172
instSSHAddress: inst.SSHAddress,
173173
sshConfig: sshConfig,
174-
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, inst.VMType),
174+
portForwarder: newPortForwarder(sshConfig, inst.SSHAddress, sshLocalPort, rules, inst.VMType),
175175
driver: limaDriver,
176176
sigintCh: sigintCh,
177177
eventEnc: json.NewEncoder(stdout),
@@ -559,7 +559,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
559559
for _, rule := range a.y.PortForwards {
560560
if rule.GuestSocket != "" {
561561
local := hostAddress(rule, guestagentapi.IPPort{})
562-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
562+
_ = forwardSSH(ctx, a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
563563
}
564564
}
565565
}
@@ -571,7 +571,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
571571
if rule.GuestSocket != "" {
572572
local := hostAddress(rule, guestagentapi.IPPort{})
573573
// using ctx.Background() because ctx has already been cancelled
574-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
574+
if err := forwardSSH(context.Background(), a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
575575
errs = append(errs, err)
576576
}
577577
}
@@ -653,11 +653,11 @@ const (
653653
verbCancel = "cancel"
654654
)
655655

656-
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
656+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, command ...string) error {
657657
args := sshConfig.Args()
658658
args = append(args,
659659
"-p", strconv.Itoa(port),
660-
"127.0.0.1",
660+
addr,
661661
"--",
662662
)
663663
args = append(args, command...)
@@ -668,7 +668,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
668668
return nil
669669
}
670670

671-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string, verb string, reverse bool) error {
671+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote string, verb string, reverse bool) error {
672672
args := sshConfig.Args()
673673
args = append(args,
674674
"-T",
@@ -687,15 +687,15 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
687687
"-N",
688688
"-f",
689689
"-p", strconv.Itoa(port),
690-
"127.0.0.1",
690+
addr,
691691
"--",
692692
)
693693
if strings.HasPrefix(local, "/") {
694694
switch verb {
695695
case verbForward:
696696
if reverse {
697697
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
698-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
698+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
699699
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
700700
}
701701
} else {
@@ -710,7 +710,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
710710
case verbCancel:
711711
if reverse {
712712
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
713-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
713+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
714714
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
715715
}
716716
} else {
@@ -730,7 +730,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
730730
if verb == verbForward && strings.HasPrefix(local, "/") {
731731
if reverse {
732732
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
733-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
733+
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
734734
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
735735
}
736736
} else {

Diff for: pkg/hostagent/mount.go

+1-1
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),

Diff for: pkg/hostagent/port.go

+5-3
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
}

Diff for: pkg/hostagent/port_darwin.go

+6-6
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 string, verb string) error {
19+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote string, 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

Diff for: pkg/hostagent/port_others.go

+2-2
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 string, 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 string, verb string) error {
12+
return forwardSSH(ctx, sshConfig, addr, port, local, remote, verb, false)
1313
}
1414

1515
func getFreeVSockPort() (int, error) {

Diff for: pkg/hostagent/port_windows.go

+2-2
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 string, 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 string, 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)