updated ipbl with some functions reliability

This commit is contained in:
Paul 2022-03-26 19:46:04 +01:00
parent 551b3311a6
commit d8ab04aedf
3 changed files with 42 additions and 45 deletions

View File

@ -45,8 +45,8 @@ func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (ap
return return
} }
func (i *IP) UpdateRDNS() (result string, err error) { func (ip *IP) UpdateRDNS() (result string, err error) {
res, err := net.LookupAddr(i.IP) res, err := net.LookupAddr(ip.IP)
if err != nil { if err != nil {
result = "" result = ""
} else { } else {
@ -55,13 +55,30 @@ func (i *IP) UpdateRDNS() (result string, err error) {
return return
} }
func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) { func (ip *IP) InsertOrUpdate(cfg *config.Config) (numinsert int64, numupdate int64, err error) {
num, err = cfg.Db.Insert(i) var ips = []IP{}
err = cfg.Db.Where("ip = ?", ip.IP).And("src = ?", ip.Src).And("hostname = ?", ip.Hostname).Find(&ips)
if len(ips) > 0 {
numupdate, err = cfg.Db.Where("id = ?", ips[0].ID).Cols("updated").Update(&IP{})
if err != nil {
log.Println(err)
}
} else {
numinsert, err = cfg.Db.Insert(&ip)
if err != nil {
log.Println(err)
}
}
return return
} }
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) { func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinsert int64, numupdate int64, err error) {
var iplist []string for _, ip := range *ips {
numinsert, numupdate, err = ip.InsertOrUpdate(cfg)
}
Cleanup(cfg)
return
/*var iplist []string
hostname := (*ips)[0].Hostname.String hostname := (*ips)[0].Hostname.String
for _, ip := range *ips { for _, ip := range *ips {
iplist = append(iplist, ip.IP) iplist = append(iplist, ip.IP)
@ -74,14 +91,14 @@ func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates i
for _, ip := range searchips { for _, ip := range searchips {
toupdateips = append(toupdateips, ip.IP) toupdateips = append(toupdateips, ip.IP)
} }
numupdates, _ = cfg.Db.In("ip", toupdateips).Where("hostname = ?", hostname).Cols("updated").Update(&IP{}) numupdate, _ = cfg.Db.In("ip", toupdateips).Where("hostname = ?", hostname).Cols("updated").Update(&IP{})
var toinsertip, _ = differ(*ips, searchips) var toinsertip, _ = differ(*ips, searchips)
numinserts, err = cfg.Db.Insert(toinsertip) numinsert, err = cfg.Db.Insert(toinsertip)
Cleanup(cfg) Cleanup(cfg)
return return*/
} }
func ScanIP(cfg *config.Config) (err error) { func ScanIP(cfg *config.Config) (err error) {
@ -139,13 +156,13 @@ func (ip *APIIP) APIConvert() *IP {
} }
type IP struct { type IP struct {
ID int `xorm:"pk autoincr" json:"-"` ID int `xorm:"pk autoincr"`
IP string `xorm:"text notnull unique(ipsrc)" json:"ip"` IP string `xorm:"text notnull unique(ipsrc)" json:"ip"`
Rdns sql.NullString `xorm:"text default" json:"rdns"` Rdns sql.NullString `xorm:"text default"`
Src string `xorm:"text notnull unique(ipsrc)" json:"src"` Src string `xorm:"text notnull unique(ipsrc)" json:"src"`
Hostname sql.NullString `xorm:"text default '' unique(ipsrc)" json:"hostname"` Hostname sql.NullString `xorm:"text default '' unique(ipsrc)" json:"hostname"`
Created time.Time `xorm:"created notnull" json:"-"` Created time.Time `xorm:"created notnull"`
Updated time.Time `xorm:"updated notnull" json:"-"` Updated time.Time `xorm:"updated notnull"`
} }
type APIIP struct { type APIIP struct {

View File

@ -33,14 +33,13 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
if err != nil { if err != nil {
return Result(c, fmt.Errorf("error when parsing body"), "") return Result(c, fmt.Errorf("error when parsing body"), "")
} }
num, err := ip.InsertIP(cfg) numinsert, numupdate, err := ip.InsertOrUpdate(cfg)
fmt.Println(err) fmt.Println(err)
if err != nil { if err != nil {
return Result(c, err, "") return Result(c, err, "")
} }
if num > 0 { msg = fmt.Sprintf("zmq: Inserted %d IP, Updated %d IP", numinsert, numupdate)
msg = fmt.Sprintf("Inserted %d IP", num) log.Println(msg)
}
return Result(c, err, msg) return Result(c, err, msg)
}) })
e.GET("/ips", func(c echo.Context) (err error) { e.GET("/ips", func(c echo.Context) (err error) {
@ -70,19 +69,9 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
for _, v := range apiips { for _, v := range apiips {
ips = append(ips, *v.APIConvert()) ips = append(ips, *v.APIConvert())
} }
numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips) numinsert, numupdate, _ := models.InsertIPBulk(cfg, &ips)
if numinsert > 0 { msg = fmt.Sprintf("zmq: Inserted %d IP, Updated %d IP", numinsert, numupdate)
msg = fmt.Sprintf("inserted %d IP", numinsert)
log.Println(msg) log.Println(msg)
}
if numupdate > 0 {
if len(msg) > 0 {
msg = fmt.Sprintf("%s, updated %d IP", msg, numupdate)
} else {
msg = fmt.Sprintf("updated %d IP", numupdate)
}
log.Println(msg)
}
return Result(c, err, msg) return Result(c, err, msg)
}) })
e.GET("/config/trustlist", func(c echo.Context) (err error) { e.GET("/config/trustlist", func(c echo.Context) (err error) {

View File

@ -36,28 +36,19 @@ func Handle(cfg *config.Config, reqsock *goczmq.Sock, pubsock *goczmq.Sock, chan
} }
var topub [][]byte var topub [][]byte
for _, val := range req { for _, val := range req {
var apiip = models.APIIP{}
var ip = models.IP{} var ip = models.IP{}
err = json.Unmarshal(val, &ip) err = json.Unmarshal(val, &apiip)
if err != nil { if err != nil {
log.Println("unable to parse ip address", err) log.Println("unable to parse ip address", err)
continue continue
} }
has, err := cfg.Db.Get(&ip) ip = *apiip.APIConvert()
numinsert, numupdate, err := ip.InsertOrUpdate(cfg)
if err != nil { if err != nil {
log.Println("error fetching ip address", err) log.Println(err)
continue
}
if !has {
num, err := cfg.Db.Insert(&ip)
if err != nil {
log.Println("error inserting ip address", num, err)
}
} else {
num, err := cfg.Db.Where("id = ?", ip.ID).Update(&ip)
if err != nil {
log.Println("error updating ip address", num, err)
}
} }
log.Println(fmt.Sprintf("zmq: Inserted %d IP, Updated %d IP", numinsert, numupdate))
tmpval := fmt.Sprintf("%s %s", channel, string(val)) tmpval := fmt.Sprintf("%s %s", channel, string(val))
val = []byte(tmpval) val = []byte(tmpval)
topub = append(topub, val) topub = append(topub, val)