2019-06-03 06:26:16 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "http_connection.h"
|
|
|
|
#include <functional>
|
2020-09-22 04:47:03 +02:00
|
|
|
#include <optional>
|
2019-06-03 06:26:16 +02:00
|
|
|
|
2021-01-05 06:38:40 +01:00
|
|
|
namespace oxen {
|
2019-06-03 06:26:16 +02:00
|
|
|
using http_callback_t = std::function<void(sn_response_t)>;
|
|
|
|
|
|
|
|
void make_https_request(boost::asio::io_context& ioc, const std::string& ip,
|
2019-06-27 06:08:14 +02:00
|
|
|
uint16_t port, const std::string& sn_pubkey_b32z,
|
|
|
|
const std::shared_ptr<request_t>& req,
|
2019-06-03 06:26:16 +02:00
|
|
|
http_callback_t&& cb);
|
|
|
|
|
2019-12-18 04:34:40 +01:00
|
|
|
void make_https_request(boost::asio::io_context& ioc, const std::string& url,
|
|
|
|
const std::shared_ptr<request_t>& req,
|
|
|
|
http_callback_t&& cb);
|
|
|
|
|
2019-06-03 06:26:16 +02:00
|
|
|
class HttpsClientSession
|
|
|
|
: public std::enable_shared_from_this<HttpsClientSession> {
|
|
|
|
|
2019-09-25 08:51:05 +02:00
|
|
|
// For debugging purposes mostly
|
|
|
|
uint64_t connection_idx;
|
|
|
|
|
2019-06-03 06:26:16 +02:00
|
|
|
using tcp = boost::asio::ip::tcp;
|
|
|
|
|
|
|
|
boost::asio::io_context& ioc_;
|
|
|
|
ssl::context& ssl_ctx_;
|
|
|
|
tcp::resolver::results_type resolve_results_;
|
|
|
|
http_callback_t callback_;
|
|
|
|
boost::asio::steady_timer deadline_timer_;
|
|
|
|
|
2019-06-27 06:08:14 +02:00
|
|
|
// keep the cert in memory for post-handshake verification
|
|
|
|
std::string server_cert_;
|
|
|
|
|
2019-06-03 06:26:16 +02:00
|
|
|
ssl::stream<tcp::socket> stream_;
|
|
|
|
boost::beast::flat_buffer buffer_;
|
|
|
|
/// NOTE: this needs to be a shared pointer since
|
|
|
|
/// it is very common for the same request to be
|
|
|
|
/// sent to multiple snodes
|
|
|
|
std::shared_ptr<request_t> req_;
|
2020-11-24 08:05:58 +01:00
|
|
|
|
|
|
|
http::response_parser<http::string_body> response_;
|
2019-12-18 04:34:40 +01:00
|
|
|
|
2020-09-22 04:47:03 +02:00
|
|
|
// Snode's pub key (none if signature verification is not used / not a
|
|
|
|
// snode)
|
2020-08-08 07:21:33 +02:00
|
|
|
std::optional<std::string> server_pub_key_b32z_;
|
2019-06-03 06:26:16 +02:00
|
|
|
|
|
|
|
bool used_callback_ = false;
|
|
|
|
|
|
|
|
void on_connect();
|
|
|
|
|
|
|
|
void on_write(boost::system::error_code ec, std::size_t bytes_transferred);
|
|
|
|
|
|
|
|
void on_read(boost::system::error_code ec, std::size_t bytes_transferred);
|
|
|
|
|
2019-12-13 02:20:17 +01:00
|
|
|
void
|
|
|
|
trigger_callback(SNodeError error, std::shared_ptr<std::string>&& body,
|
2020-08-08 07:21:33 +02:00
|
|
|
std::optional<response_t> raw_response = std::nullopt);
|
2019-06-03 06:26:16 +02:00
|
|
|
|
|
|
|
void on_handshake(boost::system::error_code ec);
|
2019-06-27 06:08:14 +02:00
|
|
|
bool verify_signature();
|
2019-06-03 06:26:16 +02:00
|
|
|
|
|
|
|
void do_close();
|
|
|
|
void on_shutdown(boost::system::error_code ec);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Resolver and socket require an io_context
|
|
|
|
HttpsClientSession(boost::asio::io_context& ioc, ssl::context& ssl_ctx,
|
|
|
|
tcp::resolver::results_type resolve_results,
|
|
|
|
const std::shared_ptr<request_t>& req,
|
2019-12-18 04:34:40 +01:00
|
|
|
http_callback_t&& cb,
|
2020-08-08 07:21:33 +02:00
|
|
|
std::optional<std::string> sn_pubkey_b32z);
|
2019-06-03 06:26:16 +02:00
|
|
|
|
|
|
|
// initiate the client connection
|
|
|
|
void start();
|
|
|
|
|
|
|
|
~HttpsClientSession();
|
|
|
|
};
|
2021-01-05 06:38:40 +01:00
|
|
|
} // namespace oxen
|