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.

HttpHandler.cs 7.1 kB

8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Shadowsocks.Model;
  8. using Shadowsocks.Util.Sockets;
  9. using System.Text.RegularExpressions;
  10. namespace Shadowsocks.Controller.Service
  11. {
  12. class HttpHandlerHandlerFactory : ITCPHandlerFactory
  13. {
  14. private static readonly ByteSearch.SearchTarget HttpSearch =
  15. new ByteSearch.SearchTarget(Encoding.UTF8.GetBytes("HTTP"));
  16. public bool CanHandle(byte[] firstPacket, int length)
  17. {
  18. return HttpSearch.SearchIn(firstPacket, 0, length) != -1;
  19. }
  20. public TCPHandler NewHandler(ShadowsocksController controller, Configuration config, TCPRelay tcprelay, Socket socket)
  21. {
  22. return new HttpHandler(controller, config, tcprelay, socket);
  23. }
  24. }
  25. class HttpHandler : TCPHandler
  26. {
  27. private const string HTTP_CRLF = "\r\n";
  28. private const string HTTP_CONNECT_200 =
  29. "HTTP/1.1 200 Connection established" + HTTP_CRLF +
  30. "Proxy-Connection: close" + HTTP_CRLF +
  31. "Proxy-Agent: Shadowsocks" + HTTP_CRLF +
  32. "" + HTTP_CRLF; // End with an empty line
  33. private readonly WrappedSocket _localSocket;
  34. private byte[] _lastBytes;
  35. private int _lastBytesIndex;
  36. private int _lastBytesLength;
  37. public HttpHandler(ShadowsocksController controller, Configuration config, TCPRelay tcprelay, Socket socket) : base(controller, config, tcprelay, socket)
  38. {
  39. _localSocket = new WrappedSocket(socket);
  40. }
  41. public override void StartHandshake(byte[] firstPacket, int length)
  42. {
  43. new LineReader(firstPacket, _localSocket, firstPacket, 0, length, OnLineRead, OnException, OnFinish,
  44. Encoding.UTF8, HTTP_CRLF, null);
  45. }
  46. #region Header Parse
  47. private void OnException(Exception ex, object state)
  48. {
  49. Logging.LogUsefulException(ex);
  50. Close();
  51. }
  52. private static readonly Regex HttpRequestHeaderRegex = new Regex(@"^([A-Z]+?) ([^\s]+) HTTP/1\.\d$");
  53. private int _requestLineCount = 0;
  54. private bool _isConnect = false;
  55. private string _targetHost;
  56. private int _targetPort;
  57. private readonly Queue<string> _headers = new Queue<string>();
  58. private bool ParseHost(string host)
  59. {
  60. var locs = host.Split(':');
  61. _targetHost = locs[0];
  62. if (locs.Length > 1)
  63. {
  64. if (!int.TryParse(locs[1], out _targetPort))
  65. {
  66. return false;
  67. }
  68. }
  69. else
  70. {
  71. _targetPort = 80;
  72. }
  73. return true;
  74. }
  75. private bool OnLineRead(string line, object state)
  76. {
  77. if (Closed)
  78. {
  79. return true;
  80. }
  81. Logging.Debug(line);
  82. if (!line.StartsWith("Proxy-"))
  83. {
  84. _headers.Enqueue(line);
  85. }
  86. if (_requestLineCount == 0)
  87. {
  88. var m = HttpRequestHeaderRegex.Match(line);
  89. if (m.Success)
  90. {
  91. var method = m.Groups[1].Value;
  92. if (method == "CONNECT")
  93. {
  94. _isConnect = true;
  95. if (!ParseHost(m.Groups[2].Value))
  96. {
  97. throw new Exception("Bad http header: " + line);
  98. }
  99. }
  100. }
  101. }
  102. else
  103. {
  104. if (line.IsNullOrEmpty())
  105. {
  106. return true;
  107. }
  108. if (!_isConnect)
  109. {
  110. if (line.StartsWith("Host: "))
  111. {
  112. if (!ParseHost(line.Substring(6).Trim()))
  113. {
  114. throw new Exception("Bad http header: " + line);
  115. }
  116. }
  117. }
  118. }
  119. _requestLineCount++;
  120. return false;
  121. }
  122. private void OnFinish(byte[] lastBytes, int index, int length, object state)
  123. {
  124. if (Closed)
  125. {
  126. return;
  127. }
  128. if (_targetHost == null)
  129. {
  130. Logging.Error("Unknown host");
  131. Close();
  132. }
  133. else
  134. {
  135. if (length > 0)
  136. {
  137. _lastBytes = lastBytes;
  138. _lastBytesIndex = index;
  139. _lastBytesLength = length;
  140. }
  141. StartConnect(SocketUtil.GetEndPoint(_targetHost, _targetPort));
  142. }
  143. }
  144. #endregion
  145. protected override void OnServerConnected(AsyncSession session)
  146. {
  147. if (_isConnect)
  148. {
  149. // http connect response
  150. SendConnectResponse(session);
  151. }
  152. else
  153. {
  154. // send header
  155. SendHeader(session);
  156. }
  157. }
  158. #region CONNECT
  159. private void SendConnectResponse(AsyncSession session)
  160. {
  161. var len = Encoding.UTF8.GetBytes(HTTP_CONNECT_200, 0, HTTP_CONNECT_200.Length, RemoteRecvBuffer, 0);
  162. _localSocket.BeginSend(RemoteRecvBuffer, 0, len, SocketFlags.None, Http200SendCallback, session);
  163. }
  164. private void Http200SendCallback(IAsyncResult ar)
  165. {
  166. if (Closed)
  167. {
  168. return;
  169. }
  170. try
  171. {
  172. _localSocket.EndSend(ar);
  173. StartPipe((AsyncSession) ar.AsyncState);
  174. }
  175. catch (ArgumentException)
  176. {
  177. }
  178. catch (Exception e)
  179. {
  180. Logging.LogUsefulException(e);
  181. Close();
  182. }
  183. }
  184. #endregion
  185. #region Other http method except CONNECT
  186. private void SendHeader(AsyncSession session)
  187. {
  188. var h = _headers.Dequeue() + HTTP_CRLF;
  189. var len = Encoding.UTF8.GetBytes(h, 0, h.Length, ConnetionRecvBuffer, 0);
  190. BeginSendToServer(len, session, HeaderSendCallback);
  191. }
  192. private void HeaderSendCallback(IAsyncResult ar)
  193. {
  194. if (Closed)
  195. {
  196. return;
  197. }
  198. try
  199. {
  200. var session = EndSendToServer(ar);
  201. if (_headers.Count > 0)
  202. {
  203. SendHeader(session);
  204. }
  205. else
  206. {
  207. StartPipe(session);
  208. }
  209. }
  210. catch (Exception e)
  211. {
  212. Logging.LogUsefulException(e);
  213. Close();
  214. }
  215. }
  216. #endregion
  217. }
  218. }