Make linter happy and update dependencies.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-06-26 22:26:39 +05:00
parent 7b6a425908
commit f8f0302564
30 changed files with 325 additions and 363 deletions

View File

@@ -32,30 +32,30 @@ import (
var regexInts = regexp.MustCompile("[0-9]+")
var c *context.Context
var ctx *context.Context
// New initializes pastes package and adds necessary HTTP and API
// endpoints.
func New(cc *context.Context) {
c = cc
ctx = cc
////////////////////////////////////////////////////////////
// HTTP endpoints.
////////////////////////////////////////////////////////////
// New paste.
c.Echo.POST("/paste/", pastePOSTWebInterface)
ctx.Echo.POST("/paste/", pastePOSTWebInterface)
// Show public paste.
c.Echo.GET("/paste/:id", pasteGETWebInterface)
ctx.Echo.GET("/paste/:id", pasteGETWebInterface)
// Show RAW representation of public paste.
c.Echo.GET("/paste/:id/raw", pasteRawGETWebInterface)
ctx.Echo.GET("/paste/:id/raw", pasteRawGETWebInterface)
// Show private paste.
c.Echo.GET("/paste/:id/:timestamp", pasteGETWebInterface)
ctx.Echo.GET("/paste/:id/:timestamp", pasteGETWebInterface)
// Show RAW representation of private paste.
c.Echo.GET("/paste/:id/:timestamp/raw", pasteRawGETWebInterface)
ctx.Echo.GET("/paste/:id/:timestamp/raw", pasteRawGETWebInterface)
// Verify access to passworded paste.
c.Echo.GET("/paste/:id/:timestamp/verify", pastePasswordedVerifyGet)
c.Echo.POST("/paste/:id/:timestamp/verify", pastePasswordedVerifyPost)
ctx.Echo.GET("/paste/:id/:timestamp/verify", pastePasswordedVerifyGet)
ctx.Echo.POST("/paste/:id/:timestamp/verify", pastePasswordedVerifyPost)
// Pastes list.
c.Echo.GET("/pastes/", pastesGET)
c.Echo.GET("/pastes/:page", pastesGET)
ctx.Echo.GET("/pastes/", pastesGET)
ctx.Echo.GET("/pastes/:page", pastesGET)
}

View File

@@ -31,16 +31,16 @@ const (
// value (they both will be ignored), but private will.
func pasteGetData(pasteID int, timestamp int64, cookieValue string) (*structs.Paste, string) {
// Get paste.
paste, err1 := c.Database.GetPaste(pasteID)
paste, err1 := ctx.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
ctx.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
return nil, pasteNotFound
}
// Check if paste is expired.
if paste.IsExpired() {
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
ctx.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
return nil, pasteExpired
}
@@ -49,7 +49,7 @@ func pasteGetData(pasteID int, timestamp int64, cookieValue string) (*structs.Pa
if paste.Private {
pasteTS := paste.CreatedAt.Unix()
if timestamp != pasteTS {
c.Logger.Error().Int("paste ID", pasteID).Int64("paste timestamp", pasteTS).Int64("provided timestamp", timestamp).Msg("Incorrect timestamp provided for private paste")
ctx.Logger.Error().Int("paste ID", pasteID).Int64("paste timestamp", pasteTS).Int64("provided timestamp", timestamp).Msg("Incorrect timestamp provided for private paste")
return nil, pasteTimestampInvalid
}
@@ -78,7 +78,7 @@ func pasteGETWebInterface(ectx echo.Context) error {
// error.
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
pasteIDStr := strconv.Itoa(pasteID)
c.Logger.Debug().Int("paste ID", pasteID).Msg("Trying to get paste data")
ctx.Logger.Debug().Int("paste ID", pasteID).Msg("Trying to get paste data")
// Check if we have timestamp passed.
// If passed timestamp is invalid (isn't a real UNIX timestamp) we
@@ -89,7 +89,7 @@ func pasteGETWebInterface(ectx echo.Context) error {
if tsProvidedStr != "" {
tsProvided, err := strconv.ParseInt(tsProvidedStr, 10, 64)
if err != nil {
c.Logger.Error().Err(err).Int("paste ID", pasteID).Int64("provided timestamp", tsProvided).Msg("Invalid timestamp provided for getting private paste")
ctx.Logger.Error().Err(err).Int("paste ID", pasteID).Int64("provided timestamp", tsProvided).Msg("Invalid timestamp provided for getting private paste")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+pasteIDStr+" not found")
@@ -122,7 +122,7 @@ func pasteGETWebInterface(ectx echo.Context) error {
// If passed cookie value was invalid - go to paste authorization
// page.
if err == pasteCookieInvalid {
c.Logger.Info().Int("paste ID", pasteID).Msg("Invalid cookie, redirecting to auth page")
ctx.Logger.Info().Int("paste ID", pasteID).Msg("Invalid cookie, redirecting to auth page")
// nolint:wrapcheck
return ectx.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDStr+"/"+ectx.Param("timestamp")+"/verify")
@@ -159,7 +159,7 @@ func pasteGETWebInterface(ectx echo.Context) error {
// Tokenize paste data.
lexered, err3 := lexer.Tokenise(nil, paste.Data)
if err3 != nil {
c.Logger.Error().Err(err3).Msg("Failed to tokenize paste data")
ctx.Logger.Error().Err(err3).Msg("Failed to tokenize paste data")
}
// Get style for HTML output.
style := styles.Get("monokai")
@@ -174,7 +174,7 @@ func pasteGETWebInterface(ectx echo.Context) error {
err4 := formatter.Format(buf, style, lexered)
if err4 != nil {
c.Logger.Error().Err(err4).Msg("Failed to format paste data")
ctx.Logger.Error().Err(err4).Msg("Failed to format paste data")
}
pasteData["pastedata"] = buf.String()
@@ -195,9 +195,9 @@ func pastePasswordedVerifyGet(ectx echo.Context) error {
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
// Get paste.
paste, err1 := c.Database.GetPaste(pasteID)
paste, err1 := ctx.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste data")
ctx.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste data")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+pasteIDRaw+" not found")
@@ -209,19 +209,19 @@ func pastePasswordedVerifyGet(ectx echo.Context) error {
cookie, err := ectx.Cookie("PASTE-" + strconv.Itoa(pasteID))
if err == nil {
// No cookie, redirect to auth page.
c.Logger.Debug().Msg("Paste cookie found, checking it...")
ctx.Logger.Debug().Msg("Paste cookie found, checking it...")
// Generate cookie value to check.
cookieValue := paste.GenerateCryptedCookieValue()
if cookieValue == cookie.Value {
c.Logger.Info().Msg("Valid cookie, redirecting to paste page...")
ctx.Logger.Info().Msg("Valid cookie, redirecting to paste page...")
// nolint:wrapcheck
return ectx.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDRaw+"/"+ectx.Param("timestamp"))
}
c.Logger.Debug().Msg("Invalid cookie, showing auth page")
ctx.Logger.Debug().Msg("Invalid cookie, showing auth page")
}
// HTML data.
@@ -238,9 +238,9 @@ func pastePasswordedVerifyGet(ectx echo.Context) error {
// POST for "/paste/PASTE_ID/TIMESTAMP/verify" - a password verify page.
func pastePasswordedVerifyPost(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
dbConn := ctx.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
if ctx.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
@@ -250,12 +250,12 @@ func pastePasswordedVerifyPost(ectx echo.Context) error {
// We already get numbers from string, so we will not check strconv.Atoi()
// error.
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
c.Logger.Debug().Int("paste ID", pasteID).Msg("Requesting paste")
ctx.Logger.Debug().Int("paste ID", pasteID).Msg("Requesting paste")
// Get paste.
paste, err1 := c.Database.GetPaste(pasteID)
paste, err1 := ctx.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
ctx.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+strconv.Itoa(pasteID)+" not found")
// nolint:wrapcheck
@@ -264,7 +264,7 @@ func pastePasswordedVerifyPost(ectx echo.Context) error {
params, err2 := ectx.FormParams()
if err2 != nil {
c.Logger.Debug().Msg("No form parameters passed")
ctx.Logger.Debug().Msg("No form parameters passed")
errtpl := templater.GetErrorTemplate(ectx, "Paste #"+strconv.Itoa(pasteID)+" not found")
@@ -295,8 +295,8 @@ func pastePasswordedVerifyPost(ectx echo.Context) error {
// Web interface version.
func pasteRawGETWebInterface(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
dbConn := ctx.Database.GetDatabaseConnection()
if ctx.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ectx.Redirect(http.StatusFound, "/database_not_available/raw")
}
@@ -305,19 +305,19 @@ func pasteRawGETWebInterface(ectx echo.Context) error {
// We already get numbers from string, so we will not check strconv.Atoi()
// error.
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
c.Logger.Debug().Int("paste ID", pasteID).Msg("Requesting paste data")
ctx.Logger.Debug().Int("paste ID", pasteID).Msg("Requesting paste data")
// Get paste.
paste, err1 := c.Database.GetPaste(pasteID)
paste, err1 := ctx.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste from database")
ctx.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste from database")
// nolint:wrapcheck
return ectx.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
}
if paste.IsExpired() {
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
ctx.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
// nolint:wrapcheck
return ectx.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
@@ -329,7 +329,7 @@ func pasteRawGETWebInterface(ectx echo.Context) error {
tsProvided, err2 := strconv.ParseInt(tsProvidedStr, 10, 64)
if err2 != nil {
c.Logger.Error().Err(err2).Int("paste ID", pasteID).Str("provided timestamp", tsProvidedStr).Msg("Invalid timestamp provided for getting private paste")
ctx.Logger.Error().Err(err2).Int("paste ID", pasteID).Str("provided timestamp", tsProvidedStr).Msg("Invalid timestamp provided for getting private paste")
// nolint:wrapcheck
return ectx.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
@@ -337,7 +337,7 @@ func pasteRawGETWebInterface(ectx echo.Context) error {
pasteTS := paste.CreatedAt.Unix()
if tsProvided != pasteTS {
c.Logger.Error().Int("paste ID", pasteID).Int64("provided timestamp", tsProvided).Int64("paste timestamp", pasteTS).Msg("Incorrect timestamp provided for private paste")
ctx.Logger.Error().Int("paste ID", pasteID).Int64("provided timestamp", tsProvided).Int64("paste timestamp", pasteTS).Msg("Incorrect timestamp provided for private paste")
// nolint:wrapcheck
return ectx.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
@@ -348,7 +348,7 @@ func pasteRawGETWebInterface(ectx echo.Context) error {
// ToDo: figure out how to handle passworded pastes here.
// Return error for now.
if paste.Password != "" {
c.Logger.Error().Int("paste ID", pasteID).Msg("Cannot render paste as raw: passworded paste. Patches welcome!")
ctx.Logger.Error().Int("paste ID", pasteID).Msg("Cannot render paste as raw: passworded paste. Patches welcome!")
return ectx.String(http.StatusBadRequest, "Paste #"+pasteIDRaw+" not found")
}

View File

@@ -22,15 +22,15 @@ const KeepPastesForever = "forever"
// requests comes from browsers via web interface.
func pastePOSTWebInterface(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
dbConn := ctx.Database.GetDatabaseConnection()
if ctx.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
params, err := ectx.FormParams()
if err != nil {
c.Logger.Error().Msg("Passed paste form is empty")
ctx.Logger.Error().Msg("Passed paste form is empty")
errtpl := templater.GetErrorTemplate(ectx, "Cannot create empty paste")
@@ -38,11 +38,11 @@ func pastePOSTWebInterface(ectx echo.Context) error {
return ectx.HTML(http.StatusBadRequest, errtpl)
}
c.Logger.Debug().Msgf("Received parameters: %+v", params)
ctx.Logger.Debug().Msgf("Received parameters: %+v", params)
// Do nothing if paste contents is empty.
if len(params["paste-contents"][0]) == 0 {
c.Logger.Debug().Msg("Empty paste submitted, ignoring")
ctx.Logger.Debug().Msg("Empty paste submitted, ignoring")
errtpl := templater.GetErrorTemplate(ectx, "Empty pastes aren't allowed.")
@@ -51,7 +51,7 @@ func pastePOSTWebInterface(ectx echo.Context) error {
}
if !strings.ContainsAny(params["paste-keep-for"][0], "Mmhd") && params["paste-keep-for"][0] != KeepPastesForever {
c.Logger.Debug().Str("field value", params["paste-keep-for"][0]).Msg("'Keep paste for' field have invalid value")
ctx.Logger.Debug().Str("field value", params["paste-keep-for"][0]).Msg("'Keep paste for' field have invalid value")
errtpl := templater.GetErrorTemplate(ectx, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
@@ -61,7 +61,7 @@ func pastePOSTWebInterface(ectx echo.Context) error {
// Verify captcha.
if !captcha.Verify(params["paste-captcha-id"][0], params["paste-captcha-solution"][0]) {
c.Logger.Debug().Str("captcha ID", params["paste-captcha-id"][0]).Str("captcha solution", params["paste-captcha-solution"][0]).Msg("Invalid captcha solution")
ctx.Logger.Debug().Str("captcha ID", params["paste-captcha-id"][0]).Str("captcha solution", params["paste-captcha-solution"][0]).Msg("Invalid captcha solution")
errtpl := templater.GetErrorTemplate(ectx, "Invalid captcha solution.")
@@ -69,7 +69,7 @@ func pastePOSTWebInterface(ectx echo.Context) error {
return ectx.HTML(http.StatusBadRequest, errtpl)
}
// nolint:exhaustivestruct
// nolint:exhaustruct
paste := &structs.Paste{
Title: params["paste-title"][0],
Data: params["paste-contents"][0],
@@ -95,11 +95,11 @@ func pastePOSTWebInterface(ectx echo.Context) error {
keepFor, err = strconv.Atoi(keepForRaw)
if err != nil {
if params["paste-keep-for"][0] == KeepPastesForever {
c.Logger.Debug().Msg("Keeping paste forever!")
ctx.Logger.Debug().Msg("Keeping paste forever!")
keepFor = 0
} else {
c.Logger.Debug().Err(err).Msg("Failed to parse 'Keep for' integer")
ctx.Logger.Debug().Err(err).Msg("Failed to parse 'Keep for' integer")
errtpl := templater.GetErrorTemplate(ectx, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
@@ -138,9 +138,9 @@ func pastePOSTWebInterface(ectx echo.Context) error {
_ = paste.CreatePassword(pastePassword[0])
}
pasteID, err2 := c.Database.SavePaste(paste)
pasteID, err2 := ctx.Database.SavePaste(paste)
if err2 != nil {
c.Logger.Error().Err(err2).Msg("Failed to save paste")
ctx.Logger.Error().Err(err2).Msg("Failed to save paste")
errtpl := templater.GetErrorTemplate(ectx, "Failed to save paste. Please, try again later.")
@@ -149,7 +149,7 @@ func pastePOSTWebInterface(ectx echo.Context) error {
}
newPasteIDAsString := strconv.FormatInt(pasteID, 10)
c.Logger.Debug().Msg("Paste saved, URL: /paste/" + newPasteIDAsString)
ctx.Logger.Debug().Msg("Paste saved, URL: /paste/" + newPasteIDAsString)
// Private pastes have it's timestamp in URL.
if paste.Private {

View File

@@ -39,8 +39,8 @@ import (
// Web interface version.
func pastesGET(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
dbConn := ctx.Database.GetDatabaseConnection()
if ctx.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
// nolint:wrapcheck
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
@@ -54,17 +54,17 @@ func pastesGET(ectx echo.Context) error {
page, _ = strconv.Atoi(pageRaw)
}
c.Logger.Debug().Int("page", page).Msg("Requested page")
ctx.Logger.Debug().Int("page", page).Msg("Requested page")
// Get pastes IDs.
pastes, err3 := c.Database.GetPagedPastes(page)
c.Logger.Debug().Int("count", len(pastes)).Msg("Got pastes")
pastes, err3 := ctx.Database.GetPagedPastes(page)
ctx.Logger.Debug().Int("count", len(pastes)).Msg("Got pastes")
pastesString := "No pastes to show."
// Show "No pastes to show" on any error for now.
if err3 != nil {
c.Logger.Error().Err(err3).Msg("Failed to get pastes list from database")
ctx.Logger.Error().Err(err3).Msg("Failed to get pastes list from database")
noPastesToShowTpl := templater.GetErrorTemplate(ectx, "No pastes to show.")
@@ -100,8 +100,8 @@ func pastesGET(ectx echo.Context) error {
}
// Pagination.
pages := c.Database.GetPastesPages()
c.Logger.Debug().Int("total pages", pages).Int("current page", page).Msg("Paging data")
pages := ctx.Database.GetPastesPages()
ctx.Logger.Debug().Int("total pages", pages).Int("current page", page).Msg("Paging data")
paginationHTML := pagination.CreateHTML(page, pages, "/pastes/")
pasteListTpl := templater.GetTemplate(ectx, "pastelist_list.html", map[string]string{"pastes": pastesString, "pagination": paginationHTML})