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
10 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
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
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
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
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
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
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
12 years ago
10 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
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
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. listener.BeginAccept(
  64. new AsyncCallback(AcceptCallback),
  65. listener);
  66. Handler handler = new Handler();
  67. handler.connection = conn;
  68. handler.encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password);
  69. handler.config = _server;
  70. handler.Start();
  71. }
  72. catch
  73. {
  74. //Console.WriteLine(e.Message);
  75. }
  76. }
  77. }
  78. class Handler
  79. {
  80. //public Encryptor encryptor;
  81. public IEncryptor encryptor;
  82. public Server config;
  83. // Client socket.
  84. public Socket remote;
  85. public Socket connection;
  86. // Size of receive buffer.
  87. public const int RecvSize = 16384;
  88. public const int BufferSize = RecvSize + 32;
  89. // remote receive buffer
  90. public byte[] remoteRecvBuffer = new byte[RecvSize];
  91. // remote send buffer
  92. public byte[] remoteSendBuffer = new byte[BufferSize];
  93. // connection receive buffer
  94. public byte[] connetionRecvBuffer = new byte[RecvSize];
  95. // connection send buffer
  96. public byte[] connetionSendBuffer = new byte[BufferSize];
  97. // Received data string.
  98. public StringBuilder sb = new StringBuilder();
  99. private bool closed = false;
  100. public void Start()
  101. {
  102. try
  103. {
  104. // TODO async resolving
  105. IPAddress ipAddress;
  106. bool parsed = IPAddress.TryParse(config.server, out ipAddress);
  107. if (!parsed)
  108. {
  109. IPHostEntry ipHostInfo = Dns.GetHostEntry(config.server);
  110. ipAddress = ipHostInfo.AddressList[0];
  111. }
  112. IPEndPoint remoteEP = new IPEndPoint(ipAddress, config.server_port);
  113. remote = new Socket(ipAddress.AddressFamily,
  114. SocketType.Stream, ProtocolType.Tcp);
  115. // Connect to the remote endpoint.
  116. remote.BeginConnect(remoteEP,
  117. new AsyncCallback(ConnectCallback), null);
  118. }
  119. catch (Exception e)
  120. {
  121. Console.WriteLine(e.Message);
  122. this.Close();
  123. }
  124. }
  125. public void Close()
  126. {
  127. lock (this)
  128. {
  129. if (closed)
  130. {
  131. return;
  132. }
  133. closed = true;
  134. }
  135. if (connection != null)
  136. {
  137. try
  138. {
  139. connection.Shutdown(SocketShutdown.Send);
  140. }
  141. catch (Exception e)
  142. {
  143. Console.WriteLine(e.Message);
  144. }
  145. }
  146. if (remote != null)
  147. {
  148. try
  149. {
  150. remote.Shutdown(SocketShutdown.Send);
  151. }
  152. catch (SocketException e)
  153. {
  154. Console.WriteLine(e.Message);
  155. }
  156. }
  157. ((IDisposable)encryptor).Dispose();
  158. }
  159. private void ConnectCallback(IAsyncResult ar)
  160. {
  161. try
  162. {
  163. // Complete the connection.
  164. remote.EndConnect(ar);
  165. //Console.WriteLine("Socket connected to {0}",
  166. // remote.RemoteEndPoint.ToString());
  167. HandshakeReceive();
  168. }
  169. catch (Exception e)
  170. {
  171. Console.WriteLine(e.Message);
  172. this.Close();
  173. }
  174. }
  175. private void HandshakeReceive()
  176. {
  177. try
  178. {
  179. connection.BeginReceive(connetionRecvBuffer, 0, 256, 0,
  180. new AsyncCallback(HandshakeReceiveCallback), null);
  181. }
  182. catch (Exception e)
  183. {
  184. Console.WriteLine(e.Message);
  185. this.Close();
  186. }
  187. }
  188. private void HandshakeReceiveCallback(IAsyncResult ar)
  189. {
  190. try
  191. {
  192. int bytesRead = connection.EndReceive(ar);
  193. if (bytesRead > 1)
  194. {
  195. byte[] response = { 5, 0 };
  196. if (connetionRecvBuffer[0] != 5)
  197. {
  198. // reject socks 4
  199. response = new byte[]{ 0, 91 };
  200. }
  201. connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null);
  202. }
  203. else
  204. {
  205. this.Close();
  206. }
  207. }
  208. catch (Exception e)
  209. {
  210. Console.WriteLine(e.Message);
  211. this.Close();
  212. }
  213. }
  214. private void HandshakeSendCallback(IAsyncResult ar)
  215. {
  216. try
  217. {
  218. connection.EndSend(ar);
  219. // +----+-----+-------+------+----------+----------+
  220. // |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
  221. // +----+-----+-------+------+----------+----------+
  222. // | 1 | 1 | X'00' | 1 | Variable | 2 |
  223. // +----+-----+-------+------+----------+----------+
  224. // Skip first 3 bytes
  225. // TODO validate
  226. connection.BeginReceive(connetionRecvBuffer, 0, 3, 0,
  227. new AsyncCallback(handshakeReceive2Callback), null);
  228. }
  229. catch (Exception e)
  230. {
  231. Console.WriteLine(e.Message);
  232. this.Close();
  233. }
  234. }
  235. private void handshakeReceive2Callback(IAsyncResult ar)
  236. {
  237. try
  238. {
  239. int bytesRead = connection.EndReceive(ar);
  240. if (bytesRead > 0)
  241. {
  242. byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 };
  243. connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(StartPipe), null);
  244. }
  245. else
  246. {
  247. this.Close();
  248. }
  249. }
  250. catch (Exception e)
  251. {
  252. Console.WriteLine(e.Message);
  253. this.Close();
  254. }
  255. }
  256. private void StartPipe(IAsyncResult ar)
  257. {
  258. try
  259. {
  260. connection.EndReceive(ar);
  261. remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0,
  262. new AsyncCallback(PipeRemoteReceiveCallback), null);
  263. connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0,
  264. new AsyncCallback(PipeConnectionReceiveCallback), null);
  265. }
  266. catch (Exception e)
  267. {
  268. Console.WriteLine(e.Message);
  269. this.Close();
  270. }
  271. }
  272. private void PipeRemoteReceiveCallback(IAsyncResult ar)
  273. {
  274. try
  275. {
  276. int bytesRead = remote.EndReceive(ar);
  277. if (bytesRead > 0)
  278. {
  279. int bytesToSend;
  280. encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend);
  281. connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null);
  282. }
  283. else
  284. {
  285. //Console.WriteLine("bytesRead: " + bytesRead.ToString());
  286. this.Close();
  287. }
  288. }
  289. catch (Exception e)
  290. {
  291. Console.WriteLine(e.Message);
  292. this.Close();
  293. }
  294. }
  295. private void PipeConnectionReceiveCallback(IAsyncResult ar)
  296. {
  297. try
  298. {
  299. int bytesRead = connection.EndReceive(ar);
  300. if (bytesRead > 0)
  301. {
  302. int bytesToSend;
  303. encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend);
  304. remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null);
  305. }
  306. else
  307. {
  308. this.Close();
  309. }
  310. }
  311. catch (Exception e)
  312. {
  313. Console.WriteLine(e.Message);
  314. this.Close();
  315. }
  316. }
  317. private void PipeRemoteSendCallback(IAsyncResult ar)
  318. {
  319. try
  320. {
  321. remote.EndSend(ar);
  322. connection.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0,
  323. new AsyncCallback(PipeConnectionReceiveCallback), null);
  324. }
  325. catch (Exception e)
  326. {
  327. Console.WriteLine(e.Message);
  328. this.Close();
  329. }
  330. }
  331. private void PipeConnectionSendCallback(IAsyncResult ar)
  332. {
  333. try
  334. {
  335. connection.EndSend(ar);
  336. remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0,
  337. new AsyncCallback(PipeRemoteReceiveCallback), null);
  338. }
  339. catch (Exception e)
  340. {
  341. Console.WriteLine(e.Message);
  342. this.Close();
  343. }
  344. }
  345. }
  346. }