socks4.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. *
  6. *
  7. * ASIO Socks4 example
  8. */
  9. #include <string>
  10. #include <array>
  11. #include <asio.hpp>
  12. #include <memory>
  13. #include <system_error>
  14. #include <utility>
  15. #include "esp_log.h"
  16. #include "socks4.hpp"
  17. #include "nvs_flash.h"
  18. #include "esp_event.h"
  19. #include "protocol_examples_common.h"
  20. constexpr auto TAG = "asio_socks4";
  21. using asio::ip::tcp;
  22. namespace {
  23. void esp_init()
  24. {
  25. ESP_ERROR_CHECK(nvs_flash_init());
  26. ESP_ERROR_CHECK(esp_netif_init());
  27. ESP_ERROR_CHECK(esp_event_loop_create_default());
  28. esp_log_level_set("async_request", ESP_LOG_DEBUG);
  29. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  30. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  31. * examples/protocols/README.md for more information about this function.
  32. */
  33. ESP_ERROR_CHECK(example_connect());
  34. }
  35. /**
  36. * @brief Simple class to add the resolver to a chain of actions
  37. *
  38. */
  39. class AddressResolution : public std::enable_shared_from_this<AddressResolution> {
  40. public:
  41. explicit AddressResolution(asio::io_context &context) : ctx(context), resolver(ctx) {}
  42. /**
  43. * @brief Initiator function for the address resolution
  44. *
  45. * @tparam CompletionToken callable responsible to use the results.
  46. *
  47. * @param host Host address
  48. * @param port Port for the target, must be number due to a limitation on lwip.
  49. */
  50. template<class CompletionToken>
  51. void resolve(const std::string &host, const std::string &port, CompletionToken &&completion_handler)
  52. {
  53. auto self(shared_from_this());
  54. resolver.async_resolve(host, port, [self, completion_handler](const asio::error_code & error, tcp::resolver::results_type results) {
  55. if (error) {
  56. ESP_LOGE(TAG, "Failed to resolve: %s", error.message().c_str());
  57. return;
  58. }
  59. completion_handler(self, results);
  60. });
  61. }
  62. private:
  63. asio::io_context &ctx;
  64. tcp::resolver resolver;
  65. };
  66. /**
  67. * @brief Connection class
  68. *
  69. * The lowest level dependency on our asynchronous task, Connection provide an interface to TCP sockets.
  70. * A similar class could be provided for a TLS connection.
  71. *
  72. * @note: All read and write operations are written on an explicit strand, even though an implicit strand
  73. * occurs in this example since we run the io context in a single task.
  74. *
  75. */
  76. class Connection : public std::enable_shared_from_this<Connection> {
  77. public:
  78. explicit Connection(asio::io_context &context) : ctx(context), strand(context), socket(ctx) {}
  79. /**
  80. * @brief Start the connection
  81. *
  82. * Async operation to start a connection. As the final act of the process the Connection class pass a
  83. * std::shared_ptr of itself to the completion_handler.
  84. * Since it uses std::shared_ptr as an automatic control of its lifetime this class must be created
  85. * through a std::make_shared call.
  86. *
  87. * @tparam completion_handler A callable to act as the final handler for the process.
  88. * @param host host address
  89. * @param port port number - due to a limitation on lwip implementation this should be the number not the
  90. * service name typically seen in ASIO examples.
  91. *
  92. * @note The class could be modified to store the completion handler, as a member variable, instead of
  93. * pass it along asynchronous calls to allow the process to run again completely.
  94. *
  95. */
  96. template<class CompletionToken>
  97. void start(tcp::resolver::results_type results, CompletionToken &&completion_handler)
  98. {
  99. connect(results, completion_handler);
  100. }
  101. /**
  102. * @brief Start an async write on the socket
  103. *
  104. * @tparam data
  105. * @tparam completion_handler A callable to act as the final handler for the process.
  106. *
  107. */
  108. template<class DataType, class CompletionToken>
  109. void write_async(const DataType &data, CompletionToken &&completion_handler)
  110. {
  111. asio::async_write(socket, data, asio::bind_executor(strand, completion_handler));
  112. }
  113. /**
  114. * @brief Start an async read on the socket
  115. *
  116. * @tparam data
  117. * @tparam completion_handler A callable to act as the final handler for the process.
  118. *
  119. */
  120. template<class DataBuffer, class CompletionToken>
  121. void read_async(DataBuffer &&in_data, CompletionToken &&completion_handler)
  122. {
  123. asio::async_read(socket, in_data, asio::bind_executor(strand, completion_handler));
  124. }
  125. private:
  126. template<class CompletionToken>
  127. void connect(tcp::resolver::results_type results, CompletionToken &&completion_handler)
  128. {
  129. auto self(shared_from_this());
  130. asio::async_connect(socket, results, [self, completion_handler](const asio::error_code & error, [[maybe_unused]] const tcp::endpoint & endpoint) {
  131. if (error) {
  132. ESP_LOGE(TAG, "Failed to connect: %s", error.message().c_str());
  133. return;
  134. }
  135. completion_handler(self);
  136. });
  137. }
  138. asio::io_context &ctx;
  139. asio::io_context::strand strand;
  140. tcp::socket socket;
  141. };
  142. }
  143. namespace Socks {
  144. struct ConnectionData {
  145. ConnectionData(socks4::request::command_type cmd, const asio::ip::tcp::endpoint &endpoint,
  146. const std::string &user_id) : request(cmd, endpoint, user_id) {};
  147. socks4::request request;
  148. socks4::reply reply;
  149. };
  150. template<class CompletionToken>
  151. void async_connect(asio::io_context &context, std::string proxy, std::string proxy_port, std::string host, std::string port, CompletionToken &&completion_handler)
  152. {
  153. /*
  154. * The first step is to resolve the address of the proxy we want to connect to.
  155. * The AddressResolution itself is injected to the completion handler.
  156. */
  157. // Resolve proxy
  158. std::make_shared<AddressResolution>(context)->resolve(proxy, proxy_port,
  159. [&context, host, port, completion_handler](std::shared_ptr<AddressResolution> resolver, tcp::resolver::results_type proxy_resolution) {
  160. // We also need to resolve the target host address
  161. resolver->resolve(host, port, [&context, proxy_resolution, completion_handler](std::shared_ptr<AddressResolution> resolver, tcp::resolver::results_type host_resolution) {
  162. // Make connection with the proxy
  163. ESP_LOGI(TAG, "Startig Proxy Connection");
  164. std::make_shared<Connection>(context)->start(proxy_resolution,
  165. [resolver, host_resolution, completion_handler](std::shared_ptr<Connection> connection) {
  166. auto connect_data = std::make_shared<ConnectionData>(socks4::request::connect, *host_resolution, "");
  167. ESP_LOGI(TAG, "Sending Request to proxy for host connection.");
  168. connection->write_async(connect_data->request.buffers(), [connection, connect_data, completion_handler](std::error_code error, std::size_t bytes_received) {
  169. if (error) {
  170. ESP_LOGE(TAG, "Proxy request write error: %s", error.message().c_str());
  171. return;
  172. }
  173. connection->read_async(connect_data->reply.buffers(), [connection, connect_data, completion_handler](std::error_code error, std::size_t bytes_received) {
  174. if (error) {
  175. ESP_LOGE(TAG, "Proxy response read error: %s", error.message().c_str());
  176. return;
  177. }
  178. if (!connect_data->reply.success()) {
  179. ESP_LOGE(TAG, "Proxy error: %#x", connect_data->reply.status());
  180. }
  181. completion_handler(connection);
  182. });
  183. });
  184. });
  185. });
  186. });
  187. }
  188. } // namespace Socks
  189. namespace Http {
  190. enum class Method { GET };
  191. /**
  192. * @brief Simple HTTP request class
  193. *
  194. * The user needs to write the request information direct to header and body fields.
  195. *
  196. * Only GET verb is provided.
  197. *
  198. */
  199. class Request {
  200. public:
  201. Request(Method method, std::string host, std::string port, const std::string &target) : host_data(std::move(host)), port_data(std::move(port))
  202. {
  203. header_data.append("GET ");
  204. header_data.append(target);
  205. header_data.append(" HTTP/1.1");
  206. header_data.append("\r\n");
  207. header_data.append("Host: ");
  208. header_data.append(host_data);
  209. header_data.append("\r\n");
  210. header_data.append("\r\n");
  211. };
  212. void set_header_field(std::string const &field)
  213. {
  214. header_data.append(field);
  215. }
  216. void append_to_body(std::string const &data)
  217. {
  218. body_data.append(data);
  219. };
  220. const std::string &host() const
  221. {
  222. return host_data;
  223. }
  224. const std::string &service_port() const
  225. {
  226. return port_data;
  227. }
  228. const std::string &header() const
  229. {
  230. return header_data;
  231. }
  232. const std::string &body() const
  233. {
  234. return body_data;
  235. }
  236. private:
  237. std::string host_data;
  238. std::string port_data;
  239. std::string header_data;
  240. std::string body_data;
  241. };
  242. /**
  243. * @brief Simple HTTP response class
  244. *
  245. * The response is built from received data and only parsed to split header and body.
  246. *
  247. * A copy of the received data is kept.
  248. *
  249. */
  250. struct Response {
  251. /**
  252. * @brief Construct a response from a contiguous buffer.
  253. *
  254. * Simple http parsing.
  255. *
  256. */
  257. template<class DataIt>
  258. explicit Response(DataIt data, size_t size)
  259. {
  260. raw_response = std::string(data, size);
  261. auto header_last = raw_response.find("\r\n\r\n");
  262. if (header_last != std::string::npos) {
  263. header = raw_response.substr(0, header_last);
  264. }
  265. body = raw_response.substr(header_last + 3);
  266. }
  267. /**
  268. * @brief Print response content.
  269. */
  270. void print()
  271. {
  272. ESP_LOGI(TAG, "Header :\n %s", header.c_str());
  273. ESP_LOGI(TAG, "Body : \n %s", body.c_str());
  274. }
  275. std::string raw_response;
  276. std::string header;
  277. std::string body;
  278. };
  279. /** @brief HTTP Session
  280. *
  281. * Session class to handle HTTP protocol implementation.
  282. *
  283. */
  284. class Session : public std::enable_shared_from_this<Session> {
  285. public:
  286. explicit Session(std::shared_ptr<Connection> connection_in) : connection(std::move(connection_in))
  287. {
  288. }
  289. template<class CompletionToken>
  290. void send_request(const Request &request, CompletionToken &&completion_handler)
  291. {
  292. auto self = shared_from_this();
  293. send_data = { asio::buffer(request.header()), asio::buffer(request.body()) };
  294. connection->write_async(send_data, [self, &completion_handler](std::error_code error, std::size_t bytes_transfered) {
  295. if (error) {
  296. ESP_LOGE(TAG, "Request write error: %s", error.message().c_str());
  297. return;
  298. }
  299. ESP_LOGD(TAG, "Bytes Transfered: %d", bytes_transfered);
  300. self->get_response(completion_handler);
  301. });
  302. }
  303. private:
  304. template<class CompletionToken>
  305. void get_response(CompletionToken &&completion_handler)
  306. {
  307. auto self = shared_from_this();
  308. connection->read_async(asio::buffer(receive_buffer), [self, &completion_handler](std::error_code error, std::size_t bytes_received) {
  309. if (error and error.value() != asio::error::eof) {
  310. return;
  311. }
  312. ESP_LOGD(TAG, "Bytes Received: %d", bytes_received);
  313. if (bytes_received == 0) {
  314. return;
  315. }
  316. Response response(std::begin(self->receive_buffer), bytes_received);
  317. completion_handler(self, response);
  318. });
  319. }
  320. /*
  321. * For this example we assumed 2048 to be enough for the receive_buffer
  322. */
  323. std::array<char, 2048> receive_buffer;
  324. /*
  325. * The hardcoded 2 below is related to the type we receive the data to send. We gather the parts from Request, header
  326. * and body, to send avoiding the copy.
  327. */
  328. std::array<asio::const_buffer, 2> send_data;
  329. std::shared_ptr<Connection> connection;
  330. };
  331. }// namespace Http
  332. extern "C" void app_main(void)
  333. {
  334. // Basic initialization of ESP system
  335. esp_init();
  336. asio::io_context io_context;
  337. Http::Request request(Http::Method::GET, "www.httpbin.org", "80", "/get");
  338. Socks::async_connect(io_context, CONFIG_EXAMPLE_PROXY_ADDRESS, CONFIG_EXAMPLE_PROXY_SERVICE, request.host(), request.service_port(),
  339. [&request](std::shared_ptr<Connection> connection) {
  340. // Now we create a HTTP::Session and inject the necessary connection.
  341. std::make_shared<Http::Session>(connection)->send_request(request, [](std::shared_ptr<Http::Session> session, Http::Response response) {
  342. response.print();
  343. });
  344. });
  345. // io_context.run will block until all the tasks on the context are done.
  346. io_context.run();
  347. ESP_LOGI(TAG, "Context run done");
  348. ESP_ERROR_CHECK(example_disconnect());
  349. }