more optimizations
Build / Build (push) Successful in 32s

This commit is contained in:
wt committed 2025-07-07 22:40:06 +07:00
1 parent 952587df4f
commit efcb19aa2f
19 files changed
+276 -189

No files matched your search

+36 -17
View File
@@ -2,32 +2,51 @@ package settings
import (
"dns_service/utils"
"fmt"
"os"
"strconv"
)
func GetSettings() (map[string]any, error) {
settings := make(map[string]any)
data, e := os.ReadFile(os.Getenv("DNSMASQ_CONFIG"))
if e != nil {
return settings, e
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", "=")
if dnsmasqSettings["cache-size"] != "" {
cache_size, _ := strconv.ParseInt(dnsmasqSettings["cache-size"], 10, 64)
settings["cache_size"] = cache_size
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
}
data, e = os.ReadFile(dnsmasqSettings["resolv-file"])
if e != nil {
return settings, e
// 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))
}
resolvSettings := utils.DecodeResolvs(string(data))
settings["resolv"] = resolvSettings
data, e = os.ReadFile(dnsmasqSettings["addn-hosts"])
if e != nil {
return settings, e
// 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))
}
hosts := utils.DecodeHosts(string(data))
settings["hosts"] = hosts
return settings, nil
}
+74 -39
View File
@@ -7,51 +7,86 @@ import (
"strings"
)
func SetSettings(new_settings map[string]any, old_settings map[string]any) (map[string]any, error) {
data, err := os.ReadFile(os.Getenv("DNSMASQ_CONFIG"))
// Helper to write file content
func writeFile(path, content string) error {
return os.WriteFile(path, []byte(content), 0644)
}
func SetSettings(new_settings map[string]any) (map[string]any, error) {
// 1. Get config path and read the main dnsmasq configuration
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 old_settings, err
return nil, fmt.Errorf("failed to read config file at %s: %w", configPath, err)
}
new_data := strings.Replace(
string(data),
"cache-size="+fmt.Sprintf("%v", old_settings["cache_size"]),
"cache-size="+fmt.Sprintf("%v", new_settings["cache_size"]),
1,
)
err = os.WriteFile(os.Getenv("DNSMASQ_CONFIG"), []byte(new_data), 0644)
if err != nil {
return old_settings, err
}
var resolvs string
new_resolvs := new_settings["resolv"].([]any)
for i := range new_resolvs {
resolvs += "nameserver " + new_resolvs[i].(string) + "\n"
}
var hosts string
new_hosts := new_settings["hosts"].(map[string]any)
for key, value := range new_hosts {
hosts += key
switch value.(type) {
case []any:
arrvalue := value.([]any)
for i := range arrvalue {
hosts += " " + arrvalue[i].(string)
dnsmasqSettings := utils.DecodeSettings(string(data), "\n", "=")
// 2. Update cache-size in the main config file
if newCacheSize, ok := new_settings["cache_size"]; ok {
oldCacheSize, oldOk := dnsmasqSettings["cache-size"]
// Replace the line if it exists, otherwise dnsmasq uses a default
if oldOk {
newConfigContent := strings.Replace(
string(data),
"cache-size="+oldCacheSize,
"cache-size="+fmt.Sprintf("%v", newCacheSize),
1,
)
if err := writeFile(configPath, newConfigContent); err != nil {
return nil, fmt.Errorf("failed to write updated config to %s: %w", configPath, err)
}
hosts += "\n"
break
case string:
hosts += " " + value.(string) + "\n"
}
}
fmt.Println(resolvs, hosts)
err = os.WriteFile("/etc/dnsmasq.conf.d/resolv.conf", []byte(resolvs), 0644)
if err != nil {
return old_settings, err
// 3. Update the resolv file
if newResolvs, ok := new_settings["resolv"].([]any); ok {
resolvFile, ok := dnsmasqSettings["resolv-file"]
if !ok {
return nil, fmt.Errorf("resolv-file path not found in dnsmasq config")
}
var b strings.Builder
for _, v := range newResolvs {
if s, ok := v.(string); ok {
fmt.Fprintf(&b, "nameserver %s\n", s)
}
}
if err := writeFile(resolvFile, b.String()); err != nil {
return nil, fmt.Errorf("failed to write to resolv file %s: %w", resolvFile, err)
}
}
err = os.WriteFile("/etc/dnsmasq.conf.d/hosts", []byte(hosts), 0644)
if err != nil {
return old_settings, err
// 4. Update the hosts file
if newHosts, ok := new_settings["hosts"].(map[string]any); ok {
hostsFile, ok := dnsmasqSettings["addn-hosts"]
if !ok {
return nil, fmt.Errorf("addn-hosts path not found in dnsmasq config")
}
var b strings.Builder
for key, value := range newHosts {
b.WriteString(key)
switch v := value.(type) {
case []any:
for _, ip := range v {
fmt.Fprintf(&b, " %s", ip)
}
b.WriteString("\n")
case string:
fmt.Fprintf(&b, " %s\n", v)
}
}
if err := writeFile(hostsFile, b.String()); err != nil {
return nil, fmt.Errorf("failed to write to hosts file %s: %w", hostsFile, err)
}
}
utils.RestartDnsmasq()
// 5. Restart dnsmasq to apply changes
if err := utils.RestartDnsmasq(); err != nil {
return nil, fmt.Errorf("failed to restart dnsmasq: %w", err)
}
return new_settings, nil
}