Skip to content

Commit

Permalink
Use SSH address also for host agent
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Nov 24, 2023
1 parent 2737a11 commit 9cea961
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
20 changes: 10 additions & 10 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
instName: instName,
instSSHAddress: inst.SSHAddress,
sshConfig: sshConfig,
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, inst.VMType),
portForwarder: newPortForwarder(sshConfig, inst.SSHAddress, sshLocalPort, rules, inst.VMType),
driver: limaDriver,
sigintCh: sigintCh,
eventEnc: json.NewEncoder(stdout),
Expand Down Expand Up @@ -559,7 +559,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
for _, rule := range a.y.PortForwards {
if rule.GuestSocket != "" {
local := hostAddress(rule, guestagentapi.IPPort{})
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
_ = forwardSSH(ctx, a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
}
}
}
Expand All @@ -571,7 +571,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
if rule.GuestSocket != "" {
local := hostAddress(rule, guestagentapi.IPPort{})
// using ctx.Background() because ctx has already been cancelled
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
if err := forwardSSH(context.Background(), a.sshConfig, a.instSSHAddress, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
errs = append(errs, err)
}
}
Expand Down Expand Up @@ -653,11 +653,11 @@ const (
verbCancel = "cancel"
)

func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, command ...string) error {
args := sshConfig.Args()
args = append(args,
"-p", strconv.Itoa(port),
"127.0.0.1",
addr,
"--",
)
args = append(args, command...)
Expand All @@ -668,7 +668,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
return nil
}

func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string, verb string, reverse bool) error {
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, addr string, port int, local, remote string, verb string, reverse bool) error {
args := sshConfig.Args()
args = append(args,
"-T",
Expand All @@ -687,15 +687,15 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
"-N",
"-f",
"-p", strconv.Itoa(port),
"127.0.0.1",
addr,
"--",
)
if strings.HasPrefix(local, "/") {
switch verb {
case verbForward:
if reverse {
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
}
} else {
Expand All @@ -710,7 +710,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
case verbCancel:
if reverse {
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
}
} else {
Expand All @@ -730,7 +730,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
if verb == verbForward && strings.HasPrefix(local, "/") {
if reverse {
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
if err := executeSSH(ctx, sshConfig, addr, port, "rm", "-f", remote); err != nil {
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostagent/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (a *HostAgent) setupMount(m limayaml.Mount) (*mount, error) {
Driver: *m.SSHFS.SFTPDriver,
SSHConfig: a.sshConfig,
LocalPath: location,
Host: "127.0.0.1",
Host: a.instSSHAddress,
Port: a.sshLocalPort,
RemotePath: mountPoint,
Readonly: !(*m.Writable),
Expand Down
8 changes: 5 additions & 3 deletions pkg/hostagent/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import (

type portForwarder struct {
sshConfig *ssh.SSHConfig
sshHostAddr string
sshHostPort int
rules []limayaml.PortForward
vmType limayaml.VMType
}

const sshGuestPort = 22

func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limayaml.PortForward, vmType limayaml.VMType) *portForwarder {
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostAddr string, sshHostPort int, rules []limayaml.PortForward, vmType limayaml.VMType) *portForwarder {
return &portForwarder{
sshConfig: sshConfig,
sshHostAddr: sshHostAddr,
sshHostPort: sshHostPort,
rules: rules,
vmType: vmType,
Expand Down Expand Up @@ -88,7 +90,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev api.Event, instSSHAddre
continue
}
logrus.Infof("Stopping forwarding TCP from %s to %s", remote, local)
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbCancel); err != nil {
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostAddr, pf.sshHostPort, local, remote, verbCancel); err != nil {
logrus.WithError(err).Warnf("failed to stop forwarding tcp port %d", f.Port)
}
}
Expand All @@ -99,7 +101,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev api.Event, instSSHAddre
continue
}
logrus.Infof("Forwarding TCP from %s to %s", remote, local)
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbForward); err != nil {
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostAddr, pf.sshHostPort, local, remote, verbForward); err != nil {
logrus.WithError(err).Warnf("failed to set up forwarding tcp port %d (negligible if already forwarded)", f.Port)
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/hostagent/port_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
)

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

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

// on macOS, listening on 127.0.0.1:80 requires root while 0.0.0.0:80 does not require root.
Expand All @@ -46,7 +46,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
localUnix := plf.unixAddr.Name
_ = plf.Close()
delete(pseudoLoopbackForwarders, local)
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
if err := forwardSSH(ctx, sshConfig, addr, port, localUnix, remote, verb, false); err != nil {
return err
}
} else {
Expand All @@ -61,12 +61,12 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
}
localUnix := filepath.Join(localUnixDir, "sock")
logrus.Debugf("forwarding %q to %q", localUnix, remote)
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
if err := forwardSSH(ctx, sshConfig, addr, port, localUnix, remote, verb, false); err != nil {
return err
}
plf, err := newPseudoLoopbackForwarder(localPort, localUnix)
if err != nil {
if cancelErr := forwardSSH(ctx, sshConfig, port, localUnix, remote, verbCancel, false); cancelErr != nil {
if cancelErr := forwardSSH(ctx, sshConfig, addr, port, localUnix, remote, verbCancel, false); cancelErr != nil {
logrus.WithError(cancelErr).Warnf("failed to cancel forwarding %q to %q", localUnix, remote)
}
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostagent/port_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/lima-vm/sshocker/pkg/ssh"
)

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

func getFreeVSockPort() (int, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostagent/port_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/lima-vm/sshocker/pkg/ssh"
)

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

func getFreeVSockPort() (int, error) {
Expand Down

0 comments on commit 9cea961

Please sign in to comment.