refactor - pasdns can now look sane

This commit is contained in:
amy 2024-07-15 21:54:27 +03:30
parent cc8322f221
commit 0be492aa87
7 changed files with 97 additions and 31 deletions

View file

@ -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",

View file

@ -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

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
return err
}
message, err := helper.ReadUntilNullString(bufio.NewReader(conn))
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.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"))
}
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)))
})
}

View file

@ -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{

17
src/test/test.go Normal file
View file

@ -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)
}
}