socks5_core.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_CORE_HPP__
  11. #define __ASIO2_SOCKS5_CORE_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <cstdint>
  16. #include <memory>
  17. #include <chrono>
  18. #include <functional>
  19. #include <string>
  20. #include <string_view>
  21. #include <type_traits>
  22. #include <asio2/base/error.hpp>
  23. #include <asio2/base/define.hpp>
  24. #include <asio2/base/detail/util.hpp>
  25. namespace asio2::socks5
  26. {
  27. ///-----------------------------------------------------------------------------------------------
  28. /// SOCKS Protocol Version 5:
  29. /// https://www.ietf.org/rfc/rfc1928.txt
  30. /// Username/Password Authentication for SOCKS V5 :
  31. /// https://www.ietf.org/rfc/rfc1929.txt
  32. /// GSSAPI : Generic Security Services Application Program Interface
  33. /// https://en.wikipedia.org/wiki/Generic_Security_Services_Application_Program_Interface
  34. ///-----------------------------------------------------------------------------------------------
  35. /// The type of error category used by the library
  36. using error_category = asio::error_category;
  37. /// The type of error condition used by the library
  38. using error_condition = asio::error_condition;
  39. enum class error
  40. {
  41. success = 0,
  42. /// unsupported version.
  43. unsupported_version,
  44. /// unsupported method.
  45. unsupported_method,
  46. /// no acceptable methods.
  47. no_acceptable_methods,
  48. /// username required.
  49. username_required,
  50. /// unsupported authentication version.
  51. unsupported_authentication_version,
  52. /// authentication failed.
  53. authentication_failed,
  54. /// general failure.
  55. general_failure,
  56. /// no identd running.
  57. no_identd,
  58. /// no identd running.
  59. identd_error,
  60. /// general socks server failure
  61. general_socks_server_failure,
  62. /// connection not allowed by ruleset
  63. connection_not_allowed_by_ruleset,
  64. /// network unreachable
  65. network_unreachable,
  66. /// host unreachable
  67. host_unreachable,
  68. /// connection refused
  69. connection_refused,
  70. /// ttl expired
  71. ttl_expired,
  72. /// command not supported
  73. command_not_supported,
  74. /// address type not supported
  75. address_type_not_supported,
  76. /// to x'ff' unassigned
  77. unassigned,
  78. };
  79. enum class condition
  80. {
  81. success = 0,
  82. /// unsupported version.
  83. unsupported_version,
  84. /// unsupported method.
  85. unsupported_method,
  86. /// no acceptable methods.
  87. no_acceptable_methods,
  88. /// username required.
  89. username_required,
  90. /// unsupported authentication version.
  91. unsupported_authentication_version,
  92. /// authentication failed.
  93. authentication_failed,
  94. /// general failure.
  95. general_failure,
  96. /// no identd running.
  97. no_identd,
  98. /// no identd running.
  99. identd_error,
  100. /// general socks server failure
  101. general_socks_server_failure,
  102. /// connection not allowed by ruleset
  103. connection_not_allowed_by_ruleset,
  104. /// network unreachable
  105. network_unreachable,
  106. /// host unreachable
  107. host_unreachable,
  108. /// connection refused
  109. connection_refused,
  110. /// ttl expired
  111. ttl_expired,
  112. /// command not supported
  113. command_not_supported,
  114. /// address type not supported
  115. address_type_not_supported,
  116. /// to x'ff' unassigned
  117. unassigned,
  118. };
  119. class socks5_error_category : public error_category
  120. {
  121. public:
  122. const char* name() const noexcept override
  123. {
  124. return "asio2.socks5";
  125. }
  126. inline std::string message(int ev) const override
  127. {
  128. switch (static_cast<error>(ev))
  129. {
  130. case error::success:
  131. return "Success";
  132. case error::unsupported_version:
  133. return "unsupported version";
  134. case error::unsupported_method:
  135. return "unsupported method";
  136. case error::no_acceptable_methods:
  137. return "no acceptable methods";
  138. case error::username_required:
  139. return "username required";
  140. case error::unsupported_authentication_version:
  141. return "unsupported authentication version";
  142. case error::authentication_failed:
  143. return "authentication failed";
  144. case error::general_failure:
  145. return "general failure";
  146. case error::no_identd:
  147. return "no identd running";
  148. case error::identd_error:
  149. return "no identd running";
  150. case error::general_socks_server_failure:
  151. return "general socks server failure";
  152. case error::connection_not_allowed_by_ruleset:
  153. return "connection not allowed by ruleset";
  154. case error::network_unreachable:
  155. return "network unreachable";
  156. case error::host_unreachable:
  157. return "host unreachable";
  158. case error::connection_refused:
  159. return "connection refused";
  160. case error::ttl_expired:
  161. return "ttl expired";
  162. case error::command_not_supported:
  163. return "command not supported";
  164. case error::address_type_not_supported:
  165. return "address type not supported";
  166. case error::unassigned:
  167. return "to x'ff' unassigned";
  168. default:
  169. return "Unknown PROXY error";
  170. }
  171. }
  172. inline error_condition default_error_condition(int ev) const noexcept override
  173. {
  174. return error_condition{ ev, *this };
  175. }
  176. };
  177. inline const socks5_error_category& socks5_category() noexcept
  178. {
  179. static socks5_error_category const cat{};
  180. return cat;
  181. }
  182. inline error_code make_error_code(error e) noexcept
  183. {
  184. return error_code{ static_cast<std::underlying_type<error>::type>(e), socks5_category() };
  185. }
  186. inline error_condition make_error_condition(condition c) noexcept
  187. {
  188. return error_condition{ static_cast<std::underlying_type<condition>::type>(c), socks5_category() };
  189. }
  190. }
  191. namespace std
  192. {
  193. template<>
  194. struct is_error_code_enum<::asio2::socks5::error>
  195. {
  196. static bool const value = true;
  197. };
  198. template<>
  199. struct is_error_condition_enum<::asio2::socks5::condition>
  200. {
  201. static bool const value = true;
  202. };
  203. }
  204. namespace asio2::socks5
  205. {
  206. enum class method : std::uint8_t
  207. {
  208. anonymous = 0x00, // X'00' NO AUTHENTICATION REQUIRED
  209. gssapi = 0x01, // X'01' GSSAPI
  210. password = 0x02, // X'02' USERNAME/PASSWORD
  211. //iana = 0x03, // X'03' to X'7F' IANA ASSIGNED
  212. //reserved = 0x80, // X'80' to X'FE' RESERVED FOR PRIVATE METHODS
  213. noacceptable = 0xFF, // X'FF' NO ACCEPTABLE METHODS
  214. };
  215. enum class command : std::uint8_t
  216. {
  217. connect = 0x01, // CONNECT X'01'
  218. bind = 0x02, // BIND X'02'
  219. udp_associate = 0x03, // UDP ASSOCIATE X'03'
  220. };
  221. }
  222. namespace socks5 = ::asio2::socks5;
  223. #endif // !__ASIO2_SOCKS5_CORE_HPP__