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.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Web;
  6. using Shadowsocks.Controller;
  7. namespace Shadowsocks.Model
  8. {
  9. [Serializable]
  10. public class Server
  11. {
  12. public static readonly Regex
  13. UrlFinder = new Regex(@"ss://(?<base64>[A-Za-z0-9+-/=_]+)(?:#(?<tag>\S+))?", RegexOptions.IgnoreCase),
  14. DetailsParser = new Regex(@"^((?<method>.+?):(?<password>.*)@(?<hostname>.+?):(?<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. {
  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. timeout = DefaultServerTimeoutSec;
  63. }
  64. public static List<Server> GetServers(string ssURL)
  65. {
  66. var matches = UrlFinder.Matches(ssURL);
  67. if (matches.Count <= 0) return null;
  68. List<Server> servers = new List<Server>();
  69. foreach (Match match in matches)
  70. {
  71. Server tmp = new Server();
  72. var base64 = match.Groups["base64"].Value;
  73. var tag = match.Groups["tag"].Value;
  74. if (!tag.IsNullOrEmpty())
  75. {
  76. tmp.remarks = HttpUtility.UrlDecode(tag, Encoding.UTF8);
  77. }
  78. Match details = DetailsParser.Match(Encoding.UTF8.GetString(Convert.FromBase64String(
  79. base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '='))));
  80. if (!details.Success)
  81. continue;
  82. tmp.method = details.Groups["method"].Value;
  83. tmp.password = details.Groups["password"].Value;
  84. tmp.server = details.Groups["hostname"].Value;
  85. tmp.server_port = int.Parse(details.Groups["port"].Value);
  86. servers.Add(tmp);
  87. }
  88. return servers;
  89. }
  90. public string Identifier()
  91. {
  92. return server + ':' + server_port;
  93. }
  94. }
  95. }