this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(network): detect server address

Khue Doan 652ef96f 3ae74354

+56 -3
+15 -3
cmd/nixie/main.go
··· 8 8 "syscall" 9 9 10 10 "code.khuedoan.com/nixie/internal/hosts" 11 + "code.khuedoan.com/nixie/internal/network" 11 12 "code.khuedoan.com/nixie/internal/nixos" 12 13 "code.khuedoan.com/nixie/internal/serve" 13 14 ··· 27 28 var flags Flags 28 29 29 30 flag.BoolVar(&flags.Debug, "debug", false, "Enable debug logging") 30 - flag.StringVar(&flags.Address, "address", "0.0.0.0", "Address to listen on") 31 + flag.StringVar(&flags.Address, "address", "", "Address to listen on (default auto)") 31 32 flag.StringVar(&flags.Flake, "flake", "", "NixOS configuration flake (for example, .)") 32 33 flag.StringVar(&flags.HostsFile, "hosts", "", "Path to hosts.json file (for example, ./hosts.json)") 33 34 flag.StringVar(&flags.Installer, "installer", "", "NixOS installer flake output (for example, .#nixosConfigurations.installer)") ··· 59 60 } 60 61 log.Debug("parsed hosts config", "hosts", hostsConfig) 61 62 63 + var address string 64 + if flags.Address == "" { 65 + address, err = network.DetectServerAddress() 66 + if err != nil { 67 + log.Fatal("failed to detect server address, please specify --address manually") 68 + } 69 + } else { 70 + address = flags.Address 71 + } 72 + log.Debug("detected server IP", "address", address) 73 + 62 74 ctx, cancel := context.WithCancel(context.Background()) 63 75 defer cancel() 64 76 ··· 70 82 log.Debug("installer components", "kernel", installerComponents.Kernel, "initrd", installerComponents.Initrd, "init", installerComponents.Init) 71 83 72 84 pxeServer, err := serve.NewPXEServer( 73 - flags.Address, 85 + address, 74 86 installerComponents.Kernel, 75 87 installerComponents.Initrd, 76 88 installerComponents.Init, ··· 85 97 log.Fatal("failed to start PXE server", "error", err) 86 98 } 87 99 }() 88 - log.Info("PXE server started", "address", flags.Address) 100 + log.Info("PXE server started", "address", address) 89 101 90 102 sigCh := make(chan os.Signal, 1) 91 103 signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
+41
internal/network/ip.go
··· 1 + package network 2 + 3 + import ( 4 + "fmt" 5 + "net" 6 + 7 + "github.com/charmbracelet/log" 8 + ) 9 + 10 + // Adapted from Pixiecore's DHCP logic (Apache License 2.0) 11 + // https://github.com/danderson/netboot/blob/main/pixiecore/dhcp.go#L247-L278 12 + func DetectServerAddress() (string, error) { 13 + addresses, err := net.InterfaceAddrs() 14 + if err != nil { 15 + return "", fmt.Errorf("failed to get interface addresses: %w", err) 16 + } 17 + 18 + log.Debug("interface addresses", "addresses", addresses) 19 + 20 + // Try to find an IPv4 address to use 21 + fs := [](func(net.IP) bool){ 22 + net.IP.IsGlobalUnicast, 23 + } 24 + for _, f := range fs { 25 + for _, a := range addresses { 26 + ipaddr, ok := a.(*net.IPNet) 27 + if !ok { 28 + continue 29 + } 30 + ip := ipaddr.IP.To4() 31 + if ip == nil { 32 + continue 33 + } 34 + if f(ip) { 35 + return ip.String(), nil 36 + } 37 + } 38 + } 39 + 40 + return "", fmt.Errorf("no usable unicast address") 41 + }