diff --git a/src/models/cfg.go b/src/models/cfg.go index a73b6ea..2a2b405 100644 --- a/src/models/cfg.go +++ b/src/models/cfg.go @@ -7,6 +7,7 @@ import ( "strings" "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})/` @@ -91,14 +92,17 @@ func GetZMQ(cfg config.Config, key string) (res ZMQ, err error) { return } -func DiscoverURLS(cfg config.Config) (Discovery, error) { +func DiscoverURLS(cfg config.Config, routes []*echo.Route) (Discovery, error) { var disc Discovery - var urls []Url - urls = append(urls, Url{Key: "auth", Path: "/auth"}) - urls = append(urls, Url{Key: "folders", Path: "/config/folders"}) - urls = append(urls, Url{Key: "trustlist", Path: "/config/trustlist"}) - urls = append(urls, Url{Key: "zmqrr", Path: "/config/zmqrr"}) - urls = append(urls, Url{Key: "zmqps", Path: "/config/zmqps"}) + var urls = make(map[string]Url) + for _, j := range routes { + if strings.Contains(j.Path, ":") || j.Path == "/" || j.Path == "/discovery" { + continue + } + 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} return disc, nil } @@ -130,8 +134,8 @@ type Cfg struct { } type Discovery struct { - Version string `json:"version"` - URLs []Url `json:"urls"` + Version string `json:"version"` + URLs map[string]Url `json:"urls"` } type Url struct { diff --git a/src/models/ip.go b/src/models/ip.go index 784fefb..fbbb8ad 100644 --- a/src/models/ip.go +++ b/src/models/ip.go @@ -12,7 +12,7 @@ import ( "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) { 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) { 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 { 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) { var ip IP - - if has, err := config.Db.Where("ip = ?", ipquery).Get(&ip); !has { + has, err := config.Db.Where("ip = ?", ipquery).Get(&ip) + if !has { err = fmt.Errorf("not found") return nil, err } diff --git a/src/models/utils.go b/src/models/utils.go index 6fe59af..0ae7842 100644 --- a/src/models/utils.go +++ b/src/models/utils.go @@ -2,7 +2,7 @@ package models import "reflect" -const keyname string = "id" +//const keyname string = "id" func differ(sl1 []IP, sl2 []IP) (toinsert []IP, err error) { var m = make(map[string]IPDiffer) diff --git a/src/routers/funcs.go b/src/routers/funcs.go index 5775b16..f041222 100644 --- a/src/routers/funcs.go +++ b/src/routers/funcs.go @@ -16,8 +16,8 @@ import ( func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) { e.GET("/", func(c echo.Context) error { return c.HTML(http.StatusOK, ` - -

Welcome to ipbl software (https://git.paulbsd.com/paulbsd/ipbl)

+ +

Welcome to ipbl software (https://git.paulbsd.com/paulbsd/ipbl)

`) }) @@ -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) { interval := "30 minutes" + if c.QueryParam("interval") != "" { + interval = c.QueryParam("interval") + } ret, err := models.GetIPsLast(ctx, cfg, interval) 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) }) 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) }) }