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.

PACServer.cs 6.8 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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using Shadowsocks.Model;
  2. using Shadowsocks.Properties;
  3. using Shadowsocks.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.IO.Compression;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. namespace Shadowsocks.Controller
  13. {
  14. class PACServer : Listener.Service
  15. {
  16. public static string PAC_FILE = "pac.txt";
  17. public static string USER_RULE_FILE = "user-rule.txt";
  18. FileSystemWatcher watcher;
  19. private Configuration _config;
  20. public event EventHandler PACFileChanged;
  21. public PACServer()
  22. {
  23. this.WatchPacFile();
  24. }
  25. public void UpdateConfiguration(Configuration config)
  26. {
  27. this._config = config;
  28. }
  29. public bool Handle(byte[] firstPacket, int length, Socket socket, object state)
  30. {
  31. if (socket.ProtocolType != ProtocolType.Tcp)
  32. {
  33. return false;
  34. }
  35. try
  36. {
  37. string request = Encoding.UTF8.GetString(firstPacket, 0, length);
  38. string[] lines = request.Split('\r', '\n');
  39. bool hostMatch = false, pathMatch = false, useSocks = false;
  40. foreach (string line in lines)
  41. {
  42. string[] kv = line.Split(new char[]{':'}, 2);
  43. if (kv.Length == 2)
  44. {
  45. if (kv[0] == "Host")
  46. {
  47. if (kv[1].Trim() == ((IPEndPoint)socket.LocalEndPoint).ToString())
  48. {
  49. hostMatch = true;
  50. }
  51. }
  52. else if (kv[0] == "User-Agent")
  53. {
  54. // we need to drop connections when changing servers
  55. /* if (kv[1].IndexOf("Chrome") >= 0)
  56. {
  57. useSocks = true;
  58. } */
  59. }
  60. }
  61. else if (kv.Length == 1)
  62. {
  63. if (line.IndexOf("pac") >= 0)
  64. {
  65. pathMatch = true;
  66. }
  67. }
  68. }
  69. if (hostMatch && pathMatch)
  70. {
  71. SendResponse(firstPacket, length, socket, useSocks);
  72. return true;
  73. }
  74. return false;
  75. }
  76. catch (ArgumentException)
  77. {
  78. return false;
  79. }
  80. }
  81. public string TouchPACFile()
  82. {
  83. if (File.Exists(PAC_FILE))
  84. {
  85. return PAC_FILE;
  86. }
  87. else
  88. {
  89. FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt);
  90. return PAC_FILE;
  91. }
  92. }
  93. internal string TouchUserRuleFile()
  94. {
  95. if (File.Exists(USER_RULE_FILE))
  96. {
  97. return USER_RULE_FILE;
  98. }
  99. else
  100. {
  101. File.WriteAllText(USER_RULE_FILE, Resources.user_rule);
  102. return USER_RULE_FILE;
  103. }
  104. }
  105. private string GetPACContent()
  106. {
  107. if (File.Exists(PAC_FILE))
  108. {
  109. return File.ReadAllText(PAC_FILE, Encoding.UTF8);
  110. }
  111. else
  112. {
  113. return Utils.UnGzip(Resources.proxy_pac_txt);
  114. }
  115. }
  116. public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks)
  117. {
  118. try
  119. {
  120. string pac = GetPACContent();
  121. IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
  122. string proxy = GetPACAddress(firstPacket, length, localEndPoint, useSocks);
  123. pac = pac.Replace("__PROXY__", proxy);
  124. string text = String.Format(@"HTTP/1.1 200 OK
  125. Server: Shadowsocks
  126. Content-Type: application/x-ns-proxy-autoconfig
  127. Content-Length: {0}
  128. Connection: Close
  129. ", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac;
  130. byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
  131. socket.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), socket);
  132. Util.Utils.ReleaseMemory();
  133. }
  134. catch (Exception e)
  135. {
  136. Console.WriteLine(e);
  137. socket.Close();
  138. }
  139. }
  140. private void SendCallback(IAsyncResult ar)
  141. {
  142. Socket conn = (Socket)ar.AsyncState;
  143. try
  144. {
  145. conn.Shutdown(SocketShutdown.Send);
  146. }
  147. catch
  148. { }
  149. }
  150. private void WatchPacFile()
  151. {
  152. if (watcher != null)
  153. {
  154. watcher.Dispose();
  155. }
  156. watcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  157. watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  158. watcher.Filter = PAC_FILE;
  159. watcher.Changed += Watcher_Changed;
  160. watcher.Created += Watcher_Changed;
  161. watcher.Deleted += Watcher_Changed;
  162. watcher.Renamed += Watcher_Changed;
  163. watcher.EnableRaisingEvents = true;
  164. }
  165. private void Watcher_Changed(object sender, FileSystemEventArgs e)
  166. {
  167. if (PACFileChanged != null)
  168. {
  169. PACFileChanged(this, new EventArgs());
  170. }
  171. }
  172. private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks)
  173. {
  174. //try
  175. //{
  176. // string requestString = Encoding.UTF8.GetString(requestBuf);
  177. // if (requestString.IndexOf("AppleWebKit") >= 0)
  178. // {
  179. // string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port;
  180. // proxy = "SOCKS5 " + address + "; SOCKS " + address + ";";
  181. // }
  182. //}
  183. //catch (Exception e)
  184. //{
  185. // Console.WriteLine(e);
  186. //}
  187. return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";";
  188. }
  189. }
  190. }