Skip to content

Commit 133f803

Browse files
committed
Improve some compile error
1 parent 22b6809 commit 133f803

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

proxy/include/proxy/proxy_server.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "proxy/proxy_session.hpp"
1616

17+
#include <atomic>
18+
1719

1820
namespace proxy {
1921

@@ -368,6 +370,10 @@ namespace proxy {
368370
void udp_tproxy_forward_packet(
369371
udp_tproxy_flow_ptr flow, const char* data, std::size_t len);
370372

373+
// UDP TPROXY 使用 connect-udp 转发数据包 (RFC 9298 capsule).
374+
void udp_tproxy_forward_packet_connect_udp(
375+
udp_tproxy_flow_ptr flow, const char* data, std::size_t len);
376+
371377
// 启动 UDP TPROXY 监听协程.
372378
net::awaitable<void> start_udp_tproxy_listen(udp::socket& udp_sock) noexcept;
373379

@@ -436,7 +442,7 @@ namespace proxy {
436442
#endif // defined(__linux__)
437443

438444
// 当前服务是否中止标志.
439-
bool m_abort{ false };
445+
std::atomic<bool> m_abort{ false };
440446
};
441447

442448
}

proxy/include/proxy/proxy_session.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120

121121
#include <cstdlib>
122122
#include <algorithm>
123+
#include <atomic>
123124
#include <cstddef>
124125
#include <cstring>
125126
#include <filesystem>
@@ -1521,7 +1522,7 @@ namespace proxy {
15211522
net::ssl::context m_ssl_cli_context{ net::ssl::context::sslv23_client };
15221523

15231524
// 当前 session 是否被中止的状态.
1524-
bool m_abort{ false };
1525+
std::atomic<bool> m_abort{ false };
15251526
};
15261527
}
15271528

proxy/src/proxy_server.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,30 +1922,31 @@ net::awaitable<void> proxy_server::start_udp_tproxy_listen(udp::socket& udp_sock
19221922

19231923
size_t flow_key = make_udp_flow_key(client_ep, original_dest);
19241924

1925-
// 查找或创建 flow.
1925+
auto scheme = boost::to_lower_copy(
1926+
std::string(m_option.proxy_pass_->scheme()));
1927+
bool using_connect_udp = scheme.starts_with("http");
1928+
1929+
// 查找或创建 flow (原子操作, 避免 TOCTOU 竞态条件).
19261930
std::shared_ptr<udp_tproxy_flow> flow;
1931+
bool is_new = false;
19271932
{
19281933
std::lock_guard<std::mutex> lock(m_udp_flows_mutex);
19291934

19301935
auto it = m_udp_tproxy_flows.find(flow_key);
19311936
if (it != m_udp_tproxy_flows.end())
1937+
{
19321938
flow = it->second;
1933-
}
1934-
1935-
auto scheme = boost::to_lower_copy(
1936-
std::string(m_option.proxy_pass_->scheme()));
1937-
bool using_connect_udp = scheme.starts_with("http");
1938-
1939-
if (!flow)
1940-
{
1941-
// 创建一个新的 flow 来处理这个客户端和原始目标地址的通信.
1942-
flow = std::make_shared<udp_tproxy_flow>(client_ep, original_dest, udp_sock, flow_key);
1943-
1939+
}
1940+
else
19441941
{
1945-
std::lock_guard<std::mutex> lock(m_udp_flows_mutex);
1942+
flow = std::make_shared<udp_tproxy_flow>(client_ep, original_dest, udp_sock, flow_key);
19461943
m_udp_tproxy_flows[flow_key] = flow;
1944+
is_new = true;
19471945
}
1946+
}
19481947

1948+
if (is_new)
1949+
{
19491950
if (using_connect_udp)
19501951
{
19511952
flow->using_connect_udp_ = true;

0 commit comments

Comments
 (0)