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

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