DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: b5195a96196fb17b7b40459ef750d9f916b6c313 felu/internal/db/domains.go -rw-r--r-- 5.7 KiB
b5195a96Jonni Liljamo feat: handle update queries for _acme-challenge 17 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (C) 2024 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/~liljamo/felu/internal/util"
	"github.com/oklog/ulid/v2"
)

// DomainOwner contains a domains owners information.
type DomainOwner struct {
	ID    string
	Email string
}

// Domain contains a domains information.
type Domain struct {
	ID            string
	APIKey        string
	Domain        string
	A             string
	AcmeChallenge string
	TsigKey       string

	Owner DomainOwner
}

// FetchDomainsForUser fetches all domains for a specific user.
func FetchDomainsForUser(userID string) ([]Domain, error) {
	rows, err := DBConn.Query(`SELECT id, apikey, ddns_domain, a_record, tsigkey
		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, &domain.TsigKey)
		if err != nil {
			return nil, err
		}
		domains = append(domains, domain)
	}
	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return domains, nil
}

// FetchAllDomains fetches all domains.
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
}

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

	return nil
}

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

// DeleteDomainsForUser deletes all domains for a user.
func DeleteDomainsForUser(userID string) error {
	_, err := DBConn.Exec(`DELETE FROM domains WHERE owner = $1`, userID)
	if err != nil {
		return err
	}
	return nil
}

// FetchDomainARecord fetches the A record of a domain.
func FetchDomainARecord(ddnsDomain string) (string, error) {
	var aRecord string
	err := DBConn.QueryRow(`SELECT a_record FROM domains WHERE ddns_domain = $1`,
		ddnsDomain).Scan(&aRecord)
	if err != nil {
		return "", err
	}
	return aRecord, nil
}

// UpdateDomainARecord updates the A record of a domain.
func UpdateDomainARecord(ddnsDomain string, providedAPIKey string, aRecord string) error {
	var domainAPIKey string
	err := DBConn.QueryRow(`SELECT apikey FROM domains WHERE ddns_domain = $1`,
		ddnsDomain).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, ddnsDomain)
	if err != nil {
		return err
	}

	return nil
}

// UpdateDomainARecordManual updates the A record of a domain.
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
}

// RefreshDomainAPIKey refreshes the API key of a domain.
func RefreshDomainAPIKey(id string, userID string) error {
	apiKey := util.GenAPIKey()
	_, err := DBConn.Exec(`UPDATE domains SET apikey = $1 WHERE id = $2 AND owner = $3`, apiKey, id, userID)
	if err != nil {
		return err
	}
	return nil
}

// FetchDomainAcmeChallenge fetches the ACME challenge string of a domain.
func FetchDomainAcmeChallenge(ddnsDomain string) (string, error) {
	var acmeChallengeString string
	err := DBConn.QueryRow(`SELECT acme_challenge FROM domains WHERE ddns_domain = $1`,
		ddnsDomain).Scan(&acmeChallengeString)
	if err != nil {
		return "", err
	}
	return acmeChallengeString, nil
}

// UpdateDomainAcmeChallenge updates the ACME challenge string of a domain.
func UpdateDomainAcmeChallenge(ddnsDomain string, acmeChallengeString string) error {
	_, err := DBConn.Exec(`UPDATE domains SET acme_challenge = $1 WHERE ddns_domain = $2`,
		acmeChallengeString, ddnsDomain)
	if err != nil {
		return err
	}

	return nil
}

// DeleteDomainAcmeChallenge deletes the ACME challenge string of a domain.
func DeleteDomainAcmeChallenge(ddnsDomain string) error {
	_, err := DBConn.Exec(`UPDATE domains SET acme_challenge = NULL WHERE ddns_domain = $1`,
		ddnsDomain)
	if err != nil {
		return err
	}

	return nil
}

// FetchDomainTsigKey fetches the tsig key of a domain.
func FetchDomainTsigKey(ddnsDomain string) (string, error) {
	var tsig string
	err := DBConn.QueryRow(`SELECT tsigkey FROM domains WHERE ddns_domain = $1`,
		ddnsDomain).Scan(&tsig)
	if err != nil {
		return "", err
	}
	return tsig, nil
}

// RefreshDomainTsigKey refreshes the Tsig key of a domain.
func RefreshDomainTsigKey(id string, userID string) error {
	tsigKey := util.GenTsigKey()
	_, err := DBConn.Exec(`UPDATE domains SET tsigkey = $1 WHERE id = $2 AND owner = $3`, tsigKey, id, userID)
	if err != nil {
		return err
	}
	return nil
}