commit 6479d18547ce7b5fd9b0545867f7fad34c64b87c Author: Gergely Hegedus Date: Thu Dec 4 20:08:45 2025 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0c2bb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +disable-pihole diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c9709cd --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module disable-pihole + +go 1.25.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..48624ae --- /dev/null +++ b/main.go @@ -0,0 +1,87 @@ +package main + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "os" + "sync" +) + +func main() { + port := os.Args[1] + password := os.Args[2] + urls := os.Args[3:] + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + requestCount := len(urls) + var wg sync.WaitGroup + wg.Add(requestCount) + + for i := 0; i < requestCount; i++ { + disablePihole(urls[i], password, &wg) + } + wg.Wait() + + w.Write([]byte("OK")) + }) + + http.ListenAndServe("127.0.0.1:"+port, nil) +} + +func disablePihole(url string, password string, wg *sync.WaitGroup) error { + defer wg.Done() + sid, err := callAuthenticate(url, password) + if err != nil { + return err + } + requestBody, err := json.Marshal(PiholeDisableBlockingBody{ + Sid: sid, + Blocking: false, + Timer: 60 * 5, + }) + _, err = http.Post(url+"/api/dns/blocking", "application/json", bytes.NewReader(requestBody)) + return err +} + +func callAuthenticate(url string, password string) (string, error) { + requestBody, err := json.Marshal(PiholeAuthRequestBody{ + Password: password, + }) + if err != nil { + return "", err + } + resp, err := http.Post(url+"/api/auth", "application/json", bytes.NewReader(requestBody)) + if err != nil { + return "", err + } + defer resp.Body.Close() + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + result := PiholeAuthResponseBody{} + err = json.Unmarshal(responseBody, &result) + if err != nil { + return "", err + } + + return result.Session.Sid, nil +} + +type PiholeAuthRequestBody struct { + Password string `json:"password"` +} + +type PiholeAuthResponseBody struct { + Session struct { + Sid string `json:"sid"` + } `json:"session"` +} + +type PiholeDisableBlockingBody struct { + Sid string `json:"sid"` + Blocking bool `json:"blocking"` + Timer int64 `json:"timer"` +}