Skip to content

Commit 935aa20

Browse files
committed
portfwd: support HostSocket
Signed-off-by: Norio Nomura <[email protected]>
1 parent 85e0c77 commit 935aa20

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

pkg/limayaml/defaults.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -922,14 +922,6 @@ func FillPortForwardDefaults(rule *limatype.PortForward, instDir string, user li
922922
rule.GuestPortRange[1] = rule.GuestPort
923923
}
924924
}
925-
if rule.HostPortRange[0] == 0 && rule.HostPortRange[1] == 0 {
926-
if rule.HostPort == 0 {
927-
rule.HostPortRange = rule.GuestPortRange
928-
} else {
929-
rule.HostPortRange[0] = rule.HostPort
930-
rule.HostPortRange[1] = rule.HostPort
931-
}
932-
}
933925
if rule.GuestSocket != "" {
934926
if out, err := executeGuestTemplate(rule.GuestSocket, instDir, user, param); err == nil {
935927
rule.GuestSocket = out.String()
@@ -946,6 +938,13 @@ func FillPortForwardDefaults(rule *limatype.PortForward, instDir string, user li
946938
if !filepath.IsAbs(rule.HostSocket) {
947939
rule.HostSocket = filepath.Join(instDir, filenames.SocketDir, rule.HostSocket)
948940
}
941+
} else if rule.HostPortRange[0] == 0 && rule.HostPortRange[1] == 0 {
942+
if rule.HostPort == 0 {
943+
rule.HostPortRange = rule.GuestPortRange
944+
} else {
945+
rule.HostPortRange[0] = rule.HostPort
946+
rule.HostPortRange[1] = rule.HostPort
947+
}
949948
}
950949
}
951950

pkg/limayaml/defaults_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ func TestFillDefault(t *testing.T) {
268268
expect.PortForwards[2].HostPort = 8888
269269
expect.PortForwards[2].HostPortRange = [2]int{8888, 8888}
270270

271+
expect.PortForwards[3].HostPortRange = [2]int{0, 0}
271272
expect.PortForwards[3].GuestSocket = fmt.Sprintf("%s | %s | %s | %s", user.HomeDir, user.Uid, user.Username, y.Param["ONE"])
272273
expect.PortForwards[3].HostSocket = fmt.Sprintf("%s | %s | %s | %s | %s | %s", hostHome, instDir, instName, currentUser.Uid, currentUser.Username, y.Param["ONE"])
273274

pkg/limayaml/validate.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,10 @@ func Validate(y *limatype.LimaYAML, warn bool) error {
314314
if err := validatePort(fmt.Sprintf("%s.guestPortRange[%d]", field, j), rule.GuestPortRange[j]); err != nil {
315315
errs = errors.Join(errs, err)
316316
}
317-
if err := validatePort(fmt.Sprintf("%s.hostPortRange[%d]", field, j), rule.HostPortRange[j]); err != nil {
318-
errs = errors.Join(errs, err)
317+
if rule.HostSocket == "" {
318+
if err := validatePort(fmt.Sprintf("%s.hostPortRange[%d]", field, j), rule.HostPortRange[j]); err != nil {
319+
errs = errors.Join(errs, err)
320+
}
319321
}
320322
}
321323
if rule.GuestPortRange[0] > rule.GuestPortRange[1] {
@@ -324,9 +326,6 @@ func Validate(y *limatype.LimaYAML, warn bool) error {
324326
if rule.HostPortRange[0] > rule.HostPortRange[1] {
325327
errs = errors.Join(errs, fmt.Errorf("field `%s.hostPortRange[1]` must be greater than or equal to field `%s.hostPortRange[0]`", field, field))
326328
}
327-
if rule.GuestPortRange[1]-rule.GuestPortRange[0] != rule.HostPortRange[1]-rule.HostPortRange[0] {
328-
errs = errors.Join(errs, fmt.Errorf("field `%s.hostPortRange` must specify the same number of ports as field `%s.guestPortRange`", field, field))
329-
}
330329
if rule.GuestSocket != "" {
331330
if !path.IsAbs(rule.GuestSocket) {
332331
errs = errors.Join(errs, fmt.Errorf("field `%s.guestSocket` must be an absolute path, but is %q", field, rule.GuestSocket))
@@ -343,7 +342,10 @@ func Validate(y *limatype.LimaYAML, warn bool) error {
343342
if rule.GuestSocket == "" && rule.GuestPortRange[1]-rule.GuestPortRange[0] > 0 {
344343
errs = errors.Join(errs, fmt.Errorf("field `%s.hostSocket` can only be mapped from a single port or socket. not a range", field))
345344
}
345+
} else if rule.GuestPortRange[1]-rule.GuestPortRange[0] != rule.HostPortRange[1]-rule.HostPortRange[0] {
346+
errs = errors.Join(errs, fmt.Errorf("field `%s.hostPortRange` must specify the same number of ports as field `%s.guestPortRange`", field, field))
346347
}
348+
347349
if len(rule.HostSocket) >= osutil.UnixPathMax {
348350
errs = errors.Join(errs, fmt.Errorf("field `%s.hostSocket` must be less than UNIX_PATH_MAX=%d characters, but is %d",
349351
field, osutil.UnixPathMax, len(rule.HostSocket)))

pkg/portfwd/listener_darwin.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@ import (
77
"context"
88
"fmt"
99
"net"
10+
"os"
11+
"path/filepath"
1012
"strconv"
1113

1214
"github.com/sirupsen/logrus"
1315
)
1416

1517
func Listen(ctx context.Context, listenConfig net.ListenConfig, hostAddress string) (net.Listener, error) {
18+
if filepath.IsAbs(hostAddress) {
19+
// Handle Unix domain sockets
20+
if err := prepareUnixSocket(hostAddress); err != nil {
21+
return nil, err
22+
}
23+
unixLis, err := listenConfig.Listen(ctx, "unix", hostAddress)
24+
if err != nil {
25+
logrus.WithError(err).Errorf("failed to listen unix: %v", hostAddress)
26+
return nil, err
27+
}
28+
return unixLis, nil
29+
}
1630
localIPStr, localPortStr, _ := net.SplitHostPort(hostAddress)
1731
localIP := net.ParseIP(localIPStr)
1832
localPort, _ := strconv.Atoi(localPortStr)
@@ -34,6 +48,18 @@ func Listen(ctx context.Context, listenConfig net.ListenConfig, hostAddress stri
3448
}
3549

3650
func ListenPacket(ctx context.Context, listenConfig net.ListenConfig, hostAddress string) (net.PacketConn, error) {
51+
if filepath.IsAbs(hostAddress) {
52+
// Handle Unix domain sockets
53+
if err := prepareUnixSocket(hostAddress); err != nil {
54+
return nil, err
55+
}
56+
unixLis, err := listenConfig.ListenPacket(ctx, "unix", hostAddress)
57+
if err != nil {
58+
logrus.WithError(err).Errorf("failed to listen unix: %v", hostAddress)
59+
return nil, err
60+
}
61+
return unixLis, nil
62+
}
3763
localIPStr, localPortStr, _ := net.SplitHostPort(hostAddress)
3864
localIP := net.ParseIP(localIPStr)
3965
localPort, _ := strconv.Atoi(localPortStr)
@@ -54,6 +80,16 @@ func ListenPacket(ctx context.Context, listenConfig net.ListenConfig, hostAddres
5480
return &pseudoLoopbackPacketConn{udpConn}, nil
5581
}
5682

83+
func prepareUnixSocket(hostSocket string) error {
84+
if err := os.RemoveAll(hostSocket); err != nil {
85+
return fmt.Errorf("can't clean up %q: %w", hostSocket, err)
86+
}
87+
if err := os.MkdirAll(filepath.Dir(hostSocket), 0o755); err != nil {
88+
return fmt.Errorf("can't create directory for local socket %q: %w", hostSocket, err)
89+
}
90+
return nil
91+
}
92+
5793
type pseudoLoopbackListener struct {
5894
net.Listener
5995
}

0 commit comments

Comments
 (0)