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
|
|
|
|
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"))
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(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)
|
2022-02-22 14:44:52 +01:00
|
|
|
var msg = "No IP inserted"
|
2021-12-12 17:33:40 +01:00
|
|
|
err = c.Bind(ip)
|
|
|
|
if err != nil {
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, fmt.Errorf("error when parsing body"), "")
|
2021-12-12 17:33:40 +01:00
|
|
|
}
|
|
|
|
num, err := ip.InsertIP(cfg)
|
2022-02-12 00:33:58 +01:00
|
|
|
fmt.Println(err)
|
2021-12-12 17:33:40 +01:00
|
|
|
if err != nil {
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, "")
|
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)
|
|
|
|
}
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, 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)
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, ret)
|
2021-11-07 20:44:48 +01:00
|
|
|
})
|
2022-02-09 10:18:28 +01:00
|
|
|
// retro-compat
|
2022-01-30 18:38:03 +01:00
|
|
|
e.GET("/ips/lastday", func(c echo.Context) (err error) {
|
|
|
|
interval := "1 day"
|
2022-02-09 10:18:28 +01:00
|
|
|
ret, err := models.GetIPsLast(ctx, cfg, interval)
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, ret)
|
2022-02-09 10:18:28 +01:00
|
|
|
})
|
|
|
|
e.GET("/ips/last", func(c echo.Context) (err error) {
|
|
|
|
interval := "10 minutes"
|
|
|
|
ret, err := models.GetIPsLast(ctx, cfg, interval)
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, ret)
|
2022-01-30 18:38:03 +01:00
|
|
|
})
|
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 {
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, ips)
|
2021-12-12 17:33:40 +01:00
|
|
|
}
|
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)
|
|
|
|
}
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, 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-12 00:33:58 +01:00
|
|
|
whitelists, err := models.GetWhitelists(*cfg)
|
|
|
|
return Result(c, err, whitelists)
|
2021-12-27 23:52:22 +01:00
|
|
|
})
|
2022-02-12 00:33:58 +01:00
|
|
|
e.POST("/ips/whitelist", func(c echo.Context) (err error) {
|
2022-02-07 23:08:22 +01:00
|
|
|
var cidr models.Whitelist
|
|
|
|
err = c.Bind(&cidr)
|
|
|
|
if err == nil && cidr.Verify() {
|
2022-02-12 00:33:58 +01:00
|
|
|
err = cidr.InsertOrUpdate(*cfg)
|
|
|
|
return Result(c, err, cidr)
|
2022-02-07 23:08:22 +01:00
|
|
|
}
|
2022-02-12 00:33:58 +01:00
|
|
|
return Result(c, err, nil)
|
2022-02-07 23:08:22 +01:00
|
|
|
})
|
|
|
|
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)))
|
|
|
|
|
|
|
|
}
|