Skip to content

Commit dee7e4e

Browse files
committed
pkg/hostagent: Update all ssh execution to support SSH address other than "127.0.0.1"
affected functions: - Copy to host - Reverse SSHFS - SSH port forwarding Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 9e94993 commit dee7e4e

File tree

6 files changed

+58
-49
lines changed

6 files changed

+58
-49
lines changed

pkg/hostagent/hostagent.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
250250
instName: instName,
251251
instSSHAddress: inst.SSHAddress,
252252
sshConfig: sshConfig,
253-
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, ignoreTCP, inst.VMType),
254253
grpcPortForwarder: portfwd.NewPortForwarder(rules, ignoreTCP, ignoreUDP),
255254
driver: limaDriver,
256255
signalCh: signalCh,
@@ -260,6 +259,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
260259
guestAgentAliveCh: make(chan struct{}),
261260
showProgress: o.showProgress,
262261
}
262+
a.portForwarder = newPortForwarder(sshConfig, a.sshAddressPort, rules, ignoreTCP, inst.VMType)
263263
return a, nil
264264
}
265265

@@ -645,7 +645,8 @@ sudo chown -R "${USER}" /run/host-services`
645645
}
646646
// Copy all config files _after_ the requirements are done
647647
for _, rule := range a.instConfig.CopyToHost {
648-
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
648+
sshAddress, sshPort := a.sshAddressPort()
649+
if err := copyToHost(ctx, a.sshConfig, sshAddress, sshPort, rule.HostFile, rule.GuestFile); err != nil {
649650
errs = append(errs, err)
650651
}
651652
}
@@ -692,10 +693,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
692693
// Setup all socket forwards and defer their teardown
693694
if !(a.driver.Info().Features.DynamicSSHAddress) {
694695
logrus.Debugf("Forwarding unix sockets")
696+
sshAddress, sshPort := a.sshAddressPort()
695697
for _, rule := range a.instConfig.PortForwards {
696698
if rule.GuestSocket != "" {
697699
local := hostAddress(rule, &guestagentapi.IPPort{})
698-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
700+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbForward, rule.Reverse)
699701
}
700702
}
701703
}
@@ -706,17 +708,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
706708
a.cleanUp(func() error {
707709
logrus.Debugf("Stop forwarding unix sockets")
708710
var errs []error
711+
sshAddress, sshPort := a.sshAddressPort()
709712
for _, rule := range a.instConfig.PortForwards {
710713
if rule.GuestSocket != "" {
711714
local := hostAddress(rule, &guestagentapi.IPPort{})
712715
// using ctx.Background() because ctx has already been cancelled
713-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
716+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
714717
errs = append(errs, err)
715718
}
716719
}
717720
}
718721
if a.driver.ForwardGuestAgent() {
719-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
722+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbCancel, false); err != nil {
720723
errs = append(errs, err)
721724
}
722725
}
@@ -727,7 +730,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
727730
if a.instConfig.MountInotify != nil && *a.instConfig.MountInotify {
728731
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
729732
if a.driver.ForwardGuestAgent() {
730-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
733+
sshAddress, sshPort := a.sshAddressPort()
734+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
731735
}
732736
}
733737
err := a.startInotify(ctx)
@@ -743,7 +747,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
743747
for {
744748
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
745749
if a.driver.ForwardGuestAgent() {
746-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
750+
sshAddress, sshPort := a.sshAddressPort()
751+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
747752
}
748753
}
749754
client, err := a.getOrCreateClient(ctx)
@@ -767,6 +772,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
767772
}
768773

769774
func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPortForwards []limatype.PortForward) {
775+
sshAddress, sshPort := a.sshAddressPort()
770776
for _, rule := range staticPortForwards {
771777
if rule.GuestSocket == "" {
772778
guest := &guestagentapi.IPPort{
@@ -777,7 +783,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
777783
local, remote := a.portForwarder.forwardingAddresses(guest)
778784
if local != "" {
779785
logrus.Infof("Setting up static TCP forwarding from %s to %s", remote, local)
780-
if err := forwardTCP(ctx, a.sshConfig, a.sshLocalPort, local, remote, verbForward); err != nil {
786+
if err := forwardTCP(ctx, a.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
781787
logrus.WithError(err).Warnf("failed to set up static TCP forwarding %s -> %s", remote, local)
782788
}
783789
}
@@ -887,11 +893,11 @@ const (
887893
verbCancel = "cancel"
888894
)
889895

890-
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
896+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, command ...string) error {
891897
args := sshConfig.Args()
892898
args = append(args,
893-
"-p", strconv.Itoa(port),
894-
"127.0.0.1",
899+
"-p", strconv.Itoa(sshPort),
900+
sshAddress,
895901
"--",
896902
)
897903
args = append(args, command...)
@@ -902,7 +908,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
902908
return nil
903909
}
904910

905-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string, reverse bool) error {
911+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string, reverse bool) error {
906912
args := sshConfig.Args()
907913
args = append(args,
908914
"-T",
@@ -920,16 +926,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
920926
args = append(args,
921927
"-N",
922928
"-f",
923-
"-p", strconv.Itoa(port),
924-
"127.0.0.1",
929+
"-p", strconv.Itoa(sshPort),
930+
sshAddress,
925931
"--",
926932
)
927933
if strings.HasPrefix(local, "/") {
928934
switch verb {
929935
case verbForward:
930936
if reverse {
931937
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
932-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
938+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
933939
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
934940
}
935941
} else {
@@ -944,7 +950,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
944950
case verbCancel:
945951
if reverse {
946952
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
947-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
953+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
948954
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
949955
}
950956
} else {
@@ -965,7 +971,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
965971
if verb == verbForward && strings.HasPrefix(local, "/") {
966972
if reverse {
967973
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
968-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
974+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
969975
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
970976
}
971977
} else {
@@ -999,10 +1005,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
9991005
Active: true,
10001006
})
10011007

1008+
sshAddress, sshPort := a.sshAddressPort()
10021009
args := a.sshConfig.Args()
10031010
args = append(args,
1004-
"-p", strconv.Itoa(a.sshLocalPort),
1005-
"127.0.0.1",
1011+
"-p", strconv.Itoa(sshPort),
1012+
sshAddress,
10061013
"sh", "-c",
10071014
`"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
10081015
sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1087,8 +1094,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10871094

