2019-07-20 15:13:36 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-07-20 15:13:36 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/net/Proxy.h"
|
2019-07-22 04:04:18 +02:00
|
|
|
|
2019-07-20 15:13:36 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
2019-07-22 04:04:18 +02:00
|
|
|
|
2019-07-20 15:13:36 +02:00
|
|
|
namespace td {
|
2019-07-22 04:04:18 +02:00
|
|
|
|
2023-03-02 14:21:36 +01:00
|
|
|
Result<Proxy> Proxy::create_proxy(string server, int port, const td_api::ProxyType *proxy_type) {
|
2019-07-20 15:13:36 +02:00
|
|
|
if (proxy_type == nullptr) {
|
2020-03-02 10:52:13 +01:00
|
|
|
return Status::Error(400, "Proxy type must be non-empty");
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
|
|
|
if (server.empty()) {
|
2020-03-02 10:52:13 +01:00
|
|
|
return Status::Error(400, "Server name must be non-empty");
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
2020-01-24 13:57:28 +01:00
|
|
|
if (server.size() > 255) {
|
|
|
|
return Status::Error(400, "Server name is too long");
|
|
|
|
}
|
2019-07-20 15:13:36 +02:00
|
|
|
if (port <= 0 || port > 65535) {
|
|
|
|
return Status::Error(400, "Wrong port number");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (proxy_type->get_id()) {
|
|
|
|
case td_api::proxyTypeSocks5::ID: {
|
2023-03-02 14:21:36 +01:00
|
|
|
auto type = static_cast<const td_api::proxyTypeSocks5 *>(proxy_type);
|
|
|
|
return Proxy::socks5(std::move(server), port, type->username_, type->password_);
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
|
|
|
case td_api::proxyTypeHttp::ID: {
|
2023-03-02 14:21:36 +01:00
|
|
|
auto type = static_cast<const td_api::proxyTypeHttp *>(proxy_type);
|
2019-07-20 15:13:36 +02:00
|
|
|
if (type->http_only_) {
|
2023-03-02 14:21:36 +01:00
|
|
|
return Proxy::http_caching(std::move(server), port, type->username_, type->password_);
|
2019-07-20 15:13:36 +02:00
|
|
|
} else {
|
2023-03-02 14:21:36 +01:00
|
|
|
return Proxy::http_tcp(std::move(server), port, type->username_, type->password_);
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case td_api::proxyTypeMtproto::ID: {
|
2023-03-02 14:21:36 +01:00
|
|
|
auto type = static_cast<const td_api::proxyTypeMtproto *>(proxy_type);
|
2019-07-20 15:13:36 +02:00
|
|
|
TRY_RESULT(secret, mtproto::ProxySecret::from_link(type->secret_));
|
2019-07-22 04:04:18 +02:00
|
|
|
return Proxy::mtproto(std::move(server), port, std::move(secret));
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-07-22 04:04:18 +02:00
|
|
|
return Status::Error(400, "Wrong proxy type");
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-22 04:04:18 +02:00
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const Proxy &proxy) {
|
|
|
|
switch (proxy.type()) {
|
|
|
|
case Proxy::Type::Socks5:
|
|
|
|
return string_builder << "ProxySocks5 " << proxy.server() << ":" << proxy.port();
|
|
|
|
case Proxy::Type::HttpTcp:
|
|
|
|
return string_builder << "ProxyHttpTcp " << proxy.server() << ":" << proxy.port();
|
|
|
|
case Proxy::Type::HttpCaching:
|
|
|
|
return string_builder << "ProxyHttpCaching " << proxy.server() << ":" << proxy.port();
|
|
|
|
case Proxy::Type::Mtproto:
|
|
|
|
return string_builder << "ProxyMtproto " << proxy.server() << ":" << proxy.port() << "/"
|
|
|
|
<< proxy.secret().get_encoded_secret();
|
|
|
|
case Proxy::Type::None:
|
|
|
|
return string_builder << "ProxyEmpty";
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 15:13:36 +02:00
|
|
|
} // namespace td
|