Add LinkManager::get_proxy_link.

This commit is contained in:
levlam 2023-03-02 02:41:22 +03:00
parent 319a3f1c39
commit 235d68e587
3 changed files with 40 additions and 33 deletions

View File

@ -19,6 +19,7 @@
#include "td/telegram/MessageId.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/misc.h"
#include "td/telegram/net/Proxy.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
@ -1766,6 +1767,41 @@ string LinkManager::get_public_chat_link(Slice username) {
return PSTRING() << get_t_me_url() << url_encode(username);
}
Result<string> LinkManager::get_proxy_link(const Proxy &proxy, bool is_internal) {
string url = is_internal ? "tg://" : get_t_me_url();
bool is_socks = false;
switch (proxy.type()) {
case Proxy::Type::Socks5:
url += "socks";
is_socks = true;
break;
case Proxy::Type::HttpTcp:
case Proxy::Type::HttpCaching:
return Status::Error(400, "HTTP proxies have no public links");
case Proxy::Type::Mtproto:
url += "proxy";
break;
default:
UNREACHABLE();
}
url += "?server=";
url += url_encode(proxy.server());
url += "&port=";
url += to_string(proxy.port());
if (is_socks) {
if (!proxy.user().empty() || !proxy.password().empty()) {
url += "&user=";
url += url_encode(proxy.user());
url += "&pass=";
url += url_encode(proxy.password());
}
} else {
url += "&secret=";
url += proxy.secret().get_encoded_secret();
}
return std::move(url);
}
UserId LinkManager::get_link_user_id(Slice url) {
string lower_cased_url = to_lower(url);
url = lower_cased_url;

View File

@ -23,6 +23,7 @@
namespace td {
class Proxy;
class Td;
class LinkManager final : public Actor {
@ -92,6 +93,8 @@ class LinkManager final : public Actor {
static string get_public_chat_link(Slice username);
static Result<string> get_proxy_link(const Proxy &proxy, bool is_internal);
static UserId get_link_user_id(Slice url);
static string get_t_me_url();

View File

@ -233,39 +233,7 @@ void ConnectionCreator::get_proxy_link(int32 proxy_id, Promise<string> promise)
return promise.set_error(Status::Error(400, "Unknown proxy identifier"));
}
auto &proxy = it->second;
string url = LinkManager::get_t_me_url();
bool is_socks = false;
switch (proxy.type()) {
case Proxy::Type::Socks5:
url += "socks";
is_socks = true;
break;
case Proxy::Type::HttpTcp:
case Proxy::Type::HttpCaching:
return promise.set_error(Status::Error(400, "HTTP proxy can't have public link"));
case Proxy::Type::Mtproto:
url += "proxy";
break;
default:
UNREACHABLE();
}
url += "?server=";
url += url_encode(proxy.server());
url += "&port=";
url += to_string(proxy.port());
if (is_socks) {
if (!proxy.user().empty() || !proxy.password().empty()) {
url += "&user=";
url += url_encode(proxy.user());
url += "&pass=";
url += url_encode(proxy.password());
}
} else {
url += "&secret=";
url += proxy.secret().get_encoded_secret();
}
promise.set_value(std::move(url));
promise.set_result(LinkManager::get_proxy_link(it->second, false));
}
ActorId<GetHostByNameActor> ConnectionCreator::get_dns_resolver() {