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 2.8 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
9 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 string server;
  11. public int server_port;
  12. public string password;
  13. public string method;
  14. public string remarks;
  15. public bool auth;
  16. public override int GetHashCode()
  17. {
  18. return server.GetHashCode() ^ server_port;
  19. }
  20. public override bool Equals(object obj)
  21. {
  22. Server o2 = (Server)obj;
  23. return server == o2.server && server_port == o2.server_port;
  24. }
  25. public string FriendlyName()
  26. {
  27. if (server.IsNullOrEmpty())
  28. {
  29. return I18N.GetString("New server");
  30. }
  31. if (remarks.IsNullOrEmpty())
  32. {
  33. return server + ":" + server_port;
  34. }
  35. else
  36. {
  37. return remarks + " (" + server + ":" + server_port + ")";
  38. }
  39. }
  40. public Server()
  41. {
  42. server = "";
  43. server_port = 8388;
  44. method = "aes-256-cfb";
  45. password = "";
  46. remarks = "";
  47. auth = false;
  48. }
  49. public Server(string ssURL) : this()
  50. {
  51. string[] r1 = Regex.Split(ssURL, "ss://", RegexOptions.IgnoreCase);
  52. string base64 = r1[1].ToString();
  53. byte[] bytes = null;
  54. for (var i = 0; i < 3; i++)
  55. {
  56. try
  57. {
  58. bytes = Convert.FromBase64String(base64);
  59. }
  60. catch (FormatException)
  61. {
  62. base64 += "=";
  63. }
  64. }
  65. if (bytes == null)
  66. {
  67. throw new FormatException();
  68. }
  69. try
  70. {
  71. string data = Encoding.UTF8.GetString(bytes);
  72. int indexLastAt = data.LastIndexOf('@');
  73. string afterAt = data.Substring(indexLastAt + 1);
  74. int indexLastColon = afterAt.LastIndexOf(':');
  75. server_port = int.Parse(afterAt.Substring(indexLastColon + 1));
  76. server = afterAt.Substring(0, indexLastColon);
  77. string beforeAt = data.Substring(0, indexLastAt);
  78. string[] parts = beforeAt.Split(new[] { ':' });
  79. method = parts[0];
  80. password = parts[1];
  81. //TODO: read one_time_auth
  82. }
  83. catch (IndexOutOfRangeException)
  84. {
  85. throw new FormatException();
  86. }
  87. }
  88. }
  89. }