updated ipbl with some functions reliability
This commit is contained in:
parent
551b3311a6
commit
d8ab04aedf
@ -45,8 +45,8 @@ func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (ap
|
||||
return
|
||||
}
|
||||
|
||||
func (i *IP) UpdateRDNS() (result string, err error) {
|
||||
res, err := net.LookupAddr(i.IP)
|
||||
func (ip *IP) UpdateRDNS() (result string, err error) {
|
||||
res, err := net.LookupAddr(ip.IP)
|
||||
if err != nil {
|
||||
result = ""
|
||||
} else {
|
||||
@ -55,13 +55,30 @@ func (i *IP) UpdateRDNS() (result string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) {
|
||||
num, err = cfg.Db.Insert(i)
|
||||
func (ip *IP) InsertOrUpdate(cfg *config.Config) (numinsert int64, numupdate int64, err error) {
|
||||
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
|
||||
}
|
||||
|
||||
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) {
|
||||
var iplist []string
|
||||
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinsert int64, numupdate int64, err error) {
|
||||
for _, ip := range *ips {
|
||||
numinsert, numupdate, err = ip.InsertOrUpdate(cfg)
|
||||
}
|
||||
Cleanup(cfg)
|
||||
return
|
||||
/*var iplist []string
|
||||
hostname := (*ips)[0].Hostname.String
|
||||
for _, ip := range *ips {
|
||||
iplist = append(iplist, ip.IP)
|
||||
@ -74,14 +91,14 @@ func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates i
|
||||
for _, ip := range searchips {
|
||||
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)
|
||||
numinserts, err = cfg.Db.Insert(toinsertip)
|
||||
numinsert, err = cfg.Db.Insert(toinsertip)
|
||||
|
||||
Cleanup(cfg)
|
||||
|
||||
return
|
||||
return*/
|
||||
}
|
||||
|
||||
func ScanIP(cfg *config.Config) (err error) {
|
||||
@ -139,13 +156,13 @@ func (ip *APIIP) APIConvert() *IP {
|
||||
}
|
||||
|
||||
type IP struct {
|
||||
ID int `xorm:"pk autoincr" json:"-"`
|
||||
ID int `xorm:"pk autoincr"`
|
||||
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"`
|
||||
Hostname sql.NullString `xorm:"text default '' unique(ipsrc)" json:"hostname"`
|
||||
Created time.Time `xorm:"created notnull" json:"-"`
|
||||
Updated time.Time `xorm:"updated notnull" json:"-"`
|
||||
Created time.Time `xorm:"created notnull"`
|
||||
Updated time.Time `xorm:"updated notnull"`
|
||||
}
|
||||
|
||||
type APIIP struct {
|
||||
|
@ -33,14 +33,13 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
|
||||
if err != nil {
|
||||
return Result(c, fmt.Errorf("error when parsing body"), "")
|
||||
}
|
||||
num, err := ip.InsertIP(cfg)
|
||||
numinsert, numupdate, err := ip.InsertOrUpdate(cfg)
|
||||
fmt.Println(err)
|
||||
if err != nil {
|
||||
return Result(c, err, "")
|
||||
}
|
||||
if num > 0 {
|
||||
msg = fmt.Sprintf("Inserted %d IP", num)
|
||||
}
|
||||
msg = fmt.Sprintf("zmq: Inserted %d IP, Updated %d IP", numinsert, numupdate)
|
||||
log.Println(msg)
|
||||
return Result(c, err, msg)
|
||||
})
|
||||
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 {
|
||||
ips = append(ips, *v.APIConvert())
|
||||
}
|
||||
numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips)
|
||||
if numinsert > 0 {
|
||||
msg = fmt.Sprintf("inserted %d IP", numinsert)
|
||||
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)
|
||||
}
|
||||
numinsert, numupdate, _ := models.InsertIPBulk(cfg, &ips)
|
||||
msg = fmt.Sprintf("zmq: Inserted %d IP, Updated %d IP", numinsert, numupdate)
|
||||
log.Println(msg)
|
||||
return Result(c, err, msg)
|
||||
})
|
||||
e.GET("/config/trustlist", func(c echo.Context) (err error) {
|
||||
|
@ -36,28 +36,19 @@ func Handle(cfg *config.Config, reqsock *goczmq.Sock, pubsock *goczmq.Sock, chan
|
||||
}
|
||||
var topub [][]byte
|
||||
for _, val := range req {
|
||||
var apiip = models.APIIP{}
|
||||
var ip = models.IP{}
|
||||
err = json.Unmarshal(val, &ip)
|
||||
err = json.Unmarshal(val, &apiip)
|
||||
if err != nil {
|
||||
log.Println("unable to parse ip address", err)
|
||||
continue
|
||||
}
|
||||
has, err := cfg.Db.Get(&ip)
|
||||
ip = *apiip.APIConvert()
|
||||
numinsert, numupdate, err := ip.InsertOrUpdate(cfg)
|
||||
if err != nil {
|
||||
log.Println("error fetching ip address", 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(err)
|
||||
}
|
||||
log.Println(fmt.Sprintf("zmq: Inserted %d IP, Updated %d IP", numinsert, numupdate))
|
||||
tmpval := fmt.Sprintf("%s %s", channel, string(val))
|
||||
val = []byte(tmpval)
|
||||
topub = append(topub, val)
|
||||
|
Loading…
Reference in New Issue
Block a user