initial commit

This commit is contained in:
wt committed 2025-06-26 18:20:20 +07:00
commit 5ba52dd285
19 files changed
+639

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
package settings
import (
"dns_service/utils"
"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
}
dnsmasqSettings := utils.DecodeSettings(string(data), "\n", "=")
if dnsmasqSettings["cache-size"] != "" {
cache_size, _ := strconv.ParseInt(dnsmasqSettings["cache-size"], 10, 64)
settings["cache_size"] = cache_size
}
data, e = os.ReadFile(dnsmasqSettings["resolv-file"])
if e != nil {
return settings, e
}
resolvSettings := utils.DecodeResolvs(string(data))
settings["resolv"] = resolvSettings
data, e = os.ReadFile(dnsmasqSettings["addn-hosts"])
if e != nil {
return settings, e
}
hosts := utils.DecodeHosts(string(data))
settings["hosts"] = hosts
return settings, nil
}
+57
View File
@@ -0,0 +1,57 @@
package settings
import (
"dns_service/utils"
"fmt"
"os"
"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"))
if err != nil {
return old_settings, 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)
}
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
}
err = os.WriteFile("/etc/dnsmasq.conf.d/hosts", []byte(hosts), 0644)
if err != nil {
return old_settings, err
}
utils.RestartDnsmasq()
return new_settings, nil
}