2019-06-03 00:57:33 +00:00
|
|
|
#include "ConnectionParameters.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <Core/Defines.h>
|
|
|
|
#include <Core/Protocol.h>
|
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <IO/ConnectionTimeouts.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
#include <Common/Exception.h>
|
2021-03-29 00:43:14 +00:00
|
|
|
#include <Common/isLocalAddress.h>
|
|
|
|
#include <Common/DNSResolver.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/setTerminalEcho.h>
|
|
|
|
#include <base/scope_guard.h>
|
2020-08-10 21:36:49 +00:00
|
|
|
|
2021-10-27 23:10:39 +00:00
|
|
|
#include <readpassphrase/readpassphrase.h>
|
2019-06-03 00:57:33 +00:00
|
|
|
|
2021-07-11 20:35:29 +00:00
|
|
|
|
2019-06-03 00:57:33 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2022-01-17 22:57:05 +00:00
|
|
|
ConnectionParameters::ConnectionParameters(const Poco::Util::AbstractConfiguration & config,
|
|
|
|
std::string connection_host,
|
|
|
|
int connection_port) : host(connection_host), port(connection_port)
|
2019-06-03 00:57:33 +00:00
|
|
|
{
|
|
|
|
bool is_secure = config.getBool("secure", false);
|
|
|
|
security = is_secure ? Protocol::Secure::Enable : Protocol::Secure::Disable;
|
|
|
|
|
|
|
|
default_database = config.getString("database", "");
|
2020-04-15 01:12:32 +00:00
|
|
|
|
2019-06-03 00:57:33 +00:00
|
|
|
/// changed the default value to "default" to fix the issue when the user in the prompt is blank
|
|
|
|
user = config.getString("user", "default");
|
2020-04-15 01:12:32 +00:00
|
|
|
|
2019-06-03 00:57:33 +00:00
|
|
|
bool password_prompt = false;
|
|
|
|
if (config.getBool("ask-password", false))
|
|
|
|
{
|
|
|
|
if (config.has("password"))
|
|
|
|
throw Exception("Specified both --password and --ask-password. Remove one of them", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
password_prompt = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
password = config.getString("password", "");
|
|
|
|
/// if the value of --password is omitted, the password will be set implicitly to "\n"
|
|
|
|
if (password == "\n")
|
|
|
|
password_prompt = true;
|
|
|
|
}
|
|
|
|
if (password_prompt)
|
|
|
|
{
|
|
|
|
std::string prompt{"Password for user (" + user + "): "};
|
|
|
|
char buf[1000] = {};
|
2020-05-18 08:08:55 +00:00
|
|
|
if (auto * result = readpassphrase(prompt.c_str(), buf, sizeof(buf), 0))
|
2019-06-03 00:57:33 +00:00
|
|
|
password = result;
|
|
|
|
}
|
2020-04-15 01:12:32 +00:00
|
|
|
|
2021-03-29 00:43:14 +00:00
|
|
|
/// By default compression is disabled if address looks like localhost.
|
|
|
|
compression = config.getBool("compression", !isLocalAddress(DNSResolver::instance().resolveHost(host)))
|
2022-01-17 22:57:05 +00:00
|
|
|
? Protocol::Compression::Enable : Protocol::Compression::Disable;
|
2019-06-03 00:57:33 +00:00
|
|
|
|
|
|
|
timeouts = ConnectionTimeouts(
|
2022-01-17 22:57:05 +00:00
|
|
|
Poco::Timespan(config.getInt("connect_timeout", DBMS_DEFAULT_CONNECT_TIMEOUT_SEC), 0),
|
|
|
|
Poco::Timespan(config.getInt("send_timeout", DBMS_DEFAULT_SEND_TIMEOUT_SEC), 0),
|
|
|
|
Poco::Timespan(config.getInt("receive_timeout", DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC), 0),
|
|
|
|
Poco::Timespan(config.getInt("tcp_keep_alive_timeout", 0), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionParameters::ConnectionParameters(const Poco::Util::AbstractConfiguration & config)
|
2022-01-27 10:44:39 +00:00
|
|
|
: ConnectionParameters(config, config.getString("host", "localhost"), getPortFromConfig(config))
|
2022-01-27 06:19:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConnectionParameters::getPortFromConfig(const Poco::Util::AbstractConfiguration & config)
|
2022-01-17 22:57:05 +00:00
|
|
|
{
|
|
|
|
bool is_secure = config.getBool("secure", false);
|
2022-01-27 06:19:02 +00:00
|
|
|
return config.getInt("port",
|
|
|
|
config.getInt(is_secure ? "tcp_port_secure" : "tcp_port",
|
|
|
|
is_secure ? DBMS_DEFAULT_SECURE_PORT : DBMS_DEFAULT_PORT));
|
2019-06-03 00:57:33 +00:00
|
|
|
}
|
|
|
|
}
|