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.2 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 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>.+?)(?<auth>-auth)??:(?<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 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. 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. timeout = DefaultServerTimeoutSec;
  64. }
  65. public Server(string ssURL) : this()
  66. {
  67. var match = UrlFinder.Match(ssURL);
  68. if (!match.Success) throw new FormatException();
  69. var base64 = match.Groups[1].Value;
  70. var tag = match.Groups[3].Value;
  71. if (!tag.IsNullOrEmpty())
  72. remarks = HttpUtility.UrlDecode(tag, Encoding.UTF8);
  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. }