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 12 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
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using Shadowsocks.Model;
  2. using Shadowsocks.Properties;
  3. using System;
  4. using System.Collections;
  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. using System.Text.RegularExpressions;
  13. namespace Shadowsocks.Controller
  14. {
  15. class PACServer
  16. {
  17. private static int PORT = 8093;
  18. private static string PAC_FILE = "pac.txt";
  19. private static Configuration config;
  20. Socket _listener;
  21. FileSystemWatcher watcher;
  22. public event EventHandler PACFileChanged;
  23. public event EventHandler UpdatePACFromGFWListCompleted;
  24. public event ErrorEventHandler UpdatePACFromGFWListError;
  25. public void Start(Configuration configuration)
  26. {
  27. try
  28. {
  29. config = configuration;
  30. // Create a TCP/IP socket.
  31. _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  32. _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  33. IPEndPoint localEndPoint = null;
  34. if (configuration.shareOverLan)
  35. {
  36. localEndPoint = new IPEndPoint(IPAddress.Any, PORT);
  37. }
  38. else
  39. {
  40. localEndPoint = new IPEndPoint(IPAddress.Loopback, PORT);
  41. }
  42. // Bind the socket to the local endpoint and listen for incoming connections.
  43. _listener.Bind(localEndPoint);
  44. _listener.Listen(100);
  45. _listener.BeginAccept(
  46. new AsyncCallback(AcceptCallback),
  47. _listener);
  48. WatchPacFile();
  49. }
  50. catch (SocketException)
  51. {
  52. _listener.Close();
  53. throw;
  54. }
  55. }
  56. public void Stop()
  57. {
  58. if (_listener != null)
  59. {
  60. _listener.Close();
  61. _listener = null;
  62. }
  63. }
  64. public string TouchPACFile()
  65. {
  66. if (File.Exists(PAC_FILE))
  67. {
  68. return PAC_FILE;
  69. }
  70. else
  71. {
  72. FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt);
  73. return PAC_FILE;
  74. }
  75. }
  76. // we don't even use it
  77. static byte[] requestBuf = new byte[2048];
  78. public void AcceptCallback(IAsyncResult ar)
  79. {
  80. Socket listener = (Socket)ar.AsyncState;
  81. try
  82. {
  83. Socket conn = listener.EndAccept(ar);
  84. object[] state = new object[] {
  85. conn,
  86. requestBuf
  87. };
  88. conn.BeginReceive(requestBuf, 0, requestBuf.Length, 0,
  89. new AsyncCallback(ReceiveCallback), state);
  90. }
  91. catch (ObjectDisposedException)
  92. {
  93. }
  94. catch (Exception e)
  95. {
  96. Console.WriteLine(e);
  97. }
  98. finally
  99. {
  100. try
  101. {
  102. listener.BeginAccept(
  103. new AsyncCallback(AcceptCallback),
  104. listener);
  105. }
  106. catch (ObjectDisposedException)
  107. {
  108. // do nothing
  109. }
  110. catch (Exception e)
  111. {
  112. Logging.LogUsefulException(e);
  113. }
  114. }
  115. }
  116. private string GetPACContent()
  117. {
  118. if (File.Exists(PAC_FILE))
  119. {
  120. return File.ReadAllText(PAC_FILE, Encoding.UTF8);
  121. }
  122. else
  123. {
  124. byte[] pacGZ = Resources.proxy_pac_txt;
  125. byte[] buffer = new byte[1024]; // builtin pac gzip size: maximum 100K
  126. MemoryStream sb = new MemoryStream();
  127. int n;
  128. using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
  129. CompressionMode.Decompress, false))
  130. {
  131. while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
  132. {
  133. sb.Write(buffer, 0, n);
  134. }
  135. return System.Text.Encoding.UTF8.GetString(sb.ToArray());
  136. }
  137. }
  138. }
  139. private void ReceiveCallback(IAsyncResult ar)
  140. {
  141. object[] state = (object[])ar.AsyncState;
  142. Socket conn = (Socket)state[0];
  143. byte[] requestBuf = (byte[])state[1];
  144. try
  145. {
  146. int bytesRead = conn.EndReceive(ar);
  147. string pac = GetPACContent();
  148. IPEndPoint localEndPoint = (IPEndPoint)conn.LocalEndPoint;
  149. string proxy = GetPACAddress(requestBuf, localEndPoint);
  150. pac = pac.Replace("__PROXY__", proxy);
  151. if (bytesRead > 0)
  152. {
  153. string text = String.Format(@"HTTP/1.1 200 OK
  154. Server: Shadowsocks
  155. Content-Type: application/x-ns-proxy-autoconfig
  156. Content-Length: {0}
  157. Connection: Close
  158. ", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac;
  159. byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
  160. conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), conn);
  161. Util.Util.ReleaseMemory();
  162. }
  163. else
  164. {
  165. conn.Close();
  166. }
  167. }
  168. catch (Exception e)
  169. {
  170. Console.WriteLine(e);
  171. conn.Close();
  172. }
  173. }
  174. private void SendCallback(IAsyncResult ar)
  175. {
  176. Socket conn = (Socket)ar.AsyncState;
  177. try
  178. {
  179. conn.Shutdown(SocketShutdown.Send);
  180. }
  181. catch
  182. { }
  183. }
  184. private void WatchPacFile()
  185. {
  186. if (watcher != null)
  187. {
  188. watcher.Dispose();
  189. }
  190. watcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  191. watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  192. watcher.Filter = PAC_FILE;
  193. watcher.Changed += Watcher_Changed;
  194. watcher.Created += Watcher_Changed;
  195. watcher.Deleted += Watcher_Changed;
  196. watcher.Renamed += Watcher_Changed;
  197. watcher.EnableRaisingEvents = true;
  198. }
  199. private void Watcher_Changed(object sender, FileSystemEventArgs e)
  200. {
  201. if (PACFileChanged != null)
  202. {
  203. PACFileChanged(this, new EventArgs());
  204. }
  205. }
  206. private string GetPACAddress(byte[] requestBuf, IPEndPoint localEndPoint)
  207. {
  208. string proxy = "PROXY " + localEndPoint.Address + ":8123;";
  209. //try
  210. //{
  211. // string requestString = Encoding.UTF8.GetString(requestBuf);
  212. // if (requestString.IndexOf("AppleWebKit") >= 0)
  213. // {
  214. // string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port;
  215. // proxy = "SOCKS5 " + address + "; SOCKS " + address + ";";
  216. // }
  217. //}
  218. //catch (Exception e)
  219. //{
  220. // Console.WriteLine(e);
  221. //}
  222. return proxy;
  223. }
  224. public void UpdatePACFromGFWList()
  225. {
  226. GfwListUpdater gfwlist = new GfwListUpdater();
  227. gfwlist.DownloadCompleted += gfwlist_DownloadCompleted;
  228. gfwlist.Error += gfwlist_Error;
  229. gfwlist.proxy = new WebProxy(IPAddress.Loopback.ToString(), 8123); /* use polipo proxy*/
  230. gfwlist.Download();
  231. }
  232. private void gfwlist_DownloadCompleted(object sender, GfwListUpdater.GfwListDownloadCompletedArgs e)
  233. {
  234. GfwListUpdater.Parser parser = new GfwListUpdater.Parser(e.Content);
  235. string[] lines = parser.GetValidLines();
  236. StringBuilder rules = new StringBuilder(lines.Length * 16);
  237. SerializeRules(lines, rules);
  238. string abpContent = GetAbpContent();
  239. abpContent = abpContent.Replace("__RULES__", rules.ToString());
  240. File.WriteAllText(PAC_FILE, abpContent);
  241. if (UpdatePACFromGFWListCompleted != null)
  242. {
  243. UpdatePACFromGFWListCompleted(this, new EventArgs());
  244. }
  245. }
  246. private void gfwlist_Error(object sender, ErrorEventArgs e)
  247. {
  248. if (UpdatePACFromGFWListError != null)
  249. {
  250. UpdatePACFromGFWListError(this, e);
  251. }
  252. }
  253. private string GetAbpContent()
  254. {
  255. byte[] abpGZ = Resources.abp_js;
  256. byte[] buffer = new byte[1024]; // builtin pac gzip size: maximum 100K
  257. int n;
  258. using (MemoryStream sb = new MemoryStream())
  259. {
  260. using (GZipStream input = new GZipStream(new MemoryStream(abpGZ),
  261. CompressionMode.Decompress, false))
  262. {
  263. while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
  264. {
  265. sb.Write(buffer, 0, n);
  266. }
  267. }
  268. return System.Text.Encoding.UTF8.GetString(sb.ToArray());
  269. }
  270. }
  271. private static void SerializeRules(string[] rules, StringBuilder builder)
  272. {
  273. builder.Append("[\n");
  274. bool first = true;
  275. foreach (string rule in rules)
  276. {
  277. if (!first)
  278. builder.Append(",\n");
  279. SerializeString(rule, builder);
  280. first = false;
  281. }
  282. builder.Append("\n]");
  283. }
  284. private static void SerializeString(string aString, StringBuilder builder)
  285. {
  286. builder.Append("\t\"");
  287. char[] charArray = aString.ToCharArray();
  288. for (int i = 0; i < charArray.Length; i++)
  289. {
  290. char c = charArray[i];
  291. if (c == '"')
  292. builder.Append("\\\"");
  293. else if (c == '\\')
  294. builder.Append("\\\\");
  295. else if (c == '\b')
  296. builder.Append("\\b");
  297. else if (c == '\f')
  298. builder.Append("\\f");
  299. else if (c == '\n')
  300. builder.Append("\\n");
  301. else if (c == '\r')
  302. builder.Append("\\r");
  303. else if (c == '\t')
  304. builder.Append("\\t");
  305. else
  306. builder.Append(c);
  307. }
  308. builder.Append("\"");
  309. }
  310. }
  311. }