initial
This commit is contained in:
commit
6479d18547
3 changed files with 91 additions and 0 deletions
87
main.go
Normal file
87
main.go
Normal file
|
|
@ -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"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue