dns/prime.go

142 lines
3.4 KiB
Go
Raw Normal View History

2025-01-12 13:35:02 +01:00
package main
import (
"fmt"
2025-01-12 14:29:47 +01:00
"log"
2025-01-12 13:35:02 +01:00
"math/big"
"regexp"
"strings"
"github.com/miekg/dns"
)
var numberRegex = regexp.MustCompile(`^[0-9]+$`)
var maximumPrimeNumber, _ = new(big.Int).SetString("0xFFFF_FFFF_FFFF_FFFF", 0)
func IsNumber(stringToCheck string) bool {
return numberRegex.MatchString(stringToCheck)
}
func HandlePrimeNumberChecker(question dns.Question) dns.RR {
2025-01-12 14:29:47 +01:00
log.Println("1")
2025-01-12 13:35:02 +01:00
mainDomainPartindex := strings.LastIndex(question.Name, "prime.cringe-studios.com")
2025-01-12 14:29:47 +01:00
log.Println("2")
2025-01-12 13:35:02 +01:00
if mainDomainPartindex == 0 || mainDomainPartindex == 1 {
// TODO handle ns page part.. maybe send back a CNAME for a page to enter the number
2025-01-12 14:29:47 +01:00
log.Println("3")
2025-01-12 13:35:02 +01:00
return nil
}
isQuestionPartIndex := strings.LastIndex(question.Name, "is.prime.cringe-studios.com")
2025-01-12 14:29:47 +01:00
log.Println("4")
2025-01-12 13:35:02 +01:00
if isQuestionPartIndex == 0 || isQuestionPartIndex == 1 {
2025-01-12 14:29:47 +01:00
log.Println("5")
2025-01-12 13:35:02 +01:00
// TODO handle resource found
return nil
}
isnotQuestionPartIndex := strings.LastIndex(question.Name, "isnot.prime.cringe-studios.com")
2025-01-12 14:29:47 +01:00
log.Println("6")
2025-01-12 13:35:02 +01:00
if isnotQuestionPartIndex == 0 || isnotQuestionPartIndex == 1 {
// TODO handle resource found
2025-01-12 14:29:47 +01:00
log.Println("7")
2025-01-12 13:35:02 +01:00
return nil
}
maybeQuestionPartIndex := strings.LastIndex(question.Name, "maybe.prime.cringe-studios.com")
2025-01-12 14:29:47 +01:00
log.Println("8")
2025-01-12 13:35:02 +01:00
if maybeQuestionPartIndex == 0 || maybeQuestionPartIndex == 1 {
// TODO handle resource found
2025-01-12 14:29:47 +01:00
log.Println("9")
2025-01-12 13:35:02 +01:00
return nil
}
2025-01-12 14:47:16 +01:00
mainDomainPart := question.Name[0:mainDomainPartindex] // subtract 1 for the p in prime
2025-01-12 14:29:47 +01:00
log.Println("10:" + mainDomainPart)
2025-01-12 13:35:02 +01:00
if !strings.HasSuffix(mainDomainPart, ".") {
// unknown subdomain like 123prime.cringe-studios.com
2025-01-12 14:29:47 +01:00
log.Println("11")
2025-01-12 13:35:02 +01:00
return nil
}
2025-01-12 14:47:16 +01:00
mainDomainPart = mainDomainPart[0 : len(mainDomainPart)-1] // subtract 1 to accomodate for the length being one bigger then the last index, and subtract another one for the dot at the end
2025-01-12 14:29:47 +01:00
log.Println("12:" + mainDomainPart)
2025-01-12 13:35:02 +01:00
if len(mainDomainPart) == 0 {
// .prime.cringe-studios.com
2025-01-12 14:29:47 +01:00
log.Println("13")
2025-01-12 13:35:02 +01:00
return nil
}
firstIsIndex := strings.Index(mainDomainPart, "is.")
2025-01-12 14:29:47 +01:00
log.Println("14")
2025-01-12 13:35:02 +01:00
if firstIsIndex == 0 || firstIsIndex == 1 {
// word "is." found at the beginning of the question.. trim
2025-01-12 14:47:16 +01:00
mainDomainPart = mainDomainPart[len("is."):len(mainDomainPart)]
2025-01-12 13:35:02 +01:00
}
2025-01-12 14:29:47 +01:00
log.Println("15: " + mainDomainPart)
2025-01-12 13:35:02 +01:00
if len(mainDomainPart) == 0 {
// requested domain was is.prime.cringe-studios.com or something like that...
// TODO
2025-01-12 14:29:47 +01:00
log.Println("16")
2025-01-12 13:35:02 +01:00
return nil
}
2025-01-12 14:29:47 +01:00
log.Println("16.5")
2025-01-12 13:35:02 +01:00
if !IsNumber(mainDomainPart) {
// TODO return cname to isnot.prime.cringe-studios.com
2025-01-12 14:29:47 +01:00
log.Println("17")
2025-01-12 13:35:02 +01:00
return nil
}
2025-01-12 14:29:47 +01:00
log.Println("18")
2025-01-12 13:35:02 +01:00
bigNumber := new(big.Int)
fmt.Sscan(mainDomainPart, bigNumber)
2025-01-12 14:29:47 +01:00
log.Println("19")
2025-01-12 13:35:02 +01:00
if bigNumber.Cmp(maximumPrimeNumber) == 1 {
// number is bigger than the maximum limit for the propablyprime function
// TODO return CNAME for maybe.prime.cringe-studios.com
2025-01-12 14:29:47 +01:00
log.Println("20")
2025-01-12 13:35:02 +01:00
return nil
}
if bigNumber.ProbablyPrime(20) {
2025-01-12 14:29:47 +01:00
log.Println("21")
2025-01-12 13:35:02 +01:00
// is prime, because the cases bigger than 2^64-1 have been catched above
return &dns.CNAME{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeCNAME,
Class: dns.ClassINET,
Ttl: 30,
},
Target: "is.prime.cringe-studios.com.",
}
} else {
2025-01-12 14:29:47 +01:00
log.Println("22")
2025-01-12 13:35:02 +01:00
// is not prime
return &dns.CNAME{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeCNAME,
Class: dns.ClassINET,
Ttl: 30,
},
Target: "isnot.prime.cringe-studios.com.",
}
}
}