NTP Analyzer  0.8.2
Analyze the operation of time servers
TimeServerImporter.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Net;
25 using System.Xml;
26 using Ntp.Analyzer.Objects;
27 using Ntp.Common.Log;
28 
29 namespace Ntp.Analyzer.Data.Import
30 {
31  internal sealed class TimeServerImporter
32  {
34  {
35  this.log = log;
36  }
37 
38  private readonly LogBase log;
39 
40  internal TimeServer ParseTable(string table, int orgId)
41  {
42  if (table == null)
43  {
44  log.WriteLine("Not parsing empty HTML.", Severity.Debug);
45  return null;
46  }
47 
48  try
49  {
50  var doc = new XmlDocument();
51  doc.LoadXml(table);
52 
53  XmlNodeList rows = doc.ChildNodes[0].ChildNodes;
54 
55  bool firstRow = true;
56 
57  var entries = new Dictionary<string, string>();
58 
59  foreach (XmlNode node in rows)
60  {
61  if (firstRow)
62  {
63  firstRow = false;
64  continue;
65  }
66 
67  entries.Add(
68  node.ChildNodes[0].InnerText.Trim(),
69  node.ChildNodes[1].InnerText.Trim()
70  );
71  }
72 
73  if (!entries.ContainsKey("IPv6 Address"))
74  {
75  entries.Add("IPv6 Address", string.Empty);
76  }
77  if (!entries.ContainsKey("HostOrganization"))
78  {
79  entries.Add("HostOrganization", string.Empty);
80  }
81  if (!entries.ContainsKey("AutoKeyURL"))
82  {
83  entries.Add("AutoKeyURL", string.Empty);
84  }
85  if (!entries.ContainsKey("SymmetricKeyType"))
86  {
87  entries.Add("SymmetricKeyType", string.Empty);
88  }
89  if (!entries.ContainsKey("SymmetricKeyURL"))
90  {
91  entries.Add("SymmetricKeyURL", string.Empty);
92  }
93 
94  string stratusStr = entries["ServerStratum"];
95  string country = entries["CountryCode"];
96  string name = entries["Hostname"];
97  string ip = entries["IP Address"];
98  string ip6 = entries["IPv6 Address"];
99  string useDnsStr = entries["UseDNS"];
100  string poolMemberStr = entries["PoolMember"];
101  string location = entries["ServerLocation"];
102  string display = entries["ServerLocation"];
103  string org = entries["HostOrganization"];
104  string geo = entries["GeographicCoordinates"];
105  string server = entries["ServerSynchronization"];
106  string serviceArea = entries["ServiceArea"];
107  string accessPolicy = entries["AccessPolicy"];
108  string accessDetails = entries["AccessDetails"];
109  string notificationStr = entries["NotificationMessage"];
110  string autoKey = entries["AutoKeyURL"];
111  string symKey = entries["SymmetricKeyType"];
112  string symUrl = entries["SymmetricKeyURL"];
113  string contact = entries["ServerContact"];
114 
115  int stratus;
116 
117  if (stratusStr == "StratumOne")
118  {
119  stratus = 1;
120  }
121  else if (stratusStr == "StratumTwo")
122  {
123  stratus = 2;
124  }
125  else
126  {
127  stratus = 19;
128  }
129 
130  IPAddress address;
131  IPAddress.TryParse(ip, out address);
132 
133  bool? useDns = GetBool(useDnsStr);
134  bool? poolMember = GetBool(poolMemberStr);
135  bool? notification = GetBool(notificationStr);
136 
137  return new CalgaryTimeServer(
138  orgId, stratus, country, name, address, ip6, useDns, poolMember, location,
139  display, org, geo, server, serviceArea, accessDetails, accessPolicy,
140  notification, autoKey, symKey, symUrl, contact, null, null, DateTime.Now);
141  }
142  catch (Exception e)
143  {
144  log.WriteLine("Could not parse TimeServer HTML.", Severity.Error);
145  log.WriteLine(e);
146  return null;
147  }
148  }
149 
150  private bool? GetBool(string boolString)
151  {
152  string test = boolString.Trim().ToLower();
153 
154  if (test == "yes")
155  return true;
156  if (test == "no")
157  return false;
158  if (test == string.Empty)
159  return null;
160 
161  throw new ApplicationException("Unknown boolean type.");
162  }
163  }
164 }
var e
Definition: bootstrap.min.js:6
TimeServer ParseTable(string table, int orgId)