Skip to content

Commit 0ced41f

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 72233e7 commit 0ced41f

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

@@ -671,7 +671,8 @@ sudo chown -R "${USER}" /run/host-services`
671671
}
672672
// Copy all config files _after_ the requirements are done
673673
for _, rule := range a.instConfig.CopyToHost {
674-
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
674+
sshAddress, sshPort := a.sshAddressPort()
675+
if err := copyToHost(ctx, a.sshConfig, sshAddress, sshPort, rule.HostFile, rule.GuestFile); err != nil {
675676
errs = append(errs, err)
676677
}
677678
}
@@ -718,10 +719,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
718719
// Setup all socket forwards and defer their teardown
719720
if !(a.driver.Info().Features.DynamicSSHAddress) {
720721
logrus.Debugf("Forwarding unix sockets")
722+
sshAddress, sshPort := a.sshAddressPort()
721723
for _, rule := range a.instConfig.PortForwards {
722724
if rule.GuestSocket != "" {
723725
local := hostAddress(rule, &guestagentapi.IPPort{})
724-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbForward, rule.Reverse)
726+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbForward, rule.Reverse)
725727
}
726728
}
727729
}
@@ -732,17 +734,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
732734
a.cleanUp(func() error {
733735
logrus.Debugf("Stop forwarding unix sockets")
734736
var errs []error
737+
sshAddress, sshPort := a.sshAddressPort()
735738
for _, rule := range a.instConfig.PortForwards {
736739
if rule.GuestSocket != "" {
737740
local := hostAddress(rule, &guestagentapi.IPPort{})
738741
// using ctx.Background() because ctx has already been cancelled
739-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
742+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
740743
errs = append(errs, err)
741744
}
742745
}
743746
}
744747
if a.driver.ForwardGuestAgent() {
745-
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
748+
if err := forwardSSH(context.Background(), a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbCancel, false); err != nil {
746749
errs = append(errs, err)
747750
}
748751
}
@@ -753,7 +756,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
753756
if a.instConfig.MountInotify != nil && *a.instConfig.MountInotify {
754757
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
755758
if a.driver.ForwardGuestAgent() {
756-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
759+
sshAddress, sshPort := a.sshAddressPort()
760+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
757761
}
758762
}
759763
err := a.startInotify(ctx)
@@ -769,7 +773,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
769773
for {
770774
if a.client == nil || !isGuestAgentSocketAccessible(ctx, a.client) {
771775
if a.driver.ForwardGuestAgent() {
772-
_ = forwardSSH(ctx, a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbForward, false)
776+
sshAddress, sshPort := a.sshAddressPort()
777+
_ = forwardSSH(ctx, a.sshConfig, sshAddress, sshPort, localUnix, remoteUnix, verbForward, false)
773778
}
774779
}
775780
client, err := a.getOrCreateClient(ctx)
@@ -793,6 +798,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
793798
}
794799

795800
func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPortForwards []limatype.PortForward) {
801+
sshAddress, sshPort := a.sshAddressPort()
796802
for _, rule := range staticPortForwards {
797803
if rule.GuestSocket == "" {
798804
guest := &guestagentapi.IPPort{
@@ -803,7 +809,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
803809
local, remote := a.portForwarder.forwardingAddresses(guest)
804810
if local != "" {
805811
logrus.Infof("Setting up static TCP forwarding from %s to %s", remote, local)
806-
if err := forwardTCP(ctx, a.sshConfig, a.sshLocalPort, local, remote, verbForward); err != nil {
812+
if err := forwardTCP(ctx, a.sshConfig, sshAddress, sshPort, local, remote, verbForward); err != nil {
807813
logrus.WithError(err).Warnf("failed to set up static TCP forwarding %s -> %s", remote, local)
808814
}
809815
}
@@ -913,11 +919,11 @@ const (
913919
verbCancel = "cancel"
914920
)
915921

916-
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
922+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, command ...string) error {
917923
args := sshConfig.Args()
918924
args = append(args,
919-
"-p", strconv.Itoa(port),
920-
"127.0.0.1",
925+
"-p", strconv.Itoa(sshPort),
926+
sshAddress,
921927
"--",
922928
)
923929
args = append(args, command...)
@@ -928,7 +934,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
928934
return nil
929935
}
930936

931-
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote, verb string, reverse bool) error {
937+
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote, verb string, reverse bool) error {
932938
args := sshConfig.Args()
933939
args = append(args,
934940
"-T",
@@ -946,16 +952,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
946952
args = append(args,
947953
"-N",
948954
"-f",
949-
"-p", strconv.Itoa(port),
950-
"127.0.0.1",
955+
"-p", strconv.Itoa(sshPort),
956+
sshAddress,
951957
"--",
952958
)
953959
if strings.HasPrefix(local, "/") {
954960
switch verb {
955961
case verbForward:
956962
if reverse {
957963
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
958-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
964+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
959965
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
960966
}
961967
} else {
@@ -970,7 +976,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
970976
case verbCancel:
971977
if reverse {
972978
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
973-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
979+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
974980
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
975981
}
976982
} else {
@@ -991,7 +997,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
991997
if verb == verbForward && strings.HasPrefix(local, "/") {
992998
if reverse {
993999
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
994-
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
1000+
if err := executeSSH(ctx, sshConfig, sshAddress, sshPort, "rm", "-f", remote); err != nil {
9951001
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
9961002
}
9971003
} else {
@@ -1025,10 +1031,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10251031
Active: true,
10261032
})
10271033

1034+
sshAddress, sshPort := a.sshAddressPort()
10281035
args := a.sshConfig.Args()
10291036
args = append(args,
1030-
"-p", strconv.Itoa(a.sshLocalPort),
1031-
"127.0.0.1",
1037+
"-p", strconv.Itoa(sshPort),
1038+
sshAddress,
10321039
"sh", "-c",
10331040
`"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
10341041
sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1113,8 +1120,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
11131120

11141121
finalArgs := a.sshConfig.Args()
11151122
finalArgs = append(finalArgs,
1116-
"-p", strconv.Itoa(a.sshLocalPort),
1117-
"127.0.0.1",
1123+
"-p", strconv.Itoa(sshPort),
1124+
sshAddress,
11181125
"sudo", "tail", "-n", "20", "/var/log/cloud-init-output.log",
11191126
)
11201127

@@ -1154,11 +1161,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
11541161
return strings.HasPrefix(line, "cloud-init-main.service: consumed")
11551162
}
11561163

1157-
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string) error {
1164+
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, sshAddress string, sshPort int, local, remote string) error {
11581165
args := sshConfig.Args()
11591166
args = append(args,
1160-
"-p", strconv.Itoa(port),
1161-
"127.0.0.1",
1167+
"-p", strconv.Itoa(sshPort),
1168+
sshAddress,
11621169
"--",
11631170
)
11641171
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)