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.

Local.cs 12 kB

12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
10 years ago
10 years ago
12 years ago
10 years ago
10 years ago
10 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net.Sockets;
  5. using System.Net;
  6. using Shadowsocks.Encryption;
  7. using Shadowsocks.Model;
  8. namespace Shadowsocks.Controller
  9. {
  10. class Local : Listener.Service
  11. {
  12. private Configuration _config;
  13. public Local(Configuration config)
  14. {
  15. this._config = config;
  16. }
  17. public bool Handle(byte[] firstPacket, int length, Socket socket)
  18. {
  19. if (length < 2 || firstPacket[0] != 5)
  20. {
  21. return false;
  22. }
  23. socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  24. Handler handler = new Handler();
  25. handler.connection = socket;
  26. Server server = _config.GetCurrentServer();
  27. handler.encryptor = EncryptorFactory.GetEncryptor(server.method, server.password);
  28. handler.server = server;
  29. handler.Start(firstPacket, length);
  30. return true;
  31. }
  32. }
  33. class Handler
  34. {
  35. //public Encryptor encryptor;
  36. public IEncryptor encryptor;
  37. public Server server;
  38. // Client socket.
  39. public Socket remote;
  40. public Socket connection;
  41. private byte[] _firstPacket;
  42. private int _firstPacketLength;
  43. // Size of receive buffer.
  44. public const int RecvSize = 16384;
  45. public const int BufferSize = RecvSize + 32;
  46. // remote receive buffer
  47. private byte[] remoteRecvBuffer = new byte[RecvSize];
  48. // remote send buffer
  49. private byte[] remoteSendBuffer = new byte[BufferSize];
  50. // connection receive buffer
  51. private byte[] connetionRecvBuffer = new byte[RecvSize];
  52. // connection send buffer
  53. private byte[] connetionSendBuffer = new byte[BufferSize];
  54. // Received data string.
  55. private bool connectionShutdown = false;
  56. private bool remoteShutdown = false;
  57. private bool closed = false;
  58. private object encryptionLock = new object();
  59. private object decryptionLock = new object();
  60. public void Start(byte[] firstPacket, int length)
  61. {
  62. this._firstPacket = firstPacket;
  63. this._firstPacketLength = length;
  64. try
  65. {
  66. // TODO async resolving
  67. IPAddress ipAddress;
  68. bool parsed = IPAddress.TryParse(server.server, out ipAddress);
  69. if (!parsed)
  70. {
  71. IPHostEntry ipHostInfo = Dns.GetHostEntry(server.server);
  72. ipAddress = ipHostInfo.AddressList[0];
  73. }
  74. IPEndPoint remoteEP = new IPEndPoint(ipAddress, server.server_port);
  75. remote = new Socket(ipAddress.AddressFamily,
  76. SocketType.Stream, ProtocolType.Tcp);
  77. remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  78. // Connect to the remote endpoint.
  79. remote.BeginConnect(remoteEP,
  80. new AsyncCallback(ConnectCallback), null);
  81. }
  82. catch (Exception e)
  83. {
  84. Logging.LogUsefulException(e);
  85. this.Close();
  86. }
  87. }
  88. private void CheckClose()
  89. {
  90. if (connectionShutdown && remoteShutdown)
  91. {
  92. this.Close();
  93. }
  94. }
  95. public void Close()
  96. {
  97. lock (this)
  98. {
  99. if (closed)
  100. {
  101. return;
  102. }
  103. closed = true;
  104. }
  105. if (connection != null)
  106. {
  107. try
  108. {
  109. connection.Shutdown(SocketShutdown.Both);
  110. connection.Close();
  111. }
  112. catch (Exception e)
  113. {
  114. Logging.LogUsefulException(e);
  115. }
  116. }
  117. if (remote != null)
  118. {
  119. try
  120. {
  121. remote.Shutdown(SocketShutdown.Both);
  122. remote.Close();
  123. }
  124. catch (SocketException e)
  125. {
  126. Logging.LogUsefulException(e);
  127. }
  128. }
  129. lock (encryptionLock)
  130. {
  131. lock (decryptionLock)
  132. {
  133. ((IDisposable)encryptor).Dispose();
  134. }
  135. }
  136. }
  137. private void ConnectCallback(IAsyncResult ar)
  138. {
  139. if (closed)
  140. {
  141. return;
  142. }
  143. try
  144. {
  145. // Complete the connection.
  146. remote.EndConnect(ar);
  147. //Console.WriteLine("Socket connected to {0}",
  148. // remote.RemoteEndPoint.ToString());
  149. HandshakeReceive();
  150. }
  151. catch (Exception e)
  152. {
  153. Logging.LogUsefulException(e);
  154. this.Close();
  155. }
  156. }
  157. private void HandshakeReceive()
  158. {
  159. if (closed)
  160. {
  161. return;
  162. }
  163. try
  164. {
  165. int bytesRead = _firstPacketLength;
  166. if (bytesRead > 1)
  167. {
  168. byte[] response = { 5, 0 };
  169. if (_firstPacket[0] != 5)
  170. {
  171. // reject socks 4
  172. response = new byte[] { 0, 91 };
  173. Console.WriteLine("socks 5 protocol error");
  174. }
  175. connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null);
  176. }
  177. else
  178. {
  179. this.Close();
  180. }
  181. }
  182. catch (Exception e)
  183. {
  184. Logging.LogUsefulException(e);
  185. this.Close();
  186. }
  187. }
  188. private void HandshakeSendCallback(IAsyncResult ar)
  189. {
  190. if (closed)
  191. {
  192. return;
  193. }
  194. try
  195. {
  196. connection.EndSend(ar);
  197. // +----+-----+-------+------+----------+----------+
  198. // |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
  199. // +----+-----+-------+------+----------+----------+
  200. // | 1 | 1 | X'00' | 1 | Variable | 2 |
  201. // +----+-----+-------+------+----------+----------+
  202. // Skip first 3 bytes
  203. // TODO validate
  204. connection.BeginReceive(connetionRecvBuffer, 0, 3, 0,
  205. new AsyncCallback(handshakeReceive2Callback), null);
  206. }
  207. catch (Exception e)
  208. {
  209. Logging.LogUsefulException(e);
  210. this.Close();
  211. }
  212. }
  213. private void handshakeReceive2Callback(IAsyncResult ar)
  214. {
  215. if (closed)
  216. {
  217. return;
  218. }
  219. try
  220. {
  221. int bytesRead = connection.EndReceive(ar);
  222. if (bytesRead > 0)
  223. {
  224. byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
  225. connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(StartPipe), null);
  226. }
  227. else
  228. {
  229. Console.WriteLine("failed to recv data in handshakeReceive2Callback");
  230. this.Close();
  231. }
  232. }
  233. catch (Exception e)
  234. {
  235. Logging.LogUsefulException(e);
  236. this.Close();
  237. }
  238. }
  239. private void StartPipe(IAsyncResult ar)
  240. {
  241. if (closed)
  242. {
  243. return;
  244. }
  245. try
  246. {
  247. connection.EndReceive(ar);
  248. remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0,
  249. new AsyncCallback(PipeRemoteReceiveCallback), null);
  250. connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
  251. new AsyncCallback(PipeConnectionReceiveCallback), null);
  252. }
  253. catch (Exception e)
  254. {
  255. Logging.LogUsefulException(e);
  256. this.Close();
  257. }
  258. }
  259. private void PipeRemoteReceiveCallback(IAsyncResult ar)
  260. {
  261. if (closed)
  262. {
  263. return;
  264. }
  265. try
  266. {
  267. int bytesRead = remote.EndReceive(ar);
  268. if (bytesRead > 0)
  269. {
  270. int bytesToSend;
  271. lock (decryptionLock)
  272. {
  273. if (closed)
  274. {
  275. return;
  276. }
  277. encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend);
  278. }
  279. connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null);
  280. }
  281. else
  282. {
  283. //Console.WriteLine("bytesRead: " + bytesRead.ToString());
  284. connection.Shutdown(SocketShutdown.Send);
  285. connectionShutdown = true;
  286. CheckClose();
  287. }
  288. }
  289. catch (Exception e)
  290. {
  291. Logging.LogUsefulException(e);
  292. this.Close();
  293. }
  294. }
  295. private void PipeConnectionReceiveCallback(IAsyncResult ar)
  296. {
  297. if (closed)
  298. {
  299. return;
  300. }
  301. try
  302. {
  303. int bytesRead = connection.EndReceive(ar);
  304. if (bytesRead > 0)
  305. {
  306. int bytesToSend;
  307. lock (encryptionLock)
  308. {
  309. if (closed)
  310. {
  311. return;
  312. }
  313. encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend);
  314. }
  315. remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null);
  316. }
  317. else
  318. {
  319. remote.Shutdown(SocketShutdown.Send);
  320. remoteShutdown = true;
  321. CheckClose();
  322. }
  323. }
  324. catch (Exception e)
  325. {
  326. Logging.LogUsefulException(e);
  327. this.Close();
  328. }
  329. }
  330. private void PipeRemoteSendCallback(IAsyncResult ar)
  331. {
  332. if (closed)
  333. {
  334. return;
  335. }
  336. try
  337. {
  338. remote.EndSend(ar);
  339. connection.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0,
  340. new AsyncCallback(PipeConnectionReceiveCallback), null);
  341. }
  342. catch (Exception e)
  343. {
  344. Logging.LogUsefulException(e);
  345. this.Close();
  346. }
  347. }
  348. private void PipeConnectionSendCallback(IAsyncResult ar)
  349. {
  350. if (closed)
  351. {
  352. return;
  353. }
  354. try
  355. {
  356. connection.EndSend(ar);
  357. remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0,
  358. new AsyncCallback(PipeRemoteReceiveCallback), null);
  359. }
  360. catch (Exception e)
  361. {
  362. Logging.LogUsefulException(e);
  363. this.Close();
  364. }
  365. }
  366. }
  367. }