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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = 8090;
  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. _listener.Close();
  55. _listener = null;
  56. }
  57. public string TouchPACFile()
  58. {
  59. if (File.Exists(PAC_FILE))
  60. {
  61. return PAC_FILE;
  62. }
  63. else
  64. {
  65. FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt);
  66. return PAC_FILE;
  67. }
  68. }
  69. public void AcceptCallback(IAsyncResult ar)
  70. {
  71. try
  72. {
  73. Socket listener = (Socket)ar.AsyncState;
  74. Socket conn = listener.EndAccept(ar);
  75. listener.BeginAccept(
  76. new AsyncCallback(AcceptCallback),
  77. listener);
  78. byte[] buf = new byte[2048];
  79. object[] state = new object[] {
  80. conn,
  81. buf
  82. };
  83. conn.BeginReceive(buf, 0, 1024, 0,
  84. new AsyncCallback(ReceiveCallback), state);
  85. }
  86. catch (ObjectDisposedException)
  87. {
  88. }
  89. catch (Exception e)
  90. {
  91. Console.WriteLine(e);
  92. }
  93. }
  94. private string GetPACContent()
  95. {
  96. if (File.Exists(PAC_FILE))
  97. {
  98. return File.ReadAllText(PAC_FILE, Encoding.UTF8);
  99. }
  100. else
  101. {
  102. byte[] pacGZ = Resources.proxy_pac_txt;
  103. byte[] buffer = new byte[1024 * 1024]; // builtin pac gzip size: maximum 1M
  104. int n;
  105. using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
  106. CompressionMode.Decompress, false))
  107. {
  108. n = input.Read(buffer, 0, buffer.Length);
  109. if (n == 0)
  110. {
  111. throw new IOException("can not decompress pac");
  112. }
  113. return System.Text.Encoding.UTF8.GetString(buffer, 0, n);
  114. }
  115. }
  116. }
  117. private void ReceiveCallback(IAsyncResult ar)
  118. {
  119. object[] state = (object[])ar.AsyncState;
  120. Socket conn = (Socket)state[0];
  121. byte[] requestBuf = (byte[])state[1];
  122. try
  123. {
  124. int bytesRead = conn.EndReceive(ar);
  125. string pac = GetPACContent();
  126. IPEndPoint localEndPoint = (IPEndPoint)conn.LocalEndPoint;
  127. string proxy = GetPACAddress(requestBuf, localEndPoint);
  128. pac = pac.Replace("__PROXY__", proxy);
  129. if (bytesRead > 0)
  130. {
  131. string text = String.Format(@"HTTP/1.1 200 OK
  132. Server: Shadowsocks
  133. Content-Type: application/x-ns-proxy-autoconfig
  134. Content-Length: {0}
  135. Connection: Close
  136. ", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac;
  137. byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
  138. conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), conn);
  139. }
  140. else
  141. {
  142. conn.Close();
  143. }
  144. }
  145. catch (Exception e)
  146. {
  147. Console.WriteLine(e);
  148. conn.Close();
  149. }
  150. }
  151. private void SendCallback(IAsyncResult ar)
  152. {
  153. Socket conn = (Socket)ar.AsyncState;
  154. try
  155. {
  156. conn.Shutdown(SocketShutdown.Send);
  157. }
  158. catch
  159. { }
  160. }
  161. private void WatchPacFile()
  162. {
  163. if (watcher != null)
  164. {
  165. watcher.Dispose();
  166. }
  167. watcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  168. watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  169. watcher.Filter = PAC_FILE;
  170. watcher.Changed += Watcher_Changed;
  171. watcher.Created += Watcher_Changed;
  172. watcher.Deleted += Watcher_Changed;
  173. watcher.Renamed += Watcher_Changed;
  174. watcher.EnableRaisingEvents = true;
  175. }
  176. private void Watcher_Changed(object sender, FileSystemEventArgs e)
  177. {
  178. if (PACFileChanged != null)
  179. {
  180. PACFileChanged(this, new EventArgs());
  181. }
  182. }
  183. private string GetPACAddress(byte[] requestBuf, IPEndPoint localEndPoint)
  184. {
  185. string proxy = "PROXY " + localEndPoint.Address + ":8123;";
  186. //try
  187. //{
  188. // string requestString = Encoding.UTF8.GetString(requestBuf);
  189. // if (requestString.IndexOf("AppleWebKit") >= 0)
  190. // {
  191. // string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port;
  192. // proxy = "SOCKS5 " + address + "; SOCKS " + address + ";";
  193. // }
  194. //}
  195. //catch (Exception e)
  196. //{
  197. // Console.WriteLine(e);
  198. //}
  199. return proxy;
  200. }
  201. }
  202. }