fix cme connector code
This commit is contained in:
1 parent
df11e6564f
commit
c8fb032a13
29 files changed
+6036
No files matched your search
@@ -0,0 +1,37 @@
|
||||
cmake_minimum_required(VERSION 3.23)
|
||||
project(bts-websocket-listener)
|
||||
|
||||
FetchContent_MakeAvailable(curlpp)
|
||||
FetchContent_GetProperties(
|
||||
curlpp
|
||||
SOURCE_DIR CURLPP_SOURCE_DIR
|
||||
BINARY_DIR CURLPP_BINARY_DIR)
|
||||
|
||||
message("CMAKE_BUILD_RPATH before: ${CMAKE_BUILD_RPATH}")
|
||||
message("CMAKE_INSTALL_RPATH before: ${CMAKE_INSTALL_RPATH}")
|
||||
list(APPEND CMAKE_BUILD_RPATH ${curlpp_BINARY_DIR})
|
||||
# set(CMAKE_BUILD_RPATH "${CMAKE_BUILD_RPATH};${curlpp_BINARY_DIR}")
|
||||
message("CMAKE_BUILD_RPATH after : ${CMAKE_BUILD_RPATH}")
|
||||
message("CMAKE_INSTALL_RPATH after : ${CMAKE_INSTALL_RPATH}")
|
||||
|
||||
file(
|
||||
GLOB_RECURSE ${PROJECT_NAME}_Sources
|
||||
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${PROJECT_NAME}/*.cpp)
|
||||
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_Sources})
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC bts-system bts-http-client curl)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC crypto curlpp ssl)
|
||||
|
||||
# # cli11
|
||||
# FetchContent_Declare(
|
||||
# cli11
|
||||
# GIT_REPOSITORY git@github.com:CLIUtils/CLI11.git
|
||||
# GIT_TAG v2.3.2
|
||||
# SYSTEM
|
||||
# OVERRIDE_FIND_PACKAGE
|
||||
# )
|
||||
# FetchContent_MakeAvailable(cli11)
|
||||
# target_include_directories(${PROJECT_NAME} PUBLIC ${cli11_SOURCE_DIR}/include)
|
||||
# target_link_libraries(${PROJECT_NAME} PUBLIC CLI11)
|
||||
@@ -0,0 +1,87 @@
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace beast = boost::beast; // from <boost/beast.hpp>
|
||||
namespace http = beast::http; // from <boost/beast/http.hpp>
|
||||
// namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
namespace net = boost::asio; // from <boost/asio.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class BoostExample {
|
||||
public:
|
||||
// Sends a WebSocket message and prints the response
|
||||
static int run(int argc, char** argv) {
|
||||
try {
|
||||
// Check command line arguments.
|
||||
if (argc != 4) {
|
||||
std::cerr << "Usage: websocket-client-sync <host> <port> <text>\n"
|
||||
<< "Example:\n"
|
||||
<< " websocket-client-sync echo.websocket.org 80 \"Hello, "
|
||||
"world!\"\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
std::string host = argv[1];
|
||||
auto const port = argv[2];
|
||||
auto const text = argv[3];
|
||||
|
||||
// The io_context is required for all I/O
|
||||
net::io_context ioc;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ioc};
|
||||
beast::websocket::stream<tcp::socket> ws{ioc};
|
||||
|
||||
// Look up the domain name
|
||||
auto const results = resolver.resolve(host, port);
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
auto ep = net::connect(ws.next_layer(), results);
|
||||
|
||||
// Update the host_ string. This will provide the value of the
|
||||
// Host HTTP header during the WebSocket handshake.
|
||||
// See https://tools.ietf.org/html/rfc7230#section-5.4
|
||||
host += ':' + std::to_string(ep.port());
|
||||
|
||||
// Set a decorator to change the User-Agent of the handshake
|
||||
ws.set_option(
|
||||
beast::websocket::stream_base::decorator([](beast::websocket::request_type& req) {
|
||||
req.set(http::field::user_agent,
|
||||
std::string(BOOST_BEAST_VERSION_STRING) +
|
||||
" websocket-client-coro");
|
||||
}));
|
||||
|
||||
// Perform the websocket handshake
|
||||
ws.handshake(host, "/");
|
||||
|
||||
// Send the message
|
||||
ws.write(net::buffer(std::string(text)));
|
||||
|
||||
// This buffer will hold the incoming message
|
||||
beast::flat_buffer buffer;
|
||||
|
||||
// Read a message into our buffer
|
||||
ws.read(buffer);
|
||||
|
||||
// Close the WebSocket connection
|
||||
ws.close(beast::websocket::close_code::normal);
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
|
||||
// The make_printable() function helps print a ConstBufferSequence
|
||||
std::cout << beast::make_printable(buffer.data()) << std::endl;
|
||||
} catch (std::exception const& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,79 @@
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
namespace beast = boost::beast; // from <boost/beast.hpp>
|
||||
namespace http = beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
namespace net = boost::asio; // from <boost/asio.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
class BoostExample2 {
|
||||
public:
|
||||
// Performs an HTTP GET and prints the response
|
||||
static int run(std::string &host, const std::string &port, const std::string &text) {
|
||||
try {
|
||||
// The io_context is required for all I/O
|
||||
net::io_context ioc;
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ioc};
|
||||
websocket::stream<tcp::socket> ws{ioc};
|
||||
|
||||
// Look up the domain name
|
||||
auto const results = resolver.resolve(host, port);
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
auto ep = net::connect(ws.next_layer(), results);
|
||||
|
||||
// Update the host_ string. This will provide the value of the
|
||||
// Host HTTP header during the WebSocket handshake.
|
||||
// See https://tools.ietf.org/html/rfc7230#section-5.4
|
||||
host += ':' + std::to_string(ep.port());
|
||||
|
||||
// Set a decorator to change the User-Agent of the handshake
|
||||
ws.set_option(websocket::stream_base::decorator(
|
||||
[](websocket::request_type& req) {
|
||||
req.set(
|
||||
http::field::user_agent,
|
||||
std::string(BOOST_BEAST_VERSION_STRING) +
|
||||
" websocket-client-coro");
|
||||
}));
|
||||
|
||||
// Perform the websocket handshake
|
||||
ws.handshake(host, "/");
|
||||
|
||||
// Send the message
|
||||
ws.write(net::buffer(std::string(text)));
|
||||
|
||||
// This buffer will hold the incoming message
|
||||
beast::flat_buffer buffer;
|
||||
|
||||
// Read a message into our buffer
|
||||
ws.read(buffer);
|
||||
|
||||
// Close the WebSocket connection
|
||||
ws.close(websocket::close_code::normal);
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
|
||||
// The make_printable() function helps print a ConstBufferSequence
|
||||
std::cout << beast::make_printable(buffer.data()) << std::endl;
|
||||
} catch (std::exception const& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
};
|
||||
} // namespace bts::websocket
|
||||
|
||||
//]
|
||||
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
// Example: WebSocket SSL client, synchronous
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl/stream.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/ssl.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <boost/beast/websocket/ssl.hpp>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "root_certificates.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
namespace beast = boost::beast; // from <boost/beast.hpp>
|
||||
namespace http = beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
namespace net = boost::asio; // from <boost/asio.hpp>
|
||||
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
class BoostExample3 {
|
||||
public:
|
||||
// Sends a WebSocket message and prints the response
|
||||
static int run(
|
||||
std::string host,
|
||||
const std::string &port,
|
||||
const std::string &endpoint,
|
||||
const std::string &text,
|
||||
const std::string &access_token) {
|
||||
try {
|
||||
// The io_context is required for all I/O
|
||||
net::io_context ioc;
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::tlsv12_client};
|
||||
|
||||
// This holds the root certificate used for verification
|
||||
load_root_certificates(ctx);
|
||||
|
||||
// These objects perform our I/O
|
||||
tcp::resolver resolver{ioc};
|
||||
websocket::stream<beast::ssl_stream<tcp::socket>> ws{ioc, ctx};
|
||||
|
||||
// Look up the domain name
|
||||
auto const results = resolver.resolve(host, port);
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
auto ep = net::connect(get_lowest_layer(ws), results);
|
||||
|
||||
// Set SNI Hostname (many hosts need this to handshake successfully)
|
||||
if (!SSL_set_tlsext_host_name(
|
||||
ws.next_layer().native_handle(), host.c_str()))
|
||||
throw beast::system_error(
|
||||
beast::error_code(
|
||||
static_cast<int>(::ERR_get_error()),
|
||||
net::error::get_ssl_category()),
|
||||
"Failed to set SNI Hostname");
|
||||
|
||||
// Update the host_ string. This will provide the value of the
|
||||
// Host HTTP header during the WebSocket handshake.
|
||||
// See https://tools.ietf.org/html/rfc7230#section-5.4
|
||||
host += ':' + std::to_string(ep.port());
|
||||
|
||||
// Perform the SSL handshake
|
||||
ws.next_layer().handshake(ssl::stream_base::client);
|
||||
|
||||
// Set a decorator to change the User-Agent of the handshake
|
||||
ws.set_option(websocket::stream_base::decorator(
|
||||
[&access_token](websocket::request_type &req) {
|
||||
req.set(
|
||||
http::field::user_agent,
|
||||
std::string(BOOST_BEAST_VERSION_STRING) +
|
||||
" websocket-client-coro");
|
||||
if (!access_token.empty()) {
|
||||
req.set(
|
||||
beast::http::field::authorization,
|
||||
"Bearer " + access_token);
|
||||
}
|
||||
}));
|
||||
|
||||
// Perform the websocket handshake
|
||||
ws.handshake(host, "/" + endpoint);
|
||||
|
||||
// Send the message
|
||||
if (!text.empty()) {
|
||||
ws.write(net::buffer(text));
|
||||
}
|
||||
|
||||
// This buffer will hold the incoming message
|
||||
beast::flat_buffer buffer;
|
||||
|
||||
// Read a message into our buffer
|
||||
ws.read(buffer);
|
||||
|
||||
// Close the WebSocket connection
|
||||
ws.close(websocket::close_code::normal);
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
|
||||
// The make_printable() function helps print a ConstBufferSequence
|
||||
std::cout << beast::make_printable(buffer.data()) << std::endl;
|
||||
} catch (std::exception const &e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,161 @@
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/core/stream_traits.hpp>
|
||||
#include <boost/beast/core/tcp_stream.hpp>
|
||||
#include <boost/beast/http/field.hpp>
|
||||
#include <boost/beast/ssl.hpp>
|
||||
#include <boost/beast/ssl/ssl_stream.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <boost/beast/websocket/detail/decorator.hpp>
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <boost/beast/websocket/stream.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include "bts-websocket-listener/http_field_formatter.h"
|
||||
#include "bts-websocket-listener/http_field_reader.h"
|
||||
#include "bts-websocket-listener/http_field_writer.h"
|
||||
#include "bts-websocket-listener/ip_resolver_results_formatter.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
namespace beast = boost::beast;
|
||||
namespace net = boost::asio;
|
||||
namespace ssl = net::ssl;
|
||||
namespace websocket = beast::websocket;
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
|
||||
class ChatGptExample {
|
||||
public:
|
||||
static std::string generateWebSocketKey() {
|
||||
// Generate a random 16-byte key (you can use a suitable random number generator)
|
||||
// Here, we are generating a simple example key for illustration.
|
||||
const std::string key = "dGhlIHNhbXBsZSBub25jZQ==";
|
||||
return key;
|
||||
}
|
||||
|
||||
static int run(
|
||||
std::string host, const std::string& port, const std::string& endpoint) {
|
||||
using std::map;
|
||||
using std::string;
|
||||
try {
|
||||
net::io_context ioc;
|
||||
ssl::context ctx(ssl::context::tlsv12_client);
|
||||
ctx.set_verify_mode(ssl::verify_none);
|
||||
websocket::stream<beast::ssl_stream<tcp::socket>> ws(ioc, ctx);
|
||||
|
||||
std::string websocketKey = generateWebSocketKey();
|
||||
std::map<beast::http::field, std::string> fields{
|
||||
{beast::http::field::sec_websocket_key, websocketKey},
|
||||
{beast::http::field::sec_websocket_version, "13"}};
|
||||
ws.set_option(boost::beast::websocket::stream_base::decorator(
|
||||
[&fields, host](boost::beast::websocket::request_type& req) {
|
||||
auto curr_fields = HttpFieldReader::read(req);
|
||||
map<beast::http::field, string> overwrites;
|
||||
for (const auto& [f, v] : fields) {
|
||||
auto curr = curr_fields.find(f);
|
||||
if (curr != curr_fields.end()) {
|
||||
overwrites[curr->first] = v;
|
||||
}
|
||||
}
|
||||
std::cout << "Websocket Request Headers:\n"
|
||||
<< HttpFieldFormatter::format(curr_fields)
|
||||
<< std::endl;
|
||||
std::cout << "Custom:\n"
|
||||
<< HttpFieldFormatter::format(fields) << std::endl;
|
||||
std::cout << "Overwrites:\n"
|
||||
<< HttpFieldFormatter::format(overwrites)
|
||||
<< std::endl;
|
||||
for (const auto& [f, v] : fields) {
|
||||
req.set(f, v);
|
||||
}
|
||||
req.set(
|
||||
beast::http::field::accept_encoding, "gzip, deflate, br");
|
||||
req.set(beast::http::field::accept_language, "en-US,en;q=0.9");
|
||||
req.set(beast::http::field::cache_control, "no-cache");
|
||||
req.set(beast::http::field::connection, "Upgrade");
|
||||
req.set(beast::http::field::host, host);
|
||||
req.set(beast::http::field::origin, "https://" + host);
|
||||
req.set(beast::http::field::pragma, "no-cache");
|
||||
// req.set(beast::http::field::sec_websocket_extensions, "permessage-deflate; client_max_window_bits");
|
||||
req.set(beast::http::field::sec_websocket_version, "13");
|
||||
req.set(beast::http::field::upgrade, "websocket");
|
||||
req.set(beast::http::field::user_agent, "Boost.Beast/216");
|
||||
}));
|
||||
// HttpFieldWriter::write(
|
||||
// ws, {{beast::http::field::sec_websocket_key, websocketKey},
|
||||
// {beast::http::field::sec_websocket_version, "13"}});
|
||||
|
||||
// Resolve the host and connect
|
||||
net::ip::tcp::resolver resolver(ioc);
|
||||
std::cout << "Resolving " << host << ":" << port << "..." << std::endl;
|
||||
auto results = resolver.resolve(host, port);
|
||||
std::cout << "Resolved " << host << ":" << port << " to "
|
||||
<< IpResolverResultsResponseFormatter::format(results)
|
||||
<< std::endl;
|
||||
std::cout << "Connecting..." << std::endl;
|
||||
boost::asio::connect(
|
||||
ws.next_layer().next_layer(), results.begin(), results.end());
|
||||
|
||||
std::cout << "Performing handshake with " << host << "/" << endpoint
|
||||
<< std::endl;
|
||||
// Perform WebSocket handshake with the custom headers
|
||||
ws.handshake(host, "/" + endpoint);
|
||||
std::cout << "Writing message" << std::endl;
|
||||
// Send a WebSocket message
|
||||
ws.write(net::buffer("Hello, WebSocket!"));
|
||||
|
||||
// Read a WebSocket message
|
||||
beast::flat_buffer buffer;
|
||||
ws.read(buffer);
|
||||
|
||||
std::cout << "Received: " << beast::buffers_to_string(buffer.data())
|
||||
<< std::endl;
|
||||
|
||||
// Close the WebSocket connection
|
||||
ws.close(websocket::close_code::normal);
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void blah(
|
||||
std::string host, const std::string& port, const std::string& endpoint) {
|
||||
try {
|
||||
net::io_context ioc;
|
||||
ssl::context ctx(ssl::context::tlsv12_client);
|
||||
// Create a WebSocket stream
|
||||
websocket::stream<beast::ssl_stream<boost::asio::ip::tcp::socket>> ws{
|
||||
ioc, ctx};
|
||||
|
||||
// Set the server hostname, host, and target
|
||||
const std::string target = "/" + endpoint;
|
||||
|
||||
// Resolve the host and connect using the lowest-level socket
|
||||
boost::asio::ip::tcp::resolver resolver{ioc};
|
||||
auto const results = resolver.resolve(host, port);
|
||||
boost::asio::connect(
|
||||
ws.next_layer().next_layer(), results.begin(), results.end());
|
||||
|
||||
ws.handshake(host, target);
|
||||
ws.write(net::buffer("Hello, WebSocket!"));
|
||||
|
||||
// Read a WebSocket message
|
||||
beast::flat_buffer buffer;
|
||||
ws.read(buffer);
|
||||
|
||||
std::cout << "Received: " << beast::buffers_to_string(buffer.data())
|
||||
<< std::endl;
|
||||
|
||||
// Close the WebSocket connection
|
||||
ws.close(websocket::close_code::normal);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "bts-http/URL.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeOauthConfig {
|
||||
public:
|
||||
std::string protocol{"https"};
|
||||
std::string host;
|
||||
std::uint16_t port{443};
|
||||
std::string endpoint;
|
||||
std::string client_id;
|
||||
std::string client_secret;
|
||||
|
||||
io::URL get_url() const;
|
||||
void get_url(io::URL &dest) const;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
CmeOauthConfig, protocol, host, port, endpoint, client_id, client_secret);
|
||||
|
||||
inline io::URL CmeOauthConfig::get_url() const {
|
||||
io::URL url;
|
||||
get_url(url);
|
||||
return url;
|
||||
}
|
||||
|
||||
inline void CmeOauthConfig::get_url(io::URL &dest) const {
|
||||
dest.fill_host(host);
|
||||
dest.fill_path(endpoint);
|
||||
dest.fill_port(port);
|
||||
dest.fill_protocol("https");
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "bts-http/URL.h"
|
||||
#include "bts-websocket-listener/cme_oauth_config.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeWebsocketConfig {
|
||||
public:
|
||||
// Needed to obtain the access token (ignored if access token is non-empty)
|
||||
CmeOauthConfig oauth;
|
||||
std::string access_token;
|
||||
std::string protocol{"wss"};
|
||||
std::string host;
|
||||
std::uint16_t port{443};
|
||||
std::string endpoint;
|
||||
|
||||
io::URL get_url() const;
|
||||
void get_url(io::URL &dest) const;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
CmeWebsocketConfig, oauth, access_token, protocol, host, port, endpoint);
|
||||
|
||||
inline io::URL CmeWebsocketConfig::get_url() const {
|
||||
io::URL url;
|
||||
get_url(url);
|
||||
return url;
|
||||
}
|
||||
|
||||
inline void CmeWebsocketConfig::get_url(io::URL &dest) const {
|
||||
dest.fill_host(host);
|
||||
dest.fill_path(endpoint);
|
||||
dest.fill_port(port);
|
||||
dest.fill_protocol(protocol);
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,380 @@
|
||||
#include "cme_websocket_session.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <curl/easy.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/write.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/core/detail/stream_traits.hpp>
|
||||
#include <boost/beast/core/error.hpp>
|
||||
#include <boost/beast/core/stream_traits.hpp>
|
||||
#include <boost/beast/core/tcp_stream.hpp>
|
||||
#include <boost/beast/http/field.hpp>
|
||||
#include <boost/beast/http/status.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <boost/beast/websocket/stream_base.hpp>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <websocketpp/common/connection_hdl.hpp>
|
||||
#include <websocketpp/common/system_error.hpp>
|
||||
#include <websocketpp/config/asio_client.hpp>
|
||||
#include <websocketpp/roles/client_endpoint.hpp>
|
||||
|
||||
#include "bts-http-client/curl_requestor.h"
|
||||
#include "bts-http-client/http_client.h"
|
||||
#include "bts-http/QueryParams.h"
|
||||
#include "bts-http/URL.h"
|
||||
#include "bts-http/http_request.h"
|
||||
#include "bts-http/http_response.h"
|
||||
#include "bts-http/oauth_response.h"
|
||||
#include "bts-json/JsonDumper.h"
|
||||
#include "bts-json/JsonReader.h"
|
||||
#include "bts-system/UNUSED.h"
|
||||
#include "bts-system/bts_assert.h"
|
||||
#include "bts-websocket-listener/boost_example3.h"
|
||||
#include "bts-websocket-listener/cme_oauth_config.h"
|
||||
#include "bts-websocket-listener/cme_websocket_config.h"
|
||||
#include "bts-websocket-listener/http_field_formatter.h"
|
||||
#include "bts-websocket-listener/http_field_reader.h"
|
||||
#include "bts-websocket-listener/ip_endpoint_formatter.h"
|
||||
#include "bts-websocket-listener/ip_resolver_results_formatter.h"
|
||||
#include "bts-websocket-listener/websocket_response_formatter.h"
|
||||
// #include "google/cloud/pubsub/subscriber.h"
|
||||
|
||||
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
|
||||
typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
|
||||
|
||||
using websocketpp::lib::placeholders::_1;
|
||||
using websocketpp::lib::placeholders::_2;
|
||||
using websocketpp::lib::bind;
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
namespace beast = boost::beast;
|
||||
|
||||
using bts::http::HTTPClient;
|
||||
using bts::http::HTTPRequest;
|
||||
using bts::http::HTTPResponse;
|
||||
using std::string;
|
||||
|
||||
// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
|
||||
|
||||
|
||||
/// Verify that one of the subject alternative names matches the given hostname
|
||||
bool verify_subject_alternative_name(const char * hostname, X509 * cert) {
|
||||
STACK_OF(GENERAL_NAME) * san_names = NULL;
|
||||
|
||||
san_names = (STACK_OF(GENERAL_NAME) *) X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
|
||||
if (san_names == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int san_names_count = sk_GENERAL_NAME_num(san_names);
|
||||
|
||||
bool result = false;
|
||||
|
||||
for (int i = 0; i < san_names_count; i++) {
|
||||
const GENERAL_NAME * current_name = sk_GENERAL_NAME_value(san_names, i);
|
||||
|
||||
if (current_name->type != GEN_DNS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
char const * dns_name = (char const *) ASN1_STRING_get0_data(current_name->d.dNSName);
|
||||
|
||||
// Make sure there isn't an embedded NUL character in the DNS name
|
||||
if (static_cast<size_t>(ASN1_STRING_length(current_name->d.dNSName)) != strlen(dns_name)) {
|
||||
break;
|
||||
}
|
||||
// Compare expected hostname with the CN
|
||||
result = (strcasecmp(hostname, dns_name) == 0);
|
||||
}
|
||||
sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Verify that the certificate common name matches the given hostname
|
||||
bool verify_common_name(char const * hostname, X509 * cert) {
|
||||
// Find the position of the CN field in the Subject field of the certificate
|
||||
int common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name(cert), NID_commonName, -1);
|
||||
if (common_name_loc < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extract the CN field
|
||||
X509_NAME_ENTRY * common_name_entry = X509_NAME_get_entry(X509_get_subject_name(cert), common_name_loc);
|
||||
if (common_name_entry == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert the CN field to a C string
|
||||
ASN1_STRING * common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
|
||||
if (common_name_asn1 == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char const * common_name_str = (char const *) ASN1_STRING_get0_data(common_name_asn1);
|
||||
|
||||
// Make sure there isn't an embedded NUL character in the CN
|
||||
if (static_cast<size_t>(ASN1_STRING_length(common_name_asn1)) != strlen(common_name_str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compare expected hostname with the CN
|
||||
return (strcasecmp(hostname, common_name_str) == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* This code is derived from examples and documentation found ato00po
|
||||
* http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/example/cpp03/ssl/client.cpp
|
||||
* and
|
||||
* https://github.com/iSECPartners/ssl-conservatory
|
||||
*/
|
||||
bool verify_certificate(const char * hostname, bool preverified, boost::asio::ssl::verify_context& ctx) {
|
||||
// The verify callback can be used to check whether the certificate that is
|
||||
// being presented is valid for the peer. For example, RFC 2818 describes
|
||||
// the steps involved in doing this for HTTPS. Consult the OpenSSL
|
||||
// documentation for more details. Note that the callback is called once
|
||||
// for each certificate in the certificate chain, starting from the root
|
||||
// certificate authority.
|
||||
|
||||
// Retrieve the depth of the current cert in the chain. 0 indicates the
|
||||
// actual server cert, upon which we will perform extra validation
|
||||
// (specifically, ensuring that the hostname matches. For other certs we
|
||||
// will use the 'preverified' flag from Asio, which incorporates a number of
|
||||
// non-implementation specific OpenSSL checking, such as the formatting of
|
||||
// certs and the trusted status based on the CA certs we imported earlier.
|
||||
int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
|
||||
|
||||
// if we are on the final cert and everything else checks out, ensure that
|
||||
// the hostname is present on the list of SANs or the common name (CN).
|
||||
if (depth == 0 && preverified) {
|
||||
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
|
||||
|
||||
if (verify_subject_alternative_name(hostname, cert)) {
|
||||
return true;
|
||||
} else if (verify_common_name(hostname, cert)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return preverified;
|
||||
}
|
||||
|
||||
|
||||
context_ptr on_tls_init(const char * hostname, websocketpp::connection_hdl) {
|
||||
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
|
||||
|
||||
try {
|
||||
ctx->set_options(boost::asio::ssl::context::default_workarounds |
|
||||
boost::asio::ssl::context::no_sslv2 |
|
||||
boost::asio::ssl::context::no_sslv3 |
|
||||
boost::asio::ssl::context::single_dh_use);
|
||||
|
||||
|
||||
ctx->set_verify_mode(boost::asio::ssl::verify_none);
|
||||
ctx->set_verify_callback(bind(&verify_certificate, hostname, ::_1, ::_2));
|
||||
|
||||
// Here we load the CA certificates of all CA's that this client trusts.
|
||||
// ctx->load_verify_file("ca-chain.cert.pem");
|
||||
} catch (std::exception& e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static HTTPRequest build_oauth_request(const CmeOauthConfig &cfg) {
|
||||
HTTPRequest http_req;
|
||||
http_req.url = cfg.get_url().to_string();
|
||||
io::QueryParams qp;
|
||||
qp["grant_type"] = "client_credentials";
|
||||
qp["client_id"] = cfg.client_id;
|
||||
qp["client_secret"] = cfg.client_secret;
|
||||
http_req.body = qp.to_string();
|
||||
return http_req;
|
||||
}
|
||||
|
||||
static OauthResponse request_oauth_token(const CmeOauthConfig &cfg) {
|
||||
HTTPRequest req = build_oauth_request(cfg);
|
||||
LOG(Gen, Info, "Requesting oauth:\n{}", io::JsonDumper::dump(req));
|
||||
HTTPClient client;
|
||||
HTTPResponse res = client.post(req);
|
||||
LOG(Gen, Info, "Received oauth response:\n{}", io::JsonDumper::dump(res));
|
||||
const auto j = nlohmann::json::parse(res.body());
|
||||
return io::JsonReader{&j}.read<OauthResponse>();
|
||||
}
|
||||
|
||||
// // Define a TLS initialization handler
|
||||
// struct tls_init_handler
|
||||
// : public websocketpp::lib::enable_shared_from_this<tls_init_handler> {
|
||||
// void operator()(websocketpp::connection_hdl hdl) {
|
||||
// try {
|
||||
// // Create a TLS context
|
||||
// ssl::context ctx(ssl::context::sslv23);
|
||||
|
||||
// const char *client_certificate_key =
|
||||
// "/home/bts/spark/src/lib/utils/crypto/spark_public.pem";
|
||||
// const char *client_private_key =
|
||||
// "/home/bts/spark/src/lib/utils/crypto/spark_private.pem";
|
||||
// // Load the client certificate and private key
|
||||
// ctx.use_certificate_file(client_certificate_key, ssl::context::pem);
|
||||
// ctx.use_private_key_file(client_private_key, ssl::context::pem);
|
||||
|
||||
// // Set up the TLS context for the connection
|
||||
// con->set_tls_init_handler(bind(
|
||||
// &tls_init_handler::on_tls_init, this,
|
||||
// websocketpp::lib::placeholders::_1));
|
||||
// con->set_tls_init_handler(bind(
|
||||
// &tls_init_handler::on_tls_init, this,
|
||||
// websocketpp::lib::placeholders::_1));
|
||||
|
||||
// // con->init_asio();
|
||||
// // con->connect(uri);
|
||||
// } catch (const std::exception &e) {
|
||||
// std::cerr << "TLS initialization error: " << e.what() << std::endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
// void on_tls_init(ssl::context &ctx) {
|
||||
// // Perform any additional TLS initialization here if needed
|
||||
// }
|
||||
|
||||
// websocketpp::connection_hdl hdl;
|
||||
// websocketpp::client<websocketpp::config::asio_tls_client>::connection_ptr
|
||||
// con;
|
||||
// std::string uri;
|
||||
// };
|
||||
|
||||
struct CmeWebsocketSession::PImpl {
|
||||
PImpl() : resolver(io), ws(io) {}
|
||||
|
||||
CmeWebsocketConfig cfg;
|
||||
boost::asio::io_context io;
|
||||
beast::net::ip::tcp::resolver resolver;
|
||||
beast::websocket::stream<tcp::socket> ws;
|
||||
|
||||
static void on_open(websocketpp::connection_hdl hdl) {
|
||||
(void)(hdl);
|
||||
// TODO
|
||||
LOG(gen, Info, "on_open {}", string{"yay!"});
|
||||
}
|
||||
|
||||
// static std::shared_ptr<ssl::context> on_tls_init(
|
||||
// websocketpp::connection_hdl hdl) {
|
||||
// (void)(hdl);
|
||||
// // TODO
|
||||
// LOG(gen, Info, "on_init {}", string{"yay!"});
|
||||
// }
|
||||
|
||||
static void on_message(
|
||||
websocketpp::connection_hdl,
|
||||
websocketpp::client<websocketpp::config::asio_tls_client>::message_ptr
|
||||
msg) {
|
||||
std::cout << "CmeWebsocketSession::on_message " << std::endl;
|
||||
std::cout << msg->get_payload() << std::endl;
|
||||
LOG(Md, Info, "CmeWebsocketSession::on_message {}", msg->get_payload());
|
||||
}
|
||||
|
||||
void run() {
|
||||
const auto url = cfg.get_url();
|
||||
string path = url.path();
|
||||
if (path.starts_with('/')) {
|
||||
path.erase(path.begin());
|
||||
}
|
||||
const auto url_str = fmt::format("wss://{}/{}", url.host(), url.path());
|
||||
websocketpp::client<websocketpp::config::asio_tls_client> client;
|
||||
try {
|
||||
client.set_access_channels(websocketpp::log::alevel::all);
|
||||
client.clear_access_channels(websocketpp::log::alevel::frame_payload);
|
||||
client.set_error_channels(websocketpp::log::elevel::all);
|
||||
|
||||
client.init_asio();
|
||||
client.set_message_handler(&on_message);
|
||||
client.set_open_handler(&on_open);
|
||||
client.set_tls_init_handler(bind(&on_tls_init, url.host().c_str(), ::_1));
|
||||
websocketpp::lib::error_code ec;
|
||||
websocketpp::client<
|
||||
websocketpp::config::asio_tls_client>::connection_ptr con =
|
||||
client.get_connection(url_str, ec);
|
||||
if (ec) {
|
||||
bts_assert_msgf(
|
||||
false, "Failed to get_connection: {}", ec.message());
|
||||
}
|
||||
con->append_header("Authorization", "Bearer " + cfg.access_token);
|
||||
client.connect(con);
|
||||
client.run();
|
||||
} catch (const websocketpp::exception &ex) {
|
||||
LOG(Md, Error, "Failed to connect to cme websocket server: {}",
|
||||
string{ex.what()});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
CmeWebsocketSession::CmeWebsocketSession(CmeWebsocketConfig cfg)
|
||||
: me_(std::make_unique<PImpl>()) {
|
||||
me_->cfg = std::move(cfg);
|
||||
me_->run();
|
||||
// string text{R"({
|
||||
// "header": {
|
||||
// "messageType": "subscribe",
|
||||
// "sentTime": "",
|
||||
// "version": "",
|
||||
// "requestId": "1",
|
||||
// "token": ""
|
||||
// },
|
||||
// "payload": {
|
||||
// "subscriptionMessageTypes": ["TRD"],
|
||||
// "subscriptions": [
|
||||
// {
|
||||
// "exchangeMic": "XCME",
|
||||
// "productType": "FUT",
|
||||
// "productCode": "BTC"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// })"};
|
||||
// BoostExample3::run(
|
||||
// me_->cfg.host, std::to_string(me_->cfg.port), me_->cfg.endpoint, text,
|
||||
// me_->cfg.access_token);
|
||||
}
|
||||
|
||||
CmeWebsocketSession::~CmeWebsocketSession() = default;
|
||||
|
||||
CmeWebsocketSession CmeWebsocketSession::create(CmeWebsocketConfig cfg) {
|
||||
std::cout << io::JsonDumper::dump(cfg) << std::endl;
|
||||
// bts_assert(false);
|
||||
if (cfg.access_token.empty()) {
|
||||
auto oauth_res = request_oauth_token(cfg.oauth);
|
||||
const auto oauth_res_str = io::JsonDumper::dump(oauth_res);
|
||||
cfg.access_token = oauth_res.access_token;
|
||||
if (cfg.access_token.empty()) {
|
||||
throw std::runtime_error{"Failed to obtain oauth access token"};
|
||||
}
|
||||
}
|
||||
return CmeWebsocketSession{std::move(cfg)};
|
||||
}
|
||||
|
||||
int CmeWebsocketSession::run(const CmeWebsocketConfig &cfg) {
|
||||
auto session = create(cfg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "bts-http/URL.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeOauthConfig;
|
||||
class CmeWebsocketConfig;
|
||||
|
||||
class CmeWebsocketSession {
|
||||
public:
|
||||
explicit CmeWebsocketSession(CmeWebsocketConfig cfg);
|
||||
~CmeWebsocketSession();
|
||||
|
||||
static CmeWebsocketSession create(CmeWebsocketConfig cfg);
|
||||
|
||||
static int run(const CmeWebsocketConfig &cfg);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me_;
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "bts-websocket-listener/cme_websocket_config.h"
|
||||
#include "bts-websocket-listener/io/http/HTTPRequest.h"
|
||||
#include "bts-websocket-listener/io/http/HTTPClient.h"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
namespace asio = boost::asio;
|
||||
namespace beast = boost::beast;
|
||||
using tcp = asio::ip::tcp;
|
||||
using json = nlohmann::json;
|
||||
|
||||
class CmeWebsocketSession_Old {
|
||||
public:
|
||||
static CmeWebsocketConfig getDefaultCfg() {
|
||||
CmeWebsocketConfig cfg{"markets.api.uat.cmegroup.com", 443, "/marketdatastream/v1", "API_THINKALPHA123",
|
||||
"$MM9Rn*#qYARY$$QTMA#AMn*"};
|
||||
return cfg;
|
||||
}
|
||||
|
||||
static std::string getToken() {
|
||||
auto cfg = getDefaultCfg();
|
||||
io::http::HTTPRequest req;
|
||||
req.url().fill_host(cfg.host);
|
||||
req.url().fill_path(cfg.endpoint);
|
||||
req.url().fill_port(cfg.port);
|
||||
req.url().fill_protocol("https");
|
||||
io::http::HTTPClient client;
|
||||
return "";
|
||||
}
|
||||
|
||||
static int run() {
|
||||
return run(getDefaultCfg());
|
||||
}
|
||||
|
||||
static int run(const CmeWebsocketConfig &cfg) {
|
||||
asio::io_context io;
|
||||
tcp::resolver resolver(io);
|
||||
tcp::socket socket(io);
|
||||
|
||||
try
|
||||
{
|
||||
// Resolve the WebSocket server host and port
|
||||
auto const results = resolver.resolve(cfg.host, std::to_string(cfg.port));
|
||||
|
||||
// Connect to the WebSocket server
|
||||
asio::connect(socket, results.begin(), results.end());
|
||||
|
||||
// Perform a WebSocket handshake
|
||||
beast::websocket::stream<tcp::socket> ws(std::move(socket));
|
||||
ws.handshake(cfg.host, cfg.endpoint);
|
||||
|
||||
// Send a JSON-RPC request or notification
|
||||
json request = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "subscribe"},
|
||||
{"params", {"query", "literal(\"n\", \"2.0\")"}},
|
||||
{"id", 1} // Use a unique ID for requests, null for notifications
|
||||
};
|
||||
|
||||
ws.write(asio::buffer(request.dump()));
|
||||
|
||||
// Receive a response or notification
|
||||
beast::multi_buffer buffer;
|
||||
ws.read(buffer);
|
||||
|
||||
// Handle the response or notification
|
||||
json response = json::parse(beast::buffers_to_string(buffer.data()));
|
||||
std::cout << "Received: " << response << std::endl;
|
||||
|
||||
// Close the WebSocket connection
|
||||
ws.close(beast::websocket::close_code::normal);
|
||||
} catch (std::exception const &e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace asio = boost::asio;
|
||||
namespace beast = boost::beast;
|
||||
using tcp = asio::ip::tcp;
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace bts::websocket {
|
||||
class ExampleMain {
|
||||
public:
|
||||
static int run() {
|
||||
asio::io_context io;
|
||||
tcp::resolver resolver(io);
|
||||
tcp::socket socket(io);
|
||||
|
||||
// const std::string host{"example.com"};
|
||||
// const std::string endpoint{"/your-ws-endpoint"};
|
||||
// const std::string port{"443"};
|
||||
const std::string host{"192.168.50.94"};
|
||||
const std::string endpoint{"/"};
|
||||
const std::string port{"443"};
|
||||
try
|
||||
{
|
||||
// Resolve the WebSocket server host and port
|
||||
auto const results = resolver.resolve(host, port);
|
||||
|
||||
// Connect to the WebSocket server
|
||||
asio::connect(socket, results.begin(), results.end());
|
||||
|
||||
// Perform a WebSocket handshake
|
||||
beast::websocket::stream<tcp::socket> ws(std::move(socket));
|
||||
ws.handshake(host, endpoint);
|
||||
|
||||
// Send a JSON-RPC request or notification
|
||||
json request = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", "subscribe"},
|
||||
{"params", {"query", "literal(\"n\", \"2.0\")"}},
|
||||
{"id", 1} // Use a unique ID for requests, null for notifications
|
||||
};
|
||||
|
||||
ws.write(asio::buffer(request.dump()));
|
||||
|
||||
// Receive a response or notification
|
||||
beast::multi_buffer buffer;
|
||||
ws.read(buffer);
|
||||
|
||||
// Handle the response or notification
|
||||
json response = json::parse(beast::buffers_to_string(buffer.data()));
|
||||
std::cout << "Received: " << response << std::endl;
|
||||
|
||||
// Close the WebSocket connection
|
||||
ws.close(beast::websocket::close_code::normal);
|
||||
} catch (std::exception const &e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/beast/http/verb.hpp>
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
|
||||
#include "bts-system/table_formatter.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class HttpFieldFormatter {
|
||||
public:
|
||||
static std::string format(
|
||||
const std::map<boost::beast::http::field, std::string> &fields);
|
||||
static std::string format(const std::map<std::string, std::string> &fields);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::string HttpFieldFormatter::format(
|
||||
const std::map<boost::beast::http::field, std::string> &fields) {
|
||||
std::map<std::string, std::string> by_name;
|
||||
for (const auto &[f, v] : fields) {
|
||||
const std::string fname{boost::beast::http::to_string(
|
||||
boost::beast::http::field::http2_settings)};
|
||||
by_name[fname] = v;
|
||||
}
|
||||
return format(by_name);
|
||||
}
|
||||
|
||||
inline std::string HttpFieldFormatter::format(
|
||||
const std::map<std::string, std::string> &fields) {
|
||||
using SchemaBuilder = system::TableFormatter::SchemaBuilder<
|
||||
std::pair<std::string, std::string>>;
|
||||
if (fields.empty()) {
|
||||
return {};
|
||||
}
|
||||
auto max_field_sz_it = std::max_element(
|
||||
fields.begin(), fields.end(), [](const auto &p1, const auto &p2) {
|
||||
return p1.first.size() < p2.first.size();
|
||||
});
|
||||
SchemaBuilder schema;
|
||||
schema.add(
|
||||
"Field", [](const auto &p) { return p.first; },
|
||||
max_field_sz_it->first.size());
|
||||
schema.add("Value", [](const auto &p) { return p.second; });
|
||||
|
||||
std::stringstream out;
|
||||
system::TableFormatter::format(out, fields, schema.result);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,385 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class HttpFieldReader {
|
||||
public:
|
||||
static std::map<boost::beast::http::field, std::string> read(
|
||||
boost::beast::websocket::request_type &req);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::map<boost::beast::http::field, std::string> HttpFieldReader::read(
|
||||
boost::beast::websocket::request_type &req) {
|
||||
static std::vector<boost::beast::http::field> fields{
|
||||
boost::beast::http::field::a_im,
|
||||
boost::beast::http::field::accept,
|
||||
boost::beast::http::field::accept_additions,
|
||||
boost::beast::http::field::accept_charset,
|
||||
boost::beast::http::field::accept_datetime,
|
||||
boost::beast::http::field::accept_encoding,
|
||||
boost::beast::http::field::accept_features,
|
||||
boost::beast::http::field::accept_language,
|
||||
boost::beast::http::field::accept_patch,
|
||||
boost::beast::http::field::accept_post,
|
||||
boost::beast::http::field::accept_ranges,
|
||||
boost::beast::http::field::access_control,
|
||||
boost::beast::http::field::access_control_allow_credentials,
|
||||
boost::beast::http::field::access_control_allow_headers,
|
||||
boost::beast::http::field::access_control_allow_methods,
|
||||
boost::beast::http::field::access_control_allow_origin,
|
||||
boost::beast::http::field::access_control_expose_headers,
|
||||
boost::beast::http::field::access_control_max_age,
|
||||
boost::beast::http::field::access_control_request_headers,
|
||||
boost::beast::http::field::access_control_request_method,
|
||||
boost::beast::http::field::age,
|
||||
boost::beast::http::field::allow,
|
||||
boost::beast::http::field::alpn,
|
||||
boost::beast::http::field::also_control,
|
||||
boost::beast::http::field::alt_svc,
|
||||
boost::beast::http::field::alt_used,
|
||||
boost::beast::http::field::alternate_recipient,
|
||||
boost::beast::http::field::alternates,
|
||||
boost::beast::http::field::apparently_to,
|
||||
boost::beast::http::field::apply_to_redirect_ref,
|
||||
boost::beast::http::field::approved,
|
||||
boost::beast::http::field::archive,
|
||||
boost::beast::http::field::archived_at,
|
||||
boost::beast::http::field::article_names,
|
||||
boost::beast::http::field::article_updates,
|
||||
boost::beast::http::field::authentication_control,
|
||||
boost::beast::http::field::authentication_info,
|
||||
boost::beast::http::field::authentication_results,
|
||||
boost::beast::http::field::authorization,
|
||||
boost::beast::http::field::auto_submitted,
|
||||
boost::beast::http::field::autoforwarded,
|
||||
boost::beast::http::field::autosubmitted,
|
||||
boost::beast::http::field::base,
|
||||
boost::beast::http::field::bcc,
|
||||
boost::beast::http::field::body,
|
||||
boost::beast::http::field::c_ext,
|
||||
boost::beast::http::field::c_man,
|
||||
boost::beast::http::field::c_opt,
|
||||
boost::beast::http::field::c_pep,
|
||||
boost::beast::http::field::c_pep_info,
|
||||
boost::beast::http::field::cache_control,
|
||||
boost::beast::http::field::caldav_timezones,
|
||||
boost::beast::http::field::cancel_key,
|
||||
boost::beast::http::field::cancel_lock,
|
||||
boost::beast::http::field::cc,
|
||||
boost::beast::http::field::close,
|
||||
boost::beast::http::field::comments,
|
||||
boost::beast::http::field::compliance,
|
||||
boost::beast::http::field::connection,
|
||||
boost::beast::http::field::content_alternative,
|
||||
boost::beast::http::field::content_base,
|
||||
boost::beast::http::field::content_description,
|
||||
boost::beast::http::field::content_disposition,
|
||||
boost::beast::http::field::content_duration,
|
||||
boost::beast::http::field::content_encoding,
|
||||
boost::beast::http::field::content_features,
|
||||
boost::beast::http::field::content_id,
|
||||
boost::beast::http::field::content_identifier,
|
||||
boost::beast::http::field::content_language,
|
||||
boost::beast::http::field::content_length,
|
||||
boost::beast::http::field::content_location,
|
||||
boost::beast::http::field::content_md5,
|
||||
boost::beast::http::field::content_range,
|
||||
boost::beast::http::field::content_return,
|
||||
boost::beast::http::field::content_script_type,
|
||||
boost::beast::http::field::content_style_type,
|
||||
boost::beast::http::field::content_transfer_encoding,
|
||||
boost::beast::http::field::content_type,
|
||||
boost::beast::http::field::content_version,
|
||||
boost::beast::http::field::control,
|
||||
boost::beast::http::field::conversion,
|
||||
boost::beast::http::field::conversion_with_loss,
|
||||
boost::beast::http::field::cookie,
|
||||
boost::beast::http::field::cookie2,
|
||||
boost::beast::http::field::cost,
|
||||
boost::beast::http::field::dasl,
|
||||
boost::beast::http::field::date,
|
||||
boost::beast::http::field::date_received,
|
||||
boost::beast::http::field::dav,
|
||||
boost::beast::http::field::default_style,
|
||||
boost::beast::http::field::deferred_delivery,
|
||||
boost::beast::http::field::delivery_date,
|
||||
boost::beast::http::field::delta_base,
|
||||
boost::beast::http::field::depth,
|
||||
boost::beast::http::field::derived_from,
|
||||
boost::beast::http::field::destination,
|
||||
boost::beast::http::field::differential_id,
|
||||
boost::beast::http::field::digest,
|
||||
boost::beast::http::field::discarded_x400_ipms_extensions,
|
||||
boost::beast::http::field::discarded_x400_mts_extensions,
|
||||
boost::beast::http::field::disclose_recipients,
|
||||
boost::beast::http::field::disposition_notification_options,
|
||||
boost::beast::http::field::disposition_notification_to,
|
||||
boost::beast::http::field::distribution,
|
||||
boost::beast::http::field::dkim_signature,
|
||||
boost::beast::http::field::dl_expansion_history,
|
||||
boost::beast::http::field::downgraded_bcc,
|
||||
boost::beast::http::field::downgraded_cc,
|
||||
boost::beast::http::field::downgraded_disposition_notification_to,
|
||||
boost::beast::http::field::downgraded_final_recipient,
|
||||
boost::beast::http::field::downgraded_from,
|
||||
boost::beast::http::field::downgraded_in_reply_to,
|
||||
boost::beast::http::field::downgraded_mail_from,
|
||||
boost::beast::http::field::downgraded_message_id,
|
||||
boost::beast::http::field::downgraded_original_recipient,
|
||||
boost::beast::http::field::downgraded_rcpt_to,
|
||||
boost::beast::http::field::downgraded_references,
|
||||
boost::beast::http::field::downgraded_reply_to,
|
||||
boost::beast::http::field::downgraded_resent_bcc,
|
||||
boost::beast::http::field::downgraded_resent_cc,
|
||||
boost::beast::http::field::downgraded_resent_from,
|
||||
boost::beast::http::field::downgraded_resent_reply_to,
|
||||
boost::beast::http::field::downgraded_resent_sender,
|
||||
boost::beast::http::field::downgraded_resent_to,
|
||||
boost::beast::http::field::downgraded_return_path,
|
||||
boost::beast::http::field::downgraded_sender,
|
||||
boost::beast::http::field::downgraded_to,
|
||||
boost::beast::http::field::ediint_features,
|
||||
boost::beast::http::field::eesst_version,
|
||||
boost::beast::http::field::encoding,
|
||||
boost::beast::http::field::encrypted,
|
||||
boost::beast::http::field::errors_to,
|
||||
boost::beast::http::field::etag,
|
||||
boost::beast::http::field::expect,
|
||||
boost::beast::http::field::expires,
|
||||
boost::beast::http::field::expiry_date,
|
||||
boost::beast::http::field::ext,
|
||||
boost::beast::http::field::followup_to,
|
||||
boost::beast::http::field::forwarded,
|
||||
boost::beast::http::field::from,
|
||||
boost::beast::http::field::generate_delivery_report,
|
||||
boost::beast::http::field::getprofile,
|
||||
boost::beast::http::field::hobareg,
|
||||
boost::beast::http::field::host,
|
||||
boost::beast::http::field::http2_settings,
|
||||
boost::beast::http::field::if_,
|
||||
boost::beast::http::field::if_match,
|
||||
boost::beast::http::field::if_modified_since,
|
||||
boost::beast::http::field::if_none_match,
|
||||
boost::beast::http::field::if_range,
|
||||
boost::beast::http::field::if_schedule_tag_match,
|
||||
boost::beast::http::field::if_unmodified_since,
|
||||
boost::beast::http::field::im,
|
||||
boost::beast::http::field::importance,
|
||||
boost::beast::http::field::in_reply_to,
|
||||
boost::beast::http::field::incomplete_copy,
|
||||
boost::beast::http::field::injection_date,
|
||||
boost::beast::http::field::injection_info,
|
||||
boost::beast::http::field::jabber_id,
|
||||
boost::beast::http::field::keep_alive,
|
||||
boost::beast::http::field::keywords,
|
||||
boost::beast::http::field::label,
|
||||
boost::beast::http::field::language,
|
||||
boost::beast::http::field::last_modified,
|
||||
boost::beast::http::field::latest_delivery_time,
|
||||
boost::beast::http::field::lines,
|
||||
boost::beast::http::field::link,
|
||||
boost::beast::http::field::list_archive,
|
||||
boost::beast::http::field::list_help,
|
||||
boost::beast::http::field::list_id,
|
||||
boost::beast::http::field::list_owner,
|
||||
boost::beast::http::field::list_post,
|
||||
boost::beast::http::field::list_subscribe,
|
||||
boost::beast::http::field::list_unsubscribe,
|
||||
boost::beast::http::field::list_unsubscribe_post,
|
||||
boost::beast::http::field::location,
|
||||
boost::beast::http::field::lock_token,
|
||||
boost::beast::http::field::man,
|
||||
boost::beast::http::field::max_forwards,
|
||||
boost::beast::http::field::memento_datetime,
|
||||
boost::beast::http::field::message_context,
|
||||
boost::beast::http::field::message_id,
|
||||
boost::beast::http::field::message_type,
|
||||
boost::beast::http::field::meter,
|
||||
boost::beast::http::field::method_check,
|
||||
boost::beast::http::field::method_check_expires,
|
||||
boost::beast::http::field::mime_version,
|
||||
boost::beast::http::field::mmhs_acp127_message_identifier,
|
||||
boost::beast::http::field::mmhs_authorizing_users,
|
||||
boost::beast::http::field::mmhs_codress_message_indicator,
|
||||
boost::beast::http::field::mmhs_copy_precedence,
|
||||
boost::beast::http::field::mmhs_exempted_address,
|
||||
boost::beast::http::field::mmhs_extended_authorisation_info,
|
||||
boost::beast::http::field::mmhs_handling_instructions,
|
||||
boost::beast::http::field::mmhs_message_instructions,
|
||||
boost::beast::http::field::mmhs_message_type,
|
||||
boost::beast::http::field::mmhs_originator_plad,
|
||||
boost::beast::http::field::mmhs_originator_reference,
|
||||
boost::beast::http::field::mmhs_other_recipients_indicator_cc,
|
||||
boost::beast::http::field::mmhs_other_recipients_indicator_to,
|
||||
boost::beast::http::field::mmhs_primary_precedence,
|
||||
boost::beast::http::field::mmhs_subject_indicator_codes,
|
||||
boost::beast::http::field::mt_priority,
|
||||
boost::beast::http::field::negotiate,
|
||||
boost::beast::http::field::newsgroups,
|
||||
boost::beast::http::field::nntp_posting_date,
|
||||
boost::beast::http::field::nntp_posting_host,
|
||||
boost::beast::http::field::non_compliance,
|
||||
boost::beast::http::field::obsoletes,
|
||||
boost::beast::http::field::opt,
|
||||
boost::beast::http::field::optional,
|
||||
boost::beast::http::field::optional_www_authenticate,
|
||||
boost::beast::http::field::ordering_type,
|
||||
boost::beast::http::field::organization,
|
||||
boost::beast::http::field::origin,
|
||||
boost::beast::http::field::original_encoded_information_types,
|
||||
boost::beast::http::field::original_from,
|
||||
boost::beast::http::field::original_message_id,
|
||||
boost::beast::http::field::original_recipient,
|
||||
boost::beast::http::field::original_sender,
|
||||
boost::beast::http::field::original_subject,
|
||||
boost::beast::http::field::originator_return_address,
|
||||
boost::beast::http::field::overwrite,
|
||||
boost::beast::http::field::p3p,
|
||||
boost::beast::http::field::path,
|
||||
boost::beast::http::field::pep,
|
||||
boost::beast::http::field::pep_info,
|
||||
boost::beast::http::field::pics_label,
|
||||
boost::beast::http::field::position,
|
||||
boost::beast::http::field::posting_version,
|
||||
boost::beast::http::field::pragma,
|
||||
boost::beast::http::field::prefer,
|
||||
boost::beast::http::field::preference_applied,
|
||||
boost::beast::http::field::prevent_nondelivery_report,
|
||||
boost::beast::http::field::priority,
|
||||
boost::beast::http::field::privicon,
|
||||
boost::beast::http::field::profileobject,
|
||||
boost::beast::http::field::protocol,
|
||||
boost::beast::http::field::protocol_info,
|
||||
boost::beast::http::field::protocol_query,
|
||||
boost::beast::http::field::protocol_request,
|
||||
boost::beast::http::field::proxy_authenticate,
|
||||
boost::beast::http::field::proxy_authentication_info,
|
||||
boost::beast::http::field::proxy_authorization,
|
||||
boost::beast::http::field::proxy_connection,
|
||||
boost::beast::http::field::proxy_features,
|
||||
boost::beast::http::field::proxy_instruction,
|
||||
boost::beast::http::field::public_,
|
||||
boost::beast::http::field::public_key_pins,
|
||||
boost::beast::http::field::public_key_pins_report_only,
|
||||
boost::beast::http::field::range,
|
||||
boost::beast::http::field::received,
|
||||
boost::beast::http::field::received_spf,
|
||||
boost::beast::http::field::redirect_ref,
|
||||
boost::beast::http::field::references,
|
||||
boost::beast::http::field::referer,
|
||||
boost::beast::http::field::referer_root,
|
||||
boost::beast::http::field::relay_version,
|
||||
boost::beast::http::field::reply_by,
|
||||
boost::beast::http::field::reply_to,
|
||||
boost::beast::http::field::require_recipient_valid_since,
|
||||
boost::beast::http::field::resent_bcc,
|
||||
boost::beast::http::field::resent_cc,
|
||||
boost::beast::http::field::resent_date,
|
||||
boost::beast::http::field::resent_from,
|
||||
boost::beast::http::field::resent_message_id,
|
||||
boost::beast::http::field::resent_reply_to,
|
||||
boost::beast::http::field::resent_sender,
|
||||
boost::beast::http::field::resent_to,
|
||||
boost::beast::http::field::resolution_hint,
|
||||
boost::beast::http::field::resolver_location,
|
||||
boost::beast::http::field::retry_after,
|
||||
boost::beast::http::field::return_path,
|
||||
boost::beast::http::field::safe,
|
||||
boost::beast::http::field::schedule_reply,
|
||||
boost::beast::http::field::schedule_tag,
|
||||
boost::beast::http::field::sec_websocket_accept,
|
||||
boost::beast::http::field::sec_websocket_extensions,
|
||||
boost::beast::http::field::sec_websocket_key,
|
||||
boost::beast::http::field::sec_websocket_protocol,
|
||||
boost::beast::http::field::sec_websocket_version,
|
||||
boost::beast::http::field::security_scheme,
|
||||
boost::beast::http::field::see_also,
|
||||
boost::beast::http::field::sender,
|
||||
boost::beast::http::field::sensitivity,
|
||||
boost::beast::http::field::server,
|
||||
boost::beast::http::field::set_cookie,
|
||||
boost::beast::http::field::set_cookie2,
|
||||
boost::beast::http::field::setprofile,
|
||||
boost::beast::http::field::sio_label,
|
||||
boost::beast::http::field::sio_label_history,
|
||||
boost::beast::http::field::slug,
|
||||
boost::beast::http::field::soapaction,
|
||||
boost::beast::http::field::solicitation,
|
||||
boost::beast::http::field::status_uri,
|
||||
boost::beast::http::field::strict_transport_security,
|
||||
boost::beast::http::field::subject,
|
||||
boost::beast::http::field::subok,
|
||||
boost::beast::http::field::subst,
|
||||
boost::beast::http::field::summary,
|
||||
boost::beast::http::field::supersedes,
|
||||
boost::beast::http::field::surrogate_capability,
|
||||
boost::beast::http::field::surrogate_control,
|
||||
boost::beast::http::field::tcn,
|
||||
boost::beast::http::field::te,
|
||||
boost::beast::http::field::timeout,
|
||||
boost::beast::http::field::title,
|
||||
boost::beast::http::field::to,
|
||||
boost::beast::http::field::topic,
|
||||
boost::beast::http::field::trailer,
|
||||
boost::beast::http::field::transfer_encoding,
|
||||
boost::beast::http::field::ttl,
|
||||
boost::beast::http::field::ua_color,
|
||||
boost::beast::http::field::ua_media,
|
||||
boost::beast::http::field::ua_pixels,
|
||||
boost::beast::http::field::ua_resolution,
|
||||
boost::beast::http::field::ua_windowpixels,
|
||||
boost::beast::http::field::upgrade,
|
||||
boost::beast::http::field::urgency,
|
||||
boost::beast::http::field::uri,
|
||||
boost::beast::http::field::user_agent,
|
||||
boost::beast::http::field::variant_vary,
|
||||
boost::beast::http::field::vary,
|
||||
boost::beast::http::field::vbr_info,
|
||||
boost::beast::http::field::version,
|
||||
boost::beast::http::field::via,
|
||||
boost::beast::http::field::want_digest,
|
||||
boost::beast::http::field::warning,
|
||||
boost::beast::http::field::www_authenticate,
|
||||
boost::beast::http::field::x_archived_at,
|
||||
boost::beast::http::field::x_device_accept,
|
||||
boost::beast::http::field::x_device_accept_charset,
|
||||
boost::beast::http::field::x_device_accept_encoding,
|
||||
boost::beast::http::field::x_device_accept_language,
|
||||
boost::beast::http::field::x_device_user_agent,
|
||||
boost::beast::http::field::x_frame_options,
|
||||
boost::beast::http::field::x_mittente,
|
||||
boost::beast::http::field::x_pgp_sig,
|
||||
boost::beast::http::field::x_ricevuta,
|
||||
boost::beast::http::field::x_riferimento_message_id,
|
||||
boost::beast::http::field::x_tiporicevuta,
|
||||
boost::beast::http::field::x_trasporto,
|
||||
boost::beast::http::field::x_verificasicurezza,
|
||||
boost::beast::http::field::x400_content_identifier,
|
||||
boost::beast::http::field::x400_content_return,
|
||||
boost::beast::http::field::x400_content_type,
|
||||
boost::beast::http::field::x400_mts_identifier,
|
||||
boost::beast::http::field::x400_originator,
|
||||
boost::beast::http::field::x400_received,
|
||||
boost::beast::http::field::x400_recipients,
|
||||
boost::beast::http::field::x400_trace,
|
||||
boost::beast::http::field::xref,
|
||||
};
|
||||
std::map<boost::beast::http::field, std::string> rv;
|
||||
for (auto f : fields) {
|
||||
auto it = req.find(f);
|
||||
if (it != req.end()) {
|
||||
rv[f] = std::string{it->value()};
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/beast/core/tcp_stream.hpp>
|
||||
#include <boost/beast/ssl/ssl_stream.hpp>
|
||||
#include <boost/beast/websocket/stream.hpp>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class HttpFieldWriter {
|
||||
public:
|
||||
static void write(
|
||||
boost::beast::websocket::stream<boost::asio::ip::tcp::socket> &ws,
|
||||
const std::map<boost::beast::http::field, std::string> &fields);
|
||||
|
||||
static void write(
|
||||
boost::beast::websocket::stream<
|
||||
boost::beast::ssl_stream<boost::asio::ip::tcp::socket>> &ws,
|
||||
const std::map<boost::beast::http::field, std::string> &fields);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline void HttpFieldWriter::write(
|
||||
boost::beast::websocket::stream<boost::asio::ip::tcp::socket> &ws,
|
||||
const std::map<boost::beast::http::field, std::string> &fields) {
|
||||
//
|
||||
ws.set_option(boost::beast::websocket::stream_base::decorator(
|
||||
[&fields](boost::beast::websocket::request_type &req) {
|
||||
for (const auto &[f, v] : fields) {
|
||||
req.set(f, v);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
inline void HttpFieldWriter::write(
|
||||
boost::beast::websocket::stream<
|
||||
boost::beast::ssl_stream<boost::asio::ip::tcp::socket>> &ws,
|
||||
const std::map<boost::beast::http::field, std::string> &fields) {
|
||||
//
|
||||
ws.set_option(boost::beast::websocket::stream_base::decorator(
|
||||
[&fields](boost::beast::websocket::request_type &req) {
|
||||
for (const auto &[f, v] : fields) {
|
||||
req.set(f, v);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <boost/asio/ip/basic_resolver.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "bts-websocket-listener/http_field_formatter.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class IpEndpointFormatter {
|
||||
public:
|
||||
static std::string format(boost::asio::ip::basic_resolver_entry<
|
||||
boost::asio::ip::tcp>::endpoint_type &endpoint);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::string format(
|
||||
boost::asio::ip::basic_resolver_entry<boost::asio::ip::tcp>::endpoint_type
|
||||
&endpoint) {
|
||||
return fmt::format(
|
||||
"ep.addr: {}; ep.protocol: {}; "
|
||||
"ep.port: {}; ep.data: {}",
|
||||
endpoint.address().to_string(), endpoint.protocol().type(),
|
||||
endpoint.port(), std::string{endpoint.data()->sa_data});
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <boost/asio/ip/basic_resolver.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "bts-websocket-listener/http_field_formatter.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class IpResolverResultsResponseFormatter {
|
||||
public:
|
||||
static std::string format(
|
||||
const boost::asio::ip::basic_resolver<boost::asio::ip::tcp>::results_type
|
||||
&results);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::string IpResolverResultsResponseFormatter::format(
|
||||
const boost::asio::ip::basic_resolver<boost::asio::ip::tcp>::results_type
|
||||
&results) {
|
||||
std::string rv;
|
||||
int rcount = 0;
|
||||
for (auto r : results) {
|
||||
if (!rv.empty()) {
|
||||
rv.append("\n");
|
||||
}
|
||||
++rcount;
|
||||
rv.append(fmt::format(
|
||||
"{}. host: {}; ep.addr: {}; ep.protocol: {}; "
|
||||
"ep.port: {}; service_name: {}",
|
||||
rcount, r.host_name(), r.endpoint().address().to_string(),
|
||||
r.endpoint().protocol().type(), static_cast<int>(r.endpoint().port()),
|
||||
r.service_name()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
#include <curlpp/cURLpp.hpp>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include <CLI11/CLI11.hpp>
|
||||
#include "bts-json/JsonDumper.h"
|
||||
#include "bts-json/JsonReader.h"
|
||||
#include "bts-system/UNUSED.h"
|
||||
#include "bts-websocket-listener/boost_example2.h"
|
||||
#include "bts-websocket-listener/boost_example3.h"
|
||||
#include "bts-websocket-listener/chat_gpt_example.h"
|
||||
#include "bts-websocket-listener/cme_websocket_config.h"
|
||||
#include "bts-websocket-listener/cme_websocket_session.h"
|
||||
|
||||
using bts::io::JsonReader;
|
||||
using bts::websocket::CmeWebsocketConfig;
|
||||
using bts::websocket::CmeWebsocketSession;
|
||||
using CLI::App;
|
||||
using std::ifstream;
|
||||
using std::string;
|
||||
|
||||
static CmeWebsocketConfig parse_config(const string &config_arg) {
|
||||
ifstream cfg_ifs{config_arg};
|
||||
const auto cfg_j = nlohmann::json::parse(cfg_ifs);
|
||||
JsonReader cfg_jr{&cfg_j};
|
||||
CmeWebsocketConfig cfg;
|
||||
bts_assert(cfg_jr.read(cfg));
|
||||
return cfg;
|
||||
}
|
||||
|
||||
static void run(const std::string &config_arg) {
|
||||
const auto cfg = parse_config(config_arg);
|
||||
curlpp::initialize();
|
||||
CmeWebsocketSession::run(cfg);
|
||||
curlpp::terminate();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
App app{"Cme websocket listener", "bts-websocket-listener"};
|
||||
string config_arg;
|
||||
app.add_option(
|
||||
"--config", config_arg, "Path to CmeWebsocketConfig json file")
|
||||
->required();
|
||||
app.callback([&config_arg]() { run(config_arg); });
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-system/TimeStamp.h"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class MessageHeader {
|
||||
public:
|
||||
const std::string messageType;
|
||||
const std::optional<core::TimeStamp> sentTime;
|
||||
const std::int64_t version;
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class PrivateOrderQty {
|
||||
public:
|
||||
std::int64_t orderId;
|
||||
double lastOrderQty;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(PrivateOrderQty, orderId, lastOrderQty)
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class TradeSummary {
|
||||
public:
|
||||
std::int64_t aggressorSide;
|
||||
std::int64_t mdTradeEntryId;
|
||||
double tradePrice;
|
||||
double tradeQty;
|
||||
std::uint32_t tradeOrderCount;
|
||||
std::string tradeUpdateAction;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummary, aggressorSide, mdTradeEntryId, tradePrice, tradeQty, tradeOrderCount,
|
||||
tradeUpdateAction)
|
||||
|
||||
} // namespace bts::websocket
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-websocket-listener/messages/message_header.h"
|
||||
#include "bts-websocket-listener/messages/trade_summary_payload.h"
|
||||
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class TradeSummaryMessage {
|
||||
public:
|
||||
const MessageHeader header;
|
||||
const TradeSummaryPayload payload;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryMessage, header, payload)
|
||||
|
||||
} // namespace bts::websocket
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-websocket-listener/messages/trade_summary.h"
|
||||
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class TradeSummaryPayload {
|
||||
public:
|
||||
const std::int64_t lastUpdateTime;
|
||||
const TradeSummary tradeSummary;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryPayload, lastUpdateTime, tradeSummary)
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,51 @@
|
||||
# Copyright 2022 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# [START apikeys_authenticate_api_key]
|
||||
|
||||
from google.cloud import language_v1
|
||||
|
||||
|
||||
def authenticate_with_api_key(quota_project_id: str, api_key_string: str) -> None:
|
||||
"""
|
||||
Authenticates with an API key for Google Language service.
|
||||
|
||||
TODO(Developer): Replace these variables before running the sample.
|
||||
|
||||
Args:
|
||||
quota_project_id: Google Cloud project id that should be used for quota and billing purposes.
|
||||
api_key_string: The API key to authenticate to the service.
|
||||
"""
|
||||
|
||||
# Initialize the Language Service client and set the API key and the quota project id.
|
||||
client = language_v1.LanguageServiceClient(
|
||||
client_options={"api_key": api_key_string, "quota_project_id": quota_project_id}
|
||||
)
|
||||
|
||||
text = "Hello, world!"
|
||||
document = language_v1.Document(
|
||||
content=text, type_=language_v1.Document.Type.PLAIN_TEXT
|
||||
)
|
||||
|
||||
# Make a request to analyze the sentiment of the text.
|
||||
sentiment = client.analyze_sentiment(
|
||||
request={"document": document}
|
||||
).document_sentiment
|
||||
|
||||
print(f"Text: {text}")
|
||||
print(f"Sentiment: {sentiment.score}, {sentiment.magnitude}")
|
||||
print("Successfully authenticated using the API key")
|
||||
|
||||
|
||||
# [END apikeys_authenticate_api_key]
|
||||
@@ -0,0 +1,39 @@
|
||||
import requests
|
||||
|
||||
def _run():
|
||||
# Define the API endpoint and the OAuth2 token (replace with your actual values)
|
||||
api_url = "https://api.example.com/endpoint"
|
||||
oauth2_token = "your_oauth2_token_here"
|
||||
|
||||
# Define the headers with the OAuth2 token
|
||||
headers = {
|
||||
"Authorization": f"Bearer {oauth2_token}",
|
||||
"Content-Type": "application/json", # Adjust content type as needed
|
||||
}
|
||||
|
||||
# Define the data you want to send in the POST request (if any)
|
||||
payload = {
|
||||
"key1": "value1",
|
||||
"key2": "value2",
|
||||
}
|
||||
|
||||
# Send the POST request
|
||||
response = requests.post(api_url, headers=headers, json=payload)
|
||||
|
||||
# Check the response
|
||||
if response.status_code == 200:
|
||||
print("POST request was successful!")
|
||||
print("Response:", response.json())
|
||||
else:
|
||||
print(f"POST request failed with status code {response.status_code}")
|
||||
print("Error message:", response.text)
|
||||
|
||||
|
||||
def _run_safe():
|
||||
try:
|
||||
_run()
|
||||
except Exception as e:
|
||||
print(f'oath-post.py failed with error: {e}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
_run_safe()
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import asyncio
|
||||
from websockets.sync.client import connect
|
||||
|
||||
def _connect(host, protocol = 'wss', port=443):
|
||||
url=f'{protocol}://{host}:{port}'
|
||||
try:
|
||||
with connect(url) as websocket:
|
||||
websocket.send("Hello world!")
|
||||
message = websocket.recv()
|
||||
print(f"Received: {message}")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f'Error connecting to websocket server {url}: {e}')
|
||||
return False
|
||||
|
||||
|
||||
def _connect_all(hosts, protocol, port):
|
||||
for protocol in protocols:
|
||||
for port in ports:
|
||||
for host in hosts:
|
||||
_connect(host=host, protocol=protocol, port=port)
|
||||
|
||||
if __name__ == '__main__':
|
||||
hosts = [
|
||||
'172.23.144.1',
|
||||
'192.168.65.7',
|
||||
'192.168.50.94',
|
||||
]
|
||||
protocols = ['ws']
|
||||
ports = [5000]
|
||||
_connect_all(hosts, protocols, ports)
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"header": {
|
||||
"messageType": "TS",
|
||||
"sentTime": "null",
|
||||
"version": "1.0"
|
||||
},
|
||||
"payload": [
|
||||
{
|
||||
"lastUpdateTime": "2023-01-23T10:00:12.509489271Z",
|
||||
"tradeSummary": {
|
||||
"aggressorSide": "2",
|
||||
"mdTradeEntryId": "172277",
|
||||
"tradePrice": "22855",
|
||||
"tradeQty": "1",
|
||||
"tradeOrderCount": "2",
|
||||
"tradeUpdateAction": "NEW",
|
||||
"orderQty": {
|
||||
"orderId": "2",
|
||||
"lastOrdQty": "1"
|
||||
}
|
||||
},
|
||||
"instrument": {
|
||||
"definitionSource": "E",
|
||||
"exchangeMic": "XCME",
|
||||
"id": "64598",
|
||||
"marketSegmentId": "74",
|
||||
"periodCode": "202301",
|
||||
"productCode": "BTC",
|
||||
"productType": "FUT",
|
||||
"productGroup": "BF",
|
||||
"symbol": "BTCF3"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "bts-websocket-listener/http_field_formatter.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
class WebsocketResponseFormatter {
|
||||
public:
|
||||
static std::string format(boost::beast::websocket::response_type &res);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::string WebsocketResponseFormatter::format(
|
||||
boost::beast::websocket::response_type &res) {
|
||||
using string = std::string;
|
||||
|
||||
auto &hdr = res.base();
|
||||
std::map<string, string> fields;
|
||||
for (auto &h : hdr) {
|
||||
fields[h.name_string().to_string()] = h.value().to_string();
|
||||
}
|
||||
string hdr_str = HttpFieldFormatter::format(fields);
|
||||
string reason = hdr.reason().to_string();
|
||||
boost::beast::http::status status = res.base().result();
|
||||
string code = std::to_string(static_cast<int>(status));
|
||||
string body = res.body();
|
||||
string keep_alive = res.keep_alive() ? "yes" : "no";
|
||||
return fmt::format(
|
||||
"headers:\n{}\n*****\nreason: {};\ncode: {};\nbody: {};\nkeep_alive: "
|
||||
"{};",
|
||||
hdr_str, reason, code, body, keep_alive);
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"protocol": "wss",
|
||||
"host": "markets.api.uat.cmegroup.com",
|
||||
"port": 0,
|
||||
"endpoint": "marketdatastream/v1",
|
||||
"access_token": "",
|
||||
"oauth": {
|
||||
"protocol": "https",
|
||||
"host": "authnr.cmegroup.com",
|
||||
"port": 0,
|
||||
"endpoint": "as/token.oauth2",
|
||||
"client_id": "api_thinkalpha321",
|
||||
"client_secret": "4T39A4t_G#7erR$TYgrJ9hmT"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user