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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.Web;
  5. using Shadowsocks.Controller;
  6. namespace Shadowsocks.Model
  7. {
  8. [Serializable]
  9. public class Server
  10. {
  11. public static readonly Regex
  12. UrlFinder = new Regex("^(?i)ss://([A-Za-z0-9+-/=_]+)(#(.+))?$", RegexOptions.IgnoreCase),
  13. DetailsParser = new Regex("^((?<method>.+?):(?<password>.*)@(?<hostname>.+?)" +
  14. ":(?<port>\\d+?))$", RegexOptions.IgnoreCase);
  15. private const int DefaultServerTimeoutSec = 5;
  16. public const int MaxServerTimeoutSec = 20;
  17. public string server;
  18. public int server_port;
  19. public string password;
  20. public string method;
  21. public string remarks;
  22. public int timeout;
  23. public override int GetHashCode()
  24. {
  25. return server.GetHashCode() ^ server_port;
  26. }
  27. public override bool Equals(object obj)
  28. {
  29. Server o2 = (Server)obj;
  30. return server == o2.server && server_port == o2.server_port;
  31. }
  32. public string FriendlyName()
  33. {
  34. if (server.IsNullOrEmpty())
  35. {
  36. return I18N.GetString("New server");
  37. }
  38. string serverStr;
  39. // CheckHostName() won't do a real DNS lookup
  40. var hostType = Uri.CheckHostName( server );
  41. switch ( hostType ) {
  42. case UriHostNameType.IPv6:
  43. serverStr = $"[{server}]:{server_port}";
  44. break;
  45. default:
  46. // IPv4 and domain name
  47. serverStr = $"{server}:{server_port}";
  48. break;
  49. }
  50. return remarks.IsNullOrEmpty()
  51. ? serverStr
  52. : $"{remarks} ({serverStr})";
  53. }
  54. public Server()
  55. {
  56. server = "";
  57. server_port = 8388;
  58. method = "aes-256-cfb";
  59. password = "";
  60. remarks = "";
  61. timeout = DefaultServerTimeoutSec;
  62. }
  63. public Server(string ssURL) : this()
  64. {
  65. var match = UrlFinder.Match(ssURL);
  66. if (!match.Success) throw new FormatException();
  67. var base64 = match.Groups[1].Value;
  68. var tag = match.Groups[3].Value;
  69. if (!tag.IsNullOrEmpty())
  70. remarks = HttpUtility.UrlDecode(tag, Encoding.UTF8);
  71. match = DetailsParser.Match(Encoding.UTF8.GetString(Convert.FromBase64String(
  72. base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '='))));
  73. method = match.Groups["method"].Value;
  74. password = match.Groups["password"].Value;
  75. server = match.Groups["hostname"].Value;
  76. server_port = int.Parse(match.Groups["port"].Value);
  77. }
  78. public string Identifier()
  79. {
  80. return server + ':' + server_port;
  81. }
  82. }
  83. }