DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 0.1.6 felu/internal/db/domains.go -rw-r--r-- 3.4 KiB
8c173e8fJonni Liljamo feat: manual updating of A records 1 year, 14 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package db

import (
	"errors"

	"git.src.quest/~skye/felu-ddns/internal/util"
	"github.com/oklog/ulid/v2"
)

type DomainOwner struct {
	ID    string
	Email string
}

type Domain struct {
	Id      string
	ApiKey  string
	Domain  string
	A       string

	Owner DomainOwner
}

func FetchDomainsForUser(userId string) ([]Domain, error) {
	rows, err := DBConn.Query(`SELECT id, apikey, ddns_domain, a_record
		FROM domains WHERE owner = $1`, userId)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var domains []Domain
	for rows.Next() {
		var domain Domain
		err = rows.Scan(&domain.Id, &domain.ApiKey, &domain.Domain, &domain.A)
		if err != nil {
			return nil, err
		}
		domains = append(domains, domain)
	}
	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return domains, nil
}

func FetchAllDomains() ([]Domain, error) {
	rows, err := DBConn.Query(`SELECT id, ddns_domain, a_record, owner
		FROM domains`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var domains []Domain
	for rows.Next() {
		var domain Domain
		err = rows.Scan(&domain.Id, &domain.Domain, &domain.A, &domain.Owner.ID)
		if err != nil {
			return nil, err
		}

		ownerEmail, err := FetchUserEmail(domain.Owner.ID)
		if err != nil {
			return nil, err
		}
		domain.Owner.Email = ownerEmail

		domains = append(domains, domain)
	}
	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return domains, nil
}

func CreateDomain(domain string, aRecord string, owner string) error {
	ulid := ulid.Make().String()
	apikey := util.GenApiKey()
	_, err := DBConn.Exec(`INSERT INTO domains(id, apikey, ddns_domain, a_record, owner)
		VALUES ($1, $2, $3, $4, $5)`, ulid, apikey, domain, aRecord, owner)
	if err != nil {
		return err
	}

	return nil
}

func DeleteDomain(id string, user_id string) error {
	_, err := DBConn.Exec(`DELETE FROM domains WHERE id = $1 AND owner = $2`, id, user_id)
	if err != nil {
		return err
	}
	return nil
}

func DeleteDomainsForUser(userId string) error {
	_, err := DBConn.Exec(`DELETE FROM domains WHERE owner = $1`, userId)
	if err != nil {
		return err
	}
	return nil
}

func FetchDomainARecord(ddns_domain string) (string, error) {
	var aRecord string
	err := DBConn.QueryRow(`SELECT a_record FROM domains WHERE ddns_domain = $1`,
		ddns_domain).Scan(&aRecord)
	if err != nil {
		return "", err
	}
	return aRecord, nil
}

func UpdateDomainARecord(ddns_domain string, providedApiKey string, aRecord string) error {
	var domainApiKey string
	err := DBConn.QueryRow(`SELECT apikey FROM domains WHERE ddns_domain = $1`,
		ddns_domain).Scan(&domainApiKey)
	if err != nil {
		return err
	}

	if domainApiKey != providedApiKey {
		return errors.New("API key doesn't match")
	}

	_, err = DBConn.Exec(`UPDATE domains SET a_record = $1 WHERE ddns_domain = $2`,
		aRecord, ddns_domain)
	if err != nil {
		return err
	}
	
	return nil
}

func UpdateDomainARecordManual(id string, userId string, aRecord string) error {
	_, err := DBConn.Exec(`UPDATE domains SET a_record = $1 WHERE id = $2 AND owner = $3`,
		aRecord, id, userId)
	if err != nil {
		return err
	}

	return nil
}


func RefreshDomainApiKey(id string, user_id string) error {
	apiKey := util.GenApiKey()
	_, err := DBConn.Exec(`UPDATE domains SET apikey = $1 WHERE id = $2 AND owner = $3`, apiKey, id, user_id)
	if err != nil {
		return err
	}
	return nil
}