ipbl/src/models/ip.go

179 lines
4.3 KiB
Go
Raw Normal View History

2021-12-27 23:52:22 +01:00
package models
2021-11-07 20:44:48 +01:00
import (
2021-12-27 23:52:22 +01:00
"context"
2021-12-12 17:33:40 +01:00
"database/sql"
2021-12-10 09:24:27 +01:00
"fmt"
2021-12-12 17:33:40 +01:00
"log"
2021-11-07 20:44:48 +01:00
"net"
"time"
"git.paulbsd.com/paulbsd/ipbl/src/config"
)
2022-03-14 18:49:28 +01:00
//var lastday = time.Now().Add(-(time.Hour * 24))
2022-03-19 14:48:50 +01:00
func GetIPs(ctx *context.Context, config *config.Config, limit int) (apiips []*APIIP, err error) {
2021-12-27 23:52:22 +01:00
var ips []IP
2022-01-02 18:19:56 +01:00
err = config.Db.Limit(limit).Desc("created").Find(&ips)
2021-12-27 23:52:22 +01:00
for _, ml := range ips {
2022-03-19 14:48:50 +01:00
apiips = append(apiips, ml.APIFormat())
2021-12-27 23:52:22 +01:00
}
return
}
2022-03-19 14:48:50 +01:00
func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (apiips []*APIIP, err error) {
2022-01-30 18:38:03 +01:00
var ips []IP
2022-03-14 18:49:28 +01:00
err = config.Db.Where("updated >= (now()-?::interval)", interval).GroupBy("ip").Find(&ips)
2022-01-30 18:38:03 +01:00
for _, ml := range ips {
2022-03-19 14:48:50 +01:00
apiips = append(apiips, ml.APIFormat())
2022-01-30 18:38:03 +01:00
}
return
}
2022-03-19 14:48:50 +01:00
func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (apiip *APIIP, err error) {
2021-12-27 23:52:22 +01:00
var ip IP
2022-03-14 18:49:28 +01:00
has, err := config.Db.Where("ip = ?", ipquery).Get(&ip)
if !has {
2022-02-07 23:08:22 +01:00
err = fmt.Errorf("not found")
2021-12-27 23:52:22 +01:00
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)
2021-11-07 20:44:48 +01:00
if err != nil {
2021-12-27 23:52:22 +01:00
result = ""
} else {
result = res[0]
2021-11-07 20:44:48 +01:00
}
2021-12-27 23:52:22 +01:00
return
2021-11-07 20:44:48 +01:00
}
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)
}
}
2021-12-12 17:33:40 +01:00
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
2022-03-19 16:21:08 +01:00
hostname := (*ips)[0].Hostname.String
2021-12-12 17:33:40 +01:00
for _, ip := range *ips {
iplist = append(iplist, ip.IP)
}
var searchips []IP
2022-03-19 14:48:50 +01:00
cfg.Db.In("ip", iplist).Where("hostname = ?", hostname).Find(&searchips)
var toupdateips []string
for _, ip := range searchips {
toupdateips = append(toupdateips, ip.IP)
2021-12-12 17:33:40 +01:00
}
numupdate, _ = cfg.Db.In("ip", toupdateips).Where("hostname = ?", hostname).Cols("updated").Update(&IP{})
var toinsertip, _ = differ(*ips, searchips)
numinsert, err = cfg.Db.Insert(toinsertip)
2022-03-19 14:48:50 +01:00
Cleanup(cfg)
return*/
2021-12-12 17:33:40 +01:00
}
2021-12-28 07:27:39 +01:00
func ScanIP(cfg *config.Config) (err error) {
for {
2022-02-07 23:08:22 +01:00
orphans := []IP{}
if cfg.Db.Where("rdns IS NULL").Asc("ip").Find(&orphans); len(orphans) > 0 {
2021-12-28 07:27:39 +01:00
for _, i := range orphans {
reverse, _ := i.UpdateRDNS()
2022-02-23 11:17:21 +01:00
log.Printf("%s -> \"%s\"\n", i.IP, reverse)
i.Rdns.String = reverse
2021-12-28 07:27:39 +01:00
i.Rdns.Valid = true
_, err = cfg.Db.ID(i.ID).Cols("rdns").Update(&i)
if err != nil {
log.Println(err)
2021-12-10 09:24:27 +01:00
}
}
2021-12-28 07:27:39 +01:00
} else {
2022-03-13 12:08:44 +01:00
time.Sleep(10 * time.Second)
2021-12-10 09:24:27 +01:00
}
}
}
2022-03-19 14:48:50 +01:00
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 {
2021-12-30 12:03:26 +01:00
if ip == nil {
2021-12-27 23:52:22 +01:00
return nil
}
2022-03-19 14:48:50 +01:00
return &APIIP{
2022-03-13 12:08:44 +01:00
IP: ip.IP,
Rdns: ip.Rdns.String,
Src: ip.Src,
2022-03-19 14:48:50 +01:00
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},
2021-12-27 23:52:22 +01:00
}
}
2021-11-07 20:44:48 +01:00
type IP struct {
ID int `xorm:"pk autoincr"`
2022-03-13 12:08:44 +01:00
IP string `xorm:"text notnull unique(ipsrc)" json:"ip"`
Rdns sql.NullString `xorm:"text default"`
2022-03-13 12:08:44 +01:00
Src string `xorm:"text notnull unique(ipsrc)" json:"src"`
2022-03-19 14:48:50 +01:00
Hostname sql.NullString `xorm:"text default '' unique(ipsrc)" json:"hostname"`
Created time.Time `xorm:"created notnull"`
Updated time.Time `xorm:"updated notnull"`
2021-12-11 12:59:43 +01:00
}
2022-03-19 14:48:50 +01:00
type APIIP struct {
IP string `json:"ip"`
Rdns string `json:"rdns"`
Src string `json:"src"`
Hostname string `json:"hostname"`
}
2022-03-19 16:21:08 +01:00
type Src struct {
ID int `xorm:"pk autoincr" json:"-"`
Src string `xorm:"text notnull unique" json:"src"`
}