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

No Description