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