43 lines
790 B
Go
43 lines
790 B
Go
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
|
|
}
|