Skip to content

Commit 8a49651

Browse files
authored
Allow hostnames instead of IPs for liteservers (#1771)
1 parent 677961a commit 8a49651

File tree

13 files changed

+56
-26
lines changed

13 files changed

+56
-26
lines changed

adnl/adnl-ext-client.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ void AdnlExtClientImpl::alarm() {
3131
next_create_at_ = td::Timestamp::in(10.0);
3232
alarm_timestamp() = next_create_at_;
3333

34+
if (!dst_host_.empty()) {
35+
auto S = dst_addr_.init_host_port(dst_host_);
36+
if (S.is_error()) {
37+
LOG(INFO) << "failed to connect to " << dst_host_ << ": " << S;
38+
return;
39+
}
40+
LOG(DEBUG) << "resolved " << dst_host_ << " -> " << dst_addr_;
41+
}
3442
auto fd = td::SocketFd::open(dst_addr_);
3543
if (fd.is_error()) {
3644
LOG(INFO) << "failed to connect to " << dst_addr_ << ": " << fd.move_as_error();
@@ -166,6 +174,12 @@ td::actor::ActorOwn<AdnlExtClient> AdnlExtClient::create(AdnlNodeIdFull dst, td:
166174
return td::actor::create_actor<AdnlExtClientImpl>("extclient", std::move(dst), dst_addr, std::move(callback));
167175
}
168176

177+
td::actor::ActorOwn<AdnlExtClient> AdnlExtClient::create(AdnlNodeIdFull dst, std::string dst_host,
178+
std::unique_ptr<AdnlExtClient::Callback> callback) {
179+
return td::actor::create_actor<AdnlExtClientImpl>("extclient", std::move(dst), std::move(dst_host),
180+
std::move(callback));
181+
}
182+
169183
td::actor::ActorOwn<AdnlExtClient> AdnlExtClient::create(AdnlNodeIdFull dst, PrivateKey local_id,
170184
td::IPAddress dst_addr,
171185
std::unique_ptr<AdnlExtClient::Callback> callback) {

adnl/adnl-ext-client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class AdnlExtClient : public td::actor::Actor {
3939
td::Promise<td::BufferSlice> promise) = 0;
4040
static td::actor::ActorOwn<AdnlExtClient> create(AdnlNodeIdFull dst, td::IPAddress dst_addr,
4141
std::unique_ptr<AdnlExtClient::Callback> callback);
42+
static td::actor::ActorOwn<AdnlExtClient> create(AdnlNodeIdFull dst, std::string dst_host,
43+
std::unique_ptr<AdnlExtClient::Callback> callback);
4244
static td::actor::ActorOwn<AdnlExtClient> create(AdnlNodeIdFull dst, PrivateKey local_id, td::IPAddress dst_addr,
4345
std::unique_ptr<AdnlExtClient::Callback> callback);
4446
};

adnl/adnl-ext-client.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class AdnlExtClientImpl : public AdnlExtClient {
7171
AdnlExtClientImpl(AdnlNodeIdFull dst_id, td::IPAddress dst_addr, std::unique_ptr<Callback> callback)
7272
: dst_(std::move(dst_id)), dst_addr_(dst_addr), callback_(std::move(callback)) {
7373
}
74+
AdnlExtClientImpl(AdnlNodeIdFull dst_id, std::string dst_host, std::unique_ptr<Callback> callback)
75+
: dst_(std::move(dst_id)), dst_host_(std::move(dst_host)), callback_(std::move(callback)) {
76+
}
7477
AdnlExtClientImpl(AdnlNodeIdFull dst_id, PrivateKey local_id, td::IPAddress dst_addr,
7578
std::unique_ptr<Callback> callback)
7679
: dst_(std::move(dst_id)), local_id_(local_id), dst_addr_(dst_addr), callback_(std::move(callback)) {
@@ -133,6 +136,7 @@ class AdnlExtClientImpl : public AdnlExtClient {
133136
AdnlNodeIdFull dst_;
134137
PrivateKey local_id_;
135138
td::IPAddress dst_addr_;
139+
std::string dst_host_;
136140

137141
std::unique_ptr<Callback> callback_;
138142

blockchain-explorer/blockchain-explorer-query.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,10 +1429,10 @@ void HttpQueryStatus::finish_query() {
14291429
A << "<td>" << static_cast<td::int32>(x->ts_.at_unix()) << "</td>";
14301430
}
14311431
A << "</tr>\n";
1432-
for (td::uint32 i = 0; i < results_.ips.size(); i++) {
1432+
for (td::uint32 i = 0; i < results_.addrs.size(); i++) {
14331433
A << "<tr>";
1434-
if (results_.ips[i].is_valid()) {
1435-
A << "<td>" << results_.ips[i].get_ip_str() << ":" << results_.ips[i].get_port() << "</td>";
1434+
if (!results_.addrs[i].empty()) {
1435+
A << "<td>" << results_.addrs[i] << "</td>";
14361436
} else {
14371437
A << "<td>hidden</td>";
14381438
}

blockchain-explorer/blockchain-explorer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class CoreActor : public CoreActorInterface {
187187
std::mutex queue_mutex_;
188188
std::mutex res_mutex_;
189189
std::map<td::int32, std::shared_ptr<RemoteNodeStatus>> results_;
190-
std::vector<td::IPAddress> addrs_;
190+
std::vector<std::string> addrs_;
191191
static CoreActor* instance_;
192192
td::actor::ActorId<CoreActor> self_id_;
193193

@@ -220,7 +220,7 @@ class CoreActor : public CoreActorInterface {
220220
}
221221
void get_results(td::uint32 max, td::Promise<RemoteNodeStatusList> promise) override {
222222
RemoteNodeStatusList r;
223-
r.ips = hide_ips_ ? std::vector<td::IPAddress>{addrs_.size()} : addrs_;
223+
r.addrs = hide_ips_ ? std::vector<std::string>{addrs_.size()} : addrs_;
224224
auto it = results_.rbegin();
225225
while (it != results_.rend() && r.results.size() < max) {
226226
r.results.push_back(it->second);
@@ -445,14 +445,14 @@ class CoreActor : public CoreActorInterface {
445445
r_servers.ensure();
446446
servers = r_servers.move_as_ok();
447447
for (const auto& serv : servers) {
448-
addrs_.push_back(serv.addr);
448+
addrs_.push_back(serv.hostname);
449449
}
450450
} else {
451451
if (!remote_addr_.is_valid()) {
452452
LOG(FATAL) << "remote addr not set";
453453
}
454-
addrs_.push_back(remote_addr_);
455454
servers.push_back(liteclient::LiteServerConfig{ton::adnl::AdnlNodeIdFull{remote_public_key_}, remote_addr_});
455+
addrs_.push_back(servers.back().hostname);
456456
}
457457
n_servers_ = servers.size();
458458
client_ = liteclient::ExtClient::create(std::move(servers), make_callback(), true);

blockchain-explorer/blockchain-explorer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CoreActorInterface : public td::actor::Actor {
5959
};
6060

6161
struct RemoteNodeStatusList {
62-
std::vector<td::IPAddress> ips;
62+
std::vector<std::string> addrs;
6363
std::vector<std::shared_ptr<RemoteNodeStatus>> results;
6464
};
6565
virtual ~CoreActorInterface() = default;

lite-client/ext-client.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class ExtClientImpl : public ExtClient {
104104
promise.set_result(std::move(R));
105105
};
106106
LOG(DEBUG) << "Sending query " << query_info.to_str() << " to server #" << server.idx << " ("
107-
<< server.config.addr.get_ip_str() << ":" << server.config.addr.get_port() << ")";
107+
<< server.config.hostname << ")";
108108
send_closure(server.client, &ton::adnl::AdnlExtClient::send_query, std::move(name), std::move(data), timeout,
109109
std::move(P));
110110
}
@@ -173,9 +173,9 @@ class ExtClientImpl : public ExtClient {
173173
td::actor::ActorId<ExtClientImpl> parent_;
174174
size_t idx_;
175175
};
176-
LOG(INFO) << "Connecting to liteserver #" << server.idx << " (" << server.config.addr.get_ip_str() << ":"
177-
<< server.config.addr.get_port() << ") for query " << (query_info ? query_info->to_str() : "[none]");
178-
server.client = ton::adnl::AdnlExtClient::create(server.config.adnl_id, server.config.addr,
176+
LOG(INFO) << "Connecting to liteserver #" << server.idx << " (" << server.config.hostname << ") for query "
177+
<< (query_info ? query_info->to_str() : "[none]");
178+
server.client = ton::adnl::AdnlExtClient::create(server.config.adnl_id, server.config.hostname,
179179
std::make_unique<Callback>(actor_id(this), server_idx));
180180
}
181181

@@ -204,8 +204,7 @@ class ExtClientImpl : public ExtClient {
204204
continue;
205205
}
206206
if (server.timeout.is_in_past()) {
207-
LOG(INFO) << "Closing connection to liteserver #" << server.idx << " (" << server.config.addr.get_ip_str()
208-
<< ":" << server.config.addr.get_port() << ")";
207+
LOG(INFO) << "Closing connection to liteserver #" << server.idx << " (" << server.config.hostname << ")";
209208
server.client.reset();
210209
server.alive = false;
211210
server.ignore_until = {};

lite-client/lite-client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void TestNode::run() {
108108
if (single_liteserver_idx_ != -1) { // Use single liteserver from config
109109
CHECK(single_liteserver_idx_ >= 0 && (size_t)single_liteserver_idx_ < servers.size());
110110
td::TerminalIO::out() << "using liteserver #" << single_liteserver_idx_ << " with addr "
111-
<< servers[single_liteserver_idx_].addr << "\n";
111+
<< servers[single_liteserver_idx_].hostname << "\n";
112112
servers = {servers[single_liteserver_idx_]};
113113
}
114114
}

lite-client/query-utils.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,25 @@ bool LiteServerConfig::Slice::accepts_query(const QueryInfo& query_info) const {
318318
td::Result<std::vector<LiteServerConfig>> LiteServerConfig::parse_global_config(
319319
const ton_api::liteclient_config_global& config) {
320320
std::vector<LiteServerConfig> servers;
321+
auto get_hostname = [](const auto& f) -> std::string {
322+
if (f->hostname_.empty()) {
323+
return PSTRING() << td::IPAddress::ipv4_to_str(f->ip_) << ":" << f->port_;
324+
}
325+
if (f->port_ == 0) {
326+
return f->hostname_;
327+
}
328+
return PSTRING() << f->hostname_ << ":" << f->port_;
329+
};
321330
for (const auto& f : config.liteservers_) {
322331
LiteServerConfig server;
323-
TRY_STATUS(server.addr.init_host_port(td::IPAddress::ipv4_to_str(f->ip_), f->port_));
332+
server.hostname = get_hostname(f);
324333
server.adnl_id = adnl::AdnlNodeIdFull{PublicKey{f->id_}};
325334
server.is_full = true;
326335
servers.push_back(std::move(server));
327336
}
328337
for (const auto& f : config.liteservers_v2_) {
329338
LiteServerConfig server;
330-
TRY_STATUS(server.addr.init_host_port(td::IPAddress::ipv4_to_str(f->ip_), f->port_));
339+
server.hostname = get_hostname(f);
331340
server.adnl_id = adnl::AdnlNodeIdFull{PublicKey{f->id_}};
332341
server.is_full = false;
333342
for (const auto& slice_obj : f->slices_) {

lite-client/query-utils.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ struct LiteServerConfig {
7373

7474
public:
7575
ton::adnl::AdnlNodeIdFull adnl_id;
76-
td::IPAddress addr;
76+
std::string hostname;
7777

7878
LiteServerConfig() = default;
79-
LiteServerConfig(ton::adnl::AdnlNodeIdFull adnl_id, td::IPAddress addr)
80-
: is_full(true), adnl_id(adnl_id), addr(addr) {
79+
LiteServerConfig(ton::adnl::AdnlNodeIdFull adnl_id, std::string hostname)
80+
: is_full(true), adnl_id(adnl_id), hostname(std::move(hostname)) {
81+
}
82+
LiteServerConfig(ton::adnl::AdnlNodeIdFull adnl_id, td::IPAddress ip)
83+
: is_full(true), adnl_id(adnl_id), hostname(PSTRING() << ip.get_ip_str() << ":" << ip.get_port()) {
8184
}
8285

8386
bool accepts_query(const QueryInfo& query_info) const;

0 commit comments

Comments
 (0)