2021-12-27 23:52:22 +01:00
|
|
|
package routers
|
2021-11-07 20:44:48 +01:00
|
|
|
|
|
|
|
import (
|
2021-12-27 23:52:22 +01:00
|
|
|
"context"
|
2021-11-07 20:44:48 +01:00
|
|
|
"fmt"
|
2021-12-12 19:02:35 +01:00
|
|
|
"log"
|
2021-11-07 20:44:48 +01:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.paulbsd.com/paulbsd/ipbl/src/config"
|
2021-12-27 23:52:22 +01:00
|
|
|
"git.paulbsd.com/paulbsd/ipbl/src/models"
|
2021-11-07 20:44:48 +01:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2021-12-27 23:52:22 +01:00
|
|
|
// RegisterRoutes runs the main echo HTTP server
|
|
|
|
func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
|
2021-11-07 20:44:48 +01:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
2021-12-12 17:33:40 +01:00
|
|
|
return c.HTML(http.StatusOK, `<html>
|
|
|
|
<body style="background-color: black">
|
2022-02-07 23:08:22 +01:00
|
|
|
<p style="color:white">Welcome to ipbl software (<a href="https://git.paulbsd.com/paulbsd/ipbl">https://git.paulbsd.com/paulbsd/ipbl</a>)</p>
|
2021-12-12 17:33:40 +01:00
|
|
|
</body>
|
|
|
|
</html>`)
|
2021-11-07 20:44:48 +01:00
|
|
|
})
|
2021-12-27 23:52:22 +01:00
|
|
|
|
|
|
|
// IPs
|
2021-11-07 20:44:48 +01:00
|
|
|
e.GET("/ip/:ip", func(c echo.Context) (err error) {
|
2021-12-30 12:03:26 +01:00
|
|
|
ret, err := models.GetIP(ctx, cfg, c.Param("ip"))
|
2021-12-27 23:52:22 +01:00
|
|
|
return JSONResult(c, err, ret)
|
2021-11-07 20:44:48 +01:00
|
|
|
})
|
2021-12-11 12:59:43 +01:00
|
|
|
e.POST("/ip", func(c echo.Context) (err error) {
|
2021-12-27 23:52:22 +01:00
|
|
|
var ip = new(models.IP)
|
2021-12-29 14:18:27 +01:00
|
|
|
var msg string
|
2021-12-12 17:33:40 +01:00
|
|
|
err = c.Bind(ip)
|
|
|
|
if err != nil {
|
2022-01-30 18:54:01 +01:00
|
|
|
return JSONResult(c, fmt.Errorf("error when parsing body"), "")
|
2021-12-12 17:33:40 +01:00
|
|
|
}
|
|
|
|
num, err := ip.InsertIP(cfg)
|
|
|
|
if err != nil {
|
2022-01-30 18:54:01 +01:00
|
|
|
return JSONResult(c, fmt.Errorf("error inserting data"), "")
|
2021-12-12 17:33:40 +01:00
|
|
|
}
|
2021-12-29 14:18:27 +01:00
|
|
|
if num > 0 {
|
|
|
|
msg = fmt.Sprintf("Inserted %d IP", num)
|
|
|
|
}
|
2021-12-12 19:02:35 +01:00
|
|
|
return c.JSON(http.StatusOK, msg)
|
2021-12-11 12:59:43 +01:00
|
|
|
})
|
2021-11-07 20:44:48 +01:00
|
|
|
e.GET("/ips", func(c echo.Context) (err error) {
|
2021-12-27 23:52:22 +01:00
|
|
|
limit := 50
|
2021-11-07 20:44:48 +01:00
|
|
|
if c.QueryParam("limit") != "" {
|
2021-12-27 23:52:22 +01:00
|
|
|
limit, _ = strconv.Atoi(c.QueryParam("limit"))
|
2021-11-07 20:44:48 +01:00
|
|
|
}
|
2021-12-30 12:03:26 +01:00
|
|
|
ret, err := models.GetIPs(ctx, cfg, limit)
|
2021-12-27 23:52:22 +01:00
|
|
|
return JSONResult(c, err, ret)
|
2021-11-07 20:44:48 +01:00
|
|
|
})
|
2022-01-30 18:38:03 +01:00
|
|
|
e.GET("/ips/lastday", func(c echo.Context) (err error) {
|
|
|
|
interval := "1 day"
|
|
|
|
ret, err := models.GetIPsLastDay(ctx, cfg, interval)
|
|
|
|
return JSONResult(c, err, ret)
|
|
|
|
})
|
2021-12-11 12:59:43 +01:00
|
|
|
e.POST("/ips", func(c echo.Context) (err error) {
|
2021-12-27 23:52:22 +01:00
|
|
|
var ips = []models.IP{}
|
2021-12-29 14:18:27 +01:00
|
|
|
var msg string
|
2021-12-12 17:33:40 +01:00
|
|
|
err = c.Bind(&ips)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error when parsing body")
|
|
|
|
}
|
2022-01-30 18:38:03 +01:00
|
|
|
numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips)
|
2021-12-29 14:18:27 +01:00
|
|
|
if numinsert > 0 {
|
|
|
|
msg = fmt.Sprintf("Inserted %d IP", numinsert)
|
2021-12-29 14:24:26 +01:00
|
|
|
log.Println(msg)
|
2021-12-29 14:18:27 +01:00
|
|
|
}
|
2022-01-30 18:38:03 +01:00
|
|
|
if numupdate > 0 {
|
|
|
|
msg = fmt.Sprintf("Updated %d IP", numupdate)
|
|
|
|
log.Println(msg)
|
|
|
|
}
|
2021-12-12 19:02:35 +01:00
|
|
|
return c.JSON(http.StatusOK, msg)
|
2021-12-11 12:59:43 +01:00
|
|
|
})
|
2021-12-27 23:52:22 +01:00
|
|
|
e.GET("/ips/whitelist", func(c echo.Context) (err error) {
|
2022-02-07 23:08:22 +01:00
|
|
|
whitelists, _ := models.GetWhitelists(*cfg)
|
2021-12-27 23:52:22 +01:00
|
|
|
if len(whitelists) > 0 {
|
|
|
|
return c.JSON(http.StatusOK, whitelists)
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusInternalServerError, "")
|
|
|
|
})
|
2022-02-07 23:08:22 +01:00
|
|
|
e.PUT("/ips/whitelist", func(c echo.Context) (err error) {
|
|
|
|
var cidr models.Whitelist
|
|
|
|
err = c.Bind(&cidr)
|
|
|
|
if err == nil && cidr.Verify() {
|
|
|
|
cidr.Insert(*cfg)
|
|
|
|
return c.JSON(http.StatusOK, "Inserted new CIDR")
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Invalid CIDR")
|
|
|
|
})
|
|
|
|
e.DELETE("/ips/whitelist/:ip", func(c echo.Context) (err error) {
|
|
|
|
var ip = c.Param("ip")
|
|
|
|
var cidr models.Whitelist
|
|
|
|
err = cidr.Delete(*cfg, ip)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusOK, "Deleted old CIDR")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
2021-11-07 20:44:48 +01:00
|
|
|
|
|
|
|
e.Logger.Fatal(
|
|
|
|
e.Start(
|
|
|
|
fmt.Sprintf(":%d",
|
|
|
|
cfg.Switchs.Port)))
|
|
|
|
|
|
|
|
}
|