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

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