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 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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(@"^(?<method>[A-Z]+?) (?<path>[^\s]+) (?<tail>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 (_requestLineCount == 0)
  83. {
  84. var m = HttpRequestHeaderRegex.Match(line);
  85. if (m.Success)
  86. {
  87. var method = m.Groups["method"].Value;
  88. var path = m.Groups["path"].Value;
  89. if (method == "CONNECT")
  90. {
  91. _isConnect = true;
  92. if (!ParseHost(path))
  93. {
  94. throw new Exception("Bad http header: " + line);
  95. }
  96. }
  97. else
  98. {
  99. var targetUrl = new Uri(path);
  100. if (!ParseHost(targetUrl.Authority))
  101. {
  102. throw new Exception("Bad http header: " + line);
  103. }
  104. var newRequestLine = $"{method} {targetUrl.PathAndQuery} {m.Groups["tail"].Value}";
  105. _headers.Enqueue(newRequestLine);
  106. }
  107. }
  108. else
  109. {
  110. throw new FormatException("Not a vaild request line");
  111. }
  112. }
  113. else
  114. {
  115. // Handle Proxy-x Headers
  116. if (!line.StartsWith("Proxy-"))
  117. {
  118. _headers.Enqueue(line);
  119. }
  120. else
  121. {
  122. if (line.StartsWith("Proxy-Connection: "))
  123. {
  124. _headers.Enqueue(line.Substring(6));
  125. }
  126. }
  127. if (line.IsNullOrEmpty())
  128. {
  129. return true;
  130. }
  131. if (!_isConnect)
  132. {
  133. if (line.StartsWith("Host: "))
  134. {
  135. if (!ParseHost(line.Substring(6).Trim()))
  136. {
  137. throw new Exception("Bad http header: " + line);
  138. }
  139. }
  140. }
  141. }
  142. _requestLineCount++;
  143. return false;
  144. }
  145. private void OnFinish(byte[] lastBytes, int index, int length, object state)
  146. {
  147. if (Closed)
  148. {
  149. return;
  150. }
  151. if (_targetHost == null)
  152. {
  153. Logging.Error("Unknown host");
  154. Close();
  155. }
  156. else
  157. {
  158. if (length > 0)
  159. {
  160. _lastBytes = lastBytes;
  161. _lastBytesIndex = index;
  162. _lastBytesLength = length;
  163. }
  164. StartConnect(SocketUtil.GetEndPoint(_targetHost, _targetPort));
  165. }
  166. }
  167. #endregion
  168. protected override void OnServerConnected(AsyncSession session)
  169. {
  170. if (_isConnect)
  171. {
  172. // http connect response
  173. SendConnectResponse(session);
  174. }
  175. else
  176. {
  177. // send header
  178. SendHeader(session);
  179. }
  180. }
  181. #region CONNECT
  182. private void SendConnectResponse(AsyncSession session)
  183. {
  184. var len = Encoding.UTF8.GetBytes(HTTP_CONNECT_200, 0, HTTP_CONNECT_200.Length, RemoteRecvBuffer, 0);
  185. _localSocket.BeginSend(RemoteRecvBuffer, 0, len, SocketFlags.None, Http200SendCallback, session);
  186. }
  187. private void Http200SendCallback(IAsyncResult ar)
  188. {
  189. if (Closed)
  190. {
  191. return;
  192. }
  193. try
  194. {
  195. _localSocket.EndSend(ar);
  196. StartPipe((AsyncSession) ar.AsyncState);
  197. }
  198. catch (ArgumentException)
  199. {
  200. }
  201. catch (Exception e)
  202. {
  203. Logging.LogUsefulException(e);
  204. Close();
  205. }
  206. }
  207. #endregion
  208. #region Other http method except CONNECT
  209. private void SendHeader(AsyncSession session)
  210. {
  211. var h = _headers.Dequeue() + HTTP_CRLF;
  212. var len = Encoding.UTF8.GetBytes(h, 0, h.Length, ConnetionRecvBuffer, 0);
  213. BeginSendToServer(len, session, HeaderSendCallback);
  214. }
  215. private void HeaderSendCallback(IAsyncResult ar)
  216. {
  217. if (Closed)
  218. {
  219. return;
  220. }
  221. try
  222. {
  223. var session = EndSendToServer(ar);
  224. if (_headers.Count > 0)
  225. {
  226. SendHeader(session);
  227. }
  228. else
  229. {
  230. StartPipe(session);
  231. }
  232. }
  233. catch (Exception e)
  234. {
  235. Logging.LogUsefulException(e);
  236. Close();
  237. }
  238. }
  239. #endregion
  240. }
  241. }