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.4 kB

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