From 8e07692979823624ae5fba38d91ef42293a3a94f Mon Sep 17 00:00:00 2001 From: GreaterFire <32649575+GreaterFire@users.noreply.github.com> Date: Sun, 5 May 2019 20:02:45 -0700 Subject: [PATCH] Aborted data should not be passed to recv function append_paylaod is no longer needed. --- docs/config.md | 3 --- examples/client.json-example | 1 - examples/forward.json-example | 1 - src/clientsession.cpp | 22 ++++++++++------------ src/config.cpp | 1 - src/config.h | 1 - src/forwardsession.cpp | 11 +++++------ tests/LinuxSmokeTest/client.json | 1 - tests/LinuxSmokeTest/fake-client.json | 1 - tests/LinuxSmokeTest/forward.json | 1 - 10 files changed, 15 insertions(+), 28 deletions(-) diff --git a/docs/config.md b/docs/config.md index 337f72ad..1ae4d146 100644 --- a/docs/config.md +++ b/docs/config.md @@ -16,7 +16,6 @@ In this page, we will look at the config file of trojan. Trojan uses [`JSON`](ht "password": [ "password1" ], - "append_payload": true, "log_level": 1, "ssl": { "verify": true, @@ -47,7 +46,6 @@ In this page, we will look at the config file of trojan. Trojan uses [`JSON`](ht - `remote_addr`: server address (hostname) - `remote_port`: server port - `password`: password used for verification (only the first password in the array will be used) -- `append_payload`: whether to append the first packet to trojan request. It can reduce length patterns of sessions, but may cause stability issues, in which case set it to `false`. For example, if you are running trojan server and trojan client on the same machine, you should set this option to `false`, because there is a race condition at the time the client receives its first packet. - `log_level`: how much log to dump. 0: ALL; 1: INFO; 2: WARN; 3: ERROR; 4: FATAL; 5: OFF. - `ssl`: `SSL` specific configurations - `verify`: whether to verify `SSL` certificate **STRONGLY RECOMMENDED** @@ -81,7 +79,6 @@ This forward config is for port forwarding. Everything is the same as the client "password": [ "password1" ], - "append_payload": true, "udp_timeout": 60, "log_level": 1, "ssl": { diff --git a/examples/client.json-example b/examples/client.json-example index 11bcd307..f03e8ec7 100644 --- a/examples/client.json-example +++ b/examples/client.json-example @@ -7,7 +7,6 @@ "password": [ "password1" ], - "append_payload": true, "log_level": 1, "ssl": { "verify": true, diff --git a/examples/forward.json-example b/examples/forward.json-example index 789554d4..625dd0f8 100644 --- a/examples/forward.json-example +++ b/examples/forward.json-example @@ -9,7 +9,6 @@ "password": [ "password1" ], - "append_payload": true, "udp_timeout": 60, "log_level": 1, "ssl": { diff --git a/src/clientsession.cpp b/src/clientsession.cpp index 369ec64f..c32ae3bc 100644 --- a/src/clientsession.cpp +++ b/src/clientsession.cpp @@ -60,7 +60,10 @@ void ClientSession::start() { void ClientSession::in_async_read() { auto self = shared_from_this(); in_socket.async_read_some(boost::asio::buffer(in_read_buf, MAX_LENGTH), [this, self](const boost::system::error_code error, size_t length) { - if (error && error != boost::asio::error::operation_aborted) { + if (error == boost::asio::error::operation_aborted) { + return; + } + if (error) { destroy(); return; } @@ -106,7 +109,10 @@ void ClientSession::out_async_write(const string &data) { void ClientSession::udp_async_read() { auto self = shared_from_this(); udp_socket.async_receive_from(boost::asio::buffer(udp_read_buf, MAX_LENGTH), udp_recv_endpoint, [this, self](const boost::system::error_code error, size_t length) { - if (error && error != boost::asio::error::operation_aborted) { + if (error == boost::asio::error::operation_aborted) { + return; + } + if (error) { destroy(); return; } @@ -211,17 +217,9 @@ void ClientSession::in_sent() { } case REQUEST: { status = CONNECT; + in_async_read(); if (is_udp) { - in_async_read(); - } - if (config.append_payload) { - if (is_udp) { - udp_async_read(); - } else { - in_async_read(); - } - } else { - first_packet_recv = true; + udp_async_read(); } auto self = shared_from_this(); resolver.async_resolve(config.remote_addr, to_string(config.remote_port), [this, self](const boost::system::error_code error, tcp::resolver::results_type results) { diff --git a/src/config.cpp b/src/config.cpp index e0ef8f66..4b9349e9 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -59,7 +59,6 @@ void Config::populate(const ptree &tree) { string p = item.second.get_value(); password[SHA224(p)] = p; } - append_payload = tree.get("append_payload", true); udp_timeout = tree.get("udp_timeout", 60); log_level = static_cast(tree.get("log_level", 1)); ssl.verify = tree.get("ssl.verify", true); diff --git a/src/config.h b/src/config.h index 90ee12c2..310e83e0 100644 --- a/src/config.h +++ b/src/config.h @@ -39,7 +39,6 @@ class Config { std::string target_addr; uint16_t target_port; std::map password; - bool append_payload; int udp_timeout; Log::Level log_level; class SSLConfig { diff --git a/src/forwardsession.cpp b/src/forwardsession.cpp index 87812196..8704add3 100644 --- a/src/forwardsession.cpp +++ b/src/forwardsession.cpp @@ -54,11 +54,7 @@ void ForwardSession::start() { } } out_write_buf = TrojanRequest::generate(config.password.cbegin()->first, config.target_addr, config.target_port, true); - if (config.append_payload) { - in_async_read(); - } else { - first_packet_recv = true; - } + in_async_read(); Log::log_with_endpoint(in_endpoint, "forwarding to " + config.target_addr + ':' + to_string(config.target_port) + " via " + config.remote_addr + ':' + to_string(config.remote_port), Log::INFO); auto self = shared_from_this(); resolver.async_resolve(config.remote_addr, to_string(config.remote_port), [this, self](const boost::system::error_code error, tcp::resolver::results_type results) { @@ -123,7 +119,10 @@ void ForwardSession::start() { void ForwardSession::in_async_read() { auto self = shared_from_this(); in_socket.async_read_some(boost::asio::buffer(in_read_buf, MAX_LENGTH), [this, self](const boost::system::error_code error, size_t length) { - if (error && error != boost::asio::error::operation_aborted) { + if (error == boost::asio::error::operation_aborted) { + return; + } + if (error) { destroy(); return; } diff --git a/tests/LinuxSmokeTest/client.json b/tests/LinuxSmokeTest/client.json index 009cc255..49972caa 100644 --- a/tests/LinuxSmokeTest/client.json +++ b/tests/LinuxSmokeTest/client.json @@ -7,7 +7,6 @@ "password": [ "linux-smoke-test-password" ], - "append_payload": false, "log_level": 0, "ssl": { "verify": true, diff --git a/tests/LinuxSmokeTest/fake-client.json b/tests/LinuxSmokeTest/fake-client.json index b15b1085..65d18000 100644 --- a/tests/LinuxSmokeTest/fake-client.json +++ b/tests/LinuxSmokeTest/fake-client.json @@ -7,7 +7,6 @@ "password": [ "wrong-password" ], - "append_payload": false, "log_level": 0, "ssl": { "verify": true, diff --git a/tests/LinuxSmokeTest/forward.json b/tests/LinuxSmokeTest/forward.json index aea6562e..dd59953c 100644 --- a/tests/LinuxSmokeTest/forward.json +++ b/tests/LinuxSmokeTest/forward.json @@ -9,7 +9,6 @@ "password": [ "linux-smoke-test-password" ], - "append_payload": false, "udp_timeout": 60, "log_level": 0, "ssl": {