10881095
finalArgs := a.sshConfig.Args()
10891096
finalArgs = append(finalArgs,
1090-
"-p", strconv.Itoa(a.sshLocalPort),
1091-
"127.0.0.1",
1097+
"-p", strconv.Itoa(sshPort),
1098+
sshAddress,
10921099
"sudo", "tail", "-n", "20", "/var/log/cloud-init-output.log",
10931100
)
10941101

@@ -1128,11 +1135,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
11281135
return strings.HasPrefix(line, "cloud-init-main.service: consumed")
11291136
}
11301137

1131-
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string) error {
1138+
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote string) error {
11321139
args := sshConfig.Args()
11331140
args = append(args,
1134-
"-p", strconv.Itoa(port),
1135-
"127.0.0.1",
1141+
"-p", strconv.Itoa(sshPort),
1142+
sshAddress,
11361143
"--",
11371144
)
11381145
args = append(args,

pkg/hostagent/mount.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ func (a *HostAgent) setupMount(ctx context.Context, m limatype.Mount) (*mount, e
6161
}
6262
}
6363

64+
sshAddress, sshPort := a.sshAddressPort()
6465
rsf := &reversesshfs.ReverseSSHFS{
6566
Driver: *m.SSHFS.SFTPDriver,
6667
SSHConfig: a.sshConfig,
6768
LocalPath: resolvedLocation,
68-
Host: "127.0.0.1",
69-
Port: a.sshLocalPort,
69+
Host: sshAddress,
70+
Port: sshPort,
7071
RemotePath: *m.MountPoint,
7172
Readonly: !(*m.Writable),
7273
SSHFSAdditionalArgs: []string{"-o", sshfsOptions},

pkg/hostagent/port.go

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

1818
type portForwarder struct {
19-
sshConfig *ssh.SSHConfig
20-
sshHostPort int
21-
rules []limatype.PortForward
22-
ignore bool
23-
vmType limatype.VMType
19+
sshConfig *ssh.SSHConfig
20+
sshAddressPort func() (string, int)
21+
rules []limatype.PortForward
22+
ignore bool
23+
vmType limatype.VMType
2424
}
2525

2626
const sshGuestPort = 22
2727

2828
var IPv4loopback1 = limayaml.IPv4loopback1
2929

30-
func newPortForwarder(sshConfig *ssh.SSHConfig, sshHostPort int, rules []limatype.PortForward, ignore bool, vmType limatype.VMType) *portForwarder {
30+
func newPortForwarder(sshConfig *ssh.SSHConfig, sshAddressPort func() (string, int), rules []limatype.PortForward, ignore bool, vmType limatype.VMType) *portForwarder {
3131
return &portForwarder{
32-
sshConfig: sshConfig,
33-
sshHostPort: sshHostPort,
34-
rules: rules,
35-
ignore: ignore,
36-
vmType: vmType,
32+
sshConfig: sshConfig,
33+
sshAddressPort: sshAddressPort,
34+
rules: rules,
35+
ignore: ignore,
36+
vmType: vmType,
3737
}
3838
}
3939

@@ -87,6 +87,7 @@ func (pf *portForwarder) forwardingAddresses(guest *api.IPPort) (hostAddr, guest
8787
}
8888

8989
func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
90+
sshAddress, sshPort := pf.sshAddressPort()
9091
for _, f := range ev.RemovedLocalPorts {
9192
if f.Protocol != "tcp" {
9293
continue
@@ -96,7 +97,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
9697
continue
9798
}
9899
logrus.Infof("Stopping forwarding TCP from %s to %s", remote, local)
99-
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbCancel); err != nil {
100+
if err := forwardTCP(ctx, pf.sshConfig, sshAddress, sshPort, local, remote, verbCancel); err != nil {
100101
logrus.WithError(err).Warnf("failed to stop forwarding tcp port %d", f.Port)
101102
}
102103
}
@@ -112,7 +113,7 @@ func (pf *portForwarder) OnEvent(ctx context.Context, ev *api.Event) {
112113
continue
113114
}
114115
logrus.Infof("Forwarding TCP from %s to %s", remote, local)
115-
if err := forwardTCP(ctx, pf.sshConfig, pf.sshHostPort, local, remote, verbForward); err != nil {
116+
if err := forwardTCP(ctx, pf.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
116117
logrus.WithError(err).Warnf("failed to set up forwarding tcp port %d (negligible if already forwarded)", f.Port)
117118
}
118119
}

pkg/hostagent/port_darwin.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
)
2121

2222
// forwardTCP is not thread-safe.
23-
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
23+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string) error {
2424
if strings.HasPrefix(local, "/") {
25-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
25+
return forwardSSH(ctx, sshConfig, sshAddress, sshPort, local, remote, verb, false)
2626
}
2727
localIPStr, localPortStr, err := net.SplitHostPort(local)
2828
if err != nil {
@@ -35,7 +35,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
3535
}
3636

3737
if !localIP.Equal(IPv4loopback1) || localPort >= 1024 {
38-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
38+
return forwardSSH(ctx, sshConfig, sshAddress, sshPort, local, remote, verb, false)
3939
}
4040

4141
// on macOS, listening on 127.0.0.1:80 requires root while 0.0.0.0:80 does not require root.
@@ -50,7 +50,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
5050
localUnix := plf.unixAddr.Name
5151
_ = plf.Close()
5252
delete(pseudoLoopbackForwarders, local)
53-
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
53+
if err := forwardSSH(ctx, sshConfig, sshAddress, sshPort, localUnix, remote, verb, false); err != nil {
5454
return err
5555
}
5656
} else {
@@ -65,12 +65,12 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
6565
}
6666
localUnix := filepath.Join(localUnixDir, "sock")
6767
logrus.Debugf("forwarding %q to %q", localUnix, remote)
68-
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
68+
if err := forwardSSH(ctx, sshConfig, sshAddress, sshPort, localUnix, remote, verb, false); err != nil {
6969
return err
7070
}
7171
plf, err := newPseudoLoopbackForwarder(localPort, localUnix)
7272
if err != nil {
73-
if cancelErr := forwardSSH(ctx, sshConfig, port, localUnix, remote, verbCancel, false); cancelErr != nil {
73+
if cancelErr := forwardSSH(ctx, sshConfig, sshAddress, sshPort, localUnix, remote, verbCancel, false); cancelErr != nil {
7474
logrus.WithError(cancelErr).Warnf("failed to cancel forwarding %q to %q", localUnix, remote)
7575
}
7676
return err

pkg/hostagent/port_others.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ import (
1111
"github.com/lima-vm/sshocker/pkg/ssh"
1212
)
1313

14-
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string) error {
15-
return forwardSSH(ctx, sshConfig, port, local, remote, verb, false)
14+
func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string) error {
15+
return forwardSSH(ctx, sshConfig, sshAddress, sshPort, local, remote, verb, false)
1616
}

pkg/hostagent/port_windows.go

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

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

0 commit comments

Comments
 (0)