Added logic to find next available IP address
This commit is contained in:
42
configreader.go
Normal file
42
configreader.go
Normal file
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
func parseUsedIPsFromConfigs(dir string) ([]net.IP, error) {
|
||||
var usedIPs []net.IP
|
||||
|
||||
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() || !strings.HasSuffix(d.Name(), ".conf") {
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := ini.Load(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse config %s: %w", path, err)
|
||||
}
|
||||
|
||||
// Check [Interface] section
|
||||
if iface := cfg.Section("Interface"); iface != nil {
|
||||
if addr := iface.Key("Address").String(); addr != "" {
|
||||
ip := strings.Split(addr, "/")[0]
|
||||
usedIPs = append(usedIPs, net.ParseIP(ip))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return usedIPs, nil
|
||||
}
|
Reference in New Issue
Block a user