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.0 kB

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