package models import ( "context" "database/sql" "fmt" "log" "net" "time" "git.paulbsd.com/paulbsd/ipbl/src/config" ) //var lastday = time.Now().Add(-(time.Hour * 24)) func GetIPs(ctx *context.Context, config *config.Config, limit int) (apiips []*APIIP, err error) { var ips []IP err = config.Db.Limit(limit).Desc("created").Find(&ips) for _, ml := range ips { apiips = append(apiips, ml.APIFormat()) } return } func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (apiips []*APIIP, err error) { var ips []IP err = config.Db.Where("updated >= (now()-?::interval)", interval).GroupBy("ip").Find(&ips) for _, ml := range ips { apiips = append(apiips, ml.APIFormat()) } return } func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (apiip *APIIP, err error) { var ip IP has, err := config.Db.Where("ip = ?", ipquery).Get(&ip) if !has { err = fmt.Errorf("not found") return nil, err } if err != nil { return } apiip = ip.APIFormat() return } func (ip *IP) UpdateRDNS() (result string, err error) { res, err := net.LookupAddr(ip.IP) if err != nil { result = "" } else { result = res[0] } return } 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) (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) } var searchips []IP cfg.Db.In("ip", iplist).Where("hostname = ?", hostname).Find(&searchips) var toupdateips []string for _, ip := range searchips { toupdateips = append(toupdateips, ip.IP) } numupdate, _ = cfg.Db.In("ip", toupdateips).Where("hostname = ?", hostname).Cols("updated").Update(&IP{}) var toinsertip, _ = differ(*ips, searchips) numinsert, err = cfg.Db.Insert(toinsertip) Cleanup(cfg) return*/ } func ScanIP(cfg *config.Config) (err error) { for { orphans := []IP{} if cfg.Db.Where("rdns IS NULL").Asc("ip").Find(&orphans); len(orphans) > 0 { for _, i := range orphans { reverse, _ := i.UpdateRDNS() log.Printf("%s -> \"%s\"\n", i.IP, reverse) i.Rdns.String = reverse i.Rdns.Valid = true _, err = cfg.Db.ID(i.ID).Cols("rdns").Update(&i) if err != nil { log.Println(err) } } } else { time.Sleep(10 * time.Second) } } } func Cleanup(cfg *config.Config) (err error) { results, _ := cfg.Db.Query("select * from ip where ip in (select ip from ip group by ip having count(ip) > 1) and hostname is null order by updated desc;") if len(results) > 0 { _, err := cfg.Db.Query("delete from ip where ip in (select ip from ip group by ip having count(ip) > 1) and hostname is null;") if err != nil { log.Println("error deleting orphans") } } return } func (ip *IP) APIFormat() *APIIP { if ip == nil { return nil } return &APIIP{ IP: ip.IP, Rdns: ip.Rdns.String, Src: ip.Src, Hostname: ip.Hostname.String, } } func (ip *APIIP) APIConvert() *IP { if ip == nil { return nil } return &IP{ IP: ip.IP, Rdns: sql.NullString{String: ip.Rdns, Valid: true}, Src: ip.Src, Hostname: sql.NullString{String: ip.Hostname, Valid: true}, } } type IP struct { ID int `xorm:"pk autoincr"` IP string `xorm:"text notnull unique(ipsrc)" json:"ip"` 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"` Updated time.Time `xorm:"updated notnull"` } type APIIP struct { IP string `json:"ip"` Rdns string `json:"rdns"` Src string `json:"src"` Hostname string `json:"hostname"` } type Src struct { ID int `xorm:"pk autoincr" json:"-"` Src string `xorm:"text notnull unique" json:"src"` }