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