updated ipbl with misc fixes
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
Paul 2022-01-30 18:38:03 +01:00
parent 1765b94790
commit d56c89ec2f
2 changed files with 37 additions and 7 deletions

View File

@ -22,6 +22,16 @@ func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxe
return return
} }
// GetIPs ...
func GetIPsLastDay(ctx *context.Context, config *config.Config, interval string) (apimailboxes []*api.IP, err error) {
var ips []IP
err = config.Db.Where(fmt.Sprintf("created >= (now()-'%s'::interval)", interval)).Desc("created").Find(&ips)
for _, ml := range ips {
apimailboxes = append(apimailboxes, ml.APIFormat())
}
return
}
// GetIP ... // GetIP ...
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
@ -55,8 +65,18 @@ func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) {
} }
// InsertIPBulk ... // InsertIPBulk ...
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numfail int64, err error) { func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) {
for _, ip := range *ips { for _, ip := range *ips {
s_ip := IP{IP: ip.IP}
res, _ := cfg.Db.Get(&s_ip)
if res {
num, err := cfg.Db.ID(s_ip.ID).Update(&s_ip)
if err != nil {
numfail++
continue
}
numupdates += num
} else {
num, err := cfg.Db.Insert(ip) num, err := cfg.Db.Insert(ip)
if err != nil { if err != nil {
numfail++ numfail++
@ -64,6 +84,7 @@ func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numfail int6
} }
numinserts += num numinserts += num
} }
}
return return
} }

View File

@ -52,6 +52,11 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
ret, err := models.GetIPs(ctx, cfg, limit) ret, err := models.GetIPs(ctx, cfg, limit)
return JSONResult(c, err, ret) return JSONResult(c, err, ret)
}) })
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)
})
e.POST("/ips", func(c echo.Context) (err error) { e.POST("/ips", func(c echo.Context) (err error) {
var ips = []models.IP{} var ips = []models.IP{}
var msg string var msg string
@ -59,11 +64,15 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, "Error when parsing body") return c.JSON(http.StatusInternalServerError, "Error when parsing body")
} }
numinsert, _, _ := models.InsertIPBulk(cfg, &ips) numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips)
if numinsert > 0 { if numinsert > 0 {
msg = fmt.Sprintf("Inserted %d IP", numinsert) msg = fmt.Sprintf("Inserted %d IP", numinsert)
log.Println(msg) log.Println(msg)
} }
if numupdate > 0 {
msg = fmt.Sprintf("Updated %d IP", numupdate)
log.Println(msg)
}
return c.JSON(http.StatusOK, msg) return c.JSON(http.StatusOK, msg)
}) })
e.GET("/ips/whitelist", func(c echo.Context) (err error) { e.GET("/ips/whitelist", func(c echo.Context) (err error) {