Support auth in http proxy server

This commit is contained in:
klzgrad 2024-07-01 00:26:50 +08:00
parent 79510bed63
commit 60efa77f00
3 changed files with 25 additions and 3 deletions

View File

@ -10,6 +10,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/base64.h"
#include "base/functional/bind.h" #include "base/functional/bind.h"
#include "base/functional/callback_helpers.h" #include "base/functional/callback_helpers.h"
#include "base/logging.h" #include "base/logging.h"
@ -40,6 +41,8 @@ constexpr int kMaxPaddingSize = kMinPaddingSize + 32;
HttpProxyServerSocket::HttpProxyServerSocket( HttpProxyServerSocket::HttpProxyServerSocket(
std::unique_ptr<StreamSocket> transport_socket, std::unique_ptr<StreamSocket> transport_socket,
const std::string& user,
const std::string& pass,
ClientPaddingDetectorDelegate* padding_detector_delegate, ClientPaddingDetectorDelegate* padding_detector_delegate,
const NetworkTrafficAnnotationTag& traffic_annotation, const NetworkTrafficAnnotationTag& traffic_annotation,
const std::vector<PaddingType>& supported_padding_types) const std::vector<PaddingType>& supported_padding_types)
@ -53,7 +56,12 @@ HttpProxyServerSocket::HttpProxyServerSocket(
header_write_size_(-1), header_write_size_(-1),
net_log_(transport_->NetLog()), net_log_(transport_->NetLog()),
traffic_annotation_(traffic_annotation), traffic_annotation_(traffic_annotation),
supported_padding_types_(supported_padding_types) {} supported_padding_types_(supported_padding_types) {
if (!user.empty() || !pass.empty()) {
basic_auth_ =
std::string("Basic ").append(base::Base64Encode(user + ":" + pass));
}
}
HttpProxyServerSocket::~HttpProxyServerSocket() { HttpProxyServerSocket::~HttpProxyServerSocket() {
Disconnect(); Disconnect();
@ -347,6 +355,15 @@ int HttpProxyServerSocket::DoHeaderReadComplete(int result) {
headers.AddHeadersFromString(headers_str); headers.AddHeadersFromString(headers_str);
} }
if (!basic_auth_.empty()) {
std::string proxy_auth;
headers.GetHeader(HttpRequestHeaders::kProxyAuthorization, &proxy_auth);
if (proxy_auth != basic_auth_) {
LOG(WARNING) << "Invalid Proxy-Authorization: " << proxy_auth;
return ERR_INVALID_ARGUMENT;
}
}
if (is_http_1_0) { if (is_http_1_0) {
GURL url(uri); GURL url(uri);
if (!url.is_valid()) { if (!url.is_valid()) {

View File

@ -35,6 +35,8 @@ class HttpProxyServerSocket : public StreamSocket {
public: public:
HttpProxyServerSocket( HttpProxyServerSocket(
std::unique_ptr<StreamSocket> transport_socket, std::unique_ptr<StreamSocket> transport_socket,
const std::string& user,
const std::string& pass,
ClientPaddingDetectorDelegate* padding_detector_delegate, ClientPaddingDetectorDelegate* padding_detector_delegate,
const NetworkTrafficAnnotationTag& traffic_annotation, const NetworkTrafficAnnotationTag& traffic_annotation,
const std::vector<PaddingType>& supported_padding_types); const std::vector<PaddingType>& supported_padding_types);
@ -117,6 +119,8 @@ class HttpProxyServerSocket : public StreamSocket {
bool was_ever_used_; bool was_ever_used_;
int header_write_size_; int header_write_size_;
std::string basic_auth_;
HostPortPair request_endpoint_; HostPortPair request_endpoint_;
NetLogWithSource net_log_; NetLogWithSource net_log_;

View File

@ -116,8 +116,9 @@ void NaiveProxy::DoConnect() {
traffic_annotation_); traffic_annotation_);
} else if (protocol_ == ClientProtocol::kHttp) { } else if (protocol_ == ClientProtocol::kHttp) {
socket = std::make_unique<HttpProxyServerSocket>( socket = std::make_unique<HttpProxyServerSocket>(
std::move(accepted_socket_), padding_detector_delegate.get(), std::move(accepted_socket_), listen_user_, listen_pass_,
traffic_annotation_, supported_padding_types_); padding_detector_delegate.get(), traffic_annotation_,
supported_padding_types_);
} else if (protocol_ == ClientProtocol::kRedir) { } else if (protocol_ == ClientProtocol::kRedir) {
socket = std::move(accepted_socket_); socket = std::move(accepted_socket_);
} else { } else {