Skip to content

Commit a504b11

Browse files
committed
Improve resolved domain name
1 parent 14868f2 commit a504b11

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

proxy/include/proxy/proxy_server.hpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,7 @@ R"x*x*x(<html>
18421842
tcp::endpoint dst_endpoint;
18431843
std::string domain;
18441844
uint16_t port = 0;
1845+
bool resolved = true;
18451846

18461847
auto executor = co_await net::this_coro::executor;
18471848

@@ -1873,6 +1874,9 @@ R"x*x*x(<html>
18731874
dst_endpoint.address() == net::ip::make_address_v4("0.0.0.0") ||
18741875
dst_endpoint.address() == net::ip::make_address_v6("::0"))
18751876
{
1877+
if (ec)
1878+
resolved = false;
1879+
18761880
auto address = tcp_remote_endpoint(m_local_socket).address();
18771881
dst_endpoint.address(address);
18781882
}
@@ -1910,7 +1914,7 @@ R"x*x*x(<html>
19101914
if (command == SOCKS_CMD_CONNECT)
19111915
{
19121916
// 连接目标主机.
1913-
ec = co_await start_connect_host(domain, port, atyp == SOCKS5_ATYP_DOMAINNAME);
1917+
ec = co_await start_connect_host(domain, port, resolved);
19141918
if (ec)
19151919
{
19161920
log_conn_warning()
@@ -2513,11 +2517,12 @@ R"x*x*x(<html>
25132517
ec = co_await start_connect_host(
25142518
hostname,
25152519
port,
2516-
true);
2520+
false);
25172521
else
25182522
ec = co_await start_connect_host(
25192523
dst_endpoint.address().to_string(),
2520-
port);
2524+
port,
2525+
true);
25212526
if (ec)
25222527
{
25232528
log_conn_warning()
@@ -2743,7 +2748,7 @@ R"x*x*x(<html>
27432748
if (!m_remote_socket.is_open())
27442749
{
27452750
// 连接到目标主机.
2746-
ec = co_await start_connect_host(std::string(host), port, true);
2751+
ec = co_await start_connect_host(std::string(host), port, false);
27472752
if (ec)
27482753
{
27492754
log_conn_warning()
@@ -2890,7 +2895,7 @@ R"x*x*x(<html>
28902895
std::string host(target_view.substr(0, pos));
28912896
std::string port(target_view.substr(pos + 1));
28922897

2893-
ec = co_await start_connect_host(host, static_cast<uint16_t>(std::atol(port.c_str())), true);
2898+
ec = co_await start_connect_host(host, static_cast<uint16_t>(std::atol(port.c_str())), false);
28942899
if (ec)
28952900
{
28962901
log_conn_warning()
@@ -3335,7 +3340,7 @@ R"x*x*x(<html>
33353340
inline net::awaitable<boost::system::error_code> start_connect_host(
33363341
std::string target_host,
33373342
uint16_t target_port,
3338-
bool resolve = false,
3343+
bool resolved = false,
33393344
int command = SOCKS_CMD_CONNECT) noexcept
33403345
{
33413346
tcp::socket& remote_socket = net_tcp_socket(m_remote_socket);
@@ -3361,20 +3366,27 @@ R"x*x*x(<html>
33613366
}
33623367
else
33633368
{
3364-
if (resolve)
3369+
switch (static_cast<int>(resolved))
33653370
{
3366-
targets = co_await resolve_targets(target_host, target_port);
3367-
}
3368-
else
3371+
case 1: // true
33693372
{
33703373
tcp::endpoint dst_endpoint;
33713374

3372-
dst_endpoint.address(
3373-
net::ip::make_address(target_host));
3374-
dst_endpoint.port(target_port);
3375+
auto addr = net::ip::make_address(target_host, ec);
3376+
if (!ec)
3377+
{
3378+
dst_endpoint.address(addr);
3379+
dst_endpoint.port(target_port);
33753380

3376-
targets = net::ip::basic_resolver_results<tcp>::create(
3377-
dst_endpoint, "", "");
3381+
targets = net::ip::basic_resolver_results<tcp>::create(dst_endpoint, "", "");
3382+
break;
3383+
}
3384+
}
3385+
[[fallthrough]];
3386+
case 0: // false
3387+
default:
3388+
targets = co_await resolve_targets(target_host, target_port);
3389+
break;
33783390
}
33793391
}
33803392

0 commit comments

Comments
 (0)