124 lines
3.2 KiB
Go
124 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"dns_service/settings"
|
|
"dns_service/utils"
|
|
"embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func Settings(w http.ResponseWriter, req *http.Request) {
|
|
if req.Method == "GET" {
|
|
resp_settings, err := settings.GetSettings()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
jsondata, err := json.Marshal(resp_settings)
|
|
if err != nil {
|
|
http.Error(w, "Error json marshal settings", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Add("Content-Type", "application/json")
|
|
fmt.Fprintf(w, "%s", string(jsondata))
|
|
} else if req.Method == "POST" {
|
|
new_settings := make(map[string]any)
|
|
decoder := json.NewDecoder(req.Body)
|
|
decoder.Decode(&new_settings)
|
|
old_settings, err := settings.GetSettings()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
resp_settings, err := settings.SetSettings(new_settings, old_settings)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
jsondata, err := json.Marshal(resp_settings)
|
|
if err != nil {
|
|
http.Error(w, "Error json marshal settings", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Add("Content-Type", "application/json")
|
|
fmt.Println(new_settings, old_settings, resp_settings)
|
|
fmt.Fprintf(w, "%s", string(jsondata))
|
|
}
|
|
}
|
|
|
|
func SettingsFile(w http.ResponseWriter, req *http.Request) {
|
|
if req.Method == "GET" {
|
|
data, e := os.ReadFile(os.Getenv("DNSMASQ_CONFIG"))
|
|
if e != nil {
|
|
panic(e)
|
|
}
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
fmt.Fprintf(w, "%s", string(data))
|
|
} else if req.Method == "POST" {
|
|
bodyBytes, err := io.ReadAll(req.Body)
|
|
if err != nil {
|
|
http.Error(w, "Error reading request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
err = os.WriteFile(os.Getenv("DNSMASQ_CONFIG"), bodyBytes, 0644)
|
|
if err != nil {
|
|
http.Error(w, "Error writing dnsmasq config file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
fmt.Println(string(bodyBytes))
|
|
utils.RestartDnsmasq()
|
|
exec.Command("rc", "restart", "dnsmasq").Run()
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
fmt.Fprintf(w, "%s", string(bodyBytes))
|
|
}
|
|
}
|
|
|
|
//go:embed frontend/*
|
|
var frontend embed.FS
|
|
|
|
func spaHandler(fs http.FileSystem) http.Handler {
|
|
fileServer := http.FileServer(fs)
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Println("/frontend" + r.URL.Path)
|
|
r.URL.Path = "/frontend" + path.Clean(r.URL.Path)
|
|
if _, err := fs.Open(path.Clean(r.URL.Path)); err != nil {
|
|
r.URL.Path = "/"
|
|
}
|
|
fileServer.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func main() {
|
|
err := godotenv.Load("/etc/dns_service/dns_service.conf")
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
|
|
http.Handle("/", spaHandler(http.FS(frontend)))
|
|
http.HandleFunc("/api/settings", Settings)
|
|
http.HandleFunc("/api/settings/file", SettingsFile)
|
|
|
|
if os.Getenv("ENABLE_TLS") == "true" {
|
|
http.ListenAndServeTLS(
|
|
os.Getenv("SERVER_ADDRESS")+":"+os.Getenv("SERVER_PORT"),
|
|
os.Getenv("CERT_FILE"),
|
|
os.Getenv("KEY_FILE"),
|
|
nil,
|
|
)
|
|
} else {
|
|
http.ListenAndServe(
|
|
os.Getenv("SERVER_ADDRESS")+":"+os.Getenv("SERVER_PORT"),
|
|
nil,
|
|
)
|
|
}
|
|
}
|