Skip to content

Commit

Permalink
net: support forwarding to specific host IP addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Aug 2, 2024
1 parent 7ee117e commit 4d8304b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
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

0 comments on commit 4d8304b

Please sign in to comment.