From da5bd54b3e66d4fff8c60416730888ed50764276 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Fri, 6 Jan 2023 23:26:28 -0500 Subject: [PATCH] amended the Zone schema, fixed file upload size too small issue, refactoring, and zone file parsing works now --- .gitignore | 1 + internal/api/api.go | 1 + internal/api/zone.go | 74 ++++++++++++++++++++++++++++++++------- internal/configuration.go | 5 +-- internal/db/db.go | 2 +- 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 716776e..eda73ad 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,5 @@ go.work pomme test.db +test.sqlite .dccache \ No newline at end of file diff --git a/internal/api/api.go b/internal/api/api.go index 8ecb637..261268e 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -43,6 +43,7 @@ func API() http.Handler { api.Use(jwtauth.Authenticator) api.With(SetDBMiddleware).Post("/upload", RecieveFile) + api.With(SetDBMiddleware).Post("/parse", ZoneFiles) }) // Open routes diff --git a/internal/api/zone.go b/internal/api/zone.go index 0b932cf..7e8f289 100644 --- a/internal/api/zone.go +++ b/internal/api/zone.go @@ -6,8 +6,10 @@ import ( "io" "log" "net/http" + "os" "strings" + "git.freecumextremist.com/grumbulon/pomme/internal" "git.freecumextremist.com/grumbulon/pomme/internal/util" "github.com/go-chi/jwtauth/v5" "github.com/miekg/dns" @@ -24,8 +26,9 @@ type ZoneRequest struct { // Zone struct represents a zonefile. type Zone struct { gorm.Model - FileName string `json:"name"` - Body string `json:"body"` + FileName string `json:"name"` + RawFileName string `json:"rawname"` + Body string `json:"body,omitempty"` } func RecieveFile(w http.ResponseWriter, r *http.Request) { @@ -33,7 +36,7 @@ func RecieveFile(w http.ResponseWriter, r *http.Request) { var buf bytes.Buffer - r.Body = http.MaxBytesReader(w, r.Body, .5*1024*1024) // approx 500 kb max upload + r.Body = http.MaxBytesReader(w, r.Body, 1*1024*1024) // approx 500 kb max upload file, header, err := r.FormFile("file") if err != nil { @@ -58,12 +61,41 @@ func RecieveFile(w http.ResponseWriter, r *http.Request) { return } - // ugly - zoneReq := newZoneRequest(fmt.Sprintf("tmpfile-%s-%s", name[0], claims["username"].(string)), - claims["username"].(string), - ) + db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB) + if !ok { + http.Error(w, "internal server error", http.StatusInternalServerError) + + return + } + + db.Create( + &ZoneRequest{User: claims["username"].(string), + Zone: &Zone{ + FileName: fmt.Sprintf("tmpfile-%s-%s", name[0], claims["username"].(string)), + RawFileName: name[0], + }}) buf.Reset() +} + +func ZoneFiles(w http.ResponseWriter, r *http.Request) { + var result internal.ZoneRequest + _, claims, _ := jwtauth.FromContext(r.Context()) + + err := r.ParseForm() + if err != nil { + http.Error(w, "Unable to parse request", http.StatusInternalServerError) + + return + } + + filename := r.Form.Get("filename") + + if filename == "" { + http.Error(w, "No filename parsed", http.StatusInternalServerError) + + return + } db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB) if !ok { @@ -72,20 +104,36 @@ func RecieveFile(w http.ResponseWriter, r *http.Request) { return } - db.Create(zoneReq) - - if err = zoneReq.Parse(); err != nil { - http.Error(w, fmt.Sprintf("unable to parse zonefile: %v", err), http.StatusInternalServerError) + db.Where("raw_file_name = ?", filename).First(&result) + log.Println(result.RawFileName) + if result.RawFileName == "" { + http.Error(w, "Internal server error", http.StatusInternalServerError) return } + + zoneFile := newZoneRequest(result.RawFileName, claims["username"].(string)) + log.Println(zoneFile.FileName) + if err := zoneFile.Parse(); err != nil { + http.Error(w, "Unable to parse zonefile", http.StatusInternalServerError) + + return + } + } func newZoneRequest(filename string, user string) *ZoneRequest { + dat, err := os.ReadFile(fmt.Sprintf("/tmp/tmpfile-%s-%s", filename, user)) + if err != nil { + return &ZoneRequest{} + } + return &ZoneRequest{ User: user, Zone: &Zone{ - FileName: filename, + FileName: fmt.Sprintf("tmpfile-%s-%s", filename, user), + RawFileName: filename, + Body: string(dat), }, } } @@ -93,7 +141,7 @@ func newZoneRequest(filename string, user string) *ZoneRequest { // Parse will be used to parse zonefiles. func (zone *ZoneRequest) Parse() error { zp := dns.NewZoneParser(strings.NewReader(zone.Body), "", "") - + log.Println(zone.Body) for rr, ok := zp.Next(); ok; rr, ok = zp.Next() { log.Println(rr) } diff --git a/internal/configuration.go b/internal/configuration.go index 55f1718..15a44df 100644 --- a/internal/configuration.go +++ b/internal/configuration.go @@ -35,8 +35,9 @@ type ZoneRequest struct { // Zone struct represents a zonefile in the database. type Zone struct { gorm.Model - FileName string `json:"name"` - Body string `json:"body"` + FileName string `json:"name"` + RawFileName string `json:"rawfilename"` + Body string `json:"body"` } type Config struct { diff --git a/internal/db/db.go b/internal/db/db.go index 018cf24..7a6aa5d 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -8,7 +8,7 @@ import ( // InitDb is the init function for the database. func InitDb() *gorm.DB { - db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) + db, err := gorm.Open(sqlite.Open("test.sqlite"), &gorm.Config{}) if err != nil { panic("failed to connect database") }