From 0be492aa87430b85beb7c1f269b13eb3f7d57957 Mon Sep 17 00:00:00 2001 From: echo Date: Mon, 15 Jul 2024 21:54:27 +0330 Subject: [PATCH] refactor - pasdns can now look sane --- README.md | 2 +- src/Constants/constants.go | 19 ++++++++++++--- src/helper/response.go | 6 ++--- src/helper/stringfuckery.go | 18 +++++++++++++++ src/main.go | 46 ++++++++++++++++++------------------- src/parser/parser.go | 20 ++++++++++++++++ src/test/test.go | 17 ++++++++++++++ 7 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 src/test/test.go diff --git a/README.md b/README.md index 29c26b3..3f8648e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ piss and shit protocol - the new internet -a month ago, a youtuber called facedev: [made a video](https://www.youtube.com/watch?v=qiOtinFFfk8) +a month ago, a youtuber called facedev [made a video](https://www.youtube.com/watch?v=qiOtinFFfk8) this video, is PISSING ME OFF, HE DID NOT MAKE ANYTHING HE JUST USED EXISTING SOFTWARE IN A HORRIBLE INSECURE FUCKING WAY THERE'S NOTHING BEING "MADE", diff --git a/src/Constants/constants.go b/src/Constants/constants.go index 2014d21..921cf46 100644 --- a/src/Constants/constants.go +++ b/src/Constants/constants.go @@ -1,7 +1,7 @@ package Constants type Method string -type Response string +type OperationSuccessClassification string const ( UNKNOWN Method = "wtf bro" @@ -10,10 +10,23 @@ const ( ) const ( - OK Response = "OK" - NOTFOUND Response = "NOTFOUND" + OK OperationSuccessClassification = "OK" + INTERNALSERVERERROR OperationSuccessClassification = "SERVERSHATITSELF" + BADREQUEST OperationSuccessClassification = "BRUH" + NOTFOUND OperationSuccessClassification = "NOTFOUND" ) +type Request struct { + Intent Intent + Body string +} + +type Response struct { + OperationSuccessClassification OperationSuccessClassification + ResponseHeader map[string]string + Body string +} + type Intent struct { Method Method Path string diff --git a/src/helper/response.go b/src/helper/response.go index 5bd67c2..cc35d78 100644 --- a/src/helper/response.go +++ b/src/helper/response.go @@ -7,11 +7,11 @@ import ( "exhq.dev/pas/src/Constants" ) -func IntentGenerator(status Constants.Response, headers map[string]string) string { +func IntentGenerator(res Constants.Response) string { headersString := "" - for key, value := range headers { + for key, value := range res.ResponseHeader { headersString += fmt.Sprintf("%s:%s\n", key, value) } headersString = strings.TrimSuffix(headersString, "\n") - return fmt.Sprintf("PAS %s\n%s\n\n", string(status), headersString) + return fmt.Sprintf("PAS %s\n%s\n\n%s", string(res.OperationSuccessClassification), headersString, res.Body) } diff --git a/src/helper/stringfuckery.go b/src/helper/stringfuckery.go index 75fe354..8641229 100644 --- a/src/helper/stringfuckery.go +++ b/src/helper/stringfuckery.go @@ -1,5 +1,10 @@ package helper +import ( + "crypto/rand" + "math/big" +) + func Ender() string { return string(rune(0)) } @@ -7,3 +12,16 @@ func Ender() string { func StringToByteWithEnd(str string) []byte { return append([]byte(str), byte(0)) } + +func GetRandString(length int) string { + const charset = "qwertyuiopasdfghjklzxcvbnm" + result := make([]byte, length) + for i := range result { + num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset)))) + if err != nil { + panic(err) + } + result[i] = charset[num.Int64()] + } + return string(result) +} diff --git a/src/main.go b/src/main.go index 920ba5a..a65bbf9 100644 --- a/src/main.go +++ b/src/main.go @@ -1,43 +1,41 @@ -package main +package pas import ( "bufio" - "fmt" "log" "net" + "strconv" + "strings" "exhq.dev/pas/src/Constants" "exhq.dev/pas/src/helper" "exhq.dev/pas/src/parser" ) -func main() { - ln, err := net.Listen("tcp", ":42069") +func StartServer(port int, handler func(request Constants.Request, response func(Constants.Response)), errr error) error { + ln, err := net.Listen("tcp", ":"+strconv.Itoa(port)) if err != nil { - log.Fatal(err) + return err } - fmt.Println("Listening on port 42069") for { conn, err := ln.Accept() if err != nil { - log.Fatal(err) - } - message, err := helper.ReadUntilNullString(bufio.NewReader(conn)) - if err != nil { - log.Fatal(err) - } - parsed := parser.ParseIntent(message) - - if parsed.Path == "/" { - conn.Write(helper.StringToByteWithEnd(helper.IntentGenerator(Constants.OK, map[string]string{ - "version": "0.69", - }) + "welcome to the first ever shitternet page")) - } - - if parsed.Path == "/meow" { - conn.Write(helper.StringToByteWithEnd(helper.IntentGenerator(Constants.OK, map[string]string{ - "version": "0.69", - }) + "meowmeow")) + return err } + go handleConnection(conn, handler) } } + +func handleConnection(conn net.Conn, handler func(request Constants.Request, response func(Constants.Response))) { + defer conn.Close() + raw, err := helper.ReadUntilNullString(bufio.NewReader(conn)) + if err != nil { + log.Println(err) + return + } + parsed := parser.ParseIntent(raw) + request := Constants.Request{Intent: parsed, Body: strings.SplitN(raw, "\n\n", 2)[1]} + handler(request, func(c Constants.Response) { + conn.Write(helper.StringToByteWithEnd(helper.IntentGenerator(c))) + }) +} diff --git a/src/parser/parser.go b/src/parser/parser.go index c5f3121..6b9e55b 100644 --- a/src/parser/parser.go +++ b/src/parser/parser.go @@ -1,11 +1,31 @@ package parser import ( + "regexp" "strings" "exhq.dev/pas/src/Constants" ) +func CheckIpSyntax(ip string) bool { + res, _ := regexp.MatchString(`^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$`, ip) + return res +} + +func CheckDomainSyntax(domain string) bool { + res, _ := regexp.MatchString(`[a-z0-9]{2,}\.(gaming|sex2|dev|meow|horse)`, domain) + return res +} + +func CheckKeysExistDomain(m map[string]string) bool { + for _, key := range []string{"domain", "ip"} { + if _, ok := m[key]; !ok { + return false + } + } + return true +} + func ParseIntent(intent string) Constants.Intent { if intent == "" { return Constants.Intent{ diff --git a/src/test/test.go b/src/test/test.go new file mode 100644 index 0000000..b56912c --- /dev/null +++ b/src/test/test.go @@ -0,0 +1,17 @@ +package main + +import ( + "log" + + pas "exhq.dev/pas/src" + "exhq.dev/pas/src/Constants" +) + +func main() { + err := pas.StartServer(42069, func(request Constants.Request, respond func(Constants.Response)) { + respond(Constants.Response{OperationSuccessClassification: Constants.OK, ResponseHeader: map[string]string{}, Body: "say gex"}) + }, nil) + if err != nil { + log.Fatal(err) + } +}