From 8e99135f37e734fcb062127aba9a9d9e84efcade Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 27 Apr 2018 20:16:50 +0400 Subject: [PATCH] Support tg://proxy links. --- Telegram/SourceFiles/boxes/connection_box.cpp | 34 ++++++++++++++----- Telegram/SourceFiles/boxes/connection_box.h | 4 ++- .../SourceFiles/core/click_handler_types.cpp | 2 ++ Telegram/SourceFiles/core/utils.cpp | 4 +++ Telegram/SourceFiles/core/utils.h | 4 +++ Telegram/SourceFiles/messenger.cpp | 6 +++- Telegram/gyp/telegram_sources.txt | 1 + 7 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/boxes/connection_box.cpp b/Telegram/SourceFiles/boxes/connection_box.cpp index 870df61559..360e0b4551 100644 --- a/Telegram/SourceFiles/boxes/connection_box.cpp +++ b/Telegram/SourceFiles/boxes/connection_box.cpp @@ -23,18 +23,36 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "application.h" #include "styles/style_boxes.h" -void ConnectionBox::ShowApplyProxyConfirmation(const QMap &fields) { - auto server = fields.value(qsl("server")); - auto port = fields.value(qsl("port")).toUInt(); - if (!server.isEmpty() && port != 0) { +void ConnectionBox::ShowApplyProxyConfirmation( + ProxyData::Type type, + const QMap &fields) { + const auto server = fields.value(qsl("server")); + const auto port = fields.value(qsl("port")).toUInt(); + const auto secret = fields.value(qsl("secret")); + const auto valid = !server.isEmpty() + && (port != 0) + && (type != ProxyData::Type::Mtproto + || ProxyData::ValidSecret(secret)) + && (type == ProxyData::Type::Socks5 + || type == ProxyData::Type::Mtproto); + if (valid) { const auto box = std::make_shared>(); - *box = Ui::show(Box(lng_sure_enable_socks(lt_server, server, lt_port, QString::number(port)), lang(lng_sure_enable), [=] { + const auto text = lng_sure_enable_socks( + lt_server, + server, + lt_port, + QString::number(port)); + *box = Ui::show(Box(text, lang(lng_sure_enable), [=] { auto proxy = ProxyData(); - proxy.type = ProxyData::Type::Socks5; + proxy.type = type; proxy.host = server; - proxy.user = fields.value(qsl("user")); - proxy.password = fields.value(qsl("pass")); proxy.port = port; + if (type == ProxyData::Type::Socks5) { + proxy.user = fields.value(qsl("user")); + proxy.password = fields.value(qsl("pass")); + } else if (type == ProxyData::Type::Mtproto) { + proxy.password = secret; + } Global::SetConnectionType(dbictTcpProxy); Global::SetConnectionProxy(proxy); Local::writeSettings(); diff --git a/Telegram/SourceFiles/boxes/connection_box.h b/Telegram/SourceFiles/boxes/connection_box.h index c7d089f0cf..9c638d5e31 100644 --- a/Telegram/SourceFiles/boxes/connection_box.h +++ b/Telegram/SourceFiles/boxes/connection_box.h @@ -26,7 +26,9 @@ class ConnectionBox : public BoxContent { public: ConnectionBox(QWidget *parent); - static void ShowApplyProxyConfirmation(const QMap &fields); + static void ShowApplyProxyConfirmation( + ProxyData::Type type, + const QMap &fields); protected: void prepare() override; diff --git a/Telegram/SourceFiles/core/click_handler_types.cpp b/Telegram/SourceFiles/core/click_handler_types.cpp index de8d237f39..578b3ec2b2 100644 --- a/Telegram/SourceFiles/core/click_handler_types.cpp +++ b/Telegram/SourceFiles/core/click_handler_types.cpp @@ -50,6 +50,8 @@ QString tryConvertUrlToLocal(QString url) { return url; } else if (auto socksMatch = regex_match(qsl("socks/?\\?(.+)(#|$)"), query, matchOptions)) { return qsl("tg://socks?") + socksMatch->captured(1); + } else if (auto proxyMatch = regex_match(qsl("proxy/?\\?(.+)(#|$)"), query, matchOptions)) { + return qsl("tg://proxy?") + proxyMatch->captured(1); } else if (auto usernameMatch = regex_match(qsl("^([a-zA-Z0-9\\.\\_]+)(/?\\?|/?$|/(\\d+)/?(?:\\?|$))"), query, matchOptions)) { auto params = query.mid(usernameMatch->captured(0).size()).toString(); auto postParam = QString(); diff --git a/Telegram/SourceFiles/core/utils.cpp b/Telegram/SourceFiles/core/utils.cpp index 31f2b087e1..e40da4724c 100644 --- a/Telegram/SourceFiles/core/utils.cpp +++ b/Telegram/SourceFiles/core/utils.cpp @@ -236,6 +236,10 @@ namespace { _MsStarter _msStarter; } +bool ProxyData::ValidSecret(const QString &secret) { + return QRegularExpression("^[a-fA-F0-9]{32}$").match(secret).hasMatch(); +} + namespace ThirdParty { void start() { diff --git a/Telegram/SourceFiles/core/utils.h b/Telegram/SourceFiles/core/utils.h index 68235af7b8..0f25b24acb 100644 --- a/Telegram/SourceFiles/core/utils.h +++ b/Telegram/SourceFiles/core/utils.h @@ -434,10 +434,14 @@ struct ProxyData { Http, Mtproto, }; + Type type = Type::None; QString host; uint32 port = 0; QString user, password; + + static bool ValidSecret(const QString &secret); + }; enum DBIScale { diff --git a/Telegram/SourceFiles/messenger.cpp b/Telegram/SourceFiles/messenger.cpp index b1a56ac9b4..15c31a7b27 100644 --- a/Telegram/SourceFiles/messenger.cpp +++ b/Telegram/SourceFiles/messenger.cpp @@ -838,7 +838,11 @@ bool Messenger::openLocalUrl(const QString &url) { } } else if (auto socksMatch = regex_match(qsl("^socks/?\\?(.+)(#|$)"), command, matchOptions)) { auto params = url_parse_params(socksMatch->captured(1), UrlParamNameTransform::ToLower); - ConnectionBox::ShowApplyProxyConfirmation(params); + ConnectionBox::ShowApplyProxyConfirmation(ProxyData::Type::Socks5, params); + return true; + } else if (auto proxyMatch = regex_match(qsl("^proxy/?\\?(.+)(#|$)"), command, matchOptions)) { + auto params = url_parse_params(proxyMatch->captured(1), UrlParamNameTransform::ToLower); + ConnectionBox::ShowApplyProxyConfirmation(ProxyData::Type::Mtproto, params); return true; } return false; diff --git a/Telegram/gyp/telegram_sources.txt b/Telegram/gyp/telegram_sources.txt index 53aa5562f9..710ed8dacb 100644 --- a/Telegram/gyp/telegram_sources.txt +++ b/Telegram/gyp/telegram_sources.txt @@ -1,6 +1,7 @@ <(src_loc)/base/algorithm.h <(src_loc)/base/assertion.h <(src_loc)/base/build_config.h +<(src_loc)/base/bytes.h <(src_loc)/base/flags.h <(src_loc)/base/enum_mask.h <(src_loc)/base/flat_map.h