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