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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. throw ex;
  50. }
  51. private static readonly Regex HttpRequestHeaderRegex = new Regex(@"^([A-Z]+?) ([^\s]+) HTTP/1\.\d$");
  52. private int _requestLineCount = 0;
  53. private volatile bool _isConnect = false;
  54. private string _targetHost;
  55. private int _targetPort;
  56. private readonly Queue<string> _headers = new Queue<string>();
  57. private bool ParseHost(string host)
  58. {
  59. var locs = host.Split(':');
  60. _targetHost = locs[0];
  61. if (locs.Length > 1)
  62. {
  63. if (!int.TryParse(locs[1], out _targetPort))
  64. {
  65. return false;
  66. }
  67. }
  68. else
  69. {
  70. _targetPort = 80;
  71. }
  72. return true;
  73. }
  74. private bool OnLineRead(string line, object state)
  75. {
  76. if (Closed)
  77. {
  78. return true;
  79. }
  80. Logging.Debug(line);
  81. if (!line.StartsWith("Proxy-"))
  82. {
  83. _headers.Enqueue(line);
  84. }
  85. if (_requestLineCount == 0)
  86. {
  87. var m = HttpRequestHeaderRegex.Match(line);
  88. if (m.Success)
  89. {
  90. var method = m.Groups[1].Value;
  91. if (method == "CONNECT")
  92. {
  93. _isConnect = true;
  94. if (!ParseHost(m.Groups[2].Value))
  95. {
  96. throw new Exception("Bad http header: " + line);
  97. }
  98. }
  99. }
  100. }
  101. else
  102. {
  103. if (line.IsNullOrEmpty())
  104. {
  105. return true;
  106. }
  107. if (!_isConnect)
  108. {
  109. if (line.StartsWith("Host: "))
  110. {
  111. if (!ParseHost(line.Substring(6).Trim()))
  112. {
  113. throw new Exception("Bad http header: " + line);
  114. }
  115. }
  116. }
  117. }
  118. _requestLineCount++;
  119. return false;
  120. }
  121. private void OnFinish(byte[] lastBytes, int index, int length, object state)
  122. {
  123. if (Closed)
  124. {
  125. return;
  126. }
  127. if (_targetHost == null)
  128. {
  129. Logging.Error("Unkonwn host");
  130. Close();
  131. }
  132. else
  133. {
  134. if (length > 0)
  135. {
  136. _lastBytes = lastBytes;
  137. _lastBytesIndex = index;
  138. _lastBytesLength = length;
  139. }
  140. StartConnect(SocketUtil.GetEndPoint(_targetHost, _targetPort));
  141. }
  142. }
  143. #endregion
  144. protected override void OnServerConnected(AsyncSession session)
  145. {
  146. if (_isConnect)
  147. {
  148. // http connect response
  149. SendConnectResponse(session);
  150. }
  151. else
  152. {
  153. // send header
  154. SendHeader(session);
  155. }
  156. }
  157. #region CONNECT
  158. private void SendConnectResponse(AsyncSession session)
  159. {
  160. var len = Encoding.UTF8.GetBytes(HTTP_CONNECT_200, 0, HTTP_CONNECT_200.Length, RemoteRecvBuffer, 0);
  161. _localSocket.BeginSend(RemoteRecvBuffer, 0, len, SocketFlags.None, Http200SendCallback, session);
  162. }
  163. private void Http200SendCallback(IAsyncResult ar)
  164. {
  165. if (Closed)
  166. {
  167. return;
  168. }
  169. try
  170. {
  171. _localSocket.EndSend(ar);
  172. StartPipe((AsyncSession) ar.AsyncState);
  173. }
  174. catch (ArgumentException)
  175. {
  176. }
  177. catch (Exception e)
  178. {
  179. Logging.LogUsefulException(e);
  180. Close();
  181. }
  182. }
  183. #endregion
  184. #region Other http method except CONNECT
  185. private void SendHeader(AsyncSession session)
  186. {
  187. var h = _headers.Dequeue() + HTTP_CRLF;
  188. var len = Encoding.UTF8.GetBytes(h, 0, h.Length, ConnetionRecvBuffer, 0);
  189. BeginSendToServer(len, session, HeaderSendCallback);
  190. }
  191. private void HeaderSendCallback(IAsyncResult ar)
  192. {
  193. if (Closed)
  194. {
  195. return;
  196. }
  197. try
  198. {
  199. var session = EndSendToServer(ar);
  200. if (_headers.Count > 0)
  201. {
  202. SendHeader(session);
  203. }
  204. else
  205. {
  206. StartPipe(session);
  207. }
  208. }
  209. catch (Exception e)
  210. {
  211. Logging.LogUsefulException(e);
  212. Close();
  213. }
  214. }
  215. #endregion
  216. }
  217. }