security/routes/index.go

88 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-03-21 20:20:24 +00:00
package routes
import (
"math"
"strconv"
"strings"
"git.matterlinux.xyz/matter/security/lib"
2024-08-13 19:40:44 +00:00
"git.matterlinux.xyz/matter/security/log"
2024-03-21 20:20:24 +00:00
"github.com/gofiber/fiber/v2"
)
var PAGE_SIZE = 32
func GetPage(c *fiber.Ctx) (int, int, int) {
2024-08-13 19:25:36 +00:00
page, err := strconv.Atoi(c.Query("p"))
if err != nil || page <= 0 {
page = 1
}
return page, page * PAGE_SIZE, (page * PAGE_SIZE) - PAGE_SIZE
2024-03-21 20:20:24 +00:00
}
2024-08-13 19:40:44 +00:00
func GET_Details(c *fiber.Ctx) error {
2024-08-13 19:25:36 +00:00
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)
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:40:44 +00:00
return c.Render("details", &v)
2024-03-21 20:20:24 +00:00
}
2024-08-13 19:40:44 +00:00
func GET_Index(c *fiber.Ctx) error {
2024-08-13 19:25:36 +00:00
cur, max, min := GetPage(c)
search_qu := c.Query("q")
search_in := c.Query("i")
vulns, err := lib.LoadVulns()
if err != nil {
2024-08-13 19:40:44 +00:00
log.Error("Failed to load vulns: %s", err.Error())
2024-08-13 19:25:36 +00:00
return lib.RenderError(c, 500)
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
results := []lib.Vuln{}
for i := len(vulns) - 1; i >= 0; i-- {
if i >= max || i < min {
continue
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
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
}
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
results = append(results, vulns[i])
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
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,
})
2024-03-21 20:20:24 +00:00
}