179 lines
5.5 KiB
C#
179 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections;
|
|
using System.Security;
|
|
using ln.types;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using ln.skyscanner.crawl;
|
|
using ln.snmp.rfc1213;
|
|
using ln.types.odb;
|
|
using ln.types.sync;
|
|
using ln.logging;
|
|
using System.Globalization;
|
|
|
|
namespace ln.skyscanner.entities
|
|
{
|
|
public class GlobalNetwork : Persistent
|
|
{
|
|
[Unsynced]
|
|
public SkyScanner SkyScanner => SkyScanner.Instance;
|
|
[Unsynced]
|
|
public SkyEntities SkyEntities { get; private set; }
|
|
|
|
public IEnumerable<Subnet> Subnets => SkyEntities.subnetCollection;
|
|
public IEnumerable<Node> Nodes => SkyEntities.nodeCollection;
|
|
|
|
public HopMap DefaultHopMap { get; private set; } = new HopMap();
|
|
|
|
private object _updateLock = new object();
|
|
|
|
public GlobalNetwork()
|
|
{
|
|
}
|
|
|
|
public GlobalNetwork(SkyEntities skyEntities)
|
|
{
|
|
SkyEntities = skyEntities;
|
|
}
|
|
|
|
public void EnsureSubnet(CIDR network)
|
|
{
|
|
SkyScanner.Crawler.EnsureSubnet(network);
|
|
|
|
Subnet subnet = SkyEntities.subnetCollection.Where(s => s.Network.Equals(network)).FirstOrDefault();
|
|
if (subnet == null)
|
|
{
|
|
subnet = new Subnet(network);
|
|
SkyEntities.subnetCollection.Upsert(subnet);
|
|
}
|
|
|
|
}
|
|
|
|
public Node GetNode(Guid id)
|
|
{
|
|
return SkyEntities.nodeCollection.Select(id);
|
|
}
|
|
|
|
public Subnet FindSubnetForHost(CIDR host)
|
|
{
|
|
return SkyEntities.subnetCollection.Select(net => net.Network.Contains(host)).FirstOrDefault();
|
|
}
|
|
|
|
public IEnumerable<Node> FindHostsInSubnet(CIDR network)
|
|
{
|
|
return SkyEntities.nodeCollection.Select(node => network.Contains(node.IPAdresses));
|
|
}
|
|
|
|
public IEnumerable<Node> FindNeighbors(Node node)
|
|
{
|
|
return node.Interfaces.SelectMany((intf) => intf.IPs).SelectMany((arg) => FindHostsInSubnet(arg)).Distinct();
|
|
}
|
|
|
|
public Node FindNodeByIP(IEnumerable<CIDR> ips)
|
|
{
|
|
foreach (CIDR ip in ips)
|
|
{
|
|
Node node = FindNodeByIP(ip);
|
|
if (node != null)
|
|
return node;
|
|
}
|
|
return null;
|
|
}
|
|
public Node FindNodeByIP(CIDR ip)
|
|
{
|
|
ip = ip.Host;
|
|
return SkyEntities.nodeCollection.Select(node => ip.In(node.IPAdresses) || ip.In(node.PrimaryIP)).FirstOrDefault();
|
|
}
|
|
|
|
[Unsynced]
|
|
private Queue<CrawledHost> updateQueue = new Queue<CrawledHost>();
|
|
|
|
public void EnqueueUpdate(CrawledHost crawledHost)
|
|
{
|
|
lock (updateQueue)
|
|
{
|
|
if (!updateQueue.Contains(crawledHost))
|
|
updateQueue.Enqueue(crawledHost);
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
lock (updateQueue)
|
|
{
|
|
if (updateQueue.Count == 0)
|
|
return;
|
|
}
|
|
while (true)
|
|
{
|
|
CrawledHost crawledHost = null;
|
|
lock (updateQueue)
|
|
{
|
|
if (updateQueue.Count > 0)
|
|
crawledHost = updateQueue.Dequeue();
|
|
else
|
|
break;
|
|
}
|
|
|
|
Update(crawledHost);
|
|
}
|
|
|
|
//SkyScanner.Entities.DBGlobalNetwork.SavePersistent(SkyScanner.Entities.DBGlobalNetwork.Root);
|
|
}
|
|
|
|
private void Update(CrawledHost crawledHost)
|
|
{
|
|
Logging.Log(LogLevel.DEBUG, "Entity: update {0}", crawledHost.PrimaryIP);
|
|
|
|
Node node = FindNodeByIP(crawledHost.PrimaryIP);
|
|
if (node == null)
|
|
node = FindNodeByIP(crawledHost.IPAddresses);
|
|
|
|
if (node == null)
|
|
{
|
|
node = new Node(crawledHost.PrimaryIP);
|
|
node.Name = crawledHost.Name;
|
|
node.PrimaryMac = crawledHost.PrimaryHWAddr;
|
|
}
|
|
|
|
foreach (String si in crawledHost.GetHint<string[]>("rfc1213.interfaces",new string[0]))
|
|
{
|
|
String[] fields = si.Split(';');
|
|
|
|
NetworkInterface networkInterface = node.GetInterface(fields[0]);
|
|
if (networkInterface == null)
|
|
networkInterface = new NetworkInterface(node, fields[0]);
|
|
|
|
networkInterface.HWAddress = fields[1];
|
|
networkInterface.IPs = fields[2].Split(',').Where((sip)=> !String.Empty.Equals(sip)).Select((sip) => CIDR.Parse(sip)).ToArray();
|
|
|
|
SkyEntities.interfaceCollection.Upsert(networkInterface);
|
|
|
|
foreach (CIDR ip in networkInterface.IPs)
|
|
EnsureSubnet(ip);
|
|
|
|
}
|
|
|
|
node.RemoveURI("ssh");
|
|
if (crawledHost.GetHint<int>("ssh.port", -1) != -1)
|
|
{
|
|
node.AddURI(
|
|
new URI(
|
|
String.Format("ssh://{0}:{1}@{2}:{3}",
|
|
crawledHost.GetHint<string>("ssh.login"),
|
|
crawledHost.GetHint<string>("ssh.password"),
|
|
crawledHost.GetHint<CIDR>("ssh.ip"),
|
|
crawledHost.GetHint<int>("ssh.port")
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
node.LastUpdate = DateTime.Now;
|
|
SkyEntities.nodeCollection.Upsert(node);
|
|
}
|
|
}
|
|
}
|