Skip to content

Commit 6cc9d4c

Browse files
WillyPillowGreaterFire
authored andcommitted
Handle ssl.alpn_port_override only when the request is not valid.
1 parent b7320e7 commit 6cc9d4c

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/session/serversession.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ ServerSession::ServerSession(const Config &config, boost::asio::io_context &io_c
3131
out_socket(io_context),
3232
udp_resolver(io_context),
3333
auth(auth),
34-
plain_http_response(plain_http_response),
35-
remote_port(0) {}
34+
plain_http_response(plain_http_response) {}
3635

3736
tcp::socket& ServerSession::accept_socket() {
3837
return (tcp::socket&)in_socket.next_layer();
@@ -60,11 +59,6 @@ void ServerSession::start() {
6059
destroy();
6160
return;
6261
}
63-
const unsigned char *alpn_out = nullptr;
64-
unsigned int alpn_len = 0;
65-
SSL_get0_alpn_selected(in_socket.native_handle(), &alpn_out, &alpn_len);
66-
auto it = config.alpn_port.find(std::string(alpn_out, alpn_out + alpn_len));
67-
remote_port = (it != config.alpn_port.end()) ? it->second : config.remote_port;
6862
in_async_read();
6963
});
7064
}
@@ -159,7 +153,17 @@ void ServerSession::in_recv(const string &data) {
159153
}
160154
}
161155
string query_addr = valid ? req.address.address : config.remote_addr;
162-
string query_port = to_string(valid ? req.address.port : remote_port);
156+
string query_port = [&]() {
157+
if (valid) {
158+
return to_string(req.address.port);
159+
} else {
160+
const unsigned char *alpn_out = nullptr;
161+
unsigned int alpn_len = 0;
162+
SSL_get0_alpn_selected(in_socket.native_handle(), &alpn_out, &alpn_len);
163+
auto it = config.alpn_port.find(std::string(alpn_out, alpn_out + alpn_len));
164+
return to_string((it != config.alpn_port.end()) ? it->second : config.remote_port);
165+
}
166+
}();
163167
if (valid) {
164168
out_write_buf = req.payload;
165169
if (req.command == TrojanRequest::UDP_ASSOCIATE) {
@@ -172,7 +176,7 @@ void ServerSession::in_recv(const string &data) {
172176
Log::log_with_endpoint(in_endpoint, "requested connection to " + req.address.address + ':' + to_string(req.address.port), Log::INFO);
173177
}
174178
} else {
175-
Log::log_with_endpoint(in_endpoint, "not trojan request, connecting to " + config.remote_addr + ':' + to_string(remote_port), Log::WARN);
179+
Log::log_with_endpoint(in_endpoint, "not trojan request, connecting to " + config.remote_addr + ':' + query_port, Log::WARN);
176180
out_write_buf = data;
177181
}
178182
sent_len += out_write_buf.length();

src/session/serversession.h

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class ServerSession : public Session {
3838
Authenticator *auth;
3939
std::string auth_password;
4040
const std::string &plain_http_response;
41-
uint16_t remote_port;
4241
void destroy();
4342
void in_async_read();
4443
void in_async_write(const std::string &data);

0 commit comments

Comments
 (0)