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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.Socket, 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. public byte[] remoteRecvBuffer = new byte[RecvSize];
  92. // remote send buffer
  93. public byte[] remoteSendBuffer = new byte[BufferSize];
  94. // connection receive buffer
  95. public byte[] connetionRecvBuffer = new byte[RecvSize];
  96. // connection send buffer
  97. public byte[] connetionSendBuffer = new byte[BufferSize];
  98. // Received data string.
  99. public StringBuilder sb = new StringBuilder();
  100. private bool connectionShutdown = false;
  101. private bool remoteShutdown = false;
  102. private bool closed = false;
  103. public void Start()
  104. {
  105. try
  106. {
  107. // TODO async resolving
  108. IPAddress ipAddress;
  109. bool parsed = IPAddress.TryParse(config.server, out ipAddress);
  110. if (!parsed)
  111. {
  112. IPHostEntry ipHostInfo = Dns.GetHostEntry(config.server);
  113. ipAddress = ipHostInfo.AddressList[0];
  114. }
  115. IPEndPoint remoteEP = new IPEndPoint(ipAddress, config.server_port);
  116. remote = new Socket(ipAddress.AddressFamily,
  117. SocketType.Stream, ProtocolType.Tcp);
  118. remote.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
  119. // Connect to the remote endpoint.
  120. remote.BeginConnect(remoteEP,
  121. new AsyncCallback(ConnectCallback), null);
  122. }
  123. catch (Exception e)
  124. {
  125. Console.WriteLine(e.Message);
  126. this.Close();
  127. }
  128. }
  129. private void CheckClose()
  130. {
  131. if (connectionShutdown && remoteShutdown)
  132. {
  133. this.Close();
  134. }
  135. }
  136. public void Close()
  137. {
  138. lock (this)
  139. {
  140. if (closed)
  141. {
  142. return;
  143. }
  144. closed = true;
  145. }
  146. if (connection != null)
  147. {
  148. try
  149. {
  150. connection.Shutdown(SocketShutdown.Both);
  151. connection.Close();
  152. }
  153. catch (Exception e)
  154. {
  155. Console.WriteLine(e.Message);
  156. }
  157. }
  158. if (remote != null)
  159. {
  160. try
  161. {
  162. remote.Shutdown(SocketShutdown.Both);
  163. remote.Close();
  164. }
  165. catch (SocketException e)
  166. {
  167. Console.WriteLine(e.Message);
  168. }
  169. }
  170. ((IDisposable)encryptor).Dispose();
  171. }
  172. private void ConnectCallback(IAsyncResult ar)
  173. {
  174. try
  175. {
  176. // Complete the connection.
  177. remote.EndConnect(ar);
  178. //Console.WriteLine("Socket connected to {0}",
  179. // remote.RemoteEndPoint.ToString());
  180. HandshakeReceive();
  181. }
  182. catch (Exception e)
  183. {
  184. Console.WriteLine(e.Message);
  185. this.Close();
  186. }
  187. }
  188. private void HandshakeReceive()
  189. {
  190. try
  191. {
  192. connection.BeginReceive(connetionRecvBuffer, 0, 256, 0,
  193. new AsyncCallback(HandshakeReceiveCallback), null);
  194. }
  195. catch (Exception e)
  196. {
  197. Console.WriteLine(e.Message);
  198. this.Close();
  199. }
  200. }
  201. private void HandshakeReceiveCallback(IAsyncResult ar)
  202. {
  203. try
  204. {
  205. int bytesRead = connection.EndReceive(ar);
  206. if (bytesRead > 1)
  207. {
  208. byte[] response = { 5, 0 };
  209. if (connetionRecvBuffer[0] != 5)
  210. {
  211. // reject socks 4
  212. response = new byte[]{ 0, 91 };
  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. this.Close();
  261. }
  262. }
  263. catch (Exception e)
  264. {
  265. Console.WriteLine(e.Message);
  266. this.Close();
  267. }
  268. }
  269. private void StartPipe(IAsyncResult ar)
  270. {
  271. try
  272. {
  273. connection.EndReceive(ar);
  274. remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0,
  275. new AsyncCallback(PipeRemoteReceiveCallback), null);
  276. connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
  277. new AsyncCallback(PipeConnectionReceiveCallback), null);
  278. }
  279. catch (Exception e)
  280. {
  281. Console.WriteLine(e.Message);
  282. this.Close();
  283. }
  284. }
  285. private void PipeRemoteReceiveCallback(IAsyncResult ar)
  286. {
  287. try
  288. {
  289. int bytesRead = remote.EndReceive(ar);
  290. if (bytesRead > 0)
  291. {
  292. int bytesToSend;
  293. encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend);
  294. connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null);
  295. }
  296. else
  297. {
  298. //Console.WriteLine("bytesRead: " + bytesRead.ToString());
  299. connection.Shutdown(SocketShutdown.Send);
  300. connectionShutdown = true;
  301. CheckClose();
  302. }
  303. }
  304. catch (Exception e)
  305. {
  306. Console.WriteLine(e.Message);
  307. this.Close();
  308. }
  309. }
  310. private void PipeConnectionReceiveCallback(IAsyncResult ar)
  311. {
  312. try
  313. {
  314. int bytesRead = connection.EndReceive(ar);
  315. if (bytesRead > 0)
  316. {
  317. int bytesToSend;
  318. encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend);
  319. remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null);
  320. }
  321. else
  322. {
  323. remote.Shutdown(SocketShutdown.Send);
  324. remoteShutdown = true;
  325. CheckClose();
  326. }
  327. }
  328. catch (Exception e)
  329. {
  330. Console.WriteLine(e.Message);
  331. this.Close();
  332. }
  333. }
  334. private void PipeRemoteSendCallback(IAsyncResult ar)
  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. Console.WriteLine(e.Message);
  345. this.Close();
  346. }
  347. }
  348. private void PipeConnectionSendCallback(IAsyncResult ar)
  349. {
  350. try
  351. {
  352. connection.EndSend(ar);
  353. remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0,
  354. new AsyncCallback(PipeRemoteReceiveCallback), null);
  355. }
  356. catch (Exception e)
  357. {
  358. Console.WriteLine(e.Message);
  359. this.Close();
  360. }
  361. }
  362. }
  363. }