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"
|
|
|
|
|
2021-12-27 23:52:22 +01:00
|
|
|
"git.paulbsd.com/paulbsd/ipbl/src/api"
|
2021-11-07 20:44:48 +01:00
|
|
|
"git.paulbsd.com/paulbsd/ipbl/src/config"
|
|
|
|
)
|
|
|
|
|
2021-12-30 12:03:26 +01:00
|
|
|
// GetIPs ...
|
|
|
|
func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxes []*api.IP, 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 {
|
|
|
|
apimailboxes = append(apimailboxes, ml.APIFormat())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:03:26 +01:00
|
|
|
// GetIP ...
|
|
|
|
func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (apiip *api.IP, err error) {
|
2021-12-27 23:52:22 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:03:26 +01:00
|
|
|
// UpdateRDNS ...
|
2021-12-27 23:52:22 +01:00
|
|
|
func (i *IP) UpdateRDNS() (result string, err error) {
|
2021-11-07 20:44:48 +01:00
|
|
|
res, err := net.LookupAddr(i.IP)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-30 12:03:26 +01:00
|
|
|
// InsertIP ...
|
2021-12-12 17:33:40 +01:00
|
|
|
func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) {
|
|
|
|
num, err = cfg.Db.Insert(i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:03:26 +01:00
|
|
|
// InsertIPBulk ...
|
2021-12-12 17:33:40 +01:00
|
|
|
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numfail int64, err error) {
|
|
|
|
for _, ip := range *ips {
|
|
|
|
num, err := cfg.Db.Insert(ip)
|
|
|
|
if err != nil {
|
|
|
|
numfail++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numinserts += num
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:03:26 +01:00
|
|
|
// ScanIP ...
|
2021-12-28 07:27:39 +01:00
|
|
|
func ScanIP(cfg *config.Config) (err error) {
|
|
|
|
for {
|
|
|
|
var orphans = []IP{}
|
|
|
|
cfg.Db.Where("rdns IS NULL").Asc("ip").Find(&orphans)
|
|
|
|
if len(orphans) > 0 {
|
|
|
|
for _, i := range orphans {
|
|
|
|
reverse, _ := i.UpdateRDNS()
|
|
|
|
if reverse == "" {
|
2021-12-30 12:03:26 +01:00
|
|
|
log.Printf("Set \"none\" rdns to IP %s\n", i.IP)
|
2021-12-28 07:27:39 +01:00
|
|
|
i.Rdns.String = "none"
|
|
|
|
} else {
|
2021-12-30 12:03:26 +01:00
|
|
|
log.Printf("%s %s\n", i.IP, reverse)
|
2021-12-28 07:27:39 +01:00
|
|
|
i.Rdns.String = reverse
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
time.Sleep(60 * time.Second)
|
2021-12-10 09:24:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 23:52:22 +01:00
|
|
|
// APIFormat returns a JSON formatted object of IP
|
2021-12-30 12:03:26 +01:00
|
|
|
func (ip *IP) APIFormat() *api.IP {
|
|
|
|
if ip == nil {
|
2021-12-27 23:52:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &api.IP{
|
2021-12-30 12:03:26 +01:00
|
|
|
ID: ip.ID,
|
|
|
|
IP: ip.IP,
|
|
|
|
Rdns: ip.Rdns.String,
|
|
|
|
Src: ip.Src,
|
2021-12-27 23:52:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 17:33:40 +01:00
|
|
|
// IP describe IP objects
|
2021-11-07 20:44:48 +01:00
|
|
|
type IP struct {
|
2021-12-12 17:33:40 +01:00
|
|
|
ID int `xorm:"pk autoincr" json:"-"`
|
|
|
|
IP string `xorm:"text notnull unique(ipsrc)" json:"ip"`
|
|
|
|
Rdns sql.NullString `xorm:"text default" json:"rdns"`
|
|
|
|
Src string `xorm:"text notnull unique(ipsrc)" json:"src"`
|
|
|
|
Created time.Time `xorm:"created notnull" json:"-"`
|
|
|
|
Updated time.Time `xorm:"updated notnull" json:"-"`
|
2021-12-11 12:59:43 +01:00
|
|
|
}
|