ProxyFactory.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Microsoft;
  2. using Socks5.Models;
  3. using STUN.Enums;
  4. using System.Net;
  5. namespace STUN.Proxy;
  6. public static class ProxyFactory
  7. {
  8. public static IUdpProxy CreateProxy(ProxyType type, IPEndPoint local, Socks5CreateOption option)
  9. {
  10. switch (type)
  11. {
  12. case ProxyType.Plain:
  13. {
  14. return new NoneUdpProxy(local);
  15. }
  16. case ProxyType.Socks5:
  17. {
  18. Requires.NotNull(option, nameof(option));
  19. Requires.Argument(option.Address is not null, nameof(option), @"Proxy server is null");
  20. return new Socks5UdpProxy(local, option);
  21. }
  22. default:
  23. {
  24. throw Assumes.NotReachable();
  25. }
  26. }
  27. }
  28. public static ITcpProxy CreateProxy(TransportType transport, ProxyType type, Socks5CreateOption option, string targetHost)
  29. {
  30. switch (transport, type)
  31. {
  32. case (TransportType.Tcp, ProxyType.Plain):
  33. {
  34. return new DirectTcpProxy();
  35. }
  36. case (TransportType.Tcp, ProxyType.Socks5):
  37. {
  38. Requires.NotNull(option, nameof(option));
  39. Requires.Argument(option.Address is not null, nameof(option), @"Proxy server is null");
  40. return new Socks5TcpProxy(option);
  41. }
  42. case (TransportType.Tls, ProxyType.Plain):
  43. {
  44. return new TlsProxy(targetHost);
  45. }
  46. case (TransportType.Tls, ProxyType.Socks5):
  47. {
  48. Requires.NotNull(option, nameof(option));
  49. Requires.Argument(option.Address is not null, nameof(option), @"Proxy server is null");
  50. return new TlsOverSocks5Proxy(option, targetHost);
  51. }
  52. default:
  53. {
  54. throw new NotSupportedException();
  55. }
  56. }
  57. }
  58. }