Files
dyn/internal/dns/technitium.go
Tanishq Dubey ee187ff1c0 Add full integration test with real Technitium DNS server
- Add docker-compose.integration.yml with Technitium + DDNS services
- Add full-integration-test.sh for end-to-end testing
- Add technitium-init.sh for automated zone/token setup
- Add comprehensive logging to DNS client
- Test creates real DNS records and verifies them in Technitium
- 8/9 tests passing with real DNS integration
2026-02-03 08:13:06 -05:00

193 lines
4.8 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 {
// Build endpoint with token as query parameter for Technitium API
endpoint := fmt.Sprintf("%s/api/dns/records/add?token=%s", c.baseURL, c.token)
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")
log.Printf("[DNS] Using token auth via query parameter")
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
}