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.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. Socket _listener;
  18. FileSystemWatcher watcher;
  19. public event EventHandler PACFileChanged;
  20. public void Start(Configuration configuration)
  21. {
  22. try
  23. {
  24. // Create a TCP/IP socket.
  25. _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  26. _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  27. IPEndPoint localEndPoint = null;
  28. if (configuration.shareOverLan)
  29. {
  30. localEndPoint = new IPEndPoint(IPAddress.Any, PORT);
  31. }
  32. else
  33. {
  34. localEndPoint = new IPEndPoint(IPAddress.Loopback, PORT);
  35. }
  36. // Bind the socket to the local endpoint and listen for incoming connections.
  37. _listener.Bind(localEndPoint);
  38. _listener.Listen(100);
  39. _listener.BeginAccept(
  40. new AsyncCallback(AcceptCallback),
  41. _listener);
  42. WatchPacFile();
  43. }
  44. catch (SocketException)
  45. {
  46. _listener.Close();
  47. throw;
  48. }
  49. }
  50. public void Stop()
  51. {
  52. _listener.Close();
  53. _listener = null;
  54. }
  55. public string TouchPACFile()
  56. {
  57. if (File.Exists(PAC_FILE))
  58. {
  59. return PAC_FILE;
  60. }
  61. else
  62. {
  63. FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt);
  64. return PAC_FILE;
  65. }
  66. }
  67. public void AcceptCallback(IAsyncResult ar)
  68. {
  69. try
  70. {
  71. Socket listener = (Socket)ar.AsyncState;
  72. Socket conn = listener.EndAccept(ar);
  73. listener.BeginAccept(
  74. new AsyncCallback(AcceptCallback),
  75. listener);
  76. conn.BeginReceive(new byte[1024], 0, 1024, 0,
  77. new AsyncCallback(ReceiveCallback), conn);
  78. }
  79. catch (Exception e)
  80. {
  81. Console.WriteLine(e.Message);
  82. }
  83. }
  84. private string GetPACContent()
  85. {
  86. if (File.Exists(PAC_FILE))
  87. {
  88. return File.ReadAllText(PAC_FILE, Encoding.UTF8);
  89. }
  90. else
  91. {
  92. byte[] pacGZ = Resources.proxy_pac_txt;
  93. byte[] buffer = new byte[1024 * 1024]; // builtin pac gzip size: maximum 1M
  94. int n;
  95. using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
  96. CompressionMode.Decompress, false))
  97. {
  98. n = input.Read(buffer, 0, buffer.Length);
  99. if (n == 0)
  100. {
  101. throw new IOException("can not decompress pac");
  102. }
  103. return System.Text.Encoding.UTF8.GetString(buffer, 0, n);
  104. }
  105. }
  106. }
  107. private void ReceiveCallback(IAsyncResult ar)
  108. {
  109. Socket conn = (Socket)ar.AsyncState;
  110. try
  111. {
  112. int bytesRead = conn.EndReceive(ar);
  113. string pac = GetPACContent();
  114. IPEndPoint localEndPoint = (IPEndPoint)conn.LocalEndPoint;
  115. string proxy = "PROXY " + localEndPoint.Address + ":8123;";
  116. pac = pac.Replace("__PROXY__", proxy);
  117. if (bytesRead > 0)
  118. {
  119. string text = String.Format(@"HTTP/1.1 200 OK
  120. Server: Shadowsocks
  121. Content-Type: application/x-ns-proxy-autoconfig
  122. Content-Length: {0}
  123. Connection: Close
  124. ", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac;
  125. byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
  126. conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), conn);
  127. }
  128. else
  129. {
  130. conn.Close();
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. Console.WriteLine(e.Message);
  136. conn.Close();
  137. }
  138. }
  139. private void SendCallback(IAsyncResult ar)
  140. {
  141. Socket conn = (Socket)ar.AsyncState;
  142. conn.Shutdown(SocketShutdown.Send);
  143. }
  144. private void WatchPacFile()
  145. {
  146. if (watcher != null)
  147. {
  148. watcher.Dispose();
  149. }
  150. watcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  151. watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  152. watcher.Filter = PAC_FILE;
  153. watcher.Changed += Watcher_Changed;
  154. watcher.Created += Watcher_Changed;
  155. watcher.Deleted += Watcher_Changed;
  156. watcher.Renamed += Watcher_Changed;
  157. watcher.EnableRaisingEvents = true;
  158. }
  159. private void Watcher_Changed(object sender, FileSystemEventArgs e)
  160. {
  161. if (PACFileChanged != null)
  162. {
  163. PACFileChanged(this, new EventArgs());
  164. }
  165. }
  166. }
  167. }