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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using Shadowsocks.Util.Sockets;
  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 override 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 WrappedSocket _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. // Connect to the remote endpoint.
  46. _remote = new WrappedSocket();
  47. _remote.BeginConnect(remoteEP, ConnectCallback, null);
  48. }
  49. catch (Exception e)
  50. {
  51. Logging.LogUsefulException(e);
  52. this.Close();
  53. }
  54. }
  55. private void ConnectCallback(IAsyncResult ar)
  56. {
  57. if (_closed)
  58. {
  59. return;
  60. }
  61. try
  62. {
  63. _remote.EndConnect(ar);
  64. _remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  65. HandshakeReceive();
  66. }
  67. catch (Exception e)
  68. {
  69. Logging.LogUsefulException(e);
  70. this.Close();
  71. }
  72. }
  73. private void HandshakeReceive()
  74. {
  75. if (_closed)
  76. {
  77. return;
  78. }
  79. try
  80. {
  81. _remote.BeginSend(_firstPacket, 0, _firstPacketLength, 0, new AsyncCallback(StartPipe), null);
  82. }
  83. catch (Exception e)
  84. {
  85. Logging.LogUsefulException(e);
  86. this.Close();
  87. }
  88. }
  89. private void StartPipe(IAsyncResult ar)
  90. {
  91. if (_closed)
  92. {
  93. return;
  94. }
  95. try
  96. {
  97. _remote.EndSend(ar);
  98. _remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0,
  99. new AsyncCallback(PipeRemoteReceiveCallback), null);
  100. _local.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
  101. new AsyncCallback(PipeConnectionReceiveCallback), null);
  102. }
  103. catch (Exception e)
  104. {
  105. Logging.LogUsefulException(e);
  106. this.Close();
  107. }
  108. }
  109. private void PipeRemoteReceiveCallback(IAsyncResult ar)
  110. {
  111. if (_closed)
  112. {
  113. return;
  114. }
  115. try
  116. {
  117. int bytesRead = _remote.EndReceive(ar);
  118. if (bytesRead > 0)
  119. {
  120. _local.BeginSend(remoteRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeConnectionSendCallback), null);
  121. }
  122. else
  123. {
  124. _local.Shutdown(SocketShutdown.Send);
  125. _localShutdown = true;
  126. CheckClose();
  127. }
  128. }
  129. catch (Exception e)
  130. {
  131. Logging.LogUsefulException(e);
  132. this.Close();
  133. }
  134. }
  135. private void PipeConnectionReceiveCallback(IAsyncResult ar)
  136. {
  137. if (_closed)
  138. {
  139. return;
  140. }
  141. try
  142. {
  143. int bytesRead = _local.EndReceive(ar);
  144. if (bytesRead > 0)
  145. {
  146. _remote.BeginSend(connetionRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeRemoteSendCallback), null);
  147. }
  148. else
  149. {
  150. _remote.Shutdown(SocketShutdown.Send);
  151. _remoteShutdown = true;
  152. CheckClose();
  153. }
  154. }
  155. catch (Exception e)
  156. {
  157. Logging.LogUsefulException(e);
  158. this.Close();
  159. }
  160. }
  161. private void PipeRemoteSendCallback(IAsyncResult ar)
  162. {
  163. if (_closed)
  164. {
  165. return;
  166. }
  167. try
  168. {
  169. _remote.EndSend(ar);
  170. _local.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0,
  171. new AsyncCallback(PipeConnectionReceiveCallback), null);
  172. }
  173. catch (Exception e)
  174. {
  175. Logging.LogUsefulException(e);
  176. this.Close();
  177. }
  178. }
  179. private void PipeConnectionSendCallback(IAsyncResult ar)
  180. {
  181. if (_closed)
  182. {
  183. return;
  184. }
  185. try
  186. {
  187. _local.EndSend(ar);
  188. _remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0,
  189. new AsyncCallback(PipeRemoteReceiveCallback), null);
  190. }
  191. catch (Exception e)
  192. {
  193. Logging.LogUsefulException(e);
  194. this.Close();
  195. }
  196. }
  197. private void CheckClose()
  198. {
  199. if (_localShutdown && _remoteShutdown)
  200. {
  201. this.Close();
  202. }
  203. }
  204. public void Close()
  205. {
  206. lock (this)
  207. {
  208. if (_closed)
  209. {
  210. return;
  211. }
  212. _closed = true;
  213. }
  214. if (_local != null)
  215. {
  216. try
  217. {
  218. _local.Shutdown(SocketShutdown.Both);
  219. _local.Close();
  220. }
  221. catch (Exception e)
  222. {
  223. Logging.LogUsefulException(e);
  224. }
  225. }
  226. if (_remote != null)
  227. {
  228. try
  229. {
  230. _remote.Shutdown(SocketShutdown.Both);
  231. _remote.Dispose();
  232. }
  233. catch (SocketException e)
  234. {
  235. Logging.LogUsefulException(e);
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }