2024-03-28 00:55:22 -04:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-03-28 16:51:08 -04:00
|
|
|
"fmt"
|
2024-03-28 00:55:22 -04:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"log"
|
2024-03-28 16:51:08 -04:00
|
|
|
"strings"
|
2024-03-28 00:55:22 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DNSRecord struct {
|
2024-03-28 12:57:35 -04:00
|
|
|
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"`
|
2024-03-28 16:51:08 -04:00
|
|
|
Internal bool `json:"internal"`
|
2024-03-28 12:57:35 -04:00
|
|
|
CreatedAt time.Time `json:"created_at"`
|
2024-03-28 00:55:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-03-28 16:51:08 -04:00
|
|
|
err := rows.Scan(&record.ID, &record.UserID, &record.Name, &record.Type, &record.Content, &record.TTL, &record.Internal, &record.CreatedAt)
|
2024-03-28 00:55:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
records = append(records, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
2024-03-28 12:57:35 -04:00
|
|
|
|
|
|
|
func SaveDNSRecord(db *sql.DB, record *DNSRecord) (*DNSRecord, error) {
|
|
|
|
log.Println("saving dns record", record)
|
|
|
|
|
|
|
|
record.CreatedAt = time.Now()
|
2024-03-28 16:51:08 -04:00
|
|
|
_, 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)
|
2024-03-28 12:57:35 -04:00
|
|
|
|
|
|
|
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
|
2024-03-28 16:51:08 -04:00
|
|
|
err := row.Scan(&record.ID, &record.UserID, &record.Name, &record.Type, &record.Content, &record.TTL, &record.Internal, &record.CreatedAt)
|
2024-03-28 12:57:35 -04:00
|
|
|
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
|
|
|
|
}
|
2024-03-28 16:51:08 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|