You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AvailabilityStatistics.cs 12 kB

9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Net.NetworkInformation;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using Shadowsocks.Model;
  13. using Shadowsocks.Util;
  14. namespace Shadowsocks.Controller
  15. {
  16. using DataUnit = KeyValuePair<string, string>;
  17. using DataList = List<KeyValuePair<string, string>>;
  18. using Statistics = Dictionary<string, List<AvailabilityStatistics.RawStatisticsData>>;
  19. public class AvailabilityStatistics
  20. {
  21. public static readonly string DateTimePattern = "yyyy-MM-dd HH:mm:ss";
  22. private const string StatisticsFilesName = "shadowsocks.availability.csv";
  23. private const string Delimiter = ",";
  24. private const int Timeout = 500;
  25. private const int DelayBeforeStart = 1000;
  26. public Statistics RawStatistics { get; private set; }
  27. public Statistics FilteredStatistics { get; private set; }
  28. public static readonly DateTime UnknownDateTime = new DateTime(1970, 1, 1);
  29. private int Repeat => _config.RepeatTimesNum;
  30. private const int RetryInterval = 2 * 60 * 1000; //retry 2 minutes after failed
  31. private int Interval => (int)TimeSpan.FromMinutes(_config.DataCollectionMinutes).TotalMilliseconds;
  32. private Timer _timer;
  33. private State _state;
  34. private List<Server> _servers;
  35. private StatisticsStrategyConfiguration _config;
  36. public static string AvailabilityStatisticsFile;
  37. //static constructor to initialize every public static fields before refereced
  38. static AvailabilityStatistics()
  39. {
  40. AvailabilityStatisticsFile = Utils.GetTempPath(StatisticsFilesName);
  41. }
  42. public AvailabilityStatistics(Configuration config, StatisticsStrategyConfiguration statisticsConfig)
  43. {
  44. UpdateConfiguration(config, statisticsConfig);
  45. }
  46. public bool Set(StatisticsStrategyConfiguration config)
  47. {
  48. _config = config;
  49. try
  50. {
  51. if (config.StatisticsEnabled)
  52. {
  53. if (_timer?.Change(DelayBeforeStart, Interval) == null)
  54. {
  55. _state = new State();
  56. _timer = new Timer(Run, _state, DelayBeforeStart, Interval);
  57. }
  58. }
  59. else
  60. {
  61. _timer?.Dispose();
  62. }
  63. return true;
  64. }
  65. catch (Exception e)
  66. {
  67. Logging.LogUsefulException(e);
  68. return false;
  69. }
  70. }
  71. //hardcode
  72. //TODO: backup reliable isp&geolocation provider or a local database is required
  73. public static async Task<DataList> GetGeolocationAndIsp()
  74. {
  75. Logging.Debug("Retrive information of geolocation and isp");
  76. const string API = "http://ip-api.com/json";
  77. const string alternativeAPI = "http://www.telize.com/geoip"; //must be comptible with current API
  78. var result = await GetInfoFromAPI(API);
  79. if (result != null) return result;
  80. result = await GetInfoFromAPI(alternativeAPI);
  81. if (result != null) return result;
  82. return new DataList
  83. {
  84. new DataUnit(State.Geolocation, State.Unknown),
  85. new DataUnit(State.ISP, State.Unknown)
  86. };
  87. }
  88. private static async Task<DataList> GetInfoFromAPI(string API)
  89. {
  90. string jsonString;
  91. try
  92. {
  93. jsonString = await new HttpClient().GetStringAsync(API);
  94. }
  95. catch (HttpRequestException e)
  96. {
  97. Logging.LogUsefulException(e);
  98. return null;
  99. }
  100. dynamic obj;
  101. if (!SimpleJson.SimpleJson.TryDeserializeObject(jsonString, out obj)) return null;
  102. string country = obj["country"];
  103. string city = obj["city"];
  104. string isp = obj["isp"];
  105. if (country == null || city == null || isp == null) return null;
  106. return new DataList {
  107. new DataUnit(State.Geolocation, $"\"{country} {city}\""),
  108. new DataUnit(State.ISP, $"\"{isp}\"")
  109. };
  110. }
  111. private async Task<List<DataList>> ICMPTest(Server server)
  112. {
  113. Logging.Debug("Ping " + server.FriendlyName());
  114. if (server.server == "") return null;
  115. var IP = Dns.GetHostAddresses(server.server).First(ip => (ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6));
  116. var ping = new Ping();
  117. var ret = new List<DataList>();
  118. foreach (var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString(DateTimePattern)))
  119. {
  120. //ICMP echo. we can also set options and special bytes
  121. try
  122. {
  123. var reply = await ping.SendTaskAsync(IP, Timeout);
  124. ret.Add(new List<KeyValuePair<string, string>>
  125. {
  126. new KeyValuePair<string, string>("Timestamp", timestamp),
  127. new KeyValuePair<string, string>("Server", server.FriendlyName()),
  128. new KeyValuePair<string, string>("Status", reply?.Status.ToString()),
  129. new KeyValuePair<string, string>("RoundtripTime", reply?.RoundtripTime.ToString())
  130. //new KeyValuePair<string, string>("data", reply.Buffer.ToString()); // The data of reply
  131. });
  132. Thread.Sleep(Timeout + new Random().Next() % Timeout);
  133. //Do ICMPTest in a random frequency
  134. }
  135. catch (Exception e)
  136. {
  137. Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
  138. Logging.LogUsefulException(e);
  139. }
  140. }
  141. return ret;
  142. }
  143. private void Run(object obj)
  144. {
  145. LoadRawStatistics();
  146. FilterRawStatistics();
  147. evaluate();
  148. }
  149. private async void evaluate()
  150. {
  151. var geolocationAndIsp = GetGeolocationAndIsp();
  152. foreach (var dataLists in await TaskEx.WhenAll(_servers.Select(ICMPTest)))
  153. {
  154. if (dataLists == null) continue;
  155. foreach (var dataList in dataLists.Where(dataList => dataList != null))
  156. {
  157. await geolocationAndIsp;
  158. Append(dataList, geolocationAndIsp.Result);
  159. }
  160. }
  161. }
  162. private static void Append(DataList dataList, IEnumerable<DataUnit> extra)
  163. {
  164. var data = dataList.Concat(extra);
  165. var dataLine = string.Join(Delimiter, data.Select(kv => kv.Value).ToArray());
  166. string[] lines;
  167. if (!File.Exists(AvailabilityStatisticsFile))
  168. {
  169. var headerLine = string.Join(Delimiter, data.Select(kv => kv.Key).ToArray());
  170. lines = new[] { headerLine, dataLine };
  171. }
  172. else
  173. {
  174. lines = new[] { dataLine };
  175. }
  176. try
  177. {
  178. File.AppendAllLines(AvailabilityStatisticsFile, lines);
  179. }
  180. catch (IOException e)
  181. {
  182. Logging.LogUsefulException(e);
  183. }
  184. }
  185. internal void UpdateConfiguration(Configuration config, StatisticsStrategyConfiguration statisticsConfig)
  186. {
  187. Set(statisticsConfig);
  188. _servers = config.configs;
  189. }
  190. private async void FilterRawStatistics()
  191. {
  192. if (RawStatistics == null) return;
  193. if (FilteredStatistics == null)
  194. {
  195. FilteredStatistics = new Statistics();
  196. }
  197. foreach (IEnumerable<RawStatisticsData> rawData in RawStatistics.Values)
  198. {
  199. var filteredData = rawData;
  200. if (_config.ByIsp)
  201. {
  202. var current = await GetGeolocationAndIsp();
  203. filteredData =
  204. filteredData.Where(
  205. data =>
  206. data.Geolocation == current[0].Value ||
  207. data.Geolocation == State.Unknown);
  208. filteredData =
  209. filteredData.Where(
  210. data => data.ISP == current[1].Value || data.ISP == State.Unknown);
  211. if (filteredData.LongCount() == 0) return;
  212. }
  213. if (_config.ByHourOfDay)
  214. {
  215. var currentHour = DateTime.Now.Hour;
  216. filteredData = filteredData.Where(data =>
  217. data.Timestamp != UnknownDateTime && data.Timestamp.Hour.Equals(currentHour)
  218. );
  219. if (filteredData.LongCount() == 0) return;
  220. }
  221. var dataList = filteredData as List<RawStatisticsData> ?? filteredData.ToList();
  222. var serverName = dataList[0].ServerName;
  223. FilteredStatistics[serverName] = dataList;
  224. }
  225. }
  226. private void LoadRawStatistics()
  227. {
  228. try
  229. {
  230. var path = AvailabilityStatisticsFile;
  231. Logging.Debug($"loading statistics from {path}");
  232. if (!File.Exists(path))
  233. {
  234. Console.WriteLine($"statistics file does not exist, try to reload {RetryInterval / 60 / 1000} minutes later");
  235. _timer.Change(RetryInterval, Interval);
  236. return;
  237. }
  238. RawStatistics = (from l in File.ReadAllLines(path).Skip(1)
  239. let strings = l.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
  240. let rawData = new RawStatisticsData
  241. {
  242. Timestamp = ParseExactOrUnknown(strings[0]),
  243. ServerName = strings[1],
  244. ICMPStatus = strings[2],
  245. RoundtripTime = int.Parse(strings[3]),
  246. Geolocation = 5 > strings.Length ?
  247. null
  248. : strings[4],
  249. ISP = 6 > strings.Length ? null : strings[5]
  250. }
  251. group rawData by rawData.ServerName into server
  252. select new
  253. {
  254. ServerName = server.Key,
  255. data = server.ToList()
  256. }).ToDictionary(server => server.ServerName, server => server.data);
  257. }
  258. catch (Exception e)
  259. {
  260. Logging.LogUsefulException(e);
  261. }
  262. }
  263. private DateTime ParseExactOrUnknown(string str)
  264. {
  265. DateTime dateTime;
  266. return !DateTime.TryParseExact(str, DateTimePattern, null, DateTimeStyles.None, out dateTime) ? UnknownDateTime : dateTime;
  267. }
  268. public class State
  269. {
  270. public DataList dataList = new DataList();
  271. public const string Geolocation = "Geolocation";
  272. public const string ISP = "ISP";
  273. public const string Unknown = "Unknown";
  274. }
  275. public class RawStatisticsData
  276. {
  277. public DateTime Timestamp;
  278. public string ServerName;
  279. public string ICMPStatus;
  280. public int RoundtripTime;
  281. public string Geolocation;
  282. public string ISP;
  283. }
  284. public class StatisticsData
  285. {
  286. public float PackageLoss;
  287. public int AverageResponse;
  288. public int MinResponse;
  289. public int MaxResponse;
  290. }
  291. }
  292. }