Skip to content

Commit 9185149

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 79cb68b commit 9185149

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
@@ -251,7 +251,6 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
251251
instName: instName,
252252
instSSHAddress: inst.SSHAddress,
253253
sshConfig: sshConfig,
254-
portForwarder: newPortForwarder(sshConfig, sshLocalPort, rules, ignoreTCP, inst.VMType),
255254
grpcPortForwarder: portfwd.NewPortForwarder(rules, ignoreTCP, ignoreUDP),
256255
driver: limaDriver,
257256
signalCh: signalCh,
@@ -261,6 +260,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
261260
guestAgentAliveCh: make(chan struct{}),
262261
showProgress: o.showProgress,
263262
}
263+
a.portForwarder = newPortForwarder(sshConfig, a.sshAddressPort, rules, ignoreTCP, inst.VMType)
264264
return a, nil
265265
}
266266

@@ -657,7 +657,8 @@ sudo chown -R "${USER}" /run/host-services`
657657
}
658658
// Copy all config files _after_ the requirements are done
659659
for _, rule := range a.instConfig.CopyToHost {
660-
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
660+
sshAddress, sshPort := a.sshAddressPort()
661+
if err := copyToHost(ctx, a.sshConfig, sshAddress, sshPort, rule.HostFile, rule.GuestFile); err != nil {
661662
errs = append(errs, err)
662663
}
663664
}
@@ -704,10 +705,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
704705
// Setup all socket forwards and defer their teardown
705706
if !(a.driver.Info().Features.SkipSocketForwarding) {
706707
logrus.Debugf("Forwarding unix sockets")
708+
sshAddress, sshPort := a.sshAddressPort()
707709
for _, rule := range a.instConfig.PortForwards {
708710
if rule.GuestSocket != "" {
709711
local := hostAddress(rule, &guestagentapi.IPPort{})
710-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
712+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbForward, rule.Reverse)
711713
}
712714
}
713715
}
@@ -718,17 +720,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
718720
a.cleanUp(func() error {
719721
logrus.Debugf("Stop forwarding unix sockets")
720722
var errs []error
723+
sshAddress, sshPort := a.sshAddressPort()
721724
for _, rule := range a.instConfig.PortForwards {
722725
if rule.GuestSocket != "" {
723726
local := hostAddress(rule, &guestagentapi.IPPort{})
724727
// using ctx.Background() because ctx has already been cancelled
725-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
728+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
726729
errs = append(errs, err)
727730
}
728731
}
729732
}
730733
if a.driver.ForwardGuestAgent() {
731-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
734+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbCancel, false); err != nil {
732735
errs = append(errs, err)
733736
}
734737
}
@@ -739,7 +742,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
739742
if a.instConfig.MountInotify != nil && *a.instConfig.MountInotify {
740743
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
741744
if a.driver.ForwardGuestAgent() {
742-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
745+
sshAddress, sshPort := a.sshAddressPort()
746+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
743747
}
744748
}
745749
err := a.startInotify(ctx)
@@ -755,7 +759,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
755759
for {
756760
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
757761
if a.driver.ForwardGuestAgent() {
758-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
762+
sshAddress, sshPort := a.sshAddressPort()
763+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
759764
}
760765
}
761766
client, err := a.getOrCreateClient(ctx)
@@ -779,6 +784,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
779784
}
780785

781786
func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPortForwards []limatype.PortForward) {
787+
sshAddress, sshPort := a.sshAddressPort()
782788
for _, rule := range staticPortForwards {
783789
if rule.GuestSocket == "" {
784790
guest := &guestagentapi.IPPort{
@@ -789,7 +795,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
789795
local, remote := a.portForwarder.forwardingAddresses(guest)
790796
if local != "" {
791797
logrus.Infof("Setting up static TCP forwarding from %s to %s", remote, local)
792-
if err := forwardTCP(ctx, a.sshConfig, a.sshLocalPort, local, remote, verbForward); err != nil {
798+
if err := forwardTCP(ctx, a.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
793799
logrus.WithError(err).Warnf("failed to set up static TCP forwarding %s -> %s", remote, local)
794800
}
795801
}
@@ -899,11 +905,11 @@ const (
899905
verbCancel = "cancel"
900906
)
901907

902-
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
908+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, command ...string) error {
903909
args := sshConfig.Args()
904910
args = append(args,
905-
"-p", strconv.Itoa(port),
906-
"127.0.0.1",
911+
"-p", strconv.Itoa(sshPort),
912+
sshAddress,
907913
"--",
908914
)
909915
args = append(args, command...)
@@ -914,7 +920,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
914920
return nil
915921
}
916922

917-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string, reverse bool) error {
923+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string, reverse bool) error {
918924
args := sshConfig.Args()
919925
args = append(args,
920926
"-T",
@@ -932,16 +938,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
932938
args = append(args,
933939
"-N",
934940
"-f",
935-
"-p", strconv.Itoa(port),
936-
"127.0.0.1",
941+
"-p", strconv.Itoa(sshPort),
942+
sshAddress,
937943
"--",
938944
)
939945
if strings.HasPrefix(local, "/") {
940946
switch verb {
941947
case verbForward:
942948
if reverse {
943949
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
944-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
950+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
945951
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
946952
}
947953
} else {
@@ -956,7 +962,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
956962
case verbCancel:
957963
if reverse {
958964
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
959-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
965+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
960966
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
961967
}
962968
} else {
@@ -977,7 +983,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
977983
if verb == verbForward && strings.HasPrefix(local, "/") {
978984
if reverse {
979985
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
980-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
986+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
981987
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
982988
}
983989
} else {
@@ -1011,10 +1017,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10111017
Active: true,
10121018
})
10131019

1020+
sshAddress, sshPort := a.sshAddressPort()
10141021
args := a.sshConfig.Args()
10151022
args = append(args,
1016-
"-p", strconv.Itoa(a.sshLocalPort),
1017-
"127.0.0.1",
1023+
"-p", strconv.Itoa(sshPort),
1024+
sshAddress,
10181025
"sh", "-c",
10191026
`"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
10201027
sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1099,8 +1106,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10991106

11001107
finalArgs := a.sshConfig.Args()
11011108
finalArgs = append(finalArgs,
1102-
"-p", strconv.Itoa(a.sshLocalPort),
1103-
"127.0.0.1",
1109+
"-p", strconv.Itoa(sshPort),
1110+
sshAddress,
11041111
"sudo", "tail", "-n", "20", "/var/log/cloud-init-output.log",
11051112
)
11061113

@@ -1140,11 +1147,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
11401147
return strings.HasPrefix(line, "cloud-init-main.service: consumed")
11411148
}
11421149

1143-
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string) error {
1150+
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote string) error {
11441151
args := sshConfig.Args()
11451152
args = append(args,
1146-
"-p", strconv.Itoa(port),
1147-
"127.0.0.1",
1153+
"-p", strconv.Itoa(sshPort),
1154+
sshAddress,
11481155
"--",
11491156
)
11501157
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)