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

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