handle incoming requests of zonefiles somewhat

This commit is contained in:
grumbulon 2022-12-11 22:15:35 -05:00
parent 2965700048
commit 93b9f23a25
6 changed files with 111 additions and 35 deletions

View file

@ -8,8 +8,8 @@ import (
"time"
"git.freecumextremist.com/grumbulon/pomme/internal/api"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-pkgz/auth"
"github.com/go-pkgz/auth/avatar"
"github.com/go-pkgz/auth/provider"
@ -45,9 +45,8 @@ func main() {
w.Write([]byte("welcome"))
})
pomme.Get("/create", api.NewUser)
pomme.With(m.Auth).Get("/private", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("secret"))
})
pomme.Post("/check", api.Ingest)
pomme.With(m.Auth).Get("/private", api.AuthTest)
authRoutes, avaRoutes := service.Handlers()
pomme.Mount("/auth", authRoutes) // add auth handlers
pomme.Mount("/avatar", avaRoutes) // add avatar handler

4
go.mod
View file

@ -3,13 +3,14 @@ module git.freecumextremist.com/grumbulon/pomme
go 1.19
require (
github.com/go-chi/chi v1.5.4
github.com/go-chi/render v1.0.2
github.com/go-pkgz/auth v1.20.0
github.com/miekg/dns v1.1.50
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
)
require (
github.com/ajg/form v1.5.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
@ -19,6 +20,7 @@ require (
cloud.google.com/go/compute v1.6.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/dghubble/oauth1 v0.7.1 // indirect
github.com/go-chi/chi/v5 v5.0.8
github.com/go-oauth2/oauth2/v4 v4.5.1 // indirect
github.com/go-pkgz/repeater v1.1.3 // indirect
github.com/go-pkgz/rest v1.16.0 // indirect

6
go.sum
View file

@ -98,8 +98,10 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/gavv/httpexpect v2.0.0+incompatible h1:1X9kcRshkSKEjNJJxX9Y9mQ5BRfbxU5kORdjhlA1yX8=
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/render v1.0.2 h1:4ER/udB0+fMWB2Jlf15RV3F4A2FDuYi/9f+lFttR/Lg=
github.com/go-chi/render v1.0.2/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=

View file

@ -5,31 +5,28 @@ import (
"log"
"math/rand"
"net/http"
"strings"
"time"
"git.freecumextremist.com/grumbulon/pomme/internal"
"git.freecumextremist.com/grumbulon/pomme/internal/db"
"github.com/miekg/dns"
"github.com/go-chi/render"
"golang.org/x/crypto/bcrypt"
)
func Ingest(w http.ResponseWriter, r *http.Request) {
request := r.URL.Query().Get("req")
zp := dns.NewZoneParser(strings.NewReader(request), "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
log.Println(rr)
data := &internal.ZoneRequest{}
log.Println(data)
if err := render.Bind(r, data); err != nil {
http.Error(w, "Unable to parse Zonefile", http.StatusBadRequest)
return
}
zonefile := data.Zone
render.Status(r, http.StatusAccepted)
render.Render(w, r, internal.NewZoneResponse(zonefile))
// todo write to database, maybe?
// todo -- add functions to apply to master zonefile if above check is OK
if err := zp.Err(); err != nil {
log.Println(err)
http.Error(w, "Unable to parse Zonefile", http.StatusBadRequest)
}
}
func NewUser(w http.ResponseWriter, r *http.Request) {
@ -71,3 +68,7 @@ func autoUname() string {
rand.Seed(time.Now().UnixNano())
return fmt.Sprintf("user%d", rand.Intn(99999-00000))
}
func AuthTest(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("██████████"))
}

View file

@ -1,13 +0,0 @@
package internal
import "gorm.io/gorm"
type IncomingRequest struct {
}
type User struct {
gorm.Model
Username string
Password string
HashedPassword string
}

85
internal/zone.go Normal file
View file

@ -0,0 +1,85 @@
package internal
import (
"errors"
"log"
"net/http"
"strings"
"github.com/miekg/dns"
"gorm.io/gorm"
)
type ZoneRequest struct {
*Zone
User *User `json:"user,omitempty"`
RequestID string `json:"id"`
}
type ZoneResponse struct {
*Zone
User *User `json:"user,omitempty"`
Elapsed int64 `json:"elapsed"`
}
type Zone struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Body string `json:"body"`
}
type User struct {
gorm.Model
Username string `json:"name"`
Password string
HashedPassword string
}
func (zone *ZoneRequest) Parse() error {
zp := dns.NewZoneParser(strings.NewReader(zone.Body), "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
log.Println(rr)
}
if err := zp.Err(); err != nil {
log.Println(err)
return errors.New("unable to parse Zonefile")
}
return nil
}
func (zone *ZoneRequest) Bind(r *http.Request) error {
if zone.Zone == nil {
return errors.New("missing required zone file fields")
}
zone.Zone.Body = strings.ToLower(zone.Zone.Body)
return nil
}
func NewZoneResponse(zone *Zone) *ZoneResponse {
resp := &ZoneResponse{Zone: zone}
if resp.User == nil {
if user, _ := dbGetUser(resp.UserID); user != nil {
resp.User = NewUserPayloadResponse(user)
}
}
return resp
}
func NewUserPayloadResponse(user *User) *User {
return &User{Username: user.Username}
}
func dbGetUser(s string) (*User, error) {
return &User{Username: "user14651"}, nil
}
func (rd *ZoneResponse) Render(w http.ResponseWriter, r *http.Request) error {
// Pre-processing before a response is marshalled and sent across the wire
rd.Elapsed = 10
return nil
}