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.

Server.cs 7.0 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Text;
  5. using System.Web;
  6. using Shadowsocks.Controller;
  7. using System.Text.RegularExpressions;
  8. namespace Shadowsocks.Model
  9. {
  10. [Serializable]
  11. public class Server
  12. {
  13. public const string DefaultMethod = "chacha20-ietf-poly1305";
  14. public const int DefaultPort = 8388;
  15. #region ParseLegacyURL
  16. public static readonly Regex
  17. UrlFinder = new Regex(@"ss://(?<base64>[A-Za-z0-9+-/=_]+)(?:#(?<tag>\S+))?", RegexOptions.IgnoreCase),
  18. DetailsParser = new Regex(@"^((?<method>.+?):(?<password>.*)@(?<hostname>.+?):(?<port>\d+?))$", RegexOptions.IgnoreCase);
  19. #endregion ParseLegacyURL
  20. private const int DefaultServerTimeoutSec = 5;
  21. public const int MaxServerTimeoutSec = 20;
  22. public string server;
  23. public int server_port;
  24. public string password;
  25. public string method;
  26. public string plugin;
  27. public string plugin_opts;
  28. public string plugin_args;
  29. public string remarks;
  30. public int timeout;
  31. public override int GetHashCode()
  32. {
  33. return server.GetHashCode() ^ server_port;
  34. }
  35. public override bool Equals(object obj)
  36. {
  37. Server o2 = (Server)obj;
  38. return server == o2.server && server_port == o2.server_port;
  39. }
  40. public string FriendlyName()
  41. {
  42. if (server.IsNullOrEmpty())
  43. {
  44. return I18N.GetString("New server");
  45. }
  46. string serverStr = $"{FormatHostName(server)}:{server_port}";
  47. return remarks.IsNullOrEmpty()
  48. ? serverStr
  49. : $"{remarks} ({serverStr})";
  50. }
  51. public string FormatHostName(string hostName)
  52. {
  53. // CheckHostName() won't do a real DNS lookup
  54. switch (Uri.CheckHostName(hostName))
  55. {
  56. case UriHostNameType.IPv6: // Add square bracket when IPv6 (RFC3986)
  57. return $"[{hostName}]";
  58. default: // IPv4 or domain name
  59. return hostName;
  60. }
  61. }
  62. public Server()
  63. {
  64. server = "";
  65. server_port = DefaultPort;
  66. method = DefaultMethod;
  67. plugin = "";
  68. plugin_opts = "";
  69. plugin_args = "";
  70. password = "";
  71. remarks = "";
  72. timeout = DefaultServerTimeoutSec;
  73. }
  74. private static Server ParseLegacyURL(string ssURL)
  75. {
  76. var match = UrlFinder.Match(ssURL);
  77. if (!match.Success)
  78. return null;
  79. Server server = new Server();
  80. var base64 = match.Groups["base64"].Value.TrimEnd('/');
  81. var tag = match.Groups["tag"].Value;
  82. if (!tag.IsNullOrEmpty())
  83. {
  84. server.remarks = HttpUtility.UrlDecode(tag, Encoding.UTF8);
  85. }
  86. Match details = null;
  87. try
  88. {
  89. details = DetailsParser.Match(Encoding.UTF8.GetString(Convert.FromBase64String(
  90. base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '='))));
  91. }
  92. catch (FormatException)
  93. {
  94. return null;
  95. }
  96. if (!details.Success)
  97. return null;
  98. server.method = details.Groups["method"].Value;
  99. server.password = details.Groups["password"].Value;
  100. server.server = details.Groups["hostname"].Value;
  101. server.server_port = int.Parse(details.Groups["port"].Value);
  102. return server;
  103. }
  104. public static List<Server> GetServers(string ssURL)
  105. {
  106. var serverUrls = ssURL.Split('\r', '\n', ' ');
  107. List<Server> servers = new List<Server>();
  108. foreach (string serverUrl in serverUrls)
  109. {
  110. string _serverUrl = serverUrl.Trim();
  111. if (!_serverUrl.BeginWith("ss://", StringComparison.InvariantCultureIgnoreCase))
  112. {
  113. continue;
  114. }
  115. Server legacyServer = ParseLegacyURL(serverUrl);
  116. if (legacyServer != null) //legacy
  117. {
  118. servers.Add(legacyServer);
  119. }
  120. else //SIP002
  121. {
  122. Uri parsedUrl;
  123. try
  124. {
  125. parsedUrl = new Uri(serverUrl);
  126. }
  127. catch (UriFormatException)
  128. {
  129. continue;
  130. }
  131. Server server = new Server
  132. {
  133. remarks = parsedUrl.GetComponents(UriComponents.Fragment, UriFormat.Unescaped),
  134. server = parsedUrl.IdnHost,
  135. server_port = parsedUrl.Port,
  136. };
  137. // parse base64 UserInfo
  138. string rawUserInfo = parsedUrl.GetComponents(UriComponents.UserInfo, UriFormat.Unescaped);
  139. string base64 = rawUserInfo.Replace('-', '+').Replace('_', '/'); // Web-safe base64 to normal base64
  140. string userInfo = "";
  141. try
  142. {
  143. userInfo = Encoding.UTF8.GetString(Convert.FromBase64String(
  144. base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '=')));
  145. }
  146. catch (FormatException)
  147. {
  148. continue;
  149. }
  150. string[] userInfoParts = userInfo.Split(new char[] { ':' }, 2);
  151. if (userInfoParts.Length != 2)
  152. {
  153. continue;
  154. }
  155. server.method = userInfoParts[0];
  156. server.password = userInfoParts[1];
  157. NameValueCollection queryParameters = HttpUtility.ParseQueryString(parsedUrl.Query);
  158. string[] pluginParts = (queryParameters["plugin"] ?? "").Split(new[] { ';' }, 2);
  159. if (pluginParts.Length > 0)
  160. {
  161. server.plugin = pluginParts[0] ?? "";
  162. }
  163. if (pluginParts.Length > 1)
  164. {
  165. server.plugin_opts = pluginParts[1] ?? "";
  166. }
  167. servers.Add(server);
  168. }
  169. }
  170. return servers;
  171. }
  172. public string Identifier()
  173. {
  174. return server + ':' + server_port;
  175. }
  176. }
  177. }