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 7.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
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using Shadowsocks.Model;
  2. using Shadowsocks.Properties;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. namespace Shadowsocks.Controller
  12. {
  13. class PACServer
  14. {
  15. private static int PORT = 8093;
  16. private static string PAC_FILE = "pac.txt";
  17. private static Configuration config;
  18. Socket _listener;
  19. FileSystemWatcher watcher;
  20. public event EventHandler PACFileChanged;
  21. public void Start(Configuration configuration)
  22. {
  23. try
  24. {
  25. config = configuration;
  26. // Create a TCP/IP socket.
  27. _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  28. _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  29. IPEndPoint localEndPoint = null;
  30. if (configuration.shareOverLan)
  31. {
  32. localEndPoint = new IPEndPoint(IPAddress.Any, PORT);
  33. }
  34. else
  35. {
  36. localEndPoint = new IPEndPoint(IPAddress.Loopback, PORT);
  37. }
  38. // Bind the socket to the local endpoint and listen for incoming connections.
  39. _listener.Bind(localEndPoint);
  40. _listener.Listen(100);
  41. _listener.BeginAccept(
  42. new AsyncCallback(AcceptCallback),
  43. _listener);
  44. WatchPacFile();
  45. }
  46. catch (SocketException)
  47. {
  48. _listener.Close();
  49. throw;
  50. }
  51. }
  52. public void Stop()
  53. {
  54. if (_listener != null)
  55. {
  56. _listener.Close();
  57. _listener = null;
  58. }
  59. }
  60. public string TouchPACFile()
  61. {
  62. if (File.Exists(PAC_FILE))
  63. {
  64. return PAC_FILE;
  65. }
  66. else
  67. {
  68. FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt);
  69. return PAC_FILE;
  70. }
  71. }
  72. // we don't even use it
  73. static byte[] requestBuf = new byte[2048];
  74. public void AcceptCallback(IAsyncResult ar)
  75. {
  76. Socket listener = (Socket)ar.AsyncState;
  77. try
  78. {
  79. Socket conn = listener.EndAccept(ar);
  80. object[] state = new object[] {
  81. conn,
  82. requestBuf
  83. };
  84. conn.BeginReceive(requestBuf, 0, requestBuf.Length, 0,
  85. new AsyncCallback(ReceiveCallback), state);
  86. }
  87. catch (ObjectDisposedException)
  88. {
  89. }
  90. catch (Exception e)
  91. {
  92. Console.WriteLine(e);
  93. }
  94. finally
  95. {
  96. try
  97. {
  98. listener.BeginAccept(
  99. new AsyncCallback(AcceptCallback),
  100. listener);
  101. }
  102. catch (ObjectDisposedException)
  103. {
  104. // do nothing
  105. }
  106. catch (Exception e)
  107. {
  108. Logging.LogUsefulException(e);
  109. }
  110. }
  111. }
  112. private string GetPACContent()
  113. {
  114. if (File.Exists(PAC_FILE))
  115. {
  116. return File.ReadAllText(PAC_FILE, Encoding.UTF8);
  117. }
  118. else
  119. {
  120. byte[] pacGZ = Resources.proxy_pac_txt;
  121. byte[] buffer = new byte[1024]; // builtin pac gzip size: maximum 100K
  122. MemoryStream sb = new MemoryStream();
  123. int n;
  124. using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
  125. CompressionMode.Decompress, false))
  126. {
  127. while((n = input.Read(buffer, 0, buffer.Length)) > 0)
  128. {
  129. sb.Write(buffer, 0, n);
  130. }
  131. return System.Text.Encoding.UTF8.GetString(sb.ToArray());
  132. }
  133. }
  134. }
  135. private void ReceiveCallback(IAsyncResult ar)
  136. {
  137. object[] state = (object[])ar.AsyncState;
  138. Socket conn = (Socket)state[0];
  139. byte[] requestBuf = (byte[])state[1];
  140. try
  141. {
  142. int bytesRead = conn.EndReceive(ar);
  143. string pac = GetPACContent();
  144. IPEndPoint localEndPoint = (IPEndPoint)conn.LocalEndPoint;
  145. string proxy = GetPACAddress(requestBuf, localEndPoint);
  146. pac = pac.Replace("__PROXY__", proxy);
  147. if (bytesRead > 0)
  148. {
  149. string text = String.Format(@"HTTP/1.1 200 OK
  150. Server: Shadowsocks
  151. Content-Type: application/x-ns-proxy-autoconfig
  152. Content-Length: {0}
  153. Connection: Close
  154. ", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac;
  155. byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
  156. conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), conn);
  157. Util.Util.ReleaseMemory();
  158. }
  159. else
  160. {
  161. conn.Close();
  162. }
  163. }
  164. catch (Exception e)
  165. {
  166. Console.WriteLine(e);
  167. conn.Close();
  168. }
  169. }
  170. private void SendCallback(IAsyncResult ar)
  171. {
  172. Socket conn = (Socket)ar.AsyncState;
  173. try
  174. {
  175. conn.Shutdown(SocketShutdown.Send);
  176. }
  177. catch
  178. { }
  179. }
  180. private void WatchPacFile()
  181. {
  182. if (watcher != null)
  183. {
  184. watcher.Dispose();
  185. }
  186. watcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  187. watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  188. watcher.Filter = PAC_FILE;
  189. watcher.Changed += Watcher_Changed;
  190. watcher.Created += Watcher_Changed;
  191. watcher.Deleted += Watcher_Changed;
  192. watcher.Renamed += Watcher_Changed;
  193. watcher.EnableRaisingEvents = true;
  194. }
  195. private void Watcher_Changed(object sender, FileSystemEventArgs e)
  196. {
  197. if (PACFileChanged != null)
  198. {
  199. PACFileChanged(this, new EventArgs());
  200. }
  201. }
  202. private string GetPACAddress(byte[] requestBuf, IPEndPoint localEndPoint)
  203. {
  204. string proxy = "PROXY " + localEndPoint.Address + ":8123;";
  205. //try
  206. //{
  207. // string requestString = Encoding.UTF8.GetString(requestBuf);
  208. // if (requestString.IndexOf("AppleWebKit") >= 0)
  209. // {
  210. // string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port;
  211. // proxy = "SOCKS5 " + address + "; SOCKS " + address + ";";
  212. // }
  213. //}
  214. //catch (Exception e)
  215. //{
  216. // Console.WriteLine(e);
  217. //}
  218. return proxy;
  219. }
  220. }
  221. }