2015-01-05 19:30:17 +00:00
|
|
|
/// @file
|
|
|
|
/// @author rfree (current maintainer in monero.cc project)
|
|
|
|
/// @brief base for connection, contains e.g. the ratelimit hooks
|
|
|
|
|
2019-03-05 21:05:34 +00:00
|
|
|
// Copyright (c) 2014-2019, The Monero Project
|
2015-01-05 19:30:17 +00:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
/* rfree: implementation for the non-template base, can be used by connection<> template class in abstract_tcp_server2 file */
|
|
|
|
|
2017-11-29 19:30:06 +00:00
|
|
|
#include "net/connection_basic.hpp"
|
2015-01-05 19:30:17 +00:00
|
|
|
|
2017-07-31 15:36:52 +00:00
|
|
|
#include "net/net_utils_base.h"
|
|
|
|
#include "misc_log_ex.h"
|
2015-01-05 19:30:17 +00:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
#include "misc_language.h"
|
|
|
|
#include "pragma_comp_defs.h"
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
#include <boost/asio/basic_socket.hpp>
|
|
|
|
|
|
|
|
// TODO:
|
2017-11-29 19:30:06 +00:00
|
|
|
#include "net/network_throttle-detail.hpp"
|
2015-01-05 19:30:17 +00:00
|
|
|
|
2019-03-21 11:01:15 +00:00
|
|
|
#if BOOST_VERSION >= 107000
|
|
|
|
#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
|
|
|
|
#else
|
|
|
|
#define GET_IO_SERVICE(s) ((s).get_io_service())
|
|
|
|
#endif
|
|
|
|
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 16:34:23 +00:00
|
|
|
#undef MONERO_DEFAULT_LOG_CATEGORY
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
#define MONERO_DEFAULT_LOG_CATEGORY "net.conn"
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 16:34:23 +00:00
|
|
|
|
2015-01-05 19:30:17 +00:00
|
|
|
// ################################################################################################
|
|
|
|
// local (TU local) headers
|
|
|
|
// ################################################################################################
|
|
|
|
|
|
|
|
namespace epee
|
|
|
|
{
|
|
|
|
namespace net_utils
|
|
|
|
{
|
|
|
|
|
2019-03-15 04:03:32 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
boost::asio::ssl::context& get_context(connection_basic_shared_state* state)
|
|
|
|
{
|
|
|
|
CHECK_AND_ASSERT_THROW_MES(state != nullptr, "state shared_ptr cannot be null");
|
|
|
|
return state->ssl_context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 17:54:07 +00:00
|
|
|
std::string to_string(t_connection_type type)
|
|
|
|
{
|
|
|
|
if (type == e_connection_type_NET)
|
|
|
|
return std::string("NET");
|
|
|
|
else if (type == e_connection_type_RPC)
|
|
|
|
return std::string("RPC");
|
|
|
|
else if (type == e_connection_type_P2P)
|
|
|
|
return std::string("P2P");
|
|
|
|
|
|
|
|
return std::string("UNKNOWN");
|
|
|
|
}
|
|
|
|
|
2015-01-05 19:30:17 +00:00
|
|
|
|
|
|
|
/* ============================================================================ */
|
|
|
|
|
|
|
|
class connection_basic_pimpl {
|
|
|
|
public:
|
|
|
|
connection_basic_pimpl(const std::string &name);
|
|
|
|
|
|
|
|
static int m_default_tos;
|
|
|
|
|
|
|
|
network_throttle_bw m_throttle; // per-perr
|
|
|
|
critical_section m_throttle_lock;
|
|
|
|
|
|
|
|
int m_peer_number; // e.g. for debug/stats
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// ################################################################################################
|
|
|
|
// The implementation part
|
|
|
|
// ################################################################################################
|
|
|
|
|
|
|
|
namespace epee
|
|
|
|
{
|
|
|
|
namespace net_utils
|
|
|
|
{
|
|
|
|
|
|
|
|
// ================================================================================================
|
|
|
|
// connection_basic_pimpl
|
|
|
|
// ================================================================================================
|
|
|
|
|
2018-12-16 17:57:44 +00:00
|
|
|
connection_basic_pimpl::connection_basic_pimpl(const std::string &name) : m_throttle(name), m_peer_number(0) { }
|
2015-01-05 19:30:17 +00:00
|
|
|
|
|
|
|
// ================================================================================================
|
|
|
|
// connection_basic
|
|
|
|
// ================================================================================================
|
|
|
|
|
|
|
|
// static variables:
|
|
|
|
int connection_basic_pimpl::m_default_tos;
|
|
|
|
|
|
|
|
// methods:
|
2019-05-16 20:34:22 +00:00
|
|
|
connection_basic::connection_basic(boost::asio::ip::tcp::socket&& sock, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
|
2018-12-16 17:57:44 +00:00
|
|
|
:
|
2019-03-15 04:03:32 +00:00
|
|
|
m_state(std::move(state)),
|
2015-01-05 19:30:17 +00:00
|
|
|
mI( new connection_basic_pimpl("peer") ),
|
2019-03-21 11:01:15 +00:00
|
|
|
strand_(GET_IO_SERVICE(sock)),
|
2019-03-15 04:03:32 +00:00
|
|
|
socket_(GET_IO_SERVICE(sock), get_context(m_state.get())),
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
m_want_close_connection(false),
|
|
|
|
m_was_shutdown(false),
|
2019-09-06 22:48:16 +00:00
|
|
|
m_is_multithreaded(false),
|
2019-03-15 04:03:32 +00:00
|
|
|
m_ssl_support(ssl_support)
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
{
|
|
|
|
// add nullptr checks if removed
|
2019-03-15 04:03:32 +00:00
|
|
|
assert(m_state != nullptr); // release runtime check in get_context
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
|
|
|
|
socket_.next_layer() = std::move(sock);
|
|
|
|
|
2019-03-15 04:03:32 +00:00
|
|
|
++(m_state->sock_count); // increase the global counter
|
|
|
|
mI->m_peer_number = m_state->sock_number.fetch_add(1); // use, and increase the generated number
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
|
|
|
|
std::string remote_addr_str = "?";
|
|
|
|
try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ;
|
|
|
|
|
2019-03-15 04:03:32 +00:00
|
|
|
_note("Spawned connection #"<<mI->m_peer_number<<" to " << remote_addr_str << " currently we have sockets count:" << m_state->sock_count);
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 20:34:22 +00:00
|
|
|
connection_basic::connection_basic(boost::asio::io_service &io_service, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
:
|
2019-03-15 04:03:32 +00:00
|
|
|
m_state(std::move(state)),
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
mI( new connection_basic_pimpl("peer") ),
|
|
|
|
strand_(io_service),
|
2019-03-15 04:03:32 +00:00
|
|
|
socket_(io_service, get_context(m_state.get())),
|
|
|
|
m_want_close_connection(false),
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
m_was_shutdown(false),
|
2019-03-15 04:03:32 +00:00
|
|
|
m_ssl_support(ssl_support)
|
2018-12-16 17:57:44 +00:00
|
|
|
{
|
|
|
|
// add nullptr checks if removed
|
2019-03-15 04:03:32 +00:00
|
|
|
assert(m_state != nullptr); // release runtime check in get_context
|
2018-12-16 17:57:44 +00:00
|
|
|
|
2019-03-15 04:03:32 +00:00
|
|
|
++(m_state->sock_count); // increase the global counter
|
|
|
|
mI->m_peer_number = m_state->sock_number.fetch_add(1); // use, and increase the generated number
|
2015-02-12 19:59:39 +00:00
|
|
|
|
2017-10-10 14:47:08 +00:00
|
|
|
std::string remote_addr_str = "?";
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ;
|
2015-02-12 19:59:39 +00:00
|
|
|
|
2019-03-15 04:03:32 +00:00
|
|
|
_note("Spawned connection #"<<mI->m_peer_number<<" to " << remote_addr_str << " currently we have sockets count:" << m_state->sock_count);
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 04:57:17 +00:00
|
|
|
connection_basic::~connection_basic() noexcept(false) {
|
2019-03-15 04:03:32 +00:00
|
|
|
--(m_state->sock_count);
|
2018-12-16 17:57:44 +00:00
|
|
|
|
2017-10-10 14:47:08 +00:00
|
|
|
std::string remote_addr_str = "?";
|
epee: add SSL support
RPC connections now have optional tranparent SSL.
An optional private key and certificate file can be passed,
using the --{rpc,daemon}-ssl-private-key and
--{rpc,daemon}-ssl-certificate options. Those have as
argument a path to a PEM format private private key and
certificate, respectively.
If not given, a temporary self signed certificate will be used.
SSL can be enabled or disabled using --{rpc}-ssl, which
accepts autodetect (default), disabled or enabled.
Access can be restricted to particular certificates using the
--rpc-ssl-allowed-certificates, which takes a list of
paths to PEM encoded certificates. This can allow a wallet to
connect to only the daemon they think they're connected to,
by forcing SSL and listing the paths to the known good
certificates.
To generate long term certificates:
openssl genrsa -out /tmp/KEY 4096
openssl req -new -key /tmp/KEY -out /tmp/REQ
openssl x509 -req -days 999999 -sha256 -in /tmp/REQ -signkey /tmp/KEY -out /tmp/CERT
/tmp/KEY is the private key, and /tmp/CERT is the certificate,
both in PEM format. /tmp/REQ can be removed. Adjust the last
command to set expiration date, etc, as needed. It doesn't
make a whole lot of sense for monero anyway, since most servers
will run with one time temporary self signed certificates anyway.
SSL support is transparent, so all communication is done on the
existing ports, with SSL autodetection. This means you can start
using an SSL daemon now, but you should not enforce SSL yet or
nothing will talk to you.
2018-06-14 22:44:48 +00:00
|
|
|
try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ;
|
|
|
|
_note("Destructing connection #"<<mI->m_peer_number << " to " << remote_addr_str);
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::set_rate_up_limit(uint64_t limit) {
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out );
|
|
|
|
network_throttle_manager::get_global_throttle_out().set_target_speed(limit);
|
|
|
|
}
|
2015-02-12 19:59:39 +00:00
|
|
|
save_limit_to_file(limit);
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::set_rate_down_limit(uint64_t limit) {
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_in );
|
|
|
|
network_throttle_manager::get_global_throttle_in().set_target_speed(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_inreq );
|
|
|
|
network_throttle_manager::get_global_throttle_inreq().set_target_speed(limit);
|
|
|
|
}
|
|
|
|
save_limit_to_file(limit);
|
|
|
|
}
|
|
|
|
|
2015-05-17 12:13:02 +00:00
|
|
|
uint64_t connection_basic::get_rate_up_limit() {
|
|
|
|
uint64_t limit;
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out );
|
2015-05-17 12:16:26 +00:00
|
|
|
limit = network_throttle_manager::get_global_throttle_out().get_target_speed();
|
2015-05-17 12:13:02 +00:00
|
|
|
}
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t connection_basic::get_rate_down_limit() {
|
|
|
|
uint64_t limit;
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_in );
|
2015-05-17 12:16:26 +00:00
|
|
|
limit = network_throttle_manager::get_global_throttle_in().get_target_speed();
|
2015-05-17 12:13:02 +00:00
|
|
|
}
|
|
|
|
return limit;
|
|
|
|
}
|
2015-01-05 19:30:17 +00:00
|
|
|
|
|
|
|
void connection_basic::save_limit_to_file(int limit) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::set_tos_flag(int tos) {
|
|
|
|
connection_basic_pimpl::m_default_tos = tos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int connection_basic::get_tos_flag() {
|
|
|
|
return connection_basic_pimpl::m_default_tos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::sleep_before_packet(size_t packet_size, int phase, int q_len) {
|
|
|
|
double delay=0; // will be calculated
|
|
|
|
do
|
|
|
|
{ // rate limiting
|
|
|
|
if (m_was_shutdown) {
|
|
|
|
_dbg2("m_was_shutdown - so abort sleep");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-02-12 19:59:39 +00:00
|
|
|
CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out );
|
2017-11-26 14:26:17 +00:00
|
|
|
delay = network_throttle_manager::get_global_throttle_out().get_sleep_time_after_tick( packet_size );
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delay *= 0.50;
|
|
|
|
if (delay > 0) {
|
2015-02-12 19:59:39 +00:00
|
|
|
long int ms = (long int)(delay * 1000);
|
2017-08-10 13:30:42 +00:00
|
|
|
MTRACE("Sleeping in " << __FUNCTION__ << " for " << ms << " ms before packet_size="<<packet_size); // debug sleep
|
2015-02-12 19:59:39 +00:00
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) );
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
} while(delay > 0);
|
|
|
|
|
|
|
|
// XXX LATER XXX
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out );
|
2017-11-26 14:26:17 +00:00
|
|
|
network_throttle_manager::get_global_throttle_out().handle_trafic_exact( packet_size ); // increase counter - global
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::do_send_handler_write(const void* ptr , size_t cb ) {
|
2017-11-26 14:26:17 +00:00
|
|
|
// No sleeping here; sleeping is done once and for all in connection<t_protocol_handler>::handle_write
|
2017-08-10 13:30:42 +00:00
|
|
|
MTRACE("handler_write (direct) - before ASIO write, for packet="<<cb<<" B (after sleep)");
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::do_send_handler_write_from_queue( const boost::system::error_code& e, size_t cb, int q_len ) {
|
2017-11-26 14:26:17 +00:00
|
|
|
// No sleeping here; sleeping is done once and for all in connection<t_protocol_handler>::handle_write
|
2017-08-10 13:30:42 +00:00
|
|
|
MTRACE("handler_write (after write, from queue="<<q_len<<") - before ASIO write, for packet="<<cb<<" B (after sleep)");
|
2015-01-05 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::logger_handle_net_read(size_t size) { // network data read
|
|
|
|
}
|
|
|
|
|
|
|
|
void connection_basic::logger_handle_net_write(size_t size) {
|
|
|
|
}
|
|
|
|
|
|
|
|
double connection_basic::get_sleep_time(size_t cb) {
|
2015-02-24 19:12:56 +00:00
|
|
|
CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::network_throttle_manager::m_lock_get_global_throttle_out);
|
2015-01-05 19:30:17 +00:00
|
|
|
auto t = network_throttle_manager::get_global_throttle_out().get_sleep_time(cb);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|