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

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