Fix redirect with IPv4-mapped IPv6 addresses

Also fix bracket URL parsing in listen option.
This commit is contained in:
klzgrad 2022-08-21 19:30:24 +08:00
parent fb63815a04
commit 97915a596d
2 changed files with 13 additions and 8 deletions

View File

@ -220,16 +220,17 @@ int NaiveConnection::DoConnectServer() {
#if defined(OS_LINUX) #if defined(OS_LINUX)
const auto* socket = const auto* socket =
static_cast<const TCPClientSocket*>(client_socket_.get()); static_cast<const TCPClientSocket*>(client_socket_.get());
IPEndPoint local_address; IPEndPoint peer_endpoint;
int rv; int rv;
rv = socket->GetLocalAddress(&local_address); rv = socket->GetPeerAddress(&peer_endpoint);
if (rv != OK) { if (rv != OK) {
LOG(ERROR) << "Connection " << id_ << " cannot get local address"; LOG(ERROR) << "Connection " << id_ << " cannot get peer address";
return rv; return rv;
} }
int sd = socket->SocketDescriptorForTesting(); int sd = socket->SocketDescriptorForTesting();
SockaddrStorage dst; SockaddrStorage dst;
if (local_address.GetFamily() == ADDRESS_FAMILY_IPV4) { if (peer_endpoint.GetFamily() == ADDRESS_FAMILY_IPV4 ||
peer_endpoint.address().IsIPv4MappedIPv6()) {
rv = getsockopt(sd, SOL_IP, SO_ORIGINAL_DST, dst.addr, &dst.addr_len); rv = getsockopt(sd, SOL_IP, SO_ORIGINAL_DST, dst.addr, &dst.addr_len);
} else { } else {
rv = getsockopt(sd, SOL_IPV6, SO_ORIGINAL_DST, dst.addr, &dst.addr_len); rv = getsockopt(sd, SOL_IPV6, SO_ORIGINAL_DST, dst.addr, &dst.addr_len);
@ -249,6 +250,9 @@ int NaiveConnection::DoConnectServer() {
return ERR_ADDRESS_INVALID; return ERR_ADDRESS_INVALID;
} }
} }
} else {
LOG(ERROR) << "Failed to get original destination address";
return ERR_ADDRESS_INVALID;
} }
#else #else
static_cast<void>(resolver_); static_cast<void>(resolver_);
@ -260,7 +264,8 @@ int NaiveConnection::DoConnectServer() {
"http", CanonicalizeHost(origin.HostForURL(), &host_info), origin.port(), "http", CanonicalizeHost(origin.HostForURL(), &host_info), origin.port(),
url::SchemeHostPort::ALREADY_CANONICALIZED); url::SchemeHostPort::ALREADY_CANONICALIZED);
if (!endpoint.IsValid()) { if (!endpoint.IsValid()) {
LOG(ERROR) << "Connection " << id_ << " to invalid origin " << origin.ToString(); LOG(ERROR) << "Connection " << id_ << " to invalid origin "
<< origin.ToString();
return ERR_ADDRESS_INVALID; return ERR_ADDRESS_INVALID;
} }
@ -268,8 +273,8 @@ int NaiveConnection::DoConnectServer() {
// Ignores socket limit set by socket pool for this type of socket. // Ignores socket limit set by socket pool for this type of socket.
return InitSocketHandleForRawConnect2( return InitSocketHandleForRawConnect2(
std::move(endpoint), session_, LOAD_IGNORE_LIMITS, MAXIMUM_PRIORITY, proxy_info_, std::move(endpoint), session_, LOAD_IGNORE_LIMITS, MAXIMUM_PRIORITY,
server_ssl_config_, proxy_ssl_config_, PRIVACY_MODE_DISABLED, proxy_info_, server_ssl_config_, proxy_ssl_config_, PRIVACY_MODE_DISABLED,
network_isolation_key_, net_log_, server_socket_handle_.get(), network_isolation_key_, net_log_, server_socket_handle_.get(),
io_callback_); io_callback_);
} }

View File

@ -264,7 +264,7 @@ bool ParseCommandLine(const CommandLine& cmdline, Params* params) {
params->listen_pass = base::UnescapeBinaryURLComponent(url.password()); params->listen_pass = base::UnescapeBinaryURLComponent(url.password());
} }
if (!url.host().empty()) { if (!url.host().empty()) {
params->listen_addr = url.host(); params->listen_addr = url.HostNoBrackets();
} }
if (!url.port().empty()) { if (!url.port().empty()) {
if (!base::StringToInt(url.port(), &params->listen_port)) { if (!base::StringToInt(url.port(), &params->listen_port)) {