2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01: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/net/Socks5.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-02-28 21:25:42 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void Socks5::send_greeting() {
|
2018-07-26 02:28:02 +02:00
|
|
|
VLOG(proxy) << "Send greeting to proxy";
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(state_ == State::SendGreeting);
|
|
|
|
state_ = State::WaitGreetingResponse;
|
|
|
|
|
|
|
|
string greeting;
|
|
|
|
greeting += '\x05';
|
|
|
|
bool use_username = !username_.empty();
|
|
|
|
char authentication_count = use_username ? '\x02' : '\x01';
|
|
|
|
greeting += authentication_count;
|
|
|
|
greeting += '\0';
|
|
|
|
if (use_username) {
|
|
|
|
greeting += '\x02';
|
|
|
|
}
|
|
|
|
|
|
|
|
fd_.output_buffer().append(greeting);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Socks5::wait_greeting_response() {
|
|
|
|
auto &buf = fd_.input_buffer();
|
2018-07-26 02:28:02 +02:00
|
|
|
VLOG(proxy) << "Receive greeting response of size " << buf.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (buf.size() < 2) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
auto buffer_slice = buf.read_as_buffer_slice(2);
|
|
|
|
auto slice = buffer_slice.as_slice();
|
|
|
|
if (slice[0] != '\x05') {
|
2019-12-24 14:46:10 +01:00
|
|
|
return Status::Error(PSLICE() << "Unsupported socks protocol version " << static_cast<int>(slice[0]));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto authentication_method = slice[1];
|
|
|
|
if (authentication_method == '\0') {
|
|
|
|
send_ip_address();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (authentication_method == '\x02') {
|
|
|
|
return send_username_password();
|
|
|
|
}
|
|
|
|
return Status::Error("Unsupported authentication mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Socks5::send_username_password() {
|
2018-07-26 02:28:02 +02:00
|
|
|
VLOG(proxy) << "Send username and password";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (username_.size() >= 128) {
|
|
|
|
return Status::Error("Username is too long");
|
|
|
|
}
|
|
|
|
if (password_.size() >= 128) {
|
|
|
|
return Status::Error("Password is too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
string request;
|
|
|
|
request += '\x01';
|
|
|
|
request += narrow_cast<char>(username_.size());
|
|
|
|
request += username_;
|
|
|
|
request += narrow_cast<char>(password_.size());
|
|
|
|
request += password_;
|
|
|
|
fd_.output_buffer().append(request);
|
|
|
|
state_ = State::WaitPasswordResponse;
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Socks5::wait_password_response() {
|
|
|
|
auto &buf = fd_.input_buffer();
|
2018-07-26 02:28:02 +02:00
|
|
|
VLOG(proxy) << "Receive password response of size " << buf.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (buf.size() < 2) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
auto buffer_slice = buf.read_as_buffer_slice(2);
|
|
|
|
auto slice = buffer_slice.as_slice();
|
|
|
|
if (slice[0] != '\x01') {
|
2019-12-24 14:46:10 +01:00
|
|
|
return Status::Error(PSLICE() << "Unsupported socks subnegotiation protocol version "
|
|
|
|
<< static_cast<int>(slice[0]));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (slice[1] != '\x00') {
|
|
|
|
return Status::Error("Wrong username or password");
|
|
|
|
}
|
|
|
|
|
|
|
|
send_ip_address();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socks5::send_ip_address() {
|
2018-07-26 02:28:02 +02:00
|
|
|
VLOG(proxy) << "Send IP address";
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_->on_connected();
|
|
|
|
string request;
|
|
|
|
request += '\x05';
|
|
|
|
request += '\x01';
|
|
|
|
request += '\x00';
|
|
|
|
if (ip_address_.is_ipv4()) {
|
|
|
|
request += '\x01';
|
2018-09-11 18:40:53 +02:00
|
|
|
auto ipv4 = ntohl(ip_address_.get_ipv4());
|
2018-12-31 20:04:05 +01:00
|
|
|
request += static_cast<char>(ipv4 & 255);
|
|
|
|
request += static_cast<char>((ipv4 >> 8) & 255);
|
|
|
|
request += static_cast<char>((ipv4 >> 16) & 255);
|
|
|
|
request += static_cast<char>((ipv4 >> 24) & 255);
|
|
|
|
} else {
|
|
|
|
request += '\x04';
|
2020-05-16 15:11:03 +02:00
|
|
|
request += ip_address_.get_ipv6();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto port = ip_address_.get_port();
|
|
|
|
request += static_cast<char>((port >> 8) & 255);
|
|
|
|
request += static_cast<char>(port & 255);
|
|
|
|
fd_.output_buffer().append(request);
|
|
|
|
state_ = State::WaitIpAddressResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Socks5::wait_ip_address_response() {
|
|
|
|
CHECK(state_ == State::WaitIpAddressResponse);
|
|
|
|
auto it = fd_.input_buffer().clone();
|
2018-07-26 02:28:02 +02:00
|
|
|
VLOG(proxy) << "Receive IP address response of size " << it.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it.size() < 4) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
char c;
|
|
|
|
MutableSlice c_slice(&c, 1);
|
|
|
|
it.advance(1, c_slice);
|
|
|
|
if (c != '\x05') {
|
|
|
|
return Status::Error("Invalid response");
|
|
|
|
}
|
|
|
|
it.advance(1, c_slice);
|
|
|
|
if (c != '\0') {
|
2018-05-08 16:48:30 +02:00
|
|
|
return Status::Error(PSLICE() << "Receive error code " << static_cast<int32>(c) << " from server");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
it.advance(1, c_slice);
|
|
|
|
if (c != '\0') {
|
2018-09-11 18:40:53 +02:00
|
|
|
return Status::Error("Byte must be zero");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
it.advance(1, c_slice);
|
2018-07-26 02:28:02 +02:00
|
|
|
size_t total_size = 6;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (c == '\x01') {
|
|
|
|
if (it.size() < 4) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
it.advance(4);
|
2018-07-26 02:28:02 +02:00
|
|
|
total_size += 4;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (c == '\x04') {
|
|
|
|
if (it.size() < 16) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
it.advance(16);
|
2018-07-26 02:28:02 +02:00
|
|
|
total_size += 16;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
return Status::Error("Invalid response");
|
|
|
|
}
|
|
|
|
if (it.size() < 2) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
it.advance(2);
|
2018-07-26 02:28:02 +02:00
|
|
|
fd_.input_buffer().advance(total_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-07-26 02:28:02 +02:00
|
|
|
Status Socks5::loop_impl() {
|
|
|
|
switch (state_) {
|
|
|
|
case State::SendGreeting:
|
|
|
|
send_greeting();
|
|
|
|
break;
|
|
|
|
case State::WaitGreetingResponse:
|
|
|
|
TRY_STATUS(wait_greeting_response());
|
|
|
|
break;
|
|
|
|
case State::WaitPasswordResponse:
|
|
|
|
TRY_STATUS(wait_password_response());
|
|
|
|
break;
|
|
|
|
case State::WaitIpAddressResponse:
|
|
|
|
TRY_STATUS(wait_ip_address_response());
|
|
|
|
break;
|
2018-07-26 15:49:18 +02:00
|
|
|
default:
|
2018-07-26 02:28:02 +02:00
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-26 02:28:02 +02:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-28 21:25:42 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|