summaryrefslogtreecommitdiff
path: root/database/dns.go
diff options
context:
space:
mode:
Diffstat (limited to 'database/dns.go')
-rw-r--r--database/dns.go59
1 files changed, 56 insertions, 3 deletions
diff --git a/database/dns.go b/database/dns.go
index bb5c1ef..568653d 100644
--- a/database/dns.go
+++ b/database/dns.go
@@ -2,8 +2,10 @@ package database
import (
"database/sql"
+ "fmt"
_ "github.com/mattn/go-sqlite3"
"log"
+ "strings"
"time"
)
@@ -14,6 +16,7 @@ type DNSRecord struct {
Type string `json:"type"`
Content string `json:"content"`
TTL int `json:"ttl"`
+ Internal bool `json:"internal"`
CreatedAt time.Time `json:"created_at"`
}
@@ -29,7 +32,7 @@ func GetUserDNSRecords(db *sql.DB, userID string) ([]DNSRecord, error) {
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.CreatedAt)
+ 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
}
@@ -43,7 +46,7 @@ func SaveDNSRecord(db *sql.DB, record *DNSRecord) (*DNSRecord, error) {
log.Println("saving dns record", record)
record.CreatedAt = time.Now()
- _, err := db.Exec("INSERT OR REPLACE INTO dns_records (id, user_id, name, type, content, ttl, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)", record.ID, record.UserID, record.Name, record.Type, record.Content, record.TTL, record.CreatedAt)
+ _, 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
@@ -56,7 +59,7 @@ func GetDNSRecord(db *sql.DB, recordID string) (*DNSRecord, error) {
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.CreatedAt)
+ 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
}
@@ -72,3 +75,53 @@ func DeleteDNSRecord(db *sql.DB, recordID string) error {
}
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
+}