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.

DirectConnect.cs 2.9 kB

10 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
10 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
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using Shadowsocks.Util.Sockets;
  6. namespace Shadowsocks.Proxy
  7. {
  8. public class DirectConnect : IProxy
  9. {
  10. private class FakeAsyncResult : IAsyncResult
  11. {
  12. public FakeAsyncResult(object state)
  13. {
  14. AsyncState = state;
  15. }
  16. public bool IsCompleted { get; } = true;
  17. public WaitHandle AsyncWaitHandle { get; } = null;
  18. public object AsyncState { get; }
  19. public bool CompletedSynchronously { get; } = true;
  20. }
  21. private class FakeEndPoint : EndPoint
  22. {
  23. public override AddressFamily AddressFamily { get; } = AddressFamily.Unspecified;
  24. public override string ToString()
  25. {
  26. return "null proxy";
  27. }
  28. }
  29. private WrappedSocket _remote = new WrappedSocket();
  30. public EndPoint LocalEndPoint => _remote.LocalEndPoint;
  31. public EndPoint ProxyEndPoint { get; } = new FakeEndPoint();
  32. public EndPoint DestEndPoint { get; private set; }
  33. public void BeginConnectProxy(EndPoint remoteEP, AsyncCallback callback, object state)
  34. {
  35. // do nothing
  36. var r = new FakeAsyncResult(state);
  37. callback?.Invoke(r);
  38. }
  39. public void EndConnectProxy(IAsyncResult asyncResult)
  40. {
  41. // do nothing
  42. }
  43. public void BeginConnectDest(EndPoint destEndPoint, AsyncCallback callback, object state, NetworkCredential auth = null)
  44. {
  45. DestEndPoint = destEndPoint;
  46. _remote.BeginConnect(destEndPoint, callback, state);
  47. }
  48. public void EndConnectDest(IAsyncResult asyncResult)
  49. {
  50. _remote.EndConnect(asyncResult);
  51. _remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  52. }
  53. public void BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback,
  54. object state)
  55. {
  56. _remote.BeginSend(buffer, offset, size, socketFlags, callback, state);
  57. }
  58. public int EndSend(IAsyncResult asyncResult)
  59. {
  60. return _remote.EndSend(asyncResult);
  61. }
  62. public void BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback,
  63. object state)
  64. {
  65. _remote.BeginReceive(buffer, offset, size, socketFlags, callback, state);
  66. }
  67. public int EndReceive(IAsyncResult asyncResult)
  68. {
  69. return _remote.EndReceive(asyncResult);
  70. }
  71. public void Shutdown(SocketShutdown how)
  72. {
  73. _remote.Shutdown(how);
  74. }
  75. public void Close()
  76. {
  77. _remote.Dispose();
  78. }
  79. }
  80. }