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