updated ScanIP function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2023-01-14 20:58:32 +01:00
parent 1868bb2ea4
commit 9244e05fc3

View File

@ -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) { 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.City.GetOrCreate(session)
ip.Country.GetOrCreate(session) ip.Country.GetOrCreate(session)
ip.AutonomousSystem.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) ip.Src.GetOrCreate(session)
session.Commit() 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 { if !has {
session.Insert(ip) session.Insert(ip)
} else { } else {
@ -129,12 +133,11 @@ func ScanIP(cfg *config.Config) (err error) {
for { for {
session := cfg.Db.NewSession() session := cfg.Db.NewSession()
orphans := []IP{} 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() session.Close()
if err == nil && len(orphans) > 0 { if err == nil && len(orphans) > 0 {
session := cfg.Db.NewSession() session := cfg.Db.NewSession()
err = session.Begin()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
@ -161,17 +164,20 @@ func ScanIP(cfg *config.Config) (err error) {
log.Printf("%s -> \"%s\"\n", orphan.IP, query.Rdns) log.Printf("%s -> \"%s\"\n", orphan.IP, query.Rdns)
orphan.GetOrCreate(session) orphan.GetOrCreate(session)
}
err = session.Commit() err = session.Commit()
fmt.Println(err) if err != nil {
if err != nil { log.Println(err)
log.Println(err) }
} err = session.Close()
session.Close() if err != nil {
log.Println(err)
} }
} else { } else {
time.Sleep(10 * time.Second) time.Sleep(2 * time.Minute)
} }
time.Sleep(1 * time.Second)
} }
} }