updated ipbl

This commit is contained in:
Paul 2022-03-14 18:49:28 +01:00
parent 32784ff442
commit 881969f81e
4 changed files with 24 additions and 17 deletions

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"git.paulbsd.com/paulbsd/ipbl/src/config" "git.paulbsd.com/paulbsd/ipbl/src/config"
"github.com/labstack/echo/v4"
) )
//var ipv4_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/` //var ipv4_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/`
@ -91,14 +92,17 @@ func GetZMQ(cfg config.Config, key string) (res ZMQ, err error) {
return return
} }
func DiscoverURLS(cfg config.Config) (Discovery, error) { func DiscoverURLS(cfg config.Config, routes []*echo.Route) (Discovery, error) {
var disc Discovery var disc Discovery
var urls []Url var urls = make(map[string]Url)
urls = append(urls, Url{Key: "auth", Path: "/auth"}) for _, j := range routes {
urls = append(urls, Url{Key: "folders", Path: "/config/folders"}) if strings.Contains(j.Path, ":") || j.Path == "/" || j.Path == "/discovery" {
urls = append(urls, Url{Key: "trustlist", Path: "/config/trustlist"}) continue
urls = append(urls, Url{Key: "zmqrr", Path: "/config/zmqrr"}) }
urls = append(urls, Url{Key: "zmqps", Path: "/config/zmqps"}) names := strings.Split(j.Path, "/")
name := names[len(names)-1]
urls[name] = Url{Key: name, Path: j.Path}
}
disc = Discovery{Version: "1.0", URLs: urls} disc = Discovery{Version: "1.0", URLs: urls}
return disc, nil return disc, nil
} }
@ -130,8 +134,8 @@ type Cfg struct {
} }
type Discovery struct { type Discovery struct {
Version string `json:"version"` Version string `json:"version"`
URLs []Url `json:"urls"` URLs map[string]Url `json:"urls"`
} }
type Url struct { type Url struct {

View File

@ -12,7 +12,7 @@ import (
"git.paulbsd.com/paulbsd/ipbl/src/config" "git.paulbsd.com/paulbsd/ipbl/src/config"
) )
var lastday = time.Now().Add(-(time.Hour * 24)) //var lastday = time.Now().Add(-(time.Hour * 24))
func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxes []*api.IP, err error) { func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxes []*api.IP, err error) {
var ips []IP var ips []IP
@ -25,7 +25,7 @@ func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxe
func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (apimailboxes []*api.IP, err error) { func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (apimailboxes []*api.IP, err error) {
var ips []IP var ips []IP
err = config.Db.Where(fmt.Sprintf("updated >= (now()-'%s'::interval)", interval)).GroupBy("ip").Find(&ips) err = config.Db.Where("updated >= (now()-?::interval)", interval).GroupBy("ip").Find(&ips)
for _, ml := range ips { for _, ml := range ips {
apimailboxes = append(apimailboxes, ml.APIFormat()) apimailboxes = append(apimailboxes, ml.APIFormat())
} }
@ -34,8 +34,8 @@ func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (a
func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (apiip *api.IP, err error) { func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (apiip *api.IP, err error) {
var ip IP var ip IP
has, err := config.Db.Where("ip = ?", ipquery).Get(&ip)
if has, err := config.Db.Where("ip = ?", ipquery).Get(&ip); !has { if !has {
err = fmt.Errorf("not found") err = fmt.Errorf("not found")
return nil, err return nil, err
} }

View File

@ -2,7 +2,7 @@ package models
import "reflect" import "reflect"
const keyname string = "id" //const keyname string = "id"
func differ(sl1 []IP, sl2 []IP) (toinsert []IP, err error) { func differ(sl1 []IP, sl2 []IP) (toinsert []IP, err error) {
var m = make(map[string]IPDiffer) var m = make(map[string]IPDiffer)

View File

@ -16,8 +16,8 @@ import (
func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) { func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
e.GET("/", func(c echo.Context) error { e.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, `<html> return c.HTML(http.StatusOK, `<html>
<body style="background-color: black"> <body>
<p style="color:trust">Welcome to ipbl software (<a href="https://git.paulbsd.com/paulbsd/ipbl">https://git.paulbsd.com/paulbsd/ipbl</a>)</p> <p>Welcome to ipbl software (<a href="https://git.paulbsd.com/paulbsd/ipbl">https://git.paulbsd.com/paulbsd/ipbl</a>)</p>
</body> </body>
</html>`) </html>`)
}) })
@ -53,6 +53,9 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
}) })
e.GET("/ips/last", func(c echo.Context) (err error) { e.GET("/ips/last", func(c echo.Context) (err error) {
interval := "30 minutes" interval := "30 minutes"
if c.QueryParam("interval") != "" {
interval = c.QueryParam("interval")
}
ret, err := models.GetIPsLast(ctx, cfg, interval) ret, err := models.GetIPsLast(ctx, cfg, interval)
return Result(c, err, ret) return Result(c, err, ret)
}) })
@ -135,7 +138,7 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
return Result(c, err, folders) return Result(c, err, folders)
}) })
e.GET("/discovery", func(c echo.Context) (err error) { e.GET("/discovery", func(c echo.Context) (err error) {
disc, err := models.DiscoverURLS(*cfg) disc, err := models.DiscoverURLS(*cfg, e.Routes())
return Result(c, err, disc) return Result(c, err, disc)
}) })
} }