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

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.NetworkInformation;
  6. using System.Threading;
  7. using Shadowsocks.Model;
  8. using System.Reflection;
  9. using Shadowsocks.Util;
  10. namespace Shadowsocks.Controller
  11. {
  12. class AvailabilityStatistics
  13. {
  14. private static readonly string StatisticsFilesName = "shadowsocks.availability.csv";
  15. private static readonly string Delimiter = ",";
  16. private static readonly int Timeout = 500;
  17. private static readonly int Repeat = 4; //repeat times every evaluation
  18. private static readonly int Interval = 10 * 60 * 1000; //evaluate proxies every 15 minutes
  19. private Timer timer = null;
  20. private State state = null;
  21. private List<Server> servers;
  22. public static string AvailabilityStatisticsFile;
  23. //static constructor to initialize every public static fields before refereced
  24. static AvailabilityStatistics()
  25. {
  26. string temppath = Utils.GetTempPath();
  27. AvailabilityStatisticsFile = Path.Combine(temppath, StatisticsFilesName);
  28. }
  29. public bool Set(bool enabled)
  30. {
  31. try
  32. {
  33. if (enabled)
  34. {
  35. if (timer?.Change(0, Interval) == null)
  36. {
  37. state = new State();
  38. timer = new Timer(Evaluate, state, 0, Interval);
  39. }
  40. }
  41. else
  42. {
  43. timer?.Dispose();
  44. }
  45. return true;
  46. }
  47. catch (Exception e)
  48. {
  49. Logging.LogUsefulException(e);
  50. return false;
  51. }
  52. }
  53. private void Evaluate(object obj)
  54. {
  55. Ping ping = new Ping();
  56. State state = (State) obj;
  57. foreach (var server in servers)
  58. {
  59. Logging.Debug("eveluating " + server.FriendlyName());
  60. foreach (var _ in Enumerable.Range(0, Repeat))
  61. {
  62. //TODO: do simple analyze of data to provide friendly message, like package loss.
  63. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  64. //ICMP echo. we can also set options and special bytes
  65. //seems no need to use SendPingAsync
  66. try
  67. {
  68. PingReply reply = ping.Send(server.server, Timeout);
  69. state.data = new List<KeyValuePair<string, string>>();
  70. state.data.Add(new KeyValuePair<string, string>("Timestamp", timestamp));
  71. state.data.Add(new KeyValuePair<string, string>("Server", server.FriendlyName()));
  72. state.data.Add(new KeyValuePair<string, string>("Status", reply.Status.ToString()));
  73. state.data.Add(new KeyValuePair<string, string>("RoundtripTime", reply.RoundtripTime.ToString()));
  74. //state.data.Add(new KeyValuePair<string, string>("data", reply.Buffer.ToString())); // The data of reply
  75. Append(state.data);
  76. }
  77. catch (Exception e)
  78. {
  79. Console.WriteLine($"An exception occured when eveluating {server.FriendlyName()}");
  80. Logging.LogUsefulException(e);
  81. }
  82. }
  83. }
  84. }
  85. private static void Append(List<KeyValuePair<string, string>> data)
  86. {
  87. string dataLine = string.Join(Delimiter, data.Select(kv => kv.Value).ToArray());
  88. string[] lines;
  89. if (!File.Exists(AvailabilityStatisticsFile))
  90. {
  91. string headerLine = string.Join(Delimiter, data.Select(kv => kv.Key).ToArray());
  92. lines = new string[] { headerLine, dataLine };
  93. }
  94. else
  95. {
  96. lines = new string[] { dataLine };
  97. }
  98. File.AppendAllLines(AvailabilityStatisticsFile, lines);
  99. }
  100. internal void UpdateConfiguration(Configuration _config)
  101. {
  102. Set(_config.availabilityStatistics);
  103. servers = _config.configs;
  104. }
  105. private class State
  106. {
  107. public List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();
  108. }
  109. }
  110. }