@@ -31,8 +31,7 @@ ServerSession::ServerSession(const Config &config, boost::asio::io_context &io_c
31
31
out_socket(io_context),
32
32
udp_resolver(io_context),
33
33
auth(auth),
34
- plain_http_response(plain_http_response),
35
- remote_port(0 ) {}
34
+ plain_http_response(plain_http_response) {}
36
35
37
36
tcp::socket& ServerSession::accept_socket () {
38
37
return (tcp::socket&)in_socket.next_layer ();
@@ -60,11 +59,6 @@ void ServerSession::start() {
60
59
destroy ();
61
60
return ;
62
61
}
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 ;
68
62
in_async_read ();
69
63
});
70
64
}
@@ -159,7 +153,17 @@ void ServerSession::in_recv(const string &data) {
159
153
}
160
154
}
161
155
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
+ }();
163
167
if (valid) {
164
168
out_write_buf = req.payload ;
165
169
if (req.command == TrojanRequest::UDP_ASSOCIATE) {
@@ -172,7 +176,7 @@ void ServerSession::in_recv(const string &data) {
172
176
Log::log_with_endpoint (in_endpoint, " requested connection to " + req.address .address + ' :' + to_string (req.address .port ), Log::INFO);
173
177
}
174
178
} 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);
176
180
out_write_buf = data;
177
181
}
178
182
sent_len += out_write_buf.length ();
0 commit comments