socks5_server.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef __ASIO2_SOCKS5_SERVER_HPP__
  11. #define __ASIO2_SOCKS5_SERVER_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/base/detail/push_options.hpp>
  16. #include <asio2/tcp/tcp_server.hpp>
  17. #include <asio2/proxy/socks5_session.hpp>
  18. namespace asio2::detail
  19. {
  20. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  21. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  22. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  23. template<class derived_t, class session_t>
  24. class socks5_server_impl_t
  25. : public tcp_server_impl_t<derived_t, session_t>
  26. {
  27. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  28. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  29. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  30. public:
  31. using super = tcp_server_impl_t <derived_t, session_t>;
  32. using self = socks5_server_impl_t<derived_t, session_t>;
  33. using session_type = session_t;
  34. public:
  35. /**
  36. * @brief constructor
  37. */
  38. template<class... Args>
  39. explicit socks5_server_impl_t(Args&&... args)
  40. : super(std::forward<Args>(args)...)
  41. {
  42. }
  43. /**
  44. * @brief destructor
  45. */
  46. ~socks5_server_impl_t()
  47. {
  48. this->stop();
  49. }
  50. /**
  51. * @brief start the server
  52. * @param host - A string identifying a location. May be a descriptive name or
  53. * a numeric address string.
  54. * @param service - A string identifying the requested service. This may be a
  55. * descriptive name or a numeric string corresponding to a port number.
  56. */
  57. template<typename String, typename StrOrInt, typename... Args>
  58. inline bool start(String&& host, StrOrInt&& service, Args&&... args)
  59. {
  60. return this->derived()._do_start(
  61. std::forward<String>(host), std::forward<StrOrInt>(service),
  62. ecs_helper::make_ecs(detail::socks5_server_match_role{}, std::forward<Args>(args)...));
  63. }
  64. public:
  65. /**
  66. * @brief bind socks5 handshake listener
  67. * @param fun - a user defined callback function.
  68. * @param obj - a pointer or reference to a class object, this parameter can be none.
  69. * if fun is nonmember function, the obj param must be none, otherwise the obj must be the
  70. * the class object's pointer or reference.
  71. * Function signature : void(std::shared_ptr<asio2::socks5_session>& session_ptr)
  72. */
  73. template<class F, class ...C>
  74. inline derived_t& bind_socks5_handshake(F&& fun, C&&... obj)
  75. {
  76. this->listener_.bind(event_type::upgrade,
  77. observer_t<std::shared_ptr<session_t>&>(
  78. std::forward<F>(fun), std::forward<C>(obj)...));
  79. return (this->derived());
  80. }
  81. };
  82. }
  83. namespace asio2
  84. {
  85. template<class derived_t, class session_t>
  86. using socks5_server_impl_t = detail::socks5_server_impl_t<derived_t, session_t>;
  87. /**
  88. * @brief socks5 tcp server
  89. * @throws constructor maybe throw exception "Too many open files" (exception code : 24)
  90. * asio::error::no_descriptors - Too many open files
  91. */
  92. template<class session_t>
  93. class socks5_server_t : public detail::socks5_server_impl_t<socks5_server_t<session_t>, session_t>
  94. {
  95. public:
  96. using detail::socks5_server_impl_t<socks5_server_t<session_t>, session_t>::socks5_server_impl_t;
  97. };
  98. /**
  99. * @brief socks5 tcp server
  100. * @throws constructor maybe throw exception "Too many open files" (exception code : 24)
  101. * asio::error::no_descriptors - Too many open files
  102. */
  103. using socks5_server = socks5_server_t<socks5_session>;
  104. }
  105. #include <asio2/base/detail/pop_options.hpp>
  106. #endif // !__ASIO2_SOCKS5_SERVER_HPP__