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.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  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. 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 bool auth;
  23. public int timeout;
  24. public override int GetHashCode()
  25. {
  26. return server.GetHashCode() ^ server_port;
  27. }
  28. public override bool Equals(object obj)
  29. {
  30. Server o2 = (Server)obj;
  31. return server == o2.server && server_port == o2.server_port;
  32. }
  33. public string FriendlyName()
  34. {
  35. if (server.IsNullOrEmpty())
  36. {
  37. return I18N.GetString("New server");
  38. }
  39. string serverStr;
  40. // CheckHostName() won't do a real DNS lookup
  41. var hostType = Uri.CheckHostName( server );
  42. if ( hostType == UriHostNameType.Unknown ) {
  43. throw new FormatException("Invalid Server Address.");
  44. }
  45. switch ( hostType ) {
  46. case UriHostNameType.IPv6:
  47. serverStr = $"[{server}]:{server_port}";
  48. break;
  49. default:
  50. // IPv4 and domain name
  51. serverStr = $"{server}:{server_port}";
  52. break;
  53. }
  54. return remarks.IsNullOrEmpty()
  55. ? serverStr
  56. : $"{remarks} ({serverStr})";
  57. }
  58. public Server()
  59. {
  60. server = "";
  61. server_port = 8388;
  62. method = "aes-256-cfb";
  63. password = "";
  64. remarks = "";
  65. auth = false;
  66. timeout = DefaultServerTimeoutSec;
  67. }
  68. public Server(string ssURL) : this()
  69. {
  70. var match = UrlFinder.Match(ssURL);
  71. if (!match.Success) throw new FormatException();
  72. var base64 = match.Groups[1].Value;
  73. match = DetailsParser.Match(Encoding.UTF8.GetString(Convert.FromBase64String(
  74. base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '='))));
  75. method = match.Groups["method"].Value;
  76. auth = match.Groups["auth"].Success;
  77. password = match.Groups["password"].Value;
  78. server = match.Groups["hostname"].Value;
  79. server_port = int.Parse(match.Groups["port"].Value);
  80. }
  81. public string Identifier()
  82. {
  83. return server + ':' + server_port;
  84. }
  85. }
  86. }