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