updated messaging system
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2023-01-15 16:04:35 +01:00
parent 9244e05fc3
commit 4bada1c166
3 changed files with 30 additions and 21 deletions

View File

@ -16,23 +16,27 @@ func (event *Event) Insert(cfg *config.Config) (err error) {
return return
} }
/*
func (event *Event) APIFormat() *APIEvent { func (event *Event) APIFormat() *APIEvent {
if event == nil { if event == nil {
return nil return nil
} }
return &APIEvent{ return &APIEvent{
IP: event.IP.IP, MSgType: "none",
Src: event.Src, IPData: {
Hostname: event.Hostname.String, IP: event.IP.IP,
Src: event.Src,
Hostname: event.Hostname.String,
},
} }
} }*/
func (event *Event) APIParse(session *xorm.Session, apievent APIEvent) (err error) { func (event *Event) APIParse(session *xorm.Session, apievent APIEvent) (err error) {
*event = Event{ *event = Event{
IP: &IP{IP: apievent.IP}, IP: &IP{IP: apievent.IPData.IP},
Src: apievent.Src, Src: apievent.IPData.Src,
Hostname: sql.NullString{ Hostname: sql.NullString{
String: apievent.Hostname, String: apievent.IPData.Hostname,
Valid: true}, Valid: true},
} }
event.IP.GetOrCreate(session) event.IP.GetOrCreate(session)
@ -49,9 +53,12 @@ type Event struct {
} }
type APIEvent struct { type APIEvent struct {
IP string `json:"ip"` MsgType string `json:"msgtype"`
Src string `json:"src"` IPData struct {
Hostname string `json:"hostname"` IP string `json:"ip"`
Mode string `json:"mode"` Src string `json:"src"`
Created string `json:"created"` Hostname string `json:"hostname"`
Mode string `json:"mode"`
Created string `json:"created"`
} `json:"ipdata"`
} }

View File

@ -14,7 +14,9 @@ import (
"xorm.io/xorm" "xorm.io/xorm"
) )
const ScanLimit = 10 const SCANLIMIT = 10
const IPINFO_WS = "https://ip.paulbsd.com"
var lastday = time.Now().Add(-(time.Hour * 24)) var lastday = time.Now().Add(-(time.Hour * 24))
@ -130,10 +132,11 @@ func InsertIPBulk(session *xorm.Session, ips *[]IP) (numinsert int64, numupdate
} }
func ScanIP(cfg *config.Config) (err error) { func ScanIP(cfg *config.Config) (err error) {
queryclient := http.Client{}
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 city_id is null or rdns = '') and updated < now()-'1d'::interval").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 {
@ -142,7 +145,7 @@ func ScanIP(cfg *config.Config) (err error) {
log.Println(err) log.Println(err)
} }
for _, orphan := range orphans { for _, orphan := range orphans {
query, err := QueryInfo(orphan.IP) query, err := QueryInfo(&queryclient, orphan.IP)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
continue continue
@ -181,9 +184,8 @@ func ScanIP(cfg *config.Config) (err error) {
} }
} }
func QueryInfo(ip string) (query QueryIP, err error) { func QueryInfo(client *http.Client, ip string) (query QueryIP, err error) {
client := http.Client{} var url = fmt.Sprintf("%s/%s", IPINFO_WS, ip)
var url = fmt.Sprintf("https://ip.paulbsd.com/%s", ip)
req, _ := http.NewRequest("GET", url, nil) req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "*/*") req.Header.Add("Accept", "*/*")
req.Header.Add("User-Agent", "ipbl") req.Header.Add("User-Agent", "ipbl")

View File

@ -51,11 +51,11 @@ func Handle(cfg *config.Config, reqsock *goczmq.Sock, pubsock *goczmq.Sock, chan
log.Println("unable to parse ip address", err) log.Println("unable to parse ip address", err)
continue continue
} }
if apievent.IP != "" && apievent.IP == lastip { if apievent.IPData.IP != "" && apievent.IPData.IP == lastip {
continue continue
} }
if apievent.Mode != "init" { if apievent.MsgType != "init" {
session := cfg.Db.NewSession() session := cfg.Db.NewSession()
event.APIParse(session, apievent) event.APIParse(session, apievent)
session.Close() session.Close()
@ -69,7 +69,7 @@ func Handle(cfg *config.Config, reqsock *goczmq.Sock, pubsock *goczmq.Sock, chan
tmpval := fmt.Sprintf("%s %s", channel, string(val)) tmpval := fmt.Sprintf("%s %s", channel, string(val))
ipjson := []byte(tmpval) ipjson := []byte(tmpval)
topub = append(topub, ipjson) topub = append(topub, ipjson)
lastip = apievent.IP lastip = apievent.IPData.IP
} }
err = pubsock.SendMessage(topub) err = pubsock.SendMessage(topub)