Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: support forwarding to specific host IP addresses #1091

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions environment/vm/lima/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@ func (l *limaVM) addPostStartActions(a *cli.ActiveCommandChain, conf config.Conf
return nil
})

// replicate addresses
a.Add(func() error {
if err := l.replicateHostAddresses(); err != nil {
logrus.Warnln(fmt.Errorf("unable to assign host IP addresses to the VM: %w", err))
}
return nil
})

// preserve state
a.Add(func() error {
if err := configmanager.SaveToFile(conf, config.CurrentProfile().StateFile()); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions environment/vm/lima/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/abiosoft/colima/embedded"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/abiosoft/colima/util"
)

func (l *limaVM) writeNetworkFile() error {
Expand All @@ -24,3 +25,12 @@ func (l *limaVM) writeNetworkFile() error {
}
return nil
}

func (l *limaVM) replicateHostAddresses() error {
for _, ip := range util.HostIPAddresses() {
if err := l.RunQuiet("sudo", "ip", "address", "add", ip.String()+"/24", "dev", "lo"); err != nil {
return err
}
}
return nil
}
13 changes: 13 additions & 0 deletions environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err
Proto: limaconfig.TCP,
},
)

// bind all host addresses
for _, ip := range util.HostIPAddresses() {
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestIP: ip,
GuestPortRange: [2]int{1, 65535},
HostIP: ip,
HostPortRange: [2]int{1, 65535},
Proto: limaconfig.TCP,
},
)
}
}

switch strings.ToLower(conf.MountType) {
Expand Down
19 changes: 19 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ func RandomAvailablePort() int {
return listener.Addr().(*net.TCPAddr).Port
}

// HostIPAddresses returns all IPv4 addresses on the host.
func HostIPAddresses() []net.IP {
var addresses []net.IP
ints, err := net.InterfaceAddrs()
if err != nil {
return nil
}
for i := range ints {
split := strings.Split(ints[i].String(), "/")
addr := net.ParseIP(split[0]).To4()
// ignore default loopback
if addr != nil && addr.String() != "127.0.0.1" {
addresses = append(addresses, addr)
}
}

return addresses
}

// ShellSplit splits cmd into arguments using.
func ShellSplit(cmd string) []string {
split, err := shlex.Split(cmd)
Expand Down
Loading