NicoFgrx / pihole-api-go

Simple go client for pihole API
MIT License
2 stars 0 forks source link

Mishandled error while record creation #1

Closed NicoFgrx closed 1 year ago

NicoFgrx commented 1 year ago

No error is return when record already exist in func AddCustomDNS

Example :

func main() {

    url := os.Getenv("PIHOLE_API_URL") // must be http[s]://<IP>:<port>/admin/api.php
    key := os.Getenv("PIHOLE_TOKEN")

    fmt.Println("[+] Creating client")
    client := pihole.NewClient(url, key)

    fmt.Println("[+] Get all existing dns records")
    customdns, err := client.GetAllCustomDNS()
    if err != nil {
        log.Fatalf("An error occured : %s", err)
    }

    for _, record := range customdns {
        fmt.Printf("    - %s: %s\n", record.Domain, record.IP)
    }

    fmt.Println("[+] Create new dns records")
    res, err := client.AddCustomDNS(
        &pihole.DNSRecordParams{
            Domain: "box.pasfastoche.lan",
            IP:     "192.168.1.1",
        },
    )
    if err != nil {
        log.Fatalf("An error occured : %s", err)
    }

    var post2 pihole.PostCustomDNSResponse

    if err := json.NewDecoder(res.Body).Decode(&post2); err != nil {
        log.Fatalf("An error occured : %s", err)
    }

    fmt.Println("[+] Get the new dns records only")
    customdnsrecord, err := client.GetCustomDNS("box.pasfastoche.lan")
    if err != nil {
        log.Fatalf("An error occured : %s", err)
    }

    fmt.Printf("    - %s: %s\n", customdnsrecord.Domain, customdnsrecord.IP)

    fmt.Println("[+] Create AGAIN the dns records")
    res, err = client.AddCustomDNS(
        &pihole.DNSRecordParams{
            Domain: "box.pasfastoche.lan",
            IP:     "192.168.1.1",
        },
    )
    if err != nil {
        log.Fatalf("An error occured : %s", err)
    }
}

Output :

nicolas@cb015nf:~/5AS10/pihole-api-go$ go run examples/main.go 
[+] Creating client
[+] Get all existing dns records
        - pve.pasfastoche.lan: 192.168.1.27
        - dns.pasfastoche.lan: 192.168.1.53
        - pve-idrac.pasfastoche.lan: 10.17.11.1
        - truenas.pasfastoche.lan: 10.50.11.70
        - centreon.pasfastoche.lan: 192.168.1.40
        - box.pasfastoche.lan: 192.168.1.1
[+] Create new dns records
[+] Get the new dns records only
        - box.pasfastoche.lan: 192.168.1.1
[+] Create AGAIN the dns records
[+] Get the new dns records only
        - box.pasfastoche.lan: 192.168.1.1

Must be return an error like "Error : DNS Record already exist ( $domain -> $IP)"

NicoFgrx commented 1 year ago

fixed on v0.3.2