Add internalLinkTypeProxy.

This commit is contained in:
levlam 2021-05-29 03:51:31 +03:00
parent d17d40203c
commit 2f07ad223e
4 changed files with 147 additions and 4 deletions

View File

@ -3023,6 +3023,10 @@ internalLinkTypeMessageDraft text:formattedText contains_link:Bool = InternalLin
//@hash Hash value from the link @phone_number Phone number value from the link
internalLinkTypePhoneNumberConfirmation hash:string phone_number:string = InternalLinkType;
//@description The link is a link to a proxy. Call addProxy to process the link and add the proxy
//@server Proxy server IP address @port Proxy server port @type Type of the proxy
internalLinkTypeProxy server:string port:int32 type:ProxyType = InternalLinkType;
//@description The link can be used to login the current user on another device, but it must be scanned from QR-code using in-app camera. An alert similar to
//-"This code can be used to allow someone to log in to your Telegram account. To confirm Telegram login, please go to Settings > Devices > Scan QR and scan the code" needs to be shown
internalLinkTypeQrCodeAuthentication = InternalLinkType;

View File

@ -15,6 +15,8 @@
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/mtproto/ProxySecret.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/HttpUrl.h"
@ -127,6 +129,41 @@ class LinkManager::InternalLinkMessageDraft : public InternalLink {
}
};
class LinkManager::InternalLinkProxy : public InternalLink {
string server_;
int32 port_;
td_api::object_ptr<td_api::ProxyType> type_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
CHECK(type_ != nullptr);
auto proxy_type = [type = type_.get()]() -> td_api::object_ptr<td_api::ProxyType> {
switch (type->get_id()) {
case td_api::proxyTypeSocks5::ID: {
auto type_socks = static_cast<const td_api::proxyTypeSocks5 *>(type);
return td_api::make_object<td_api::proxyTypeSocks5>(type_socks->username_, type_socks->password_);
}
case td_api::proxyTypeMtproto::ID: {
auto type_mtproto = static_cast<const td_api::proxyTypeMtproto *>(type);
return td_api::make_object<td_api::proxyTypeMtproto>(type_mtproto->secret_);
}
default:
UNREACHABLE();
return nullptr;
}
}();
return td_api::make_object<td_api::internalLinkTypeProxy>(server_, port_, std::move(proxy_type));
}
InternalLinkType get_type() const final {
return InternalLinkType::Proxy;
}
public:
InternalLinkProxy(string server, int32 port, td_api::object_ptr<td_api::ProxyType> type)
: server_(std::move(server)), port_(port), type_(std::move(type)) {
}
};
class LinkManager::InternalLinkQrCodeAuthentication : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeQrCodeAuthentication>();
@ -538,6 +575,24 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice que
// confirmphone?phone=<phone>&hash=<hash>
return td::make_unique<InternalLinkConfirmPhone>(get_arg("hash"), get_arg("phone"));
}
} else if (path.size() == 1 && path[0] == "socks") {
if (has_arg("server") && has_arg("port")) {
// socks?server=<server>&port=<port>&user=<user>&pass=<pass>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536) {
return td::make_unique<InternalLinkProxy>(
get_arg("server"), port, td_api::make_object<td_api::proxyTypeSocks5>(get_arg("user"), get_arg("pass")));
}
}
} else if (path.size() == 1 && path[0] == "proxy") {
if (has_arg("server") && has_arg("port")) {
// proxy?server=<server>&port=<port>&secret=<secret>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536 && mtproto::ProxySecret::from_link(get_arg("secret")).is_ok()) {
return td::make_unique<InternalLinkProxy>(get_arg("server"), port,
td_api::make_object<td_api::proxyTypeMtproto>(get_arg("secret")));
}
}
} else if (path.size() == 1 && path[0] == "privatepost") {
// privatepost?channel=123456789&msg_id=12345
if (has_arg("channel") && has_arg("msg_id")) {
@ -606,6 +661,11 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
// /joinchat/<link>
return td::make_unique<InternalLinkDialogInvite>();
}
} else if (path[0][0] == ' ' || path[0][0] == '+') {
if (path[0].size() >= 2) {
// /+<link>
return td::make_unique<InternalLinkDialogInvite>();
}
} else if (path[0] == "addstickers") {
if (path.size() >= 2 && !path[1].empty()) {
// /addstickers/<name>
@ -626,10 +686,23 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
// /confirmphone?phone=<phone>&hash=<hash>
return td::make_unique<InternalLinkConfirmPhone>(get_arg("hash"), get_arg("phone"));
}
} else if (path[0][0] == ' ' || path[0][0] == '+') {
if (path[0].size() >= 2) {
// /+<link>
return td::make_unique<InternalLinkDialogInvite>();
} else if (path[0] == "socks") {
if (has_arg("server") && has_arg("port")) {
// /socks?server=<server>&port=<port>&user=<user>&pass=<pass>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536) {
return td::make_unique<InternalLinkProxy>(
get_arg("server"), port, td_api::make_object<td_api::proxyTypeSocks5>(get_arg("user"), get_arg("pass")));
}
}
} else if (path[0] == "proxy") {
if (has_arg("server") && has_arg("port")) {
// /proxy?server=<server>&port=<port>&secret=<secret>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536 && mtproto::ProxySecret::from_link(get_arg("secret")).is_ok()) {
return td::make_unique<InternalLinkProxy>(get_arg("server"), port,
td_api::make_object<td_api::proxyTypeMtproto>(get_arg("secret")));
}
}
} else if (path[0] == "bg") {
if (path.size() >= 2 && !path[1].empty()) {

View File

@ -42,6 +42,7 @@ class LinkManager : public Actor {
Language,
Message,
MessageDraft,
Proxy,
QrCodeAuthentication,
StickerSet,
Theme,
@ -93,6 +94,7 @@ class LinkManager : public Actor {
class InternalLinkLanguage;
class InternalLinkMessage;
class InternalLinkMessageDraft;
class InternalLinkProxy;
class InternalLinkQrCodeAuthentication;
class InternalLinkStickerSet;
class InternalLinkTheme;

View File

@ -86,6 +86,14 @@ TEST(Link, parse_internal_link) {
auto phone_number_confirmation = [](td::string hash, td::string phone_number) {
return td::td_api::make_object<td::td_api::internalLinkTypePhoneNumberConfirmation>(hash, phone_number);
};
auto proxy_mtproto = [](td::string server, td::int32 port, td::string secret) {
return td::td_api::make_object<td::td_api::internalLinkTypeProxy>(
server, port, td::td_api::make_object<td::td_api::proxyTypeMtproto>(secret));
};
auto proxy_socks = [](td::string server, td::int32 port, td::string username, td::string password) {
return td::td_api::make_object<td::td_api::internalLinkTypeProxy>(
server, port, td::td_api::make_object<td::td_api::proxyTypeSocks5>(username, password));
};
auto qr_code_authentication = []() {
return td::td_api::make_object<td::td_api::internalLinkTypeQrCodeAuthentication>();
};
@ -364,4 +372,60 @@ TEST(Link, parse_internal_link) {
parse_internal_link("tg:addtheme?slug=abcdef", theme("abcdef"));
parse_internal_link("tg:addtheme?slug=abc%30ef", theme("abc0ef"));
parse_internal_link("tg://addtheme?slug=", unknown_deep_link());
parse_internal_link("t.me/proxy?server=1.2.3.4&port=80&secret=1234567890abcdef1234567890ABCDEF",
proxy_mtproto("1.2.3.4", 80, "1234567890abcdef1234567890ABCDEF"));
parse_internal_link("t.me/proxy?server=1.2.3.4&port=80adasdas&secret=1234567890abcdef1234567890ABCDEF",
proxy_mtproto("1.2.3.4", 80, "1234567890abcdef1234567890ABCDEF"));
parse_internal_link("t.me/proxy?server=1.2.3.4&port=adasdas&secret=1234567890abcdef1234567890ABCDEF", nullptr);
parse_internal_link("t.me/proxy?server=1.2.3.4&port=65536&secret=1234567890abcdef1234567890ABCDEF", nullptr);
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=", nullptr);
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=12", nullptr);
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=1234567890abcdef1234567890ABCDEF",
proxy_mtproto("google.com", 80, "1234567890abcdef1234567890ABCDEF"));
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=dd1234567890abcdef1234567890ABCDEF",
proxy_mtproto("google.com", 80, "dd1234567890abcdef1234567890ABCDEF"));
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=de1234567890abcdef1234567890ABCDEF", nullptr);
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=ee1234567890abcdef1234567890ABCDEF", nullptr);
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=ee1234567890abcdef1234567890ABCDEF0", nullptr);
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=ee1234567890abcdef1234567890ABCDEF%30%30",
proxy_mtproto("google.com", 80, "ee1234567890abcdef1234567890ABCDEF00"));
parse_internal_link(
"t.me/proxy?server=google.com&port=8%30&secret=ee1234567890abcdef1234567890ABCDEF010101010101010101",
proxy_mtproto("google.com", 80, "ee1234567890abcdef1234567890ABCDEF010101010101010101"));
parse_internal_link("t.me/proxy?server=google.com&port=8%30&secret=7tAAAAAAAAAAAAAAAAAAAAAAAAcuZ29vZ2xlLmNvbQ",
proxy_mtproto("google.com", 80, "7tAAAAAAAAAAAAAAAAAAAAAAAAcuZ29vZ2xlLmNvbQ"));
parse_internal_link("tg:proxy?server=1.2.3.4&port=80&secret=1234567890abcdef1234567890ABCDEF",
proxy_mtproto("1.2.3.4", 80, "1234567890abcdef1234567890ABCDEF"));
parse_internal_link("tg:proxy?server=1.2.3.4&port=80adasdas&secret=1234567890abcdef1234567890ABCDEF",
proxy_mtproto("1.2.3.4", 80, "1234567890abcdef1234567890ABCDEF"));
parse_internal_link("tg:proxy?server=1.2.3.4&port=adasdas&secret=1234567890abcdef1234567890ABCDEF",
unknown_deep_link());
parse_internal_link("tg:proxy?server=1.2.3.4&port=65536&secret=1234567890abcdef1234567890ABCDEF",
unknown_deep_link());
parse_internal_link("tg:proxy?server=google.com&port=8%30&secret=1234567890abcdef1234567890ABCDEF",
proxy_mtproto("google.com", 80, "1234567890abcdef1234567890ABCDEF"));
parse_internal_link("tg:proxy?server=google.com&port=8%30&secret=dd1234567890abcdef1234567890ABCDEF",
proxy_mtproto("google.com", 80, "dd1234567890abcdef1234567890ABCDEF"));
parse_internal_link("tg:proxy?server=google.com&port=8%30&secret=de1234567890abcdef1234567890ABCDEF",
unknown_deep_link());
parse_internal_link("t.me/socks?server=1.2.3.4&port=80", proxy_socks("1.2.3.4", 80, "", ""));
parse_internal_link("t.me/socks?server=1.2.3.4&port=80adasdas", proxy_socks("1.2.3.4", 80, "", ""));
parse_internal_link("t.me/socks?server=1.2.3.4&port=adasdas", nullptr);
parse_internal_link("t.me/socks?server=1.2.3.4&port=65536", nullptr);
parse_internal_link("t.me/socks?server=google.com&port=8%30", proxy_socks("google.com", 80, "", ""));
parse_internal_link("t.me/socks?server=google.com&port=8%30&user=1&pass=", proxy_socks("google.com", 80, "1", ""));
parse_internal_link("t.me/socks?server=google.com&port=8%30&user=&pass=2", proxy_socks("google.com", 80, "", "2"));
parse_internal_link("t.me/socks?server=google.com&port=80&user=1&pass=2", proxy_socks("google.com", 80, "1", "2"));
parse_internal_link("tg:socks?server=1.2.3.4&port=80", proxy_socks("1.2.3.4", 80, "", ""));
parse_internal_link("tg:socks?server=1.2.3.4&port=80adasdas", proxy_socks("1.2.3.4", 80, "", ""));
parse_internal_link("tg:socks?server=1.2.3.4&port=adasdas", unknown_deep_link());
parse_internal_link("tg:socks?server=1.2.3.4&port=65536", unknown_deep_link());
parse_internal_link("tg:socks?server=google.com&port=8%30", proxy_socks("google.com", 80, "", ""));
parse_internal_link("tg:socks?server=google.com&port=8%30&user=1&pass=", proxy_socks("google.com", 80, "1", ""));
parse_internal_link("tg:socks?server=google.com&port=8%30&user=&pass=2", proxy_socks("google.com", 80, "", "2"));
parse_internal_link("tg:socks?server=google.com&port=80&user=1&pass=2", proxy_socks("google.com", 80, "1", "2"));
}