Merge commit '622ff58'

This commit is contained in:
Eric Frias 2017-06-20 17:27:58 -04:00
commit 2c8cdf84b7
7 changed files with 192 additions and 213 deletions

View file

@ -148,6 +148,17 @@ find_package(OpenSSL REQUIRED)
set( CMAKE_FIND_LIBRARY_SUFFIXES ${ORIGINAL_LIB_SUFFIXES} )
# We are now building in support for deflate compression into our websockets layer by default,
# which requires zlib. Aside from that, all of fc compiles without zlib, so this could be
# made optional without much effort
# (important exception, apple: as of 10.10 yosemite, the OpenSSL static libraries shipped with
# os x have a dependency on zlib)
# On a side note, fc's fc::zlib_compress() function uses a separate implementation of zlib
# from the miniz library. If we're comfortable requiring an external zlib, we can
# reimplement fc::zlib_compress() to call the real zlib, and remove miniz.c from our
# repository.
find_package( ZLIB REQUIRED )
option( UNITY_BUILD OFF )
set( fc_sources
@ -481,14 +492,6 @@ if(WIN32)
endif(WIN32)
IF(APPLE)
# As of 10.10 yosemite, the OpenSSL static libraries shipped with os x have a dependency
# on zlib, so any time you link in openssl you also need to link zlib. . We really want to detect whether openssl was configured with the --no-zlib
# option or not when it was built, but that's difficult to do in practice, so we
# just always try to link it in on mac.
find_package( ZLIB REQUIRED )
ENDIF(APPLE)
SET(OPENSSL_CONF_TARGET )
IF(DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
SET (OPENSSL_CONF_TARGET ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

View file

@ -123,7 +123,7 @@ namespace fc {
fc::sha512 get_shared_secret( const public_key& pub )const;
// signature sign( const fc::sha256& digest )const;
compact_signature sign_compact( const fc::sha256& digest )const;
compact_signature sign_compact( const fc::sha256& digest, bool require_canonical = true )const;
// bool verify( const fc::sha256& digest, const signature& sig );
public_key get_public_key()const;

View file

@ -8,8 +8,7 @@
namespace fc { namespace http {
namespace detail {
class websocket_server_impl;
class websocket_tls_server_impl;
class abstract_websocket_server;
class websocket_client_impl;
class websocket_tls_client_impl;
} // namespace detail;
@ -42,7 +41,7 @@ namespace fc { namespace http {
class websocket_server
{
public:
websocket_server();
websocket_server(bool enable_permessage_deflate = true);
~websocket_server();
void on_connection( const on_connection_handler& handler);
@ -51,8 +50,7 @@ namespace fc { namespace http {
void start_accept();
private:
friend class detail::websocket_server_impl;
std::unique_ptr<detail::websocket_server_impl> my;
std::unique_ptr<detail::abstract_websocket_server> my;
};
@ -60,7 +58,8 @@ namespace fc { namespace http {
{
public:
websocket_tls_server(const std::string& server_pem = std::string(),
const std::string& ssl_password = std::string());
const std::string& ssl_password = std::string(),
bool enable_permessage_deflate = false);
~websocket_tls_server();
void on_connection( const on_connection_handler& handler);
@ -69,8 +68,7 @@ namespace fc { namespace http {
void start_accept();
private:
friend class detail::websocket_tls_server_impl;
std::unique_ptr<detail::websocket_tls_server_impl> my;
std::unique_ptr<detail::abstract_websocket_server> my;
};
class websocket_client

View file

@ -21,6 +21,8 @@ namespace fc {
class optional
{
public:
typedef T value_type;
optional():_valid(false){}
~optional(){ reset(); }

View file

@ -85,7 +85,7 @@ namespace fc { namespace ecc {
return secp256k1_nonce_function_default( nonce32, msg32, key32, *extra, nullptr );
}
compact_signature private_key::sign_compact( const fc::sha256& digest )const
compact_signature private_key::sign_compact( const fc::sha256& digest, bool require_canonical )const
{
FC_ASSERT( my->_key != empty_priv );
compact_signature result;
@ -94,7 +94,7 @@ namespace fc { namespace ecc {
do
{
FC_ASSERT( secp256k1_ecdsa_sign_compact( detail::_get_context(), (unsigned char*) digest.data(), (unsigned char*) result.begin() + 1, (unsigned char*) my->_key.data(), extended_nonce_function, &counter, &recid ));
} while( !public_key::is_canonical( result ) );
} while( require_canonical && !public_key::is_canonical( result ) );
result.begin()[0] = 27 + 4 + recid;
return result;
}

View file

@ -3,6 +3,7 @@
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/logger/stub.hpp>
@ -19,12 +20,11 @@
namespace fc { namespace http {
namespace detail {
struct asio_with_stub_log : public websocketpp::config::asio {
typedef asio_with_stub_log type;
typedef asio base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
@ -34,14 +34,7 @@ namespace fc { namespace http {
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
/// Custom Logging policies
/*typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::elevel> elog_type;
typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::alevel> alog_type;
*/
//typedef base::alog_type alog_type;
//typedef base::elog_type elog_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
@ -60,13 +53,15 @@ namespace fc { namespace http {
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
// override default value of 5 sec timeout
static const long timeout_open_handshake = 0;
};
struct asio_tls_with_stub_log : public websocketpp::config::asio_tls {
typedef asio_with_stub_log type;
typedef asio_tls base;
struct asio_with_stub_log_and_deflate : public websocketpp::config::asio {
typedef asio_with_stub_log_and_deflate type;
typedef asio base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
@ -76,14 +71,7 @@ namespace fc { namespace http {
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
/// Custom Logging policies
/*typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::elevel> elog_type;
typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::alevel> alog_type;
*/
//typedef base::alog_type alog_type;
//typedef base::elog_type elog_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
@ -95,18 +83,27 @@ namespace fc { namespace http {
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::tls_socket::endpoint socket_type;
typedef websocketpp::transport::asio::basic_socket::endpoint
socket_type;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
/// enable the permessage_compress extension
struct permessage_deflate_config {};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
// override default value of 5 sec timeout
static const long timeout_open_handshake = 0;
};
struct asio_tls_stub_log : public websocketpp::config::asio_tls {
typedef asio_tls_stub_log type;
typedef asio_tls base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
@ -116,8 +113,7 @@ namespace fc { namespace http {
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
//typedef base::alog_type alog_type;
//typedef base::elog_type elog_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
@ -136,13 +132,45 @@ namespace fc { namespace http {
transport_type;
};
struct asio_tls_stub_log_and_deflate : public websocketpp::config::asio_tls {
typedef asio_tls_stub_log_and_deflate type;
typedef asio_tls base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
typedef base::response_type response_type;
typedef base::message_type message_type;
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
typedef base::rng_type rng_type;
struct transport_config : public base::transport_config {
typedef type::concurrency_type concurrency_type;
typedef type::alog_type alog_type;
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::tls_socket::endpoint socket_type;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
/// enable the permessage_compress extension
struct permessage_deflate_config {};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
};
using websocketpp::connection_hdl;
typedef websocketpp::server<asio_with_stub_log> websocket_server_type;
typedef websocketpp::server<asio_tls_stub_log> websocket_tls_server_type;
template<typename T>
class websocket_connection_impl : public websocket_connection
@ -173,7 +201,19 @@ namespace fc { namespace http {
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
class websocket_server_impl
class abstract_websocket_server
{
public:
virtual ~abstract_websocket_server() {}
virtual void on_connection( const on_connection_handler& handler) = 0;
virtual void listen( uint16_t port ) = 0;
virtual void listen( const fc::ip::endpoint& ep ) = 0;
virtual void start_accept() = 0;
};
template <typename config>
class websocket_server_impl : public abstract_websocket_server
{
public:
websocket_server_impl()
@ -185,15 +225,15 @@ namespace fc { namespace http {
_server.set_reuse_addr(true);
_server.set_open_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto new_con = std::make_shared<websocket_connection_impl<websocket_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
websocket_connection_ptr new_con = std::make_shared<websocket_connection_impl<typename websocketpp::server<config>::connection_ptr>>( _server.get_con_from_hdl(hdl) );
_on_connection( _connections[hdl] = new_con );
}).wait();
});
_server.set_message_handler( [&]( connection_hdl hdl, websocket_server_type::message_ptr msg ){
_server.set_message_handler( [&]( connection_hdl hdl, typename websocketpp::server<config>::message_ptr msg ){
_server_thread.async( [&](){
auto current_con = _connections.find(hdl);
assert( current_con != _connections.end() );
idump(("server")(msg->get_payload()));
//wdump(("server")(msg->get_payload()));
//std::cerr<<"recv: "<<msg->get_payload()<<"\n";
auto payload = msg->get_payload();
std::shared_ptr<websocket_connection> con = current_con->second;
@ -206,13 +246,13 @@ namespace fc { namespace http {
_server.set_http_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto current_con = std::make_shared<websocket_connection_impl<websocket_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
auto current_con = std::make_shared<websocket_connection_impl<typename websocketpp::server<config>::connection_ptr>>( _server.get_con_from_hdl(hdl) );
_on_connection( current_con );
auto con = _server.get_con_from_hdl(hdl);
con->defer_http_response();
std::string request_body = con->get_request_body();
wdump(("server")(request_body));
//wdump(("server")(request_body));
fc::async([current_con, request_body, con] {
std::string response = current_con->on_http(request_body);
@ -274,25 +314,43 @@ namespace fc { namespace http {
if( _closed ) _closed->wait();
}
void on_connection( const on_connection_handler& handler ) override
{
_on_connection = handler;
}
void listen( uint16_t port ) override
{
_server.listen(port);
}
void listen( const fc::ip::endpoint& ep ) override
{
_server.listen( boost::asio::ip::tcp::endpoint( boost::asio::ip::address_v4(uint32_t(ep.get_address())),ep.port()) );
}
void start_accept() override
{
_server.start_accept();
}
typedef std::map<connection_hdl, websocket_connection_ptr,std::owner_less<connection_hdl> > con_map;
con_map _connections;
fc::thread& _server_thread;
websocket_server_type _server;
websocketpp::server<config> _server;
on_connection_handler _on_connection;
fc::promise<void>::ptr _closed;
uint32_t _pending_messages = 0;
};
class websocket_tls_server_impl
template <typename config>
class websocket_tls_server_impl : public websocket_server_impl<config>
{
public:
websocket_tls_server_impl( const string& server_pem, const string& ssl_password )
:_server_thread( fc::thread::current() )
{
//if( server_pem.size() )
{
_server.set_tls_init_handler( [=]( websocketpp::connection_hdl hdl ) -> context_ptr {
this->_server.set_tls_init_handler( [=]( websocketpp::connection_hdl hdl ) -> context_ptr {
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv1);
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
@ -308,98 +366,10 @@ namespace fc { namespace http {
return ctx;
});
}
_server.clear_access_channels( websocketpp::log::alevel::all );
_server.init_asio(&fc::asio::default_io_service());
_server.set_reuse_addr(true);
_server.set_open_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto new_con = std::make_shared<websocket_connection_impl<websocket_tls_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
_on_connection( _connections[hdl] = new_con );
}).wait();
});
_server.set_message_handler( [&]( connection_hdl hdl, websocket_server_type::message_ptr msg ){
_server_thread.async( [&](){
auto current_con = _connections.find(hdl);
assert( current_con != _connections.end() );
auto received = msg->get_payload();
std::shared_ptr<websocket_connection> con = current_con->second;
fc::async([con,received](){ con->on_message( received ); });
}).wait();
});
_server.set_http_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto current_con = std::make_shared<websocket_connection_impl<websocket_tls_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
try{
_on_connection( current_con );
auto con = _server.get_con_from_hdl(hdl);
idump(("server")(con->get_request_body()));
auto response = current_con->on_http( con->get_request_body() );
con->set_body( response );
con->set_status( websocketpp::http::status_code::ok );
} catch ( const fc::exception& e )
{
edump((e.to_detail_string()));
}
current_con->closed();
}).wait();
});
_server.set_close_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
_connections[hdl]->closed();
_connections.erase( hdl );
}).wait();
});
_server.set_fail_handler( [&]( connection_hdl hdl ){
if( _server.is_listening() )
{
_server_thread.async( [&](){
if( _connections.find(hdl) != _connections.end() )
{
_connections[hdl]->closed();
_connections.erase( hdl );
}
}).wait();
}
});
}
~websocket_tls_server_impl()
{
if( _server.is_listening() )
_server.stop_listening();
auto cpy_con = _connections;
for( auto item : cpy_con )
_server.close( item.first, 0, "server exit" );
}
typedef std::map<connection_hdl, websocket_connection_ptr,std::owner_less<connection_hdl> > con_map;
con_map _connections;
fc::thread& _server_thread;
websocket_tls_server_type _server;
on_connection_handler _on_connection;
fc::promise<void>::ptr _closed;
};
typedef websocketpp::client<asio_with_stub_log> websocket_client_type;
typedef websocketpp::client<asio_tls_stub_log> websocket_tls_client_type;
@ -542,57 +512,63 @@ namespace fc { namespace http {
} // namespace detail
websocket_server::websocket_server():my( new detail::websocket_server_impl() ) {}
websocket_server::websocket_server(bool enable_permessage_deflate /* = true */) :
my( enable_permessage_deflate ?
(detail::abstract_websocket_server*)new detail::websocket_server_impl<detail::asio_with_stub_log_and_deflate> :
(detail::abstract_websocket_server*)new detail::websocket_server_impl<detail::asio_with_stub_log> )
{}
websocket_server::~websocket_server(){}
void websocket_server::on_connection( const on_connection_handler& handler )
{
my->_on_connection = handler;
my->on_connection(handler);
}
void websocket_server::listen( uint16_t port )
{
my->_server.listen(port);
my->listen(port);
}
void websocket_server::listen( const fc::ip::endpoint& ep )
{
my->_server.listen( boost::asio::ip::tcp::endpoint( boost::asio::ip::address_v4(uint32_t(ep.get_address())),ep.port()) );
my->listen(ep);
}
void websocket_server::start_accept() {
my->_server.start_accept();
my->start_accept();
}
websocket_tls_server::websocket_tls_server( const string& server_pem, const string& ssl_password ):my( new detail::websocket_tls_server_impl(server_pem, ssl_password) ) {}
websocket_tls_server::websocket_tls_server(const string& server_pem,
const string& ssl_password,
bool enable_permessage_deflate /* = true */) :
my( enable_permessage_deflate ?
(detail::abstract_websocket_server*)new detail::websocket_tls_server_impl<detail::asio_tls_stub_log_and_deflate>(server_pem, ssl_password) :
(detail::abstract_websocket_server*)new detail::websocket_tls_server_impl<detail::asio_tls_stub_log>(server_pem, ssl_password) )
{}
websocket_tls_server::~websocket_tls_server(){}
void websocket_tls_server::on_connection( const on_connection_handler& handler )
{
my->_on_connection = handler;
my->on_connection(handler);
}
void websocket_tls_server::listen( uint16_t port )
{
my->_server.listen(port);
my->listen(port);
}
void websocket_tls_server::listen( const fc::ip::endpoint& ep )
{
my->_server.listen( boost::asio::ip::tcp::endpoint( boost::asio::ip::address_v4(uint32_t(ep.get_address())),ep.port()) );
my->listen(ep);
}
void websocket_tls_server::start_accept() {
my->_server.start_accept();
void websocket_tls_server::start_accept()
{
my->start_accept();
}
websocket_tls_client::websocket_tls_client():my( new detail::websocket_tls_client_impl() ) {}
websocket_tls_client::~websocket_tls_client(){ }
websocket_client::websocket_client():my( new detail::websocket_client_impl() ),smy(new detail::websocket_tls_client_impl()) {}
websocket_client::~websocket_client(){ }

2
vendor/websocketpp vendored

@ -1 +1 @@
Subproject commit c5510d6de04917812b910a8dd44735c1f17061d9
Subproject commit 378437aecdcb1dfe62096ffd5d944bf1f640ccc3