summaryrefslogtreecommitdiff
path: root/api/dns.go
diff options
context:
space:
mode:
authorElizabeth <elizabeth@simponic.xyz>2024-04-03 14:27:55 -0600
committerElizabeth <elizabeth@simponic.xyz>2024-04-03 14:27:55 -0600
commitcc33a90bfd455f36169b01b0cca064cd35e2524f (patch)
treef5c061592caedddf767afe7a8b581000e0c7e5ce /api/dns.go
parentc32ca84e8a1a87994d5233a2d51c640a368b88bc (diff)
downloadhatecomputers.club-cc33a90bfd455f36169b01b0cca064cd35e2524f.tar.gz
hatecomputers.club-cc33a90bfd455f36169b01b0cca064cd35e2524f.zip
abstract dns adapter
Diffstat (limited to 'api/dns.go')
-rw-r--r--api/dns.go177
1 files changed, 90 insertions, 87 deletions
diff --git a/api/dns.go b/api/dns.go
index ad41103..6f0e1fd 100644
--- a/api/dns.go
+++ b/api/dns.go
@@ -8,7 +8,7 @@ import (
"strconv"
"strings"
- "git.hatecomputers.club/hatecomputers/hatecomputers.club/adapters/cloudflare"
+ "git.hatecomputers.club/hatecomputers/hatecomputers.club/adapters"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
)
@@ -64,116 +64,119 @@ func ListDNSRecordsContinuation(context *RequestContext, req *http.Request, resp
}
}
-func CreateDNSRecordContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
- return func(success Continuation, failure Continuation) ContinuationChain {
- formErrors := FormError{
- Errors: []string{},
- }
+func CreateDNSRecordContinuation(dnsAdapter external_dns.ExternalDNSAdapter) func(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
+ return func(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
+ return func(success Continuation, failure Continuation) ContinuationChain {
+ formErrors := FormError{
+ Errors: []string{},
+ }
- internal := req.FormValue("internal") == "on"
- name := req.FormValue("name")
- if internal && !strings.HasSuffix(name, ".") {
- name += "."
- }
+ internal := req.FormValue("internal") == "on"
+ name := req.FormValue("name")
+ if internal && !strings.HasSuffix(name, ".") {
+ name += "."
+ }
- recordType := req.FormValue("type")
- recordType = strings.ToUpper(recordType)
+ recordType := req.FormValue("type")
+ recordType = strings.ToUpper(recordType)
- recordContent := req.FormValue("content")
- ttl := req.FormValue("ttl")
- ttlNum, err := strconv.Atoi(ttl)
- if err != nil {
- formErrors.Errors = append(formErrors.Errors, "invalid ttl")
- }
+ recordContent := req.FormValue("content")
+ ttl := req.FormValue("ttl")
+ ttlNum, err := strconv.Atoi(ttl)
+ if err != nil {
+ formErrors.Errors = append(formErrors.Errors, "invalid ttl")
+ }
- dnsRecordCount, err := database.CountUserDNSRecords(context.DBConn, context.User.ID)
- if err != nil {
- log.Println(err)
- resp.WriteHeader(http.StatusInternalServerError)
- return failure(context, req, resp)
- }
- if dnsRecordCount >= MAX_USER_RECORDS {
- formErrors.Errors = append(formErrors.Errors, "max records reached")
- }
+ dnsRecordCount, err := database.CountUserDNSRecords(context.DBConn, context.User.ID)
+ if err != nil {
+ log.Println(err)
+ resp.WriteHeader(http.StatusInternalServerError)
+ return failure(context, req, resp)
+ }
+ if dnsRecordCount >= MAX_USER_RECORDS {
+ formErrors.Errors = append(formErrors.Errors, "max records reached")
+ }
- dnsRecord := &database.DNSRecord{
- UserID: context.User.ID,
- Name: name,
- Type: recordType,
- Content: recordContent,
- TTL: ttlNum,
- Internal: internal,
- }
- if !userCanFuckWithDNSRecord(context.DBConn, context.User, dnsRecord) {
- formErrors.Errors = append(formErrors.Errors, "'name' must end with "+context.User.Username+" or you must be a domain owner for internal domains")
- }
+ dnsRecord := &database.DNSRecord{
+ UserID: context.User.ID,
+ Name: name,
+ Type: recordType,
+ Content: recordContent,
+ TTL: ttlNum,
+ Internal: internal,
+ }
+
+ if !userCanFuckWithDNSRecord(context.DBConn, context.User, dnsRecord) {
+ formErrors.Errors = append(formErrors.Errors, "'name' must end with "+context.User.Username+" or you must be a domain owner for internal domains")
+ }
- if len(formErrors.Errors) == 0 {
- if dnsRecord.Internal {
- dnsRecord.ID = utils.RandomId()
- } else {
- cloudflareRecordId, err := cloudflare.CreateDNSRecord(context.Args.CloudflareZone, context.Args.CloudflareToken, dnsRecord)
+ if len(formErrors.Errors) == 0 {
+ if dnsRecord.Internal {
+ dnsRecord.ID = utils.RandomId()
+ } else {
+ dnsRecord.ID, err = dnsAdapter.CreateDNSRecord(dnsRecord)
+ if err != nil {
+ log.Println(err)
+ formErrors.Errors = append(formErrors.Errors, err.Error())
+ }
+ }
+ }
+
+ if len(formErrors.Errors) == 0 {
+ _, err := database.SaveDNSRecord(context.DBConn, dnsRecord)
if err != nil {
log.Println(err)
- formErrors.Errors = append(formErrors.Errors, err.Error())
+ formErrors.Errors = append(formErrors.Errors, "error saving record")
}
-
- dnsRecord.ID = cloudflareRecordId
}
- }
- if len(formErrors.Errors) == 0 {
- _, err := database.SaveDNSRecord(context.DBConn, dnsRecord)
- if err != nil {
- log.Println(err)
- formErrors.Errors = append(formErrors.Errors, "error saving record")
+ if len(formErrors.Errors) == 0 {
+ http.Redirect(resp, req, "/dns", http.StatusFound)
+ return success(context, req, resp)
}
- }
- if len(formErrors.Errors) == 0 {
- http.Redirect(resp, req, "/dns", http.StatusFound)
- return success(context, req, resp)
- }
-
- (*context.TemplateData)["FormError"] = &formErrors
- (*context.TemplateData)["RecordForm"] = dnsRecord
+ (*context.TemplateData)["FormError"] = &formErrors
+ (*context.TemplateData)["RecordForm"] = dnsRecord
- resp.WriteHeader(http.StatusBadRequest)
- return failure(context, req, resp)
+ resp.WriteHeader(http.StatusBadRequest)
+ return failure(context, req, resp)
+ }
}
}
-func DeleteDNSRecordContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
- return func(success Continuation, failure Continuation) ContinuationChain {
- recordId := req.FormValue("id")
- record, err := database.GetDNSRecord(context.DBConn, recordId)
- if err != nil {
- log.Println(err)
- resp.WriteHeader(http.StatusInternalServerError)
- return failure(context, req, resp)
- }
+func DeleteDNSRecordContinuation(dnsAdapter external_dns.ExternalDNSAdapter) func(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
+ return func(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
+ return func(success Continuation, failure Continuation) ContinuationChain {
+ recordId := req.FormValue("id")
+ record, err := database.GetDNSRecord(context.DBConn, recordId)
+ if err != nil {
+ log.Println(err)
+ resp.WriteHeader(http.StatusInternalServerError)
+ return failure(context, req, resp)
+ }
- if !userCanFuckWithDNSRecord(context.DBConn, context.User, record) {
- resp.WriteHeader(http.StatusUnauthorized)
- return failure(context, req, resp)
- }
+ if !userCanFuckWithDNSRecord(context.DBConn, context.User, record) {
+ resp.WriteHeader(http.StatusUnauthorized)
+ return failure(context, req, resp)
+ }
- if !record.Internal {
- err = cloudflare.DeleteDNSRecord(context.Args.CloudflareZone, context.Args.CloudflareToken, recordId)
+ if !record.Internal {
+ err = dnsAdapter.DeleteDNSRecord(recordId)
+ if err != nil {
+ log.Println(err)
+ resp.WriteHeader(http.StatusInternalServerError)
+ return failure(context, req, resp)
+ }
+ }
+
+ err = database.DeleteDNSRecord(context.DBConn, recordId)
if err != nil {
- log.Println(err)
resp.WriteHeader(http.StatusInternalServerError)
return failure(context, req, resp)
}
- }
- err = database.DeleteDNSRecord(context.DBConn, recordId)
- if err != nil {
- resp.WriteHeader(http.StatusInternalServerError)
- return failure(context, req, resp)
+ http.Redirect(resp, req, "/dns", http.StatusFound)
+ return success(context, req, resp)
}
-
- http.Redirect(resp, req, "/dns", http.StatusFound)
- return success(context, req, resp)
}
}