- Add request logging middleware to main.go - Add debug handler with health, config, stats, and test-dns endpoints - Add detailed logging to DynDNS handler - Add logging to Technitium DNS client - Add database Ping() and GetStats() methods - New endpoints: - /health - detailed health status with database and DNS checks - /debug/config - sanitized configuration - /debug/stats - database statistics - /debug/test-dns - live DNS test endpoint This will help diagnose production issues with DNS updates.
201 lines
5.0 KiB
Go
201 lines
5.0 KiB
Go
package dns
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
token string
|
|
username string
|
|
password string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
type AddRecordRequest struct {
|
|
Domain string `json:"domain"`
|
|
Type string `json:"type"`
|
|
IPAddress string `json:"ipAddress,omitempty"`
|
|
Overwrite bool `json:"overwrite"`
|
|
TTL int `json:"ttl,omitempty"`
|
|
}
|
|
|
|
type AddRecordResponse struct {
|
|
Status string `json:"status"`
|
|
ErrorCode string `json:"errorCode,omitempty"`
|
|
Error string `json:"errorMessage,omitempty"`
|
|
}
|
|
|
|
type APIResponse struct {
|
|
Status string `json:"status"`
|
|
Response json.RawMessage `json:"response"`
|
|
Error *APIError `json:"error,omitempty"`
|
|
}
|
|
|
|
type APIError struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func NewClient(baseURL, token, username, password string) *Client {
|
|
return &Client{
|
|
baseURL: baseURL,
|
|
token: token,
|
|
username: username,
|
|
password: password,
|
|
httpClient: &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Client) AddARecord(zone, hostname, ip string, ttl int) error {
|
|
domain := fmt.Sprintf("%s.%s", hostname, zone)
|
|
|
|
reqBody := AddRecordRequest{
|
|
Domain: domain,
|
|
Type: "A",
|
|
IPAddress: ip,
|
|
Overwrite: true,
|
|
}
|
|
|
|
if ttl > 0 {
|
|
reqBody.TTL = ttl
|
|
}
|
|
|
|
return c.addRecord(reqBody)
|
|
}
|
|
|
|
func (c *Client) AddWildcardARecord(zone, hostname, ip string, ttl int) error {
|
|
domain := fmt.Sprintf("*.%s.%s", hostname, zone)
|
|
|
|
reqBody := AddRecordRequest{
|
|
Domain: domain,
|
|
Type: "A",
|
|
IPAddress: ip,
|
|
Overwrite: true,
|
|
}
|
|
|
|
if ttl > 0 {
|
|
reqBody.TTL = ttl
|
|
}
|
|
|
|
return c.addRecord(reqBody)
|
|
}
|
|
|
|
func (c *Client) addRecord(req AddRecordRequest) error {
|
|
endpoint := fmt.Sprintf("%s/api/dns/records/add", c.baseURL)
|
|
log.Printf("[DNS] Adding record: domain=%s, type=%s, ip=%s", req.Domain, req.Type, req.IPAddress)
|
|
|
|
formData := url.Values{}
|
|
formData.Set("domain", req.Domain)
|
|
formData.Set("type", req.Type)
|
|
formData.Set("ipAddress", req.IPAddress)
|
|
formData.Set("overwrite", fmt.Sprintf("%t", req.Overwrite))
|
|
if req.TTL > 0 {
|
|
formData.Set("ttl", fmt.Sprintf("%d", req.TTL))
|
|
}
|
|
|
|
httpReq, err := http.NewRequest("POST", endpoint, bytes.NewBufferString(formData.Encode()))
|
|
if err != nil {
|
|
log.Printf("[DNS] Failed to create request: %v", err)
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
if c.token != "" {
|
|
httpReq.Header.Set("Authorization", "Basic "+c.token)
|
|
log.Printf("[DNS] Using token auth")
|
|
} else if c.username != "" && c.password != "" {
|
|
httpReq.SetBasicAuth(c.username, c.password)
|
|
log.Printf("[DNS] Using basic auth (username: %s)", c.username)
|
|
} else {
|
|
log.Printf("[DNS] Warning: No authentication configured!")
|
|
}
|
|
|
|
log.Printf("[DNS] Sending request to %s", endpoint)
|
|
resp, err := c.httpClient.Do(httpReq)
|
|
if err != nil {
|
|
log.Printf("[DNS] Request failed: %v", err)
|
|
return fmt.Errorf("failed to execute request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Printf("[DNS] Failed to read response: %v", err)
|
|
return fmt.Errorf("failed to read response body: %w", err)
|
|
}
|
|
|
|
log.Printf("[DNS] Response status: %d, body: %s", resp.StatusCode, string(body))
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("HTTP error %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
var apiResp APIResponse
|
|
if err := json.Unmarshal(body, &apiResp); err != nil {
|
|
log.Printf("[DNS] Failed to parse response: %v", err)
|
|
return fmt.Errorf("failed to parse response: %w", err)
|
|
}
|
|
|
|
if apiResp.Status != "ok" {
|
|
if apiResp.Error != nil {
|
|
log.Printf("[DNS] API error: %s - %s", apiResp.Error.Code, apiResp.Error.Message)
|
|
return fmt.Errorf("API error: %s - %s", apiResp.Error.Code, apiResp.Error.Message)
|
|
}
|
|
log.Printf("[DNS] API error: status not ok")
|
|
return fmt.Errorf("API error: status not ok")
|
|
}
|
|
|
|
log.Printf("[DNS] Record added successfully")
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) DeleteRecord(zone, hostname, recordType string) error {
|
|
endpoint := fmt.Sprintf("%s/api/dns/records/delete", c.baseURL)
|
|
|
|
domain := fmt.Sprintf("%s.%s", hostname, zone)
|
|
if hostname == "" || hostname == "@" {
|
|
domain = zone
|
|
}
|
|
|
|
formData := url.Values{}
|
|
formData.Set("domain", domain)
|
|
formData.Set("type", recordType)
|
|
|
|
httpReq, err := http.NewRequest("POST", endpoint, bytes.NewBufferString(formData.Encode()))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
if c.token != "" {
|
|
httpReq.Header.Set("Authorization", "Basic "+c.token)
|
|
} else if c.username != "" && c.password != "" {
|
|
httpReq.SetBasicAuth(c.username, c.password)
|
|
}
|
|
|
|
resp, err := c.httpClient.Do(httpReq)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to execute request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("HTTP error %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
return nil
|
|
}
|