Skip to content

Commit

Permalink
cli: resolve domain name locally for sock4/socks5 on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilledheart committed May 17, 2024
1 parent b968423 commit 244cb33
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
60 changes: 34 additions & 26 deletions src/cli/cli_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,6 @@ void CliConnection::start() {
upstream_writable_ = false;
downstream_readable_ = true;

int ret = resolver_.Init();
if (ret < 0) {
LOG(WARNING) << "resolver initialize failure";
close();
return;
}
ReadMethodSelect();
}

Expand All @@ -206,7 +200,7 @@ void CliConnection::close() {
<< " disconnected with client at stage: " << CliConnection::state_to_str(CurrentState());
asio::error_code ec;
closed_ = true;
resolver_.Cancel();
resolver_.Reset();
downlink_->close(ec);
if (ec) {
VLOG(1) << "close() error: " << ec;
Expand Down Expand Up @@ -1867,26 +1861,40 @@ void CliConnection::OnCmdConnect(const std::string& domain_name, uint16_t port)
DCHECK_LE(domain_name.size(), (unsigned int)TLSEXT_MAXLEN_host_name);

if (CIPHER_METHOD_IS_SOCKS_NON_DOMAIN_NAME(method())) {
VLOG(1) << "Connection (client) " << connection_id() << " resolving domain name " << domain_name << " locally";
scoped_refptr<CliConnection> self(this);
resolver_.AsyncResolve(domain_name, port,
[this, self](const asio::error_code& ec, asio::ip::tcp::resolver::results_type results) {
// Cancelled, safe to ignore
if (UNLIKELY(ec == asio::error::operation_aborted)) {
return;
}
if (closed_) {
return;
}
if (ec) {
disconnected(ec);
return;
}
for (auto iter = std::begin(results); iter != std::end(results); ++iter) {
ss_request_ = std::make_unique<ss::request>(*iter);
OnConnect();
break;
}
});
int ret = resolver_.Init();
if (ret < 0) {
LOG(WARNING) << "resolver initialize failure";
OnDisconnect(asio::error::host_not_found);
return;
}
resolver_.AsyncResolve(
domain_name, port,
[this, self, domain_name](const asio::error_code& ec, asio::ip::tcp::resolver::results_type results) {
resolver_.Reset();
// Cancelled, safe to ignore
if (UNLIKELY(ec == asio::error::operation_aborted)) {
return;
}
if (closed_) {
return;
}
if (ec) {
OnDisconnect(ec);
return;
}
asio::ip::tcp::endpoint endpoint;
for (auto iter = std::begin(results); iter != std::end(results); ++iter) {
endpoint = iter->endpoint();
break;
}
DCHECK(!endpoint.address().is_unspecified());
VLOG(1) << "Connection (client) " << connection_id() << " resolved domain name " << domain_name << " to "
<< endpoint.address();
ss_request_ = std::make_unique<ss::request>(endpoint);
OnConnect();
});
return;
}
ss_request_ = std::make_unique<ss::request>(domain_name, port);
Expand Down
4 changes: 2 additions & 2 deletions src/net/c-ares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void CAresResolver::OnAsyncResolve(AsyncResolveCallback cb,
if (status != ARES_SUCCESS) {
asio::error_code ec = AresToAsioError(status);
VLOG(1) << "C-Ares: Host " << host << ":" << service << " Resolved error: " << ec;
cb(ec, {});
asio::post(io_context_, [cb, ec]() { cb(ec, {}); });
return;
}

Expand All @@ -349,7 +349,7 @@ void CAresResolver::OnAsyncResolve(AsyncResolveCallback cb,
ss << endpoint << " ";
}
VLOG(1) << "C-Ares: Resolved " << host << ":" << service << " to: [ " << ss.str() << " ]";
cb(asio::error_code(), std::move(results));
asio::post(io_context_, [cb, results]() { cb(asio::error_code(), std::move(results)); });
}

void CAresResolver::WaitTimer() {
Expand Down

0 comments on commit 244cb33

Please sign in to comment.