From 494eddda1c0d127208980c1fbb8d1782aa4b5650 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 16 Feb 2020 00:45:40 +0300 Subject: [PATCH] check_proxy: support human-readable domain specification. GitOrigin-RevId: a3511e63b8b68a65af2e6c0e5a68ab3b00910797 --- benchmark/check_proxy.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/benchmark/check_proxy.cpp b/benchmark/check_proxy.cpp index efe081ce..a493b1c4 100644 --- a/benchmark/check_proxy.cpp +++ b/benchmark/check_proxy.cpp @@ -7,11 +7,13 @@ #include "td/telegram/Client.h" #include "td/telegram/td_api.h" +#include "td/utils/base64.h" #include "td/utils/common.h" #include "td/utils/filesystem.h" #include "td/utils/logging.h" #include "td/utils/misc.h" +#include #include #include #include @@ -34,11 +36,36 @@ int main(int argc, char **argv) { td::vector>> requests; - auto add_proxy = [&requests](const td::string &arg) { + auto add_proxy = [&requests](td::string arg) { if (arg.empty()) { return; } + std::size_t offset = 0; + if (arg[0] == '[') { + auto end_ipv6_pos = arg.find(']'); + if (end_ipv6_pos == td::string::npos) { + td::TsCerr() << "Error: failed to find end of IPv6 address in \"" << arg << "\"\n"; + usage(); + } + offset = end_ipv6_pos; + } + if (std::count(arg.begin() + offset, arg.end(), ':') == 3) { + auto secret_domain_pos = arg.find(':', arg.find(':', offset) + 1) + 1; + auto domain_pos = arg.find(':', secret_domain_pos); + auto secret = arg.substr(secret_domain_pos, domain_pos - secret_domain_pos); + auto domain = arg.substr(domain_pos + 1); + auto r_decoded_secret = td::hex_decode(secret); + if (r_decoded_secret.is_error()) { + r_decoded_secret = td::base64url_decode(secret); + if (r_decoded_secret.is_error()) { + td::TsCerr() << "Error: failed to find proxy port and secret in \"" << arg << "\"\n"; + usage(); + } + } + arg = arg.substr(0, secret_domain_pos) + td::base64url_encode(r_decoded_secret.ok() + domain); + } + auto secret_pos = arg.rfind(':'); if (secret_pos == td::string::npos) { td::TsCerr() << "Error: failed to find proxy port and secret in \"" << arg << "\"\n"; @@ -57,6 +84,9 @@ int main(int argc, char **argv) { } auto port = r_port.move_as_ok(); auto server = arg.substr(0, port_pos); + if (server[0] == '[' && server.back() == ']') { + server = server.substr(1, server.size() - 2); + } if (server.empty() || port <= 0 || port > 65536 || secret.empty()) { td::TsCerr() << "Error: proxy address to check is in wrong format: \"" << arg << "\"\n";