From 9244e05fc389f2c7bddca8c875b3259411a7c417 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sat, 14 Jan 2023 20:58:32 +0100 Subject: [PATCH] updated ScanIP function --- src/models/ip.go | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/models/ip.go b/src/models/ip.go index b26b810..2feb6ae 100644 --- a/src/models/ip.go +++ b/src/models/ip.go @@ -37,12 +37,6 @@ func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (a } func (ip *IP) GetOrCreate(session *xorm.Session) (apiip *APIIP, err error) { - var tmpip = &IP{IP: ip.IP} - has, err := session.Get(tmpip) - if err != nil { - log.Println(err) - } - ip.City.GetOrCreate(session) ip.Country.GetOrCreate(session) ip.AutonomousSystem.GetOrCreate(session) @@ -50,6 +44,16 @@ func (ip *IP) GetOrCreate(session *xorm.Session) (apiip *APIIP, err error) { ip.Src.GetOrCreate(session) session.Commit() + var tmpip *IP + if ip.ID != 0 { + tmpip = &IP{ID: ip.ID, IP: ip.IP} + } else { + tmpip = &IP{IP: ip.IP} + } + has, err := session.Get(tmpip) + if err != nil { + log.Println(err) + } if !has { session.Insert(ip) } else { @@ -129,12 +133,11 @@ func ScanIP(cfg *config.Config) (err error) { for { session := cfg.Db.NewSession() orphans := []IP{} - err = session.Where("as_id is null or country_id is null or src_id is null or host_id is null").Asc("updated").Limit(ScanLimit).Find(&orphans) + err = session.Where("(as_id is null or country_id is null or city_id is null or rdns = '') and updated < now()-'1d'::interval").Asc("updated").Limit(ScanLimit).Find(&orphans) session.Close() if err == nil && len(orphans) > 0 { session := cfg.Db.NewSession() - err = session.Begin() if err != nil { log.Println(err) } @@ -161,17 +164,20 @@ func ScanIP(cfg *config.Config) (err error) { log.Printf("%s -> \"%s\"\n", orphan.IP, query.Rdns) orphan.GetOrCreate(session) + } - err = session.Commit() - fmt.Println(err) - if err != nil { - log.Println(err) - } - session.Close() + err = session.Commit() + if err != nil { + log.Println(err) + } + err = session.Close() + if err != nil { + log.Println(err) } } else { - time.Sleep(10 * time.Second) + time.Sleep(2 * time.Minute) } + time.Sleep(1 * time.Second) } }