From 3f000cbf8f3d27bbca479d0c96ee0810b4228681 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sun, 28 Aug 2022 16:23:21 +0200 Subject: [PATCH] updated ipbl, bug fixes, change models --- cmd/ipbl/ipbl.go | 4 +++- src/config/main.go | 32 ++++++++++++++++++-------------- src/models/cfg.go | 3 ++- src/models/ip.go | 14 ++++++++++++++ src/routers/funcs.go | 14 +++++++++----- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/cmd/ipbl/ipbl.go b/cmd/ipbl/ipbl.go index dba1965..4747fbf 100644 --- a/cmd/ipbl/ipbl.go +++ b/cmd/ipbl/ipbl.go @@ -33,7 +33,9 @@ func main() { } defer cfg.Db.Close() - go models.ScanIP(&cfg) + if !cfg.Switchs.NoScanIP { + go models.ScanIP(&cfg) + } go zmqrouter.Init(&cfg) go func() { err = routers.RunServer(&ctx, &cfg) }() if err != nil { diff --git a/src/config/main.go b/src/config/main.go index c98224b..d8c61fd 100644 --- a/src/config/main.go +++ b/src/config/main.go @@ -15,6 +15,7 @@ func (cfg *Config) GetConfig() error { var init bool var port int var version bool + var noscanip bool flag.Usage = utils.Usage @@ -24,6 +25,7 @@ func (cfg *Config) GetConfig() error { flag.BoolVar(&drop, "drop", false, "If dropping tables must occur") flag.BoolVar(&init, "init", false, "If init of database must be done") flag.BoolVar(&version, "version", false, "Show version") + flag.BoolVar(&noscanip, "noscanip", false, "Do not run scanip") flag.Parse() @@ -32,6 +34,7 @@ func (cfg *Config) GetConfig() error { cfg.Switchs.Init = init cfg.Switchs.Port = port cfg.Switchs.Version = version + cfg.Switchs.NoScanIP = noscanip var inicfg, err = ini.Load(configfile) if err != nil { @@ -54,22 +57,23 @@ func (cfg *Config) GetConfig() error { type Config struct { Db *xorm.Engine `json:"-"` DbParams struct { - DbHostname string `json:"dbhostname"` - DbUsername string `json:"dbusername"` - DbPassword string `json:"dbpassword"` - DbDatabase string `json:"dbdatabase"` - } `json:"dbparams"` + DbHostname string + DbUsername string + DbPassword string + DbDatabase string + } `json:"-"` Options struct { - Version string `json:"version"` - HideBanner bool `json:"hidebanner"` - ZMQChannel string `json:"zmqchannel"` + Version string + HideBanner bool + ZMQChannel string } `json:"-"` Switchs struct { - Port int `json:"port"` - NoFeed bool `json:"nofeed"` - Debug bool `json:"debug"` - Drop bool `json:"drop"` - Init bool `json:"init"` - Version bool `json:"version"` + Port int + NoFeed bool + Debug bool + Drop bool + Init bool + Version bool + NoScanIP bool } `json:"-"` } diff --git a/src/models/cfg.go b/src/models/cfg.go index 13d74b5..307d9d4 100644 --- a/src/models/cfg.go +++ b/src/models/cfg.go @@ -10,7 +10,7 @@ import ( "github.com/labstack/echo/v4" ) -//const ipv4_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/` +// const ipv4_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/` const ipv4_cidr_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|)){4}\/([1-3])?([0-9])?$)` func GetTrustlists(cfg config.Config) (res []string, err error) { @@ -138,6 +138,7 @@ type CfgSet struct { Filename string `xorm:"text notnull" json:"filename"` Regex string `xorm:"text notnull" json:"regex"` Blocktime int64 `xorm:"notnull default 60" json:"blocktime"` + TryFail int64 `xorm:"notnull default 5" json:"tryfail"` Enabled bool `xorm:"notnull default true" json:"-"` } diff --git a/src/models/ip.go b/src/models/ip.go index 381ce72..ff76159 100644 --- a/src/models/ip.go +++ b/src/models/ip.go @@ -33,6 +33,15 @@ func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (a return } +func GetIPsWithoutHostInfo(ctx *context.Context, config *config.Config) (apiips []string, err error) { + var ips []IP + err = config.Db.In("ip").GroupBy("ip").Find(&ips) + for _, ml := range ips { + apiips = append(apiips, ml.IP) + } + return +} + func GetIP(ctx *context.Context, cfg *config.Config, ipquery interface{}) (apiip *APIIP, err error) { session := cfg.Db.NewSession() defer session.Close() @@ -169,6 +178,7 @@ func (ip *IP) APIFormat() *APIIP { Date: ip.Updated.Local().String(), } } + func (ip *APIIP) APIConvert() *IP { if ip == nil { return nil @@ -185,6 +195,10 @@ func (ip *APIIP) APIConvert() *IP { } } +func (ip *APIIP) BeforeInsert() (err error) { + return +} + type IP struct { ID int `xorm:"pk autoincr"` IP string `xorm:"text notnull unique(ipsrc)" json:"ip"` diff --git a/src/routers/funcs.go b/src/routers/funcs.go index 2ee923b..3361a01 100644 --- a/src/routers/funcs.go +++ b/src/routers/funcs.go @@ -57,6 +57,10 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) { ret, err := models.GetIPsLast(ctx, cfg, interval) return Result(c, err, ret) }) + e.GET("/ips/withouthostinfo", func(c echo.Context) (err error) { + ret, err := models.GetIPsWithoutHostInfo(ctx, cfg) + return Result(c, err, ret) + }) e.POST("/ips", func(c echo.Context) (err error) { var apiips = []models.APIIP{} var ips = []models.IP{} @@ -74,13 +78,13 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) { return Result(c, err, msg) }) e.POST("/hostinfo", func(c echo.Context) (err error) { - var hostinfo = models.HostInfo{} - err = c.Bind(&hostinfo) + var hostinfos = []models.HostInfo{} + err = c.Bind(&hostinfos) if err != nil { - return Result(c, err, hostinfo) + return Result(c, err, hostinfos) } - num, err := cfg.Db.Insert(&hostinfo) - msg := fmt.Sprintf("Inserted %v host info (%d)", hostinfo, num) + num, err := cfg.Db.Insert(&hostinfos) + msg := fmt.Sprintf("Inserted %v host info (%d)", hostinfos, num) log.Println(msg) return Result(c, err, msg) })