updated ipbl, bug fixes, change models
All checks were successful
continuous-integration/drone Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Paul 2022-08-28 16:23:21 +02:00
parent a1329fc544
commit 3f000cbf8f
5 changed files with 46 additions and 21 deletions

View File

@ -33,7 +33,9 @@ func main() {
} }
defer cfg.Db.Close() defer cfg.Db.Close()
go models.ScanIP(&cfg) if !cfg.Switchs.NoScanIP {
go models.ScanIP(&cfg)
}
go zmqrouter.Init(&cfg) go zmqrouter.Init(&cfg)
go func() { err = routers.RunServer(&ctx, &cfg) }() go func() { err = routers.RunServer(&ctx, &cfg) }()
if err != nil { if err != nil {

View File

@ -15,6 +15,7 @@ func (cfg *Config) GetConfig() error {
var init bool var init bool
var port int var port int
var version bool var version bool
var noscanip bool
flag.Usage = utils.Usage 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(&drop, "drop", false, "If dropping tables must occur")
flag.BoolVar(&init, "init", false, "If init of database must be done") flag.BoolVar(&init, "init", false, "If init of database must be done")
flag.BoolVar(&version, "version", false, "Show version") flag.BoolVar(&version, "version", false, "Show version")
flag.BoolVar(&noscanip, "noscanip", false, "Do not run scanip")
flag.Parse() flag.Parse()
@ -32,6 +34,7 @@ func (cfg *Config) GetConfig() error {
cfg.Switchs.Init = init cfg.Switchs.Init = init
cfg.Switchs.Port = port cfg.Switchs.Port = port
cfg.Switchs.Version = version cfg.Switchs.Version = version
cfg.Switchs.NoScanIP = noscanip
var inicfg, err = ini.Load(configfile) var inicfg, err = ini.Load(configfile)
if err != nil { if err != nil {
@ -54,22 +57,23 @@ func (cfg *Config) GetConfig() error {
type Config struct { type Config struct {
Db *xorm.Engine `json:"-"` Db *xorm.Engine `json:"-"`
DbParams struct { DbParams struct {
DbHostname string `json:"dbhostname"` DbHostname string
DbUsername string `json:"dbusername"` DbUsername string
DbPassword string `json:"dbpassword"` DbPassword string
DbDatabase string `json:"dbdatabase"` DbDatabase string
} `json:"dbparams"` } `json:"-"`
Options struct { Options struct {
Version string `json:"version"` Version string
HideBanner bool `json:"hidebanner"` HideBanner bool
ZMQChannel string `json:"zmqchannel"` ZMQChannel string
} `json:"-"` } `json:"-"`
Switchs struct { Switchs struct {
Port int `json:"port"` Port int
NoFeed bool `json:"nofeed"` NoFeed bool
Debug bool `json:"debug"` Debug bool
Drop bool `json:"drop"` Drop bool
Init bool `json:"init"` Init bool
Version bool `json:"version"` Version bool
NoScanIP bool
} `json:"-"` } `json:"-"`
} }

View File

@ -10,7 +10,7 @@ import (
"github.com/labstack/echo/v4" "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])?$)` 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) { func GetTrustlists(cfg config.Config) (res []string, err error) {
@ -138,6 +138,7 @@ type CfgSet struct {
Filename string `xorm:"text notnull" json:"filename"` Filename string `xorm:"text notnull" json:"filename"`
Regex string `xorm:"text notnull" json:"regex"` Regex string `xorm:"text notnull" json:"regex"`
Blocktime int64 `xorm:"notnull default 60" json:"blocktime"` Blocktime int64 `xorm:"notnull default 60" json:"blocktime"`
TryFail int64 `xorm:"notnull default 5" json:"tryfail"`
Enabled bool `xorm:"notnull default true" json:"-"` Enabled bool `xorm:"notnull default true" json:"-"`
} }

View File

@ -33,6 +33,15 @@ func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (a
return 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) { func GetIP(ctx *context.Context, cfg *config.Config, ipquery interface{}) (apiip *APIIP, err error) {
session := cfg.Db.NewSession() session := cfg.Db.NewSession()
defer session.Close() defer session.Close()
@ -169,6 +178,7 @@ func (ip *IP) APIFormat() *APIIP {
Date: ip.Updated.Local().String(), Date: ip.Updated.Local().String(),
} }
} }
func (ip *APIIP) APIConvert() *IP { func (ip *APIIP) APIConvert() *IP {
if ip == nil { if ip == nil {
return nil return nil
@ -185,6 +195,10 @@ func (ip *APIIP) APIConvert() *IP {
} }
} }
func (ip *APIIP) BeforeInsert() (err error) {
return
}
type IP struct { type IP struct {
ID int `xorm:"pk autoincr"` ID int `xorm:"pk autoincr"`
IP string `xorm:"text notnull unique(ipsrc)" json:"ip"` IP string `xorm:"text notnull unique(ipsrc)" json:"ip"`

View File

@ -57,6 +57,10 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
ret, err := models.GetIPsLast(ctx, cfg, interval) ret, err := models.GetIPsLast(ctx, cfg, interval)
return Result(c, err, ret) 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) { e.POST("/ips", func(c echo.Context) (err error) {
var apiips = []models.APIIP{} var apiips = []models.APIIP{}
var ips = []models.IP{} var ips = []models.IP{}
@ -74,13 +78,13 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
return Result(c, err, msg) return Result(c, err, msg)
}) })
e.POST("/hostinfo", func(c echo.Context) (err error) { e.POST("/hostinfo", func(c echo.Context) (err error) {
var hostinfo = models.HostInfo{} var hostinfos = []models.HostInfo{}
err = c.Bind(&hostinfo) err = c.Bind(&hostinfos)
if err != nil { if err != nil {
return Result(c, err, hostinfo) return Result(c, err, hostinfos)
} }
num, err := cfg.Db.Insert(&hostinfo) num, err := cfg.Db.Insert(&hostinfos)
msg := fmt.Sprintf("Inserted %v host info (%d)", hostinfo, num) msg := fmt.Sprintf("Inserted %v host info (%d)", hostinfos, num)
log.Println(msg) log.Println(msg)
return Result(c, err, msg) return Result(c, err, msg)
}) })