adjustments: logging

This commit is contained in:
kayos 2021-07-29 12:40:53 -07:00
parent b78ddd28f0
commit 0dbbf728e7
3 changed files with 21 additions and 26 deletions

4
db.go

@ -27,6 +27,10 @@ func dbInit() {
} }
func dbSync() { func dbSync() {
hashDB.Merge()
keyDB.Merge()
imgDB.Merge()
urlDB.Merge()
hashDB.Sync() hashDB.Sync()
keyDB.Sync() keyDB.Sync()
imgDB.Sync() imgDB.Sync()

31
img.go

@ -21,19 +21,18 @@ var fExt string
func imgDel(c *gin.Context) { func imgDel(c *gin.Context) {
slog := log.With().Str("caller", "imgView").Logger() slog := log.With().Str("caller", "imgView").Logger()
slog.Debug().Msg("Request received!") // received request
rKey := c.Param("key") rKey := c.Param("key")
if !validateKey(rKey) { if !validateKey(rKey) {
slog.Error().Msg("delete request failed sanity check!") slog.Error().Msg("delete request failed sanity check!")
errThrow(c, 400, "400", "400") errThrow(c, 400, "invalid request", "invalid request")
return return
} }
targetImg, _ := keyDB.Get([]byte(rKey)) targetImg, _ := keyDB.Get([]byte(rKey))
if targetImg == nil || !strings.Contains(string(targetImg), "i.") { if targetImg == nil || !strings.Contains(string(targetImg), "i.") {
slog.Error().Str("rkey", rKey).Msg("no img delete entry found with provided key") slog.Error().Str("rkey", rKey).Msg("no img delete entry found with provided key")
errThrow(c, 400, "400", "400") errThrow(c, 400, "invalid request", "invalid request")
return return
} }
@ -81,7 +80,7 @@ func imgView(c *gin.Context) {
slog.Debug().Str("ext", fExt).Msg("detected file extension") slog.Debug().Str("ext", fExt).Msg("detected file extension")
if fExt != "png" && fExt != "jpg" && fExt != "jpeg" && fExt != "gif" { if fExt != "png" && fExt != "jpg" && fExt != "jpeg" && fExt != "gif" {
slog.Error().Msg("Bad file extension!") slog.Error().Msg("Bad file extension!")
errThrow(c, 400, "400", "400") errThrow(c, 400, "invalid request", "invalid request")
return return
} }
} else { } else {
@ -90,8 +89,11 @@ func imgView(c *gin.Context) {
// if it doesn't match the key size or it isn't alphanumeric - throw it out // if it doesn't match the key size or it isn't alphanumeric - throw it out
if !valid.IsAlphanumeric(rUid) || len(rUid) != uidSize { if !valid.IsAlphanumeric(rUid) || len(rUid) != uidSize {
slog.Error().Msg("request discarded as invalid") // these limits should be variables eventually slog.Warn().
errThrow(c, 400, "400", "400") Str("remoteaddr", c.ClientIP()).
Msg("request discarded as invalid")
errThrow(c, 400, "invalid request", "invalid request")
return return
} }
@ -111,7 +113,7 @@ func imgView(c *gin.Context) {
imageFormat, ok := checkImage(file) imageFormat, ok := checkImage(file)
if !ok { if !ok {
// extra sanity check to make sure we don't serve non-image data that somehow got into the database // extra sanity check to make sure we don't serve non-image data that somehow got into the database
errThrow(c, http.StatusBadRequest, "400", "400") errThrow(c, http.StatusBadRequest, "invalid request", "invalid request")
slog.Error().Str("rUid", rUid).Msg("the file we grabbed is not an image!?") slog.Error().Str("rUid", rUid).Msg("the file we grabbed is not an image!?")
return return
} else { } else {
@ -122,7 +124,7 @@ func imgView(c *gin.Context) {
// additional extension sanity check - if they're gonna use an extension it needs to be the right one // additional extension sanity check - if they're gonna use an extension it needs to be the right one
if fExt != "nil" && fExt != imageFormat { if fExt != "nil" && fExt != imageFormat {
slog.Error().Str("rUid", rUid).Msg("requested file extension does not match filetype") slog.Error().Str("rUid", rUid).Msg("requested file extension does not match filetype")
errThrow(c, 400, "400", "400") errThrow(c, 400, "invalid request", "invalid request")
return return
} }
@ -145,7 +147,7 @@ func imgPost(c *gin.Context) {
// check if incoming POST data is invalid // check if incoming POST data is invalid
f, err := c.FormFile("upload") f, err := c.FormFile("upload")
if err != nil { if err != nil {
errThrow(c, http.StatusBadRequest, err.Error(), "400") errThrow(c, http.StatusBadRequest, err.Error(), "invalid request")
} }
slog.Debug().Str("filename", f.Filename).Msg("[+] New upload") slog.Debug().Str("filename", f.Filename).Msg("[+] New upload")
@ -156,11 +158,9 @@ func imgPost(c *gin.Context) {
errThrow(c, http.StatusInternalServerError, err.Error(), "error processing file\n") errThrow(c, http.StatusInternalServerError, err.Error(), "error processing file\n")
} }
// verify the incoming file is an image
slog.Debug().Msg("verifying file is an image")
imageFormat, ok := checkImage(file) imageFormat, ok := checkImage(file)
if !ok { if !ok {
errThrow(c, http.StatusBadRequest, "400", "input does not appear to be an image") errThrow(c, http.StatusBadRequest, "invalid request", "input does not appear to be an image")
return return
} else { } else {
slog.Debug().Msg("image file type detected") slog.Debug().Msg("image file type detected")
@ -168,12 +168,11 @@ func imgPost(c *gin.Context) {
// dump this into a byte object and scrub it // dump this into a byte object and scrub it
// TO-DO: Write our own function for scrubbing exif // TO-DO: Write our own function for scrubbing exif
slog.Debug().Msg("dumping byte form of file")
fbytes, err := ioutil.ReadAll(file) fbytes, err := ioutil.ReadAll(file)
if imageFormat != "gif" { if imageFormat != "gif" {
slog.Debug().Err(err).Msg("error scrubbing exif")
Scrubbed, err = exifremove.Remove(fbytes) Scrubbed, err = exifremove.Remove(fbytes)
if err != nil { if err != nil {
slog.Warn().Err(err).Msg("error scrubbing exif")
errThrow(c, http.StatusInternalServerError, err.Error(), "error scrubbing exif") errThrow(c, http.StatusInternalServerError, err.Error(), "error scrubbing exif")
return return
} }
@ -188,8 +187,6 @@ func imgPost(c *gin.Context) {
Hashr.Write(Scrubbed) Hashr.Write(Scrubbed)
hash := Hashr.Sum(nil) hash := Hashr.Sum(nil)
slog.Debug().Msg("checking for duplicate files")
// the keys (stored in memory) for hashDb are hashes // the keys (stored in memory) for hashDb are hashes
// making it quick to find duplicates. the value is the uid // making it quick to find duplicates. the value is the uid
imgRef, _ := hashDB.Get(hash) imgRef, _ := hashDB.Get(hash)
@ -217,8 +214,6 @@ func imgPost(c *gin.Context) {
} }
} }
slog.Info().Msg("no duplicate images found, generating uid and delete key")
// generate new uid and delete key // generate new uid and delete key
uid := gouid.String(uidSize, gouid.MixedCaseAlphaNum) uid := gouid.String(uidSize, gouid.MixedCaseAlphaNum)
key = gouid.String(keySize, gouid.MixedCaseAlphaNum) key = gouid.String(keySize, gouid.MixedCaseAlphaNum)

12
txt.go

@ -192,9 +192,7 @@ func txtView(c *gin.Context) {
func txtDel(c *gin.Context) { func txtDel(c *gin.Context) {
slog := log.With(). slog := log.With().
Str("caller", "txtDel").Logger() Str("caller", "txtDel").Logger()
slog.Debug().Msg("new_request")
slog.Debug().Msg("new_request") // received request
if !validateKey(c.Param("key")) { if !validateKey(c.Param("key")) {
errThrow(c, 400, "400", "400") errThrow(c, 400, "400", "400")
return return
@ -228,15 +226,13 @@ func txtDel(c *gin.Context) {
return return
} }
slog.Info().Str("rkey", t).Msg("Image file deleted successfully") slog.Info().Str("rkey", t).Msg("Text file deleted successfully")
slog.Debug().Str("rkey", t).Msg("Removing delete key entry") slog.Debug().Str("rkey", t).Msg("Removing delete key entry")
err = keyDB.Delete([]byte(rKey)) err = keyDB.Delete([]byte(rKey))
if err != nil { if err != nil {
slog.Error().Str("rkey", t).Msg("Couldn't delete key") slog.Error().Str("rkey", t).Msg("Couldn't delete key")
// it would be insane to try and delete the hash here }
} // if someone is uploading this image again after del c.JSON(200, "DELETE_SUCCESS")
c.JSON(200, "DELETE_SUCCESS") // and the file corresponding to the hash no longer exists
// we will delete the hash entry then and re-add then
} }
//func serveTermbin() { //func serveTermbin() {