summaryrefslogtreecommitdiff
path: root/api/dns.go
diff options
context:
space:
mode:
authorsimponic <simponic@hatecomputers.club>2024-04-06 15:43:18 -0400
committersimponic <simponic@hatecomputers.club>2024-04-06 15:43:18 -0400
commit83cc6267fd5ce2f61200314424c5f400f65ff2ba (patch)
treeeafb35310236a15572cbb6e16ff8d6f181bfe240 /api/dns.go
parent569d2788ebfb90774faf361f62bfe7968e091465 (diff)
parentcad8e2c4ed5e3bab61ff243f8677f8a46eaeafb0 (diff)
downloadhatecomputers.club-83cc6267fd5ce2f61200314424c5f400f65ff2ba.tar.gz
hatecomputers.club-83cc6267fd5ce2f61200314424c5f400f65ff2ba.zip
Merge pull request 'testing | dont be recursive for external domains | finalize oauth' (#5) from dont-be-authoritative into main
Reviewed-on: https://git.hatecomputers.club/hatecomputers/hatecomputers.club/pulls/5
Diffstat (limited to 'api/dns.go')
-rw-r--r--api/dns.go179
1 files changed, 0 insertions, 179 deletions
diff --git a/api/dns.go b/api/dns.go
deleted file mode 100644
index ad41103..0000000
--- a/api/dns.go
+++ /dev/null
@@ -1,179 +0,0 @@
-package api
-
-import (
- "database/sql"
- "fmt"
- "log"
- "net/http"
- "strconv"
- "strings"
-
- "git.hatecomputers.club/hatecomputers/hatecomputers.club/adapters/cloudflare"
- "git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
- "git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
-)
-
-const MAX_USER_RECORDS = 65
-
-type FormError struct {
- Errors []string
-}
-
-func userCanFuckWithDNSRecord(dbConn *sql.DB, user *database.User, record *database.DNSRecord) bool {
- ownedByUser := (user.ID == record.UserID)
- if !ownedByUser {
- return false
- }
-
- if !record.Internal {
- userOwnedDomains := []string{
- fmt.Sprintf("%s", user.Username),
- fmt.Sprintf("%s.endpoints", user.Username),
- }
-
- for _, domain := range userOwnedDomains {
- isInSubDomain := strings.HasSuffix(record.Name, "."+domain)
- if domain == record.Name || isInSubDomain {
- return true
- }
- }
- return false
- }
-
- owner, err := database.FindFirstDomainOwnerId(dbConn, record.Name)
- if err != nil {
- log.Println(err)
- return false
- }
-
- userIsOwnerOfDomain := owner == user.ID
- return ownedByUser && userIsOwnerOfDomain
-}
-
-func ListDNSRecordsContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
- return func(success Continuation, failure Continuation) ContinuationChain {
- dnsRecords, err := database.GetUserDNSRecords(context.DBConn, context.User.ID)
- if err != nil {
- log.Println(err)
- resp.WriteHeader(http.StatusInternalServerError)
- return failure(context, req, resp)
- }
-
- (*context.TemplateData)["DNSRecords"] = dnsRecords
- return success(context, req, resp)
- }
-}
-
-func CreateDNSRecordContinuation(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 += "."
- }
-
- 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")
- }
-
- 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")
- }
-
- 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 err != nil {
- log.Println(err)
- formErrors.Errors = append(formErrors.Errors, err.Error())
- }
-
- 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)
- }
-
- (*context.TemplateData)["FormError"] = &formErrors
- (*context.TemplateData)["RecordForm"] = dnsRecord
-
- 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)
- }
-
- 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 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)
- }
-}