hatecomputers.club/database/dns.go

161 lines
4.2 KiB
Go

package database
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"strings"
"time"
)
type DomainOwner struct {
UserID string `json:"user_id"`
Domain string `json:"domain"`
CreatedAt time.Time `json:"created_at"`
}
type DNSRecord struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
TTL int `json:"ttl"`
Internal bool `json:"internal"`
CreatedAt time.Time `json:"created_at"`
}
func CountUserDNSRecords(db *sql.DB, userID string) (int, error) {
log.Println("counting dns records for user", userID)
row := db.QueryRow("SELECT COUNT(*) FROM dns_records WHERE user_id = ?", userID)
var count int
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func GetUserDNSRecords(db *sql.DB, userID string) ([]DNSRecord, error) {
log.Println("getting dns records for user", userID)
rows, err := db.Query("SELECT * FROM dns_records WHERE user_id = ?", userID)
if err != nil {
return nil, err
}
defer rows.Close()
var records []DNSRecord
for rows.Next() {
var record DNSRecord
err := rows.Scan(&record.ID, &record.UserID, &record.Name, &record.Type, &record.Content, &record.TTL, &record.Internal, &record.CreatedAt)
if err != nil {
return nil, err
}
records = append(records, record)
}
return records, nil
}
func SaveDNSRecord(db *sql.DB, record *DNSRecord) (*DNSRecord, error) {
log.Println("saving dns record", record.ID)
if (record.CreatedAt == time.Time{}) {
record.CreatedAt = time.Now()
}
_, err := db.Exec("INSERT OR REPLACE INTO dns_records (id, user_id, name, type, content, ttl, internal, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", record.ID, record.UserID, record.Name, record.Type, record.Content, record.TTL, record.Internal, record.CreatedAt)
if err != nil {
return nil, err
}
return record, nil
}
func GetDNSRecord(db *sql.DB, recordID string) (*DNSRecord, error) {
log.Println("getting dns record", recordID)
row := db.QueryRow("SELECT * FROM dns_records WHERE id = ?", recordID)
var record DNSRecord
err := row.Scan(&record.ID, &record.UserID, &record.Name, &record.Type, &record.Content, &record.TTL, &record.Internal, &record.CreatedAt)
if err != nil {
return nil, err
}
return &record, nil
}
func DeleteDNSRecord(db *sql.DB, recordID string) error {
log.Println("deleting dns record", recordID)
_, err := db.Exec("DELETE FROM dns_records WHERE id = ?", recordID)
if err != nil {
return err
}
return nil
}
func FindFirstDomainOwnerId(db *sql.DB, domain string) (string, error) {
log.Println("finding domain owner for", domain)
ownerID := ""
parts := strings.Split(domain, ".")
if len(parts) < 2 {
return ownerID, fmt.Errorf("invalid domain; must have at least two parts")
}
for ownerID == "" {
row := db.QueryRow("SELECT user_id FROM domain_owners WHERE domain = ?", strings.Join(parts, "."))
err := row.Scan(&ownerID)
if err != nil {
if len(parts) == 1 {
break
}
parts = parts[1:]
}
}
if ownerID == "" {
return ownerID, fmt.Errorf("no owner found for domain")
}
return ownerID, nil
}
func FindDNSRecords(dbConn *sql.DB, name string, qtype string) ([]DNSRecord, error) {
log.Println("finding dns record(s) for", name, qtype)
rows, err := dbConn.Query("SELECT * FROM dns_records WHERE name = ? AND type = ?", name, qtype)
if err != nil {
return nil, err
}
defer rows.Close()
var records []DNSRecord
for rows.Next() {
var record DNSRecord
err := rows.Scan(&record.ID, &record.UserID, &record.Name, &record.Type, &record.Content, &record.TTL, &record.Internal, &record.CreatedAt)
if err != nil {
return nil, err
}
records = append(records, record)
}
return records, nil
}
func SaveDomainOwner(db *sql.DB, domainOwner *DomainOwner) (*DomainOwner, error) {
log.Println("saving domain owner", domainOwner.Domain)
domainOwner.CreatedAt = time.Now()
_, err := db.Exec("INSERT OR REPLACE INTO domain_owners (user_id, domain, created_at) VALUES (?, ?, ?)", domainOwner.UserID, domainOwner.Domain, domainOwner.CreatedAt)
if err != nil {
return nil, err
}
return domainOwner, nil
}