diff --git a/src/database/main.go b/src/database/main.go index 3c0c36b..f1c876d 100644 --- a/src/database/main.go +++ b/src/database/main.go @@ -20,7 +20,7 @@ func Initialize(ctx *context.Context, cfg *config.Config) (err error) { models.CfgSet{}, models.CfgTrustlist{}, models.CfgZMQ{}, - models.Hostinfo{}} + models.HostInfo{}} cfg.Db, err = xorm.NewEngine(databaseEngine, fmt.Sprintf("%s://%s:%s@%s/%s", diff --git a/src/models/hostinfo.go b/src/models/hostinfo.go index b2e0aa8..fd6a823 100644 --- a/src/models/hostinfo.go +++ b/src/models/hostinfo.go @@ -4,9 +4,25 @@ import ( "time" ) -type Hostinfo struct { - ID int `xorm:"pk autoincr"` - Data string `xorm:"text notnull" json:"data"` - Created time.Time `xorm:"created notnull"` - Updated time.Time `xorm:"updated notnull"` +type HostInfo struct { + ID int `xorm:"pk autoincr"` + IP string `xorm:"text unique" json:"ip"` + RawWhois string `xorm:"text" json:"raw_whois"` + ReverseDNS string `xorm:"text" json:"reverse_dns"` + City string `xorm:"text" json:"city"` + Country string `xorm:"text" json:"country"` + Created time.Time `xorm:"created notnull"` + Updated time.Time `xorm:"updated notnull"` +} + +type ScanResult struct { + Protocol string `json:"proto"` + PortID int `json:"port_id"` + State string `json:"state"` + ServiceName string `json:"service"` +} + +type ASN struct { + ID int `json:"id"` + Name string `json:"name"` } diff --git a/src/models/ip.go b/src/models/ip.go index d1c8d6f..381ce72 100644 --- a/src/models/ip.go +++ b/src/models/ip.go @@ -54,9 +54,9 @@ func (ip *IP) UpdateRDNS() (result string, err error) { res, err := net.LookupAddr(ip.IP) if err != nil { result = "" - } else { - result = res[0] + return } + result = res[0] return } @@ -118,12 +118,12 @@ func ScanIP(cfg *config.Config) (err error) { log.Println(err) } if session.Where("rdns IS NULL").Asc("ip").Limit(ScanLimit).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 = session.ID(i.ID).Cols("rdns").Update(&i) + for _, orphan := range orphans { + reverse, _ := orphan.UpdateRDNS() + log.Printf("%s -> \"%s\"\n", orphan.IP, reverse) + orphan.Rdns.String = reverse + orphan.Rdns.Valid = true + _, err = session.ID(orphan.ID).Cols("rdns").Update(&orphan) if err != nil { session.Rollback() @@ -164,6 +164,8 @@ func (ip *IP) APIFormat() *APIIP { Rdns: ip.Rdns.String, Src: ip.Src, Hostname: ip.Hostname.String, + City: ip.City.String, + Country: ip.Country.String, Date: ip.Updated.Local().String(), } } @@ -172,10 +174,14 @@ func (ip *APIIP) APIConvert() *IP { return nil } return &IP{ - IP: ip.IP, - Rdns: sql.NullString{String: ip.Rdns, Valid: false}, - Src: ip.Src, - Hostname: sql.NullString{String: ip.Hostname, Valid: true}, + IP: ip.IP, + Rdns: sql.NullString{ + String: ip.Rdns, + Valid: false}, + Src: ip.Src, + Hostname: sql.NullString{ + String: ip.Hostname, + Valid: true}, } } @@ -185,6 +191,8 @@ type IP struct { 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"` + City sql.NullString `xorm:"text default '' unique(ipsrc)" json:"city"` + Country sql.NullString `xorm:"text default '' unique(ipsrc)" json:"country"` Created time.Time `xorm:"created notnull"` Updated time.Time `xorm:"updated notnull"` } @@ -194,5 +202,7 @@ type APIIP struct { Rdns string `json:"rdns"` Src string `json:"src"` Hostname string `json:"hostname"` + City string `json:"city"` + Country string `json:"country"` Date string `json:"date"` } diff --git a/src/models/utils.go b/src/models/utils.go index 5d654e9..5329019 100644 --- a/src/models/utils.go +++ b/src/models/utils.go @@ -4,7 +4,7 @@ import "reflect" //const keyname string = "id" -func differ(sl1 []IP, sl2 []IP) (toinsert []IP, err error) { +func Differ(sl1 []IP, sl2 []IP) (toinsert []IP, err error) { var m = make(map[string]IPDiffer) longslice := append(sl1, sl2...) diff --git a/src/routers/funcs.go b/src/routers/funcs.go index 00f42b4..2ee923b 100644 --- a/src/routers/funcs.go +++ b/src/routers/funcs.go @@ -74,12 +74,12 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) { return Result(c, err, msg) }) e.POST("/hostinfo", func(c echo.Context) (err error) { - var hostinfo = models.Hostinfo{} + var hostinfo = models.HostInfo{} err = c.Bind(&hostinfo) if err != nil { return Result(c, err, hostinfo) } - num, err := cfg.Db.Insert(hostinfo) + num, err := cfg.Db.Insert(&hostinfo) msg := fmt.Sprintf("Inserted %v host info (%d)", hostinfo, num) log.Println(msg) return Result(c, err, msg)