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