Skip to content

Commit e7cd753

Browse files
committed
Add unix domain listen
1 parent ce83705 commit e7cd753

File tree

1 file changed

+98
-81
lines changed

1 file changed

+98
-81
lines changed

proxy/include/proxy/proxy_server.hpp

Lines changed: 98 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5559,11 +5559,8 @@ R"x*x*x(<html>
55595559
}
55605560

55615561
private:
5562-
// start_proxy_listen 启动一个协程, 用于监听 proxy client 的连接.
5563-
// 当有新的连接到来时, 会创建一个 proxy_session 对象, 并启动 proxy_session
5564-
// 的对象.
5565-
template <typename T>
5566-
inline net::awaitable<void> start_proxy_listen(T& acceptor) noexcept
5562+
template <typename T, typename S>
5563+
net::awaitable<void> start_accept(T& acceptor, S& socket)
55675564
{
55685565
boost::system::error_code error;
55695566
net::socket_base::keep_alive keep_alive_opt(true);
@@ -5572,106 +5569,126 @@ R"x*x*x(<html>
55725569

55735570
auto self = shared_from_this();
55745571

5575-
while (!m_abort)
5572+
co_await acceptor.async_accept(
5573+
socket.lowest_layer(), net_awaitable[error]);
5574+
if (error)
55765575
{
5577-
if constexpr (std::same_as<std::decay_t<T>, tcp_acceptor>)
5578-
{
5579-
proxy_tcp_socket socket(m_executor);
5580-
5581-
co_await acceptor.async_accept(
5582-
socket.lowest_layer(), net_awaitable[error]);
5583-
if (error)
5584-
{
5585-
if (!m_abort)
5586-
XLOG_ERR << "start_proxy_listen"
5587-
", async_accept: " << error.message();
5588-
co_return;
5589-
}
5576+
if (!m_abort)
5577+
XLOG_ERR << "start_proxy_listen"
5578+
", async_accept: " << error.message();
5579+
co_return;
5580+
}
55905581

5591-
static std::atomic_size_t id{ 1 };
5592-
size_t connection_id = id++;
5582+
static std::atomic_size_t id{ 1 };
5583+
size_t connection_id = id++;
55935584

5594-
std::vector<std::string> local_info;
5585+
std::vector<std::string> local_info;
5586+
std::string client;
55955587

5596-
auto endp = tcp_remote_endpoint(socket);
5597-
auto client = endp.address().to_string();
5588+
if constexpr (std::same_as<S, proxy_tcp_socket>)
5589+
{
5590+
auto endp = tcp_remote_endpoint(socket);
5591+
client = endp.address().to_string();
55985592

5599-
local_info.push_back(client);
5600-
client += ":" + std::to_string(endp.port());
5593+
local_info.push_back(client);
5594+
client += ":" + std::to_string(endp.port());
56015595

5602-
if (m_ipip)
5596+
if (m_ipip)
5597+
{
5598+
auto [ret, isp] = m_ipip->lookup(endp.address());
5599+
if (!ret.empty())
56035600
{
5604-
auto [ret, isp] = m_ipip->lookup(endp.address());
5605-
if (!ret.empty())
5606-
{
5607-
for (auto& c : ret)
5608-
client += " " + c;
5601+
for (auto& c : ret)
5602+
client += " " + c;
56095603

5610-
local_info.insert(local_info.end(), ret.begin(), ret.end());
5611-
}
5612-
5613-
if (!isp.empty())
5614-
client += " " + isp;
5604+
local_info.insert(local_info.end(), ret.begin(), ret.end());
56155605
}
56165606

5617-
XLOG_DBG << "connection id: "
5618-
<< connection_id
5619-
<< ", start client incoming: "
5620-
<< client;
5607+
if (!isp.empty())
5608+
client += " " + isp;
5609+
}
5610+
}
5611+
else if constexpr (std::same_as<S, proxy_uds_socket>)
5612+
{
5613+
auto endp = uds_remote_endpoint(socket);
5614+
client = endp.path();
5615+
}
56215616

5622-
if (!region_filter(local_info))
5623-
{
5624-
XLOG_WARN << "connection id: "
5625-
<< connection_id
5626-
<< ", region filter: "
5627-
<< client;
5617+
XLOG_DBG << "connection id: "
5618+
<< connection_id
5619+
<< ", start client incoming: "
5620+
<< client;
56285621

5629-
continue;
5630-
}
5622+
if (!region_filter(local_info))
5623+
{
5624+
XLOG_WARN << "connection id: "
5625+
<< connection_id
5626+
<< ", region filter: "
5627+
<< client;
5628+
co_return;
5629+
}
56315630

5632-
socket.set_option(keep_alive_opt, error);
5631+
socket.set_option(keep_alive_opt, error);
56335632

5634-
// 是否启用透明代理.
5633+
// 是否启用透明代理.
56355634
#if defined (__linux__)
5636-
std::optional<net::ip::tcp::endpoint> tproxy_endpoint;
5637-
if (m_option.transparent_)
5638-
tproxy_endpoint = co_await setup_tproxy(socket, connection_id);
5635+
std::optional<net::ip::tcp::endpoint> tproxy_endpoint;
5636+
if (m_option.transparent_)
5637+
tproxy_endpoint = co_await setup_tproxy(socket, connection_id);
56395638
#endif
56405639

5641-
// 在启用 scramble 时, 刻意开启 Nagle's algorithm 以尽量保证数据包
5642-
// 被重组, 尽最大可能避免观察者通过观察 ip 数据包大小的规律来分析 tcp
5643-
// 数据发送调用, 从而增加噪声加扰的强度.
5644-
if (m_option.scramble_)
5645-
socket.set_option(delay_opt, error);
5646-
else
5647-
socket.set_option(no_delay_opt, error);
5648-
5649-
// 创建 proxy_session 对象.
5650-
auto new_session =
5651-
std::make_shared<proxy_session>(
5652-
m_executor,
5653-
m_backend_context,
5654-
m_scheduler_locking,
5655-
m_dns_cache,
5656-
init_proxy_stream(std::move(socket)),
5657-
connection_id,
5658-
self);
5659-
5660-
// 保存 proxy_session 对象到 m_clients 中.
5661-
m_clients[connection_id] = new_session;
5640+
// 在启用 scramble 时, 刻意开启 Nagle's algorithm 以尽量保证数据包
5641+
// 被重组, 尽最大可能避免观察者通过观察 ip 数据包大小的规律来分析 tcp
5642+
// 数据发送调用, 从而增加噪声加扰的强度.
5643+
if (m_option.scramble_)
5644+
socket.set_option(delay_opt, error);
5645+
else
5646+
socket.set_option(no_delay_opt, error);
5647+
5648+
// 创建 proxy_session 对象.
5649+
auto new_session =
5650+
std::make_shared<proxy_session>(
5651+
m_executor,
5652+
m_backend_context,
5653+
m_scheduler_locking,
5654+
m_dns_cache,
5655+
init_proxy_stream(std::move(socket)),
5656+
connection_id,
5657+
self);
5658+
5659+
// 保存 proxy_session 对象到 m_clients 中.
5660+
m_clients[connection_id] = new_session;
56625661

56635662
#if defined (__linux__)
5664-
if (tproxy_endpoint)
5665-
new_session->setup_tproxy(*tproxy_endpoint);
5663+
if (tproxy_endpoint)
5664+
new_session->setup_tproxy(*tproxy_endpoint);
56665665
#endif
56675666

5668-
// 启动 proxy_session 对象.
5669-
new_session->start();
5667+
// 启动 proxy_session 对象.
5668+
new_session->start();
5669+
5670+
co_return;
5671+
}
5672+
5673+
// start_proxy_listen 启动一个协程, 用于监听 proxy client 的连接.
5674+
// 当有新的连接到来时, 会创建一个 proxy_session 对象, 并启动 proxy_session
5675+
// 的对象.
5676+
template <typename T>
5677+
net::awaitable<void> start_proxy_listen(T& acceptor) noexcept
5678+
{
5679+
auto self = shared_from_this();
5680+
5681+
while (!m_abort)
5682+
{
5683+
if constexpr (std::same_as<std::decay_t<T>, tcp_acceptor>)
5684+
{
5685+
proxy_tcp_socket socket(m_executor);
5686+
co_await start_accept(acceptor, socket);
56705687
}
56715688
else if constexpr (std::same_as<std::decay_t<T>, unix_acceptor>)
56725689
{
5673-
XLOG_WARN << "start unix domain listen...";
5674-
co_return;
5690+
proxy_uds_socket socket(m_executor);
5691+
co_await start_accept(acceptor, socket);
56755692
}
56765693
}
56775694

0 commit comments

Comments
 (0)