mirror of
https://github.com/fosrl/olm.git
synced 2026-02-08 05:56:41 +00:00
156 lines
3.8 KiB
Go
156 lines
3.8 KiB
Go
//go:build !windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os/exec"
|
|
"regexp"
|
|
"runtime"
|
|
"strconv"
|
|
|
|
"github.com/fosrl/newt/logger"
|
|
"github.com/vishvananda/netlink"
|
|
)
|
|
|
|
// ConfigureInterface configures a network interface with an IP address and brings it up
|
|
func ConfigureInterface(interfaceName string, wgData WgData) error {
|
|
var ipAddr string = wgData.TunnelIP
|
|
|
|
// Parse the IP address and network
|
|
ip, ipNet, err := net.ParseCIDR(ipAddr)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid IP address: %v", err)
|
|
}
|
|
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
return configureLinux(interfaceName, ip, ipNet)
|
|
case "darwin":
|
|
return configureDarwin(interfaceName, ip, ipNet)
|
|
default:
|
|
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
|
|
}
|
|
}
|
|
|
|
func findUnusedUTUN() (string, error) {
|
|
ifaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to list interfaces: %v", err)
|
|
}
|
|
used := make(map[int]bool)
|
|
re := regexp.MustCompile(`^utun(\d+)$`)
|
|
for _, iface := range ifaces {
|
|
if matches := re.FindStringSubmatch(iface.Name); len(matches) == 2 {
|
|
if num, err := strconv.Atoi(matches[1]); err == nil {
|
|
used[num] = true
|
|
}
|
|
}
|
|
}
|
|
// Try utun0 up to utun255.
|
|
for i := 0; i < 256; i++ {
|
|
if !used[i] {
|
|
return fmt.Sprintf("utun%d", i), nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("no unused utun interface found")
|
|
}
|
|
|
|
func configureDarwin(interfaceName string, ip net.IP, ipNet *net.IPNet) error {
|
|
logger.Info("Configuring darwin interface: %s", interfaceName)
|
|
|
|
prefix, _ := ipNet.Mask.Size()
|
|
ipStr := fmt.Sprintf("%s/%d", ip.String(), prefix)
|
|
|
|
cmd := exec.Command("ifconfig", interfaceName, "inet", ipStr, ip.String(), "alias")
|
|
logger.Info("Running command: %v", cmd)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("ifconfig command failed: %v, output: %s", err, out)
|
|
}
|
|
|
|
// Bring up the interface
|
|
cmd = exec.Command("ifconfig", interfaceName, "up")
|
|
logger.Info("Running command: %v", cmd)
|
|
|
|
out, err = cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("ifconfig up command failed: %v, output: %s", err, out)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func configureLinux(interfaceName string, ip net.IP, ipNet *net.IPNet) error {
|
|
// Get the interface
|
|
link, err := netlink.LinkByName(interfaceName)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get interface %s: %v", interfaceName, err)
|
|
}
|
|
|
|
// Create the IP address attributes
|
|
addr := &netlink.Addr{
|
|
IPNet: &net.IPNet{
|
|
IP: ip,
|
|
Mask: ipNet.Mask,
|
|
},
|
|
}
|
|
|
|
// Add the IP address to the interface
|
|
if err := netlink.AddrAdd(link, addr); err != nil {
|
|
return fmt.Errorf("failed to add IP address: %v", err)
|
|
}
|
|
|
|
// Bring up the interface
|
|
if err := netlink.LinkSetUp(link); err != nil {
|
|
return fmt.Errorf("failed to bring up interface: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func DarwinAddRoute(destination string, gateway string, interfaceName string) error {
|
|
if runtime.GOOS != "darwin" {
|
|
return nil
|
|
}
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
if gateway != "" {
|
|
// Route with specific gateway
|
|
cmd = exec.Command("route", "-q", "-n", "add", "-inet", destination, "-gateway", gateway)
|
|
} else if interfaceName != "" {
|
|
// Route via interface
|
|
cmd = exec.Command("route", "-q", "-n", "add", "-inet", destination, "-interface", interfaceName)
|
|
} else {
|
|
return fmt.Errorf("either gateway or interface must be specified")
|
|
}
|
|
|
|
logger.Info("Running command: %v", cmd)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("route command failed: %v, output: %s", err, out)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func DarwinRemoveRoute(destination string) error {
|
|
if runtime.GOOS != "darwin" {
|
|
return nil
|
|
}
|
|
|
|
cmd := exec.Command("route", "-q", "-n", "delete", "-inet", destination)
|
|
logger.Info("Running command: %v", cmd)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("route delete command failed: %v, output: %s", err, out)
|
|
}
|
|
|
|
return nil
|
|
}
|