From d21f6112891c3b29fd5c5d5cb95690212695eed2 Mon Sep 17 00:00:00 2001 From: TheArrayser Date: Sun, 12 Jan 2025 13:35:02 +0100 Subject: [PATCH] added basic prime number checker --- main.go | 12 +++++- prime.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 prime.go diff --git a/main.go b/main.go index 358ef46..a01b705 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,16 @@ func (h *dnsHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { fmt.Printf("Received query: %s (%d)\n", question.Name, question.Qtype) if question.Qtype == dns.TypeCNAME || question.Qtype == dns.TypeA || question.Qtype == dns.TypeAAAA { + if strings.HasSuffix(question.Name, "prime.cringe-studios.com") || strings.HasSuffix(question.Name, "prime.cringe-studios.com.") { + primeAnswer := HandlePrimeNumberChecker(question) + + if primeAnswer != nil { + msg.Answer = append(msg.Answer, primeAnswer) + } + + continue + } + if question.Name == "google.com." { msg.Answer = append(msg.Answer, &dns.CNAME{ Hdr: dns.RR_Header{ @@ -109,7 +119,7 @@ func getPage(w http.ResponseWriter, r *http.Request) { } func main() { - log.Println("Starting Server!"); + log.Println("Starting Server!") handler := new(dnsHandler) diff --git a/prime.go b/prime.go new file mode 100644 index 0000000..0cab9f6 --- /dev/null +++ b/prime.go @@ -0,0 +1,111 @@ +package main + +import ( + "fmt" + "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 { + mainDomainPartindex := strings.LastIndex(question.Name, "prime.cringe-studios.com") + if mainDomainPartindex == 0 || mainDomainPartindex == 1 { + // TODO handle ns page part.. maybe send back a CNAME for a page to enter the number + + return nil + } + + isQuestionPartIndex := strings.LastIndex(question.Name, "is.prime.cringe-studios.com") + if isQuestionPartIndex == 0 || isQuestionPartIndex == 1 { + // TODO handle resource found + return nil + } + + isnotQuestionPartIndex := strings.LastIndex(question.Name, "isnot.prime.cringe-studios.com") + if isnotQuestionPartIndex == 0 || isnotQuestionPartIndex == 1 { + // TODO handle resource found + return nil + } + + maybeQuestionPartIndex := strings.LastIndex(question.Name, "maybe.prime.cringe-studios.com") + if maybeQuestionPartIndex == 0 || maybeQuestionPartIndex == 1 { + // TODO handle resource found + return nil + } + + mainDomainPart := question.Name[0 : mainDomainPartindex-1] // subtract 1 for the p in prime + + if !strings.HasSuffix(mainDomainPart, ".") { + // unknown subdomain like 123prime.cringe-studios.com + return nil + } + + mainDomainPart = mainDomainPart[0 : len(mainDomainPart)-2] // subtract 1 to accomodate for the length being one bigger then the last index, and subtract another one for the dot at the end + + if len(mainDomainPart) == 0 { + // .prime.cringe-studios.com + return nil + } + + firstIsIndex := strings.Index(mainDomainPart, "is.") + + if firstIsIndex == 0 || firstIsIndex == 1 { + // word "is." found at the beginning of the question.. trim + mainDomainPart = mainDomainPart[len("is.") : len(mainDomainPart)-1] + } + + if len(mainDomainPart) == 0 { + // requested domain was is.prime.cringe-studios.com or something like that... + // TODO + return nil + } + + if !IsNumber(mainDomainPart) { + // TODO return cname to isnot.prime.cringe-studios.com + return nil + } + + bigNumber := new(big.Int) + fmt.Sscan(mainDomainPart, bigNumber) + + 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 + + return nil + } + + if bigNumber.ProbablyPrime(20) { + // 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 { + // 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.", + } + } +}