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

No Description

Contributors (1)