|
@@ -1,9 +1,11 @@
|
|
package neighbour
|
|
package neighbour
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "encoding/json"
|
|
"log"
|
|
"log"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
+ "imuslab.com/arozos/mod/database"
|
|
"imuslab.com/arozos/mod/network/mdns"
|
|
"imuslab.com/arozos/mod/network/mdns"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -13,18 +15,37 @@ import (
|
|
Require MDNS Service
|
|
Require MDNS Service
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
+const (
|
|
|
|
+ AutoDeleteRecordTime = int64(2592000) //30 days = 2592000 seconds
|
|
|
|
+)
|
|
|
|
+
|
|
type Discoverer struct {
|
|
type Discoverer struct {
|
|
Host *mdns.MDNSHost
|
|
Host *mdns.MDNSHost
|
|
|
|
+ Database *database.Database
|
|
LastScanningTime int64
|
|
LastScanningTime int64
|
|
NearbyHosts []*mdns.NetworkHost
|
|
NearbyHosts []*mdns.NetworkHost
|
|
d chan bool
|
|
d chan bool
|
|
t *time.Ticker
|
|
t *time.Ticker
|
|
}
|
|
}
|
|
|
|
|
|
-//NEw Discoverer return a nearby Aroz Discover agent
|
|
|
|
-func NewDiscoverer(MDNS *mdns.MDNSHost) Discoverer {
|
|
|
|
|
|
+type HostRecord struct {
|
|
|
|
+ Name string
|
|
|
|
+ Model string
|
|
|
|
+ Version string
|
|
|
|
+ UUID string
|
|
|
|
+ LastSeenIP []string
|
|
|
|
+ MacAddr []string
|
|
|
|
+ LastOnline int64
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//New Discoverer return a nearby Aroz Discover agent
|
|
|
|
+func NewDiscoverer(MDNS *mdns.MDNSHost, Database *database.Database) Discoverer {
|
|
|
|
+ //Create a new table for neighbour records
|
|
|
|
+ Database.NewTable("neighbour")
|
|
|
|
+
|
|
return Discoverer{
|
|
return Discoverer{
|
|
Host: MDNS,
|
|
Host: MDNS,
|
|
|
|
+ Database: Database,
|
|
LastScanningTime: -1,
|
|
LastScanningTime: -1,
|
|
NearbyHosts: []*mdns.NetworkHost{},
|
|
NearbyHosts: []*mdns.NetworkHost{},
|
|
}
|
|
}
|
|
@@ -74,6 +95,60 @@ func (d *Discoverer) UpdateScan(scanDuration int) {
|
|
d.LastScanningTime = time.Now().Unix()
|
|
d.LastScanningTime = time.Now().Unix()
|
|
results := d.Host.Scan(scanDuration, d.Host.Host.Domain)
|
|
results := d.Host.Scan(scanDuration, d.Host.Host.Domain)
|
|
d.NearbyHosts = results
|
|
d.NearbyHosts = results
|
|
|
|
+
|
|
|
|
+ //Record all scanned host into database
|
|
|
|
+ for _, thisHost := range results {
|
|
|
|
+ thisHostIpString := []string{}
|
|
|
|
+ for _, ipaddr := range thisHost.IPv4 {
|
|
|
|
+ thisHostIpString = append(thisHostIpString, ipaddr.String())
|
|
|
|
+ }
|
|
|
|
+ thisHostRecord := HostRecord{
|
|
|
|
+ Name: thisHost.HostName,
|
|
|
|
+ Model: thisHost.Model,
|
|
|
|
+ Version: thisHost.MinorVersion,
|
|
|
|
+ UUID: thisHost.UUID,
|
|
|
|
+ LastSeenIP: thisHostIpString,
|
|
|
|
+ MacAddr: thisHost.MacAddr,
|
|
|
|
+ LastOnline: time.Now().Unix(),
|
|
|
|
+ }
|
|
|
|
+ d.Database.Write("neighbour", thisHost.UUID, thisHostRecord)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (d *Discoverer) GetOfflineHosts() ([]*HostRecord, error) {
|
|
|
|
+ results := []*HostRecord{}
|
|
|
|
+ entries, err := d.Database.ListTable("neighbour")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return results, err
|
|
|
|
+ }
|
|
|
|
+ for _, keypairs := range entries {
|
|
|
|
+ //Get the host record and UUI from the database
|
|
|
|
+ thisHostUUID := string(keypairs[0])
|
|
|
|
+ thisHostRecord := HostRecord{}
|
|
|
|
+ json.Unmarshal(keypairs[1], &thisHostRecord)
|
|
|
|
+
|
|
|
|
+ if time.Now().Unix()-thisHostRecord.LastOnline > AutoDeleteRecordTime {
|
|
|
|
+ //Remove this record
|
|
|
|
+ log.Println("[Neighbour] Removing network host record due to long period offline: " + thisHostUUID + " (" + thisHostRecord.Name + ")")
|
|
|
|
+ d.Database.Delete("neighbour", thisHostUUID)
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ //Check this host is online
|
|
|
|
+ nodeIsOnline := false
|
|
|
|
+ for _, thisOnlineHost := range d.NearbyHosts {
|
|
|
|
+ if thisOnlineHost.UUID == thisHostUUID {
|
|
|
|
+ //This is online node. skip this
|
|
|
|
+ nodeIsOnline = true
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if !nodeIsOnline {
|
|
|
|
+ results = append(results, &thisHostRecord)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return results, nil
|
|
}
|
|
}
|
|
|
|
|
|
func (d *Discoverer) ScannerRunning() bool {
|
|
func (d *Discoverer) ScannerRunning() bool {
|