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 13 kB

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