53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package settings
|
|
|
|
import (
|
|
"dns_service/utils"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func GetSettings() (map[string]any, error) {
|
|
configPath := os.Getenv("DNSMASQ_CONFIG")
|
|
if configPath == "" {
|
|
return nil, fmt.Errorf("DNSMASQ_CONFIG environment variable not set")
|
|
}
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read config file at %s: %w", configPath, err)
|
|
}
|
|
|
|
dnsmasqSettings := utils.DecodeSettings(string(data), "\n", "=")
|
|
settings := make(map[string]any)
|
|
|
|
// Parse cache size
|
|
if cacheSizeStr, ok := dnsmasqSettings["cache-size"]; ok && cacheSizeStr != "" {
|
|
cacheSize, err := strconv.ParseInt(cacheSizeStr, 10, 64)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse cache-size '%s': %w", cacheSizeStr, err)
|
|
}
|
|
settings["cache_size"] = cacheSize
|
|
}
|
|
|
|
// Read and parse resolv file
|
|
if resolvFile, ok := dnsmasqSettings["resolv-file"]; ok {
|
|
data, err := os.ReadFile(resolvFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read resolv-file at %s: %w", resolvFile, err)
|
|
}
|
|
settings["resolv"] = utils.DecodeResolvs(string(data))
|
|
}
|
|
|
|
// Read and parse hosts file
|
|
if hostsFile, ok := dnsmasqSettings["addn-hosts"]; ok {
|
|
data, err := os.ReadFile(hostsFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read addn-hosts file at %s: %w", hostsFile, err)
|
|
}
|
|
settings["hosts"] = utils.DecodeHosts(string(data))
|
|
}
|
|
|
|
return settings, nil
|
|
}
|