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.

PortForwarder.cs 8.3 kB

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using Shadowsocks.Util;
  5. namespace Shadowsocks.Controller
  6. {
  7. class PortForwarder : Listener.Service
  8. {
  9. int _targetPort;
  10. public PortForwarder(int targetPort)
  11. {
  12. this._targetPort = targetPort;
  13. }
  14. public bool Handle(byte[] firstPacket, int length, Socket socket, object state)
  15. {
  16. if (socket.ProtocolType != ProtocolType.Tcp)
  17. {
  18. return false;
  19. }
  20. new Handler().Start(firstPacket, length, socket, this._targetPort);
  21. return true;
  22. }
  23. class Handler
  24. {
  25. private byte[] _firstPacket;
  26. private int _firstPacketLength;
  27. private Socket _local;
  28. private Socket _remote;
  29. private bool _closed = false;
  30. private bool _localShutdown = false;
  31. private bool _remoteShutdown = false;
  32. public const int RecvSize = 16384;
  33. // remote receive buffer
  34. private byte[] remoteRecvBuffer = new byte[RecvSize];
  35. // connection receive buffer
  36. private byte[] connetionRecvBuffer = new byte[RecvSize];
  37. public void Start(byte[] firstPacket, int length, Socket socket, int targetPort)
  38. {
  39. this._firstPacket = firstPacket;
  40. this._firstPacketLength = length;
  41. this._local = socket;
  42. try
  43. {
  44. EndPoint remoteEP = SocketUtil.GetEndPoint("localhost", targetPort);
  45. _remote = SocketUtil.CreateSocket(remoteEP);
  46. _remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  47. // Connect to the remote endpoint.
  48. _remote.BeginConnect(remoteEP,
  49. new AsyncCallback(ConnectCallback), null);
  50. }
  51. catch (Exception e)
  52. {
  53. Logging.LogUsefulException(e);
  54. this.Close();
  55. }
  56. }
  57. private void ConnectCallback(IAsyncResult ar)
  58. {
  59. if (_closed)
  60. {
  61. return;
  62. }
  63. try
  64. {
  65. _remote.EndConnect(ar);
  66. HandshakeReceive();
  67. }
  68. catch (Exception e)
  69. {
  70. Logging.LogUsefulException(e);
  71. this.Close();
  72. }
  73. }
  74. private void HandshakeReceive()
  75. {
  76. if (_closed)
  77. {
  78. return;
  79. }
  80. try
  81. {
  82. _remote.BeginSend(_firstPacket, 0, _firstPacketLength, 0, new AsyncCallback(StartPipe), null);
  83. }
  84. catch (Exception e)
  85. {
  86. Logging.LogUsefulException(e);
  87. this.Close();
  88. }
  89. }
  90. private void StartPipe(IAsyncResult ar)
  91. {
  92. if (_closed)
  93. {
  94. return;
  95. }
  96. try
  97. {
  98. _remote.EndSend(ar);
  99. _remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0,
  100. new AsyncCallback(PipeRemoteReceiveCallback), null);
  101. _local.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
  102. new AsyncCallback(PipeConnectionReceiveCallback), null);
  103. }
  104. catch (Exception e)
  105. {
  106. Logging.LogUsefulException(e);
  107. this.Close();
  108. }
  109. }
  110. private void PipeRemoteReceiveCallback(IAsyncResult ar)
  111. {
  112. if (_closed)
  113. {
  114. return;
  115. }
  116. try
  117. {
  118. int bytesRead = _remote.EndReceive(ar);
  119. if (bytesRead > 0)
  120. {
  121. _local.BeginSend(remoteRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeConnectionSendCallback), null);
  122. }
  123. else
  124. {
  125. _local.Shutdown(SocketShutdown.Send);
  126. _localShutdown = true;
  127. CheckClose();
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. Logging.LogUsefulException(e);
  133. this.Close();
  134. }
  135. }
  136. private void PipeConnectionReceiveCallback(IAsyncResult ar)
  137. {
  138. if (_closed)
  139. {
  140. return;
  141. }
  142. try
  143. {
  144. int bytesRead = _local.EndReceive(ar);
  145. if (bytesRead > 0)
  146. {
  147. _remote.BeginSend(connetionRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeRemoteSendCallback), null);
  148. }
  149. else
  150. {
  151. _remote.Shutdown(SocketShutdown.Send);
  152. _remoteShutdown = true;
  153. CheckClose();
  154. }
  155. }
  156. catch (Exception e)
  157. {
  158. Logging.LogUsefulException(e);
  159. this.Close();
  160. }
  161. }
  162. private void PipeRemoteSendCallback(IAsyncResult ar)
  163. {
  164. if (_closed)
  165. {
  166. return;
  167. }
  168. try
  169. {
  170. _remote.EndSend(ar);
  171. _local.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0,
  172. new AsyncCallback(PipeConnectionReceiveCallback), null);
  173. }
  174. catch (Exception e)
  175. {
  176. Logging.LogUsefulException(e);
  177. this.Close();
  178. }
  179. }
  180. private void PipeConnectionSendCallback(IAsyncResult ar)
  181. {
  182. if (_closed)
  183. {
  184. return;
  185. }
  186. try
  187. {
  188. _local.EndSend(ar);
  189. _remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0,
  190. new AsyncCallback(PipeRemoteReceiveCallback), null);
  191. }
  192. catch (Exception e)
  193. {
  194. Logging.LogUsefulException(e);
  195. this.Close();
  196. }
  197. }
  198. private void CheckClose()
  199. {
  200. if (_localShutdown && _remoteShutdown)
  201. {
  202. this.Close();
  203. }
  204. }
  205. public void Close()
  206. {
  207. lock (this)
  208. {
  209. if (_closed)
  210. {
  211. return;
  212. }
  213. _closed = true;
  214. }
  215. if (_local != null)
  216. {
  217. try
  218. {
  219. _local.Shutdown(SocketShutdown.Both);
  220. _local.Close();
  221. }
  222. catch (Exception e)
  223. {
  224. Logging.LogUsefulException(e);
  225. }
  226. }
  227. if (_remote != null)
  228. {
  229. try
  230. {
  231. _remote.Shutdown(SocketShutdown.Both);
  232. _remote.Close();
  233. }
  234. catch (SocketException e)
  235. {
  236. Logging.LogUsefulException(e);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }