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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace Shadowsocks.Net.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 readonly Socket _remote = new Socket(SocketType.Stream, ProtocolType.Tcp);
  30. public EndPoint LocalEndPoint => _remote.LocalEndPoint;
  31. public EndPoint ProxyEndPoint { get; } = new FakeEndPoint();
  32. public EndPoint DestEndPoint { get; private set; }
  33. public void Shutdown(SocketShutdown how)
  34. {
  35. _remote.Shutdown(how);
  36. }
  37. public void Close()
  38. {
  39. _remote.Dispose();
  40. }
  41. public Task ConnectProxyAsync(EndPoint remoteEP, NetworkCredential auth = null, CancellationToken token = default)
  42. {
  43. return Task.CompletedTask;
  44. }
  45. public async Task ConnectRemoteAsync(EndPoint destEndPoint, CancellationToken token = default)
  46. {
  47. DestEndPoint = destEndPoint;
  48. await _remote.ConnectAsync(destEndPoint);
  49. }
  50. public async Task<int> SendAsync(ReadOnlyMemory<byte> buffer, CancellationToken token = default)
  51. {
  52. return await _remote.SendAsync(buffer, SocketFlags.None, token);
  53. }
  54. public async Task<int> ReceiveAsync(Memory<byte> buffer, CancellationToken token = default)
  55. {
  56. return await _remote.ReceiveAsync(buffer, SocketFlags.None, token);
  57. }
  58. }
  59. }