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.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 : Listener.Service
  15. {
  16. public static string PAC_FILE = "pac.txt";
  17. FileSystemWatcher watcher;
  18. private Configuration _config;
  19. public event EventHandler PACFileChanged;
  20. public PACServer()
  21. {
  22. this.WatchPacFile();
  23. }
  24. public void UpdateConfiguration(Configuration config)
  25. {
  26. this._config = config;
  27. }
  28. public bool Handle(byte[] firstPacket, int length, Socket socket)
  29. {
  30. try
  31. {
  32. string request = Encoding.UTF8.GetString(firstPacket, 0, length);
  33. string[] lines = request.Split('\r', '\n');
  34. bool hostMatch = false, pathMatch = false, useSocks = false;
  35. foreach (string line in lines)
  36. {
  37. string[] kv = line.Split(new char[]{':'}, 2);
  38. if (kv.Length == 2)
  39. {
  40. if (kv[0] == "Host")
  41. {
  42. if (kv[1].Trim() == ((IPEndPoint)socket.LocalEndPoint).ToString())
  43. {
  44. hostMatch = true;
  45. }
  46. }
  47. else if (kv[0] == "User-Agent")
  48. {
  49. if (kv[1].IndexOf("Chrome") >= 0)
  50. {
  51. useSocks = true;
  52. }
  53. }
  54. }
  55. else if (kv.Length == 1)
  56. {
  57. if (line.IndexOf("pac") >= 0)
  58. {
  59. pathMatch = true;
  60. }
  61. }
  62. }
  63. if (hostMatch && pathMatch)
  64. {
  65. SendResponse(firstPacket, length, socket, useSocks);
  66. return true;
  67. }
  68. return false;
  69. }
  70. catch (ArgumentException)
  71. {
  72. return false;
  73. }
  74. }
  75. public string TouchPACFile()
  76. {
  77. if (File.Exists(PAC_FILE))
  78. {
  79. return PAC_FILE;
  80. }
  81. else
  82. {
  83. FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt);
  84. return PAC_FILE;
  85. }
  86. }
  87. private string GetPACContent()
  88. {
  89. if (File.Exists(PAC_FILE))
  90. {
  91. return File.ReadAllText(PAC_FILE, Encoding.UTF8);
  92. }
  93. else
  94. {
  95. return Utils.UnGzip(Resources.proxy_pac_txt);
  96. }
  97. }
  98. public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks)
  99. {
  100. try
  101. {
  102. string pac = GetPACContent();
  103. IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
  104. string proxy = GetPACAddress(firstPacket, length, localEndPoint, useSocks);
  105. pac = pac.Replace("__PROXY__", proxy);
  106. string text = String.Format(@"HTTP/1.1 200 OK
  107. Server: Shadowsocks
  108. Content-Type: application/x-ns-proxy-autoconfig
  109. Content-Length: {0}
  110. Connection: Close
  111. ", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac;
  112. byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
  113. socket.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), socket);
  114. Util.Utils.ReleaseMemory();
  115. }
  116. catch (Exception e)
  117. {
  118. Console.WriteLine(e);
  119. socket.Close();
  120. }
  121. }
  122. private void SendCallback(IAsyncResult ar)
  123. {
  124. Socket conn = (Socket)ar.AsyncState;
  125. try
  126. {
  127. conn.Shutdown(SocketShutdown.Send);
  128. }
  129. catch
  130. { }
  131. }
  132. private void WatchPacFile()
  133. {
  134. if (watcher != null)
  135. {
  136. watcher.Dispose();
  137. }
  138. watcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  139. watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  140. watcher.Filter = PAC_FILE;
  141. watcher.Changed += Watcher_Changed;
  142. watcher.Created += Watcher_Changed;
  143. watcher.Deleted += Watcher_Changed;
  144. watcher.Renamed += Watcher_Changed;
  145. watcher.EnableRaisingEvents = true;
  146. }
  147. private void Watcher_Changed(object sender, FileSystemEventArgs e)
  148. {
  149. if (PACFileChanged != null)
  150. {
  151. PACFileChanged(this, new EventArgs());
  152. }
  153. }
  154. private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks)
  155. {
  156. //try
  157. //{
  158. // string requestString = Encoding.UTF8.GetString(requestBuf);
  159. // if (requestString.IndexOf("AppleWebKit") >= 0)
  160. // {
  161. // string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port;
  162. // proxy = "SOCKS5 " + address + "; SOCKS " + address + ";";
  163. // }
  164. //}
  165. //catch (Exception e)
  166. //{
  167. // Console.WriteLine(e);
  168. //}
  169. return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";";
  170. }
  171. }
  172. }