Skip to content

Commit

Permalink
check_proxy: support human-readable domain specification.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: a3511e63b8b68a65af2e6c0e5a68ab3b00910797
  • Loading branch information
levlam committed Feb 15, 2020
1 parent 1ead70a commit 494eddd
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion benchmark/check_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <cstdlib>
#include <iostream>
#include <utility>
Expand All @@ -34,11 +36,36 @@ int main(int argc, char **argv) {

td::vector<std::pair<td::string, td::td_api::object_ptr<td::td_api::testProxy>>> 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";
Expand All @@ -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";
Expand Down

0 comments on commit 494eddd

Please sign in to comment.