security/routes/index.go

88 lines
1.7 KiB
Go

package routes
import (
"math"
"strconv"
"strings"
"git.matterlinux.xyz/matter/security/lib"
"git.matterlinux.xyz/matter/security/log"
"github.com/gofiber/fiber/v2"
)
var PAGE_SIZE = 32
func GetPage(c *fiber.Ctx) (int, int, int) {
page, err := strconv.Atoi(c.Query("p"))
if err != nil || page <= 0 {
page = 1
}
return page, page * PAGE_SIZE, (page * PAGE_SIZE) - PAGE_SIZE
}
func GET_Details(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" || !strings.HasPrefix(id, "MPSI-") {
return lib.RenderError(c, 404)
}
v, suc := lib.FindVuln(id)
if !suc {
return lib.RenderError(c, 404)
}
return c.Render("details", &v)
}
func GET_Index(c *fiber.Ctx) error {
cur, max, min := GetPage(c)
search_qu := c.Query("q")
search_in := c.Query("i")
vulns, err := lib.LoadVulns()
if err != nil {
log.Error("Failed to load vulns: %s", err.Error())
return lib.RenderError(c, 500)
}
results := []lib.Vuln{}
for i := len(vulns) - 1; i >= 0; i-- {
if i >= max || i < min {
continue
}
switch search_in {
case "desc":
if !lib.ContainsCase(vulns[i].Desc, search_qu) {
continue
}
case "id":
if !lib.ContainsCase(vulns[i].ID, search_qu) {
continue
}
case "pkg":
if !lib.ContainsCase(vulns[i].Package, search_qu) {
continue
}
case "status":
if !lib.ContainsCase(vulns[i].Status, search_qu) {
continue
}
}
results = append(results, vulns[i])
}
pages := int64(math.Ceil(float64(len(results)) / float64(PAGE_SIZE)))
return c.Render("index", fiber.Map{
"pages": pages,
"current": cur,
"next": cur + 1,
"prev": cur - 1,
"vulns": results,
"query": search_qu,
"in": search_in,
})
}