2021-11-07 20:44:48 +01:00
|
|
|
package ipblws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
"git.paulbsd.com/paulbsd/ipbl/src/ipbl"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RunServer runs the main echo HTTP server
|
|
|
|
func RunServer(cfg *config.Config) (err error) {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
e.HideBanner = cfg.Options.HideBanner
|
|
|
|
|
|
|
|
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">
|
|
|
|
<p style="color:white">Welcome to ipbl software (https://git.paulbsd.com/paulbsd/ipbl)</p>
|
|
|
|
</body>
|
|
|
|
</html>`)
|
2021-11-07 20:44:48 +01:00
|
|
|
})
|
|
|
|
e.GET("/ip/:ip", func(c echo.Context) (err error) {
|
|
|
|
var ip = ipbl.IP{IP: c.Param("ip")}
|
|
|
|
res, _ := cfg.Db.Get(&ip)
|
|
|
|
if res {
|
|
|
|
return c.JSON(http.StatusOK, ip)
|
|
|
|
}
|
2021-12-12 19:02:35 +01:00
|
|
|
msg := fmt.Sprintf("IP %s not found", c.Param("ip"))
|
|
|
|
log.Println(msg)
|
|
|
|
return c.JSON(http.StatusNotFound, msg)
|
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-12 17:33:40 +01:00
|
|
|
var ip = new(ipbl.IP)
|
|
|
|
err = c.Bind(ip)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error when parsing body")
|
|
|
|
}
|
|
|
|
num, err := ip.InsertIP(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error inserting data")
|
|
|
|
}
|
2021-12-12 19:02:35 +01:00
|
|
|
msg := fmt.Sprintf("Inserted %d IP", num)
|
|
|
|
log.Println(msg)
|
|
|
|
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) {
|
|
|
|
var ips = []ipbl.IP{}
|
|
|
|
var limit int = 50
|
|
|
|
if c.QueryParam("limit") != "" {
|
|
|
|
limit, err = strconv.Atoi(c.QueryParam("limit"))
|
|
|
|
if err != nil {
|
|
|
|
limit = 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.Db.Limit(limit).Desc("created").Find(&ips)
|
|
|
|
return c.JSON(http.StatusOK, ips)
|
|
|
|
})
|
2021-12-11 12:59:43 +01:00
|
|
|
e.POST("/ips", func(c echo.Context) (err error) {
|
|
|
|
var ips = []ipbl.IP{}
|
2021-12-12 17:33:40 +01:00
|
|
|
err = c.Bind(&ips)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, "Error when parsing body")
|
|
|
|
}
|
|
|
|
numinsert, numfail, _ := ipbl.InsertIPBulk(cfg, &ips)
|
2021-12-12 19:02:35 +01:00
|
|
|
msg := fmt.Sprintf("Inserted %d IP, %d errors", numinsert, numfail)
|
|
|
|
log.Println(msg)
|
|
|
|
return c.JSON(http.StatusOK, msg)
|
2021-12-11 12:59:43 +01:00
|
|
|
})
|
2021-11-07 20:44:48 +01:00
|
|
|
|
|
|
|
e.Logger.Fatal(
|
|
|
|
e.Start(
|
|
|
|
fmt.Sprintf(":%d",
|
|
|
|
cfg.Switchs.Port)))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|