first commit

This commit is contained in:
2025-12-22 18:49:05 +01:00
commit 658ab8e961
19 changed files with 1356 additions and 0 deletions

44
apis/pihole/info.go Normal file
View File

@@ -0,0 +1,44 @@
package pihole
import (
"encoding/json"
"log"
)
type Version struct {
Version Versions `json:"version"`
}
type Versions struct {
Core ModuleVersion `json:"core"`
Web ModuleVersion `json:"web"`
FTL ModuleVersion `json:"FTL"`
Docker DockerVersion `json:"docker"`
}
type ModuleVersion struct {
Local LocalVersion `json:"local"`
Remote LocalVersion `json:"remote"`
}
type LocalVersion struct {
Branch string `json:"branch"`
Version string `json:"version"`
Hash string `json:"hash"`
Date string `json:"date"`
}
type DockerVersion struct {
Local string `json:"local"`
Remote string `json:"remote"`
}
func (ph *PiHConnector) Version() Version {
version := &Version{}
err := json.Unmarshal(ph.get("info/version"), version)
if err != nil {
log.Fatal(err)
}
return *version
}

100
apis/pihole/pihole.go Normal file
View File

@@ -0,0 +1,100 @@
package pihole
import (
"ArinDash/config"
"encoding/json"
"io"
"log"
"net/http"
"strings"
)
type configFile struct {
Pihole piholeConfig
}
type piholeConfig struct {
Host string
Password string
}
type PiHConnector struct {
Host string
Session PiHSession
}
type PiHAuth struct {
Session PiHSession `json:"session"`
}
type PiHSession struct {
SID string `json:"sid"`
CSRF string `json:"csrf"`
}
func (ph *PiHConnector) get(endpoint string) []byte {
return ph.do("GET", endpoint, nil)
}
func (ph *PiHConnector) post(endpoint string, body io.Reader) []byte {
return ph.do("POST", endpoint, body)
}
func (ph *PiHConnector) do(method string, endpoint string, body io.Reader) []byte {
var requestString = "http://" + ph.Host + "/api/" + endpoint
client := &http.Client{}
req, err := http.NewRequest(method, requestString, body)
if err != nil {
log.Fatal(err)
}
req.Header.Add("X-FTL-SID", ph.Session.SID)
req.Header.Add("X-FTL-CSRF", ph.Session.CSRF)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return respBody
}
func Connect() PiHConnector {
cfg := &configFile{}
config.LoadConfig(cfg)
client := &http.Client{}
req, err := http.NewRequest("POST", "http://"+cfg.Pihole.Host+"/api/auth", strings.NewReader("{\"password\": \""+cfg.Pihole.Password+"\"}"))
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
s := &PiHAuth{}
err = json.Unmarshal(respBody, s)
if err != nil {
log.Fatal(err)
}
return PiHConnector{
Host: cfg.Pihole.Host,
Session: s.Session,
}
}

41
apis/pihole/stats.go Normal file
View File

@@ -0,0 +1,41 @@
package pihole
import (
"encoding/json"
"log"
)
type Summary struct {
Queries Queries `json:"queries"`
Clients Clients `json:"clients"`
Gravity Gravity `json:"gravity"`
}
type Queries struct {
Total int64 `json:"total"`
Blocked int64 `json:"blocked"`
PercentBlocked float64 `json:"percent_blocked"`
UniqueDomains int64 `json:"unique_domains"`
Forwarded int64 `json:"forwarded"`
Cached int64 `json:"cached"`
}
type Clients struct {
Active int64 `json:"active"`
Total int64 `json:"total"`
}
type Gravity struct {
DomainsBeingBlocked int64 `json:"domains_being_blocked"`
LastUpdated int64 `json:"last_updated"`
}
func (ph *PiHConnector) Summary() Summary {
summary := &Summary{}
err := json.Unmarshal(ph.get("stats/summary"), summary)
if err != nil {
log.Fatal(err)
}
return *summary
